OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/cast_channel/cast_auth_util.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_auth_util.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <cryptohi.h> | 8 #include <cryptohi.h> |
9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
10 #include <seccomon.h> | 10 #include <seccomon.h> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" | 14 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" |
15 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" | 15 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" |
16 #include "crypto/nss_util.h" | 16 #include "crypto/nss_util.h" |
17 #include "crypto/scoped_nss_types.h" | 17 #include "crypto/scoped_nss_types.h" |
18 #include "net/base/hash_value.h" | |
19 #include "net/cert/asn1_util.h" | |
20 #include "net/cert/x509_certificate.h" | |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 // Public key of the certificate with which the peer cert should be signed. | 24 // Public key of the certificate with which the peer cert should be signed. |
22 static const unsigned char kCAPublicKeyDER[] = { | 25 static const unsigned char kCAPublicKeyDER[] = { |
23 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, | 26 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, |
24 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, | 27 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, |
25 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, | 28 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, |
26 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf, | 29 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf, |
27 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80, | 30 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80, |
28 0xa4, 0x47, 0x15, 0xc1, 0x20, 0x99, 0x50, 0xcd, 0xec, 0x14, 0x62, 0x09, | 31 0xa4, 0x47, 0x15, 0xc1, 0x20, 0x99, 0x50, 0xcd, 0xec, 0x14, 0x62, 0x09, |
29 0x5b, 0xa4, 0x98, 0xcd, 0xd2, 0x41, 0xb6, 0x36, 0x4e, 0xff, 0xe8, 0x2e, | 32 0x5b, 0xa4, 0x98, 0xcd, 0xd2, 0x41, 0xb6, 0x36, 0x4e, 0xff, 0xe8, 0x2e, |
30 0x32, 0x30, 0x4a, 0x81, 0xa8, 0x42, 0xa3, 0x6c, 0x9b, 0x33, 0x6e, 0xca, | 33 0x32, 0x30, 0x4a, 0x81, 0xa8, 0x42, 0xa3, 0x6c, 0x9b, 0x33, 0x6e, 0xca, |
31 0xb2, 0xf5, 0x53, 0x66, 0xe0, 0x27, 0x53, 0x86, 0x1a, 0x85, 0x1e, 0xa7, | 34 0xb2, 0xf5, 0x53, 0x66, 0xe0, 0x27, 0x53, 0x86, 0x1a, 0x85, 0x1e, 0xa7, |
32 0x39, 0x3f, 0x4a, 0x77, 0x8e, 0xfb, 0x54, 0x66, 0x66, 0xfb, 0x58, 0x54, | 35 0x39, 0x3f, 0x4a, 0x77, 0x8e, 0xfb, 0x54, 0x66, 0x66, 0xfb, 0x58, 0x54, |
33 0xc0, 0x5e, 0x39, 0xc7, 0xf5, 0x50, 0x06, 0x0b, 0xe0, 0x8a, 0xd4, 0xce, | 36 0xc0, 0x5e, 0x39, 0xc7, 0xf5, 0x50, 0x06, 0x0b, 0xe0, 0x8a, 0xd4, 0xce, |
34 0xe1, 0x6a, 0x55, 0x1f, 0x8b, 0x17, 0x00, 0xe6, 0x69, 0xa3, 0x27, 0xe6, | 37 0xe1, 0x6a, 0x55, 0x1f, 0x8b, 0x17, 0x00, 0xe6, 0x69, 0xa3, 0x27, 0xe6, |
35 0x08, 0x25, 0x69, 0x3c, 0x12, 0x9d, 0x8d, 0x05, 0x2c, 0xd6, 0x2e, 0xa2, | 38 0x08, 0x25, 0x69, 0x3c, 0x12, 0x9d, 0x8d, 0x05, 0x2c, 0xd6, 0x2e, 0xa2, |
36 0x31, 0xde, 0xb4, 0x52, 0x50, 0xd6, 0x20, 0x49, 0xde, 0x71, 0xa0, 0xf9, | 39 0x31, 0xde, 0xb4, 0x52, 0x50, 0xd6, 0x20, 0x49, 0xde, 0x71, 0xa0, 0xf9, |
37 0xad, 0x20, 0x40, 0x12, 0xf1, 0xdd, 0x25, 0xeb, 0xd5, 0xe6, 0xb8, 0x36, | 40 0xad, 0x20, 0x40, 0x12, 0xf1, 0xdd, 0x25, 0xeb, 0xd5, 0xe6, 0xb8, 0x36, |
38 0xf4, 0xd6, 0x8f, 0x7f, 0xca, 0x43, 0xdc, 0xd7, 0x10, 0x5b, 0xe6, 0x3f, | 41 0xf4, 0xd6, 0x8f, 0x7f, 0xca, 0x43, 0xdc, 0xd7, 0x10, 0x5b, 0xe6, 0x3f, |
39 0x51, 0x8a, 0x85, 0xb3, 0xf3, 0xff, 0xf6, 0x03, 0x2d, 0xcb, 0x23, 0x4f, | 42 0x51, 0x8a, 0x85, 0xb3, 0xf3, 0xff, 0xf6, 0x03, 0x2d, 0xcb, 0x23, 0x4f, |
40 0x9c, 0xad, 0x18, 0xe7, 0x93, 0x05, 0x8c, 0xac, 0x52, 0x9a, 0xf7, 0x4c, | 43 0x9c, 0xad, 0x18, 0xe7, 0x93, 0x05, 0x8c, 0xac, 0x52, 0x9a, 0xf7, 0x4c, |
41 0xe9, 0x99, 0x7a, 0xbe, 0x6e, 0x7e, 0x4d, 0x0a, 0xe3, 0xc6, 0x1c, 0xa9, | 44 0xe9, 0x99, 0x7a, 0xbe, 0x6e, 0x7e, 0x4d, 0x0a, 0xe3, 0xc6, 0x1c, 0xa9, |
42 0x93, 0xfa, 0x3a, 0xa5, 0x91, 0x5d, 0x1c, 0xbd, 0x66, 0xeb, 0xcc, 0x60, | 45 0x93, 0xfa, 0x3a, 0xa5, 0x91, 0x5d, 0x1c, 0xbd, 0x66, 0xeb, 0xcc, 0x60, |
43 0xdc, 0x86, 0x74, 0xca, 0xcf, 0xf8, 0x92, 0x1c, 0x98, 0x7d, 0x57, 0xfa, | 46 0xdc, 0x86, 0x74, 0xca, 0xcf, 0xf8, 0x92, 0x1c, 0x98, 0x7d, 0x57, 0xfa, |
44 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, 0x80, 0x2a, 0x92, 0xc5, | 47 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, 0x80, 0x2a, 0x92, 0xc5, |
45 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01 }; | 48 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01 }; |
46 | 49 |
50 static const net::SHA1HashValue kICAFingerprints[] = { | |
Ryan Sleevi
2014/04/30 01:32:13
Note: We don't use fingerprints for any (granting)
Munjal (Google)
2014/05/05 00:01:46
Let me see if I understand correctly. So you are s
Ryan Sleevi
2014/05/05 19:35:32
I apologize for the confusion. The goal is to have
Munjal (Google)
2014/05/05 21:00:27
So are you suggesting that we hard-code a list of
| |
51 { { 0x57, 0x16, 0xe2, 0xad, 0x73, 0x2e, 0xbe, 0xda, 0xeb, 0x18, | |
52 0xe8, 0x47, 0x15, 0xa8, 0xde, 0x90, 0x3b, 0x5e, 0x2a, 0xf4 } }, | |
53 { { 0x1b, 0xa2, 0x9e, 0xc9, 0x8e, 0x4e, 0xb3, 0x80, 0xee, 0x55, | |
54 0xb2, 0x97, 0xfd, 0x2e, 0x2b, 0x2c, 0xb6, 0x8e, 0x0b, 0x2f} }, | |
55 { { 0x97, 0x05, 0xce, 0xf6, 0x3f, 0xa9, 0x5e, 0x0f, 0xe7, 0x61, | |
56 0xfB, 0x08, 0x44, 0x31, 0xbe, 0xde, 0x01, 0xb8, 0xfB, 0xeb} } | |
57 }; | |
58 | |
47 typedef scoped_ptr< | 59 typedef scoped_ptr< |
48 CERTCertificate, | 60 CERTCertificate, |
49 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > | 61 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > |
50 ScopedCERTCertificate; | 62 ScopedCERTCertificate; |
51 | 63 |
64 // Returns whether |fingerprint| matches one of the hard-coded list of | |
65 // allowed ICA fingerprints. | |
66 static bool IsFingerprintAllowed(const net::SHA1HashValue& fingerprint) { | |
67 for (size_t i = 0; i < arraysize(kICAFingerprints); ++i) { | |
68 if (fingerprint.Equals(kICAFingerprints[i])) | |
69 return true; | |
70 } | |
71 return false; | |
72 } | |
73 | |
52 // Parses out DeviceAuthMessage from CastMessage | 74 // Parses out DeviceAuthMessage from CastMessage |
53 static bool ParseAuthMessage( | 75 static bool ParseAuthMessage( |
54 const extensions::api::cast_channel::CastMessage& challenge_reply, | 76 const extensions::api::cast_channel::CastMessage& challenge_reply, |
55 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { | 77 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { |
56 if (challenge_reply.payload_type() != | 78 if (challenge_reply.payload_type() != |
57 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { | 79 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { |
58 VLOG(1) << "Wrong payload type in challenge reply"; | 80 VLOG(1) << "Wrong payload type in challenge reply"; |
59 return false; | 81 return false; |
60 } | 82 } |
61 if (!challenge_reply.has_payload_binary()) { | 83 if (!challenge_reply.has_payload_binary()) { |
(...skipping 12 matching lines...) Expand all Loading... | |
74 if (!auth_message->has_response()) { | 96 if (!auth_message->has_response()) { |
75 VLOG(1) << "Auth message has no response field"; | 97 VLOG(1) << "Auth message has no response field"; |
76 return false; | 98 return false; |
77 } | 99 } |
78 return true; | 100 return true; |
79 } | 101 } |
80 | 102 |
81 // Authenticates the given credentials: | 103 // Authenticates the given credentials: |
82 // 1. |signature| verification of |data| using |certificate|. | 104 // 1. |signature| verification of |data| using |certificate|. |
83 // 2. |certificate| is signed by a trusted CA. | 105 // 2. |certificate| is signed by a trusted CA. |
84 bool VerifyCredentials(const std::string& certificate, | 106 bool VerifyCredentials( |
85 const std::string& signature, | 107 const extensions::api::cast_channel::AuthResponse& response, |
86 const std::string& data) { | 108 const std::string& data) { |
109 const std::string& certificate = response.client_auth_certificate(); | |
110 const std::string& signature = response.signature(); | |
111 | |
112 std::string ica_public_key_str; | |
113 // If the list of intermediates is not empty, then the fingerprint of | |
114 // the first intermediate should be one of the hard-coded list of | |
115 // fingerprints. | |
116 if (response.intermediate_certificate_size() > 0) { | |
mark a. foltz
2014/05/05 19:22:22
ISTM having two helper functions that extract the
| |
117 const std::string& ica = response.intermediate_certificate(0); | |
118 scoped_refptr<net::X509Certificate> ica_cert | |
119 = net::X509Certificate::CreateFromBytes(ica.data(), ica.length()); | |
120 if (!IsFingerprintAllowed(ica_cert->fingerprint())) { | |
121 VLOG(1) << "Disallowed intermdiate cert: " << ica; | |
mark a. foltz
2014/05/05 19:22:22
ica looks to be a binary blob that is parsed to yi
| |
122 return false; | |
123 } | |
124 base::StringPiece ica_public_key; | |
125 if (!net::asn1::ExtractSPKIFromDERCert(ica, &ica_public_key)) { | |
126 VLOG(1) << "Could not extract public key from intermediate cert: " << ica; | |
mark a. foltz
2014/05/05 19:22:22
Ditto.
| |
127 return false; | |
128 } | |
129 ica_public_key_str = ica_public_key.as_string(); | |
130 } | |
Munjal (Google)
2014/05/05 00:01:46
Ryan, does this logic look OK to you?
| |
131 | |
87 crypto::EnsureNSSInit(); | 132 crypto::EnsureNSSInit(); |
88 SECItem der_cert; | 133 SECItem der_cert; |
89 der_cert.type = siDERCertBuffer; | 134 der_cert.type = siDERCertBuffer; |
90 // Make a copy of certificate string so it is safe to type cast. | 135 // Make a copy of certificate string so it is safe to type cast. |
91 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( | 136 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( |
92 certificate.data())); | 137 certificate.data())); |
93 der_cert.len = certificate.length(); | 138 der_cert.len = certificate.length(); |
94 | 139 |
95 // Parse into a certificate structure. | 140 // Parse into a certificate structure. |
96 ScopedCERTCertificate cert(CERT_NewTempCertificate( | 141 ScopedCERTCertificate cert(CERT_NewTempCertificate( |
97 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); | 142 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); |
98 if (!cert.get()) { | 143 if (!cert.get()) { |
99 VLOG(1) << "Failed to parse certificate."; | 144 VLOG(1) << "Failed to parse certificate."; |
100 return false; | 145 return false; |
101 } | 146 } |
102 | 147 |
103 // Check that the certificate is signed by trusted CA. | 148 // Check that the certificate is signed by trusted CA. |
104 SECItem trusted_ca_key_der_item; | 149 SECItem trusted_ca_key_der_item; |
105 trusted_ca_key_der_item.type = siDERCertBuffer; | 150 trusted_ca_key_der_item.type = siDERCertBuffer; |
106 trusted_ca_key_der_item.data = const_cast<unsigned char*>(kCAPublicKeyDER); | 151 // Use the hard-coded CA public key if response has no intermediates. |
107 trusted_ca_key_der_item.len = sizeof(kCAPublicKeyDER); | 152 if (ica_public_key_str.empty()) { |
153 trusted_ca_key_der_item.data = const_cast<unsigned char*>(kCAPublicKeyDER); | |
154 trusted_ca_key_der_item.len = sizeof(kCAPublicKeyDER); | |
155 } else { | |
156 trusted_ca_key_der_item.data = reinterpret_cast<unsigned char*>( | |
mark a. foltz
2014/05/05 19:22:22
You are taking the data pointer out of ica_public_
Ryan Sleevi
2014/05/05 19:35:32
This is fine/safe/blessed. NSS is slowly becoming
| |
157 const_cast<char*>(ica_public_key_str.data())); | |
158 trusted_ca_key_der_item.len = ica_public_key_str.length(); | |
159 } | |
160 | |
108 crypto::ScopedSECKEYPublicKey ca_public_key( | 161 crypto::ScopedSECKEYPublicKey ca_public_key( |
109 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA)); | 162 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA)); |
110 SECStatus verified = CERT_VerifySignedDataWithPublicKey( | 163 SECStatus verified = CERT_VerifySignedDataWithPublicKey( |
111 &cert->signatureWrap, ca_public_key.get(), NULL); | 164 &cert->signatureWrap, ca_public_key.get(), NULL); |
112 if (verified != SECSuccess) { | 165 if (verified != SECSuccess) { |
113 VLOG(1)<< "Cert not signed by trusted CA"; | 166 VLOG(1)<< "Cert not signed by trusted CA"; |
114 return false; | 167 return false; |
115 } | 168 } |
116 | 169 |
117 // Verify that the |signature| matches |data|. | 170 // Verify that the |signature| matches |data|. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 const std::string& peer_cert) { | 203 const std::string& peer_cert) { |
151 if (peer_cert.empty()) | 204 if (peer_cert.empty()) |
152 return false; | 205 return false; |
153 | 206 |
154 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); | 207 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); |
155 DeviceAuthMessage auth_message; | 208 DeviceAuthMessage auth_message; |
156 if (!ParseAuthMessage(challenge_reply, &auth_message)) | 209 if (!ParseAuthMessage(challenge_reply, &auth_message)) |
157 return false; | 210 return false; |
158 | 211 |
159 const AuthResponse& response = auth_message.response(); | 212 const AuthResponse& response = auth_message.response(); |
160 return VerifyCredentials(response.client_auth_certificate(), | 213 return VerifyCredentials(response, peer_cert); |
161 response.signature(), | |
162 peer_cert); | |
163 } | 214 } |
164 | 215 |
165 } // namespace cast_channel | 216 } // namespace cast_channel |
166 } // namespace api | 217 } // namespace api |
167 } // namespace extensions | 218 } // namespace extensions |
OLD | NEW |