OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/cast_channel/cast_auth_util.h" | |
6 | |
7 #include <cert.h> | |
8 #include <cryptohi.h> | |
9 #include <pk11pub.h> | |
10 #include <seccomon.h> | |
11 #include <string> | |
12 | |
13 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" | |
14 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" | |
15 #include "crypto/nss_util.h" | |
16 #include "crypto/scoped_nss_types.h" | |
17 #include "net/cert/x509_certificate.h" | |
Ryan Sleevi
2013/10/23 23:28:51
Why do you need to bring this in? You don't seem t
Munjal (Google)
2013/10/24 00:00:56
Done.
| |
18 | |
19 namespace { | |
20 | |
21 // Public key of the certificate with which the peer cert should be signed. | |
22 static const unsigned char kCAPublicKeyDER[] = { | |
23 0x30, 0x82, 0x03, 0x87, 0x30, 0x82, 0x02, 0x6f, 0xa0, 0x03, 0x02, 0x01, | |
24 0x02, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | |
25 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x7c, 0x31, 0x0b, 0x30, | |
26 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, | |
27 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x43, 0x61, 0x6c, | |
28 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61, 0x31, 0x16, 0x30, 0x14, 0x06, | |
29 0x03, 0x55, 0x04, 0x07, 0x0c, 0x0d, 0x4d, 0x6f, 0x75, 0x6e, 0x74, 0x61, | |
30 0x69, 0x6e, 0x20, 0x56, 0x69, 0x65, 0x77, 0x31, 0x13, 0x30, 0x11, 0x06, | |
31 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0a, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
32 0x20, 0x49, 0x6e, 0x63, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, | |
33 0x0b, 0x0c, 0x09, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20, 0x54, 0x56, | |
34 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0e, 0x45, | |
35 0x75, 0x72, 0x65, 0x6b, 0x61, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, | |
36 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x32, 0x31, 0x32, 0x31, 0x39, 0x30, | |
37 0x30, 0x34, 0x37, 0x31, 0x32, 0x5a, 0x17, 0x0d, 0x33, 0x32, 0x31, 0x32, | |
38 0x31, 0x34, 0x30, 0x30, 0x34, 0x37, 0x31, 0x32, 0x5a, 0x30, 0x7d, 0x31, | |
39 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, | |
40 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x43, | |
41 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61, 0x31, 0x16, 0x30, | |
42 0x14, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x0d, 0x4d, 0x6f, 0x75, 0x6e, | |
43 0x74, 0x61, 0x69, 0x6e, 0x20, 0x56, 0x69, 0x65, 0x77, 0x31, 0x13, 0x30, | |
44 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0a, 0x47, 0x6f, 0x6f, 0x67, | |
45 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, | |
46 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20, | |
47 0x54, 0x56, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, | |
48 0x0f, 0x45, 0x75, 0x72, 0x65, 0x6b, 0x61, 0x20, 0x47, 0x65, 0x6e, 0x31, | |
49 0x20, 0x49, 0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, | |
50 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, | |
51 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, | |
52 0x00, 0xbc, 0x22, 0x80, 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, | |
53 0x76, 0x5e, 0x35, 0x7f, 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, | |
54 0x2f, 0x05, 0x87, 0x28, 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, | |
55 0xb8, 0x82, 0xfa, 0xdf, 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, | |
56 0xb7, 0x13, 0x72, 0x80, 0xa4, 0x47, 0x15, 0xc1, 0x20, 0x99, 0x50, 0xcd, | |
57 0xec, 0x14, 0x62, 0x09, 0x5b, 0xa4, 0x98, 0xcd, 0xd2, 0x41, 0xb6, 0x36, | |
58 0x4e, 0xff, 0xe8, 0x2e, 0x32, 0x30, 0x4a, 0x81, 0xa8, 0x42, 0xa3, 0x6c, | |
59 0x9b, 0x33, 0x6e, 0xca, 0xb2, 0xf5, 0x53, 0x66, 0xe0, 0x27, 0x53, 0x86, | |
60 0x1a, 0x85, 0x1e, 0xa7, 0x39, 0x3f, 0x4a, 0x77, 0x8e, 0xfb, 0x54, 0x66, | |
61 0x66, 0xfb, 0x58, 0x54, 0xc0, 0x5e, 0x39, 0xc7, 0xf5, 0x50, 0x06, 0x0b, | |
62 0xe0, 0x8a, 0xd4, 0xce, 0xe1, 0x6a, 0x55, 0x1f, 0x8b, 0x17, 0x00, 0xe6, | |
63 0x69, 0xa3, 0x27, 0xe6, 0x08, 0x25, 0x69, 0x3c, 0x12, 0x9d, 0x8d, 0x05, | |
64 0x2c, 0xd6, 0x2e, 0xa2, 0x31, 0xde, 0xb4, 0x52, 0x50, 0xd6, 0x20, 0x49, | |
65 0xde, 0x71, 0xa0, 0xf9, 0xad, 0x20, 0x40, 0x12, 0xf1, 0xdd, 0x25, 0xeb, | |
66 0xd5, 0xe6, 0xb8, 0x36, 0xf4, 0xd6, 0x8f, 0x7f, 0xca, 0x43, 0xdc, 0xd7, | |
67 0x10, 0x5b, 0xe6, 0x3f, 0x51, 0x8a, 0x85, 0xb3, 0xf3, 0xff, 0xf6, 0x03, | |
68 0x2d, 0xcb, 0x23, 0x4f, 0x9c, 0xad, 0x18, 0xe7, 0x93, 0x05, 0x8c, 0xac, | |
69 0x52, 0x9a, 0xf7, 0x4c, 0xe9, 0x99, 0x7a, 0xbe, 0x6e, 0x7e, 0x4d, 0x0a, | |
70 0xe3, 0xc6, 0x1c, 0xa9, 0x93, 0xfa, 0x3a, 0xa5, 0x91, 0x5d, 0x1c, 0xbd, | |
71 0x66, 0xeb, 0xcc, 0x60, 0xdc, 0x86, 0x74, 0xca, 0xcf, 0xf8, 0x92, 0x1c, | |
72 0x98, 0x7d, 0x57, 0xfa, 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, | |
73 0x80, 0x2a, 0x92, 0xc5, 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x13, | |
74 0x30, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, | |
75 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, | |
76 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x82, | |
77 0x01, 0x01, 0x00, 0x8b, 0xd4, 0xa1, 0xb1, 0xcf, 0x5d, 0xcd, 0x7b, 0x6c, | |
78 0x48, 0x4a, 0x41, 0x1f, 0x53, 0x2f, 0x18, 0x2d, 0x32, 0x45, 0xff, 0x9e, | |
79 0xab, 0xd3, 0x73, 0x3e, 0x1f, 0x22, 0xd7, 0xea, 0xfa, 0x01, 0xe6, 0x73, | |
80 0x03, 0x0f, 0x2b, 0xc6, 0x25, 0xbb, 0xa5, 0xee, 0xc5, 0xf5, 0x45, 0xcb, | |
81 0x24, 0x12, 0x2a, 0xad, 0xc2, 0x5d, 0x05, 0xf4, 0x7a, 0xf5, 0xc2, 0x9b, | |
82 0x10, 0x16, 0x5a, 0xd1, 0x0a, 0x73, 0xc5, 0x16, 0x39, 0xa0, 0x10, 0xca, | |
83 0xd1, 0x68, 0x85, 0x9e, 0xfb, 0x9e, 0x26, 0x83, 0x8e, 0x58, 0xf3, 0x77, | |
84 0xa0, 0x4e, 0xe5, 0xdb, 0x97, 0xbe, 0x2d, 0x00, 0x5f, 0xf5, 0x94, 0xdb, | |
85 0xb1, 0x9d, 0x65, 0x6b, 0xfd, 0xf0, 0xd1, 0x04, 0x51, 0xdf, 0xcc, 0x92, | |
86 0xa6, 0x99, 0x2d, 0x71, 0xf5, 0x4d, 0xd5, 0x23, 0xfe, 0x33, 0x1c, 0xa9, | |
87 0xb4, 0xab, 0xc5, 0xbf, 0x1a, 0xb8, 0xd1, 0x80, 0xef, 0x89, 0xc9, 0xe2, | |
88 0x1f, 0x9c, 0x4c, 0x48, 0x3b, 0xa2, 0xfa, 0x02, 0x0a, 0xdc, 0x84, 0x01, | |
89 0x8a, 0x87, 0x02, 0xfb, 0x59, 0xee, 0xa7, 0x4c, 0x04, 0x7d, 0x74, 0x99, | |
90 0x87, 0x6a, 0x25, 0x44, 0xad, 0x16, 0xaa, 0xec, 0x4e, 0x35, 0x1b, 0x7c, | |
91 0x7b, 0x84, 0xc9, 0xb1, 0x3f, 0xe1, 0x82, 0x70, 0xe5, 0x0d, 0xe7, 0xd9, | |
92 0x6d, 0xfa, 0x95, 0xb6, 0xc5, 0xe4, 0x1e, 0xe8, 0x11, 0x9b, 0xd8, 0xb2, | |
93 0xf3, 0xa4, 0xfd, 0x13, 0xf3, 0x83, 0x4f, 0xf7, 0x07, 0x14, 0x20, 0xbb, | |
94 0x22, 0xa5, 0xa6, 0x8f, 0xd6, 0xb5, 0xdb, 0xa9, 0x74, 0x78, 0xe2, 0x93, | |
95 0x0d, 0xe5, 0x23, 0x2f, 0x05, 0x17, 0xe0, 0xb2, 0x97, 0x67, 0x34, 0x4d, | |
96 0x0f, 0x9c, 0x76, 0x43, 0x7b, 0xa6, 0x21, 0x4a, 0x56, 0x05, 0xf6, 0x2a, | |
97 0x7c, 0xf2, 0x7f, 0x12, 0x94, 0x82, 0x26, 0x29, 0x07, 0xf0, 0x0b, 0x6c, | |
98 0x6c, 0x79, 0x14, 0xb0, 0x74, 0xd5, 0x6c }; | |
99 | |
100 typedef scoped_ptr_malloc< | |
101 CERTCertificate, | |
102 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > | |
103 ScopedCERTCertificate; | |
104 | |
105 // Parses out DeviceAuthMessage from CastMessage | |
106 static bool ParseAuthMessage( | |
107 const extensions::api::cast_channel::CastMessage& challenge_reply, | |
108 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { | |
109 if (challenge_reply.payload_type() != | |
110 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { | |
111 DVLOG(1) << "Wrong payload type in challenge reply"; | |
112 return false; | |
113 } | |
114 if (!challenge_reply.has_payload_binary()) { | |
115 DVLOG(1) << "Payload type is binary but payload_binary field not set"; | |
116 return false; | |
117 } | |
118 if (!auth_message->ParseFromString(challenge_reply.payload_binary())) { | |
119 DVLOG(1) << "Cannot parse binary payload into DeviceAuthMessage"; | |
120 return false; | |
121 } | |
122 DVLOG(1) << "Auth message: " << AuthMessageToString(*auth_message); | |
123 if (auth_message->has_error()) { | |
124 DVLOG(1) << "Auth message error: " << auth_message->error().error_type(); | |
125 return false; | |
126 } | |
127 if (!auth_message->has_response()) { | |
128 DVLOG(1) << "Auth message has no response field"; | |
129 return false; | |
130 } | |
131 return true; | |
132 } | |
133 | |
134 // Authenticates the given credentials: | |
135 // 1. |signature| verification of |data| using |certificate|. | |
136 // 2. |certificate| is signed by a trusted CA. | |
137 bool VerifyCredentials(const std::string& certificate, | |
138 const std::string& signature, | |
139 const std::string& data) { | |
140 crypto::EnsureNSSInit(); | |
141 SECItem der_cert; | |
142 der_cert.type = siDERCertBuffer; | |
143 // Make a copy of certificate string so it is safe to type cast. | |
144 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( | |
145 certificate.data())); | |
146 der_cert.len = certificate.length(); | |
147 | |
148 // Parse into a certificate structure. | |
149 ScopedCERTCertificate cert(CERT_NewTempCertificate( | |
150 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); | |
151 if (!cert.get()) { | |
152 DVLOG(1) << "Failed to parse certificate."; | |
153 return false; | |
154 } | |
155 | |
156 // Check that the certificate is signed by trusted CA. | |
157 SECItem trusted_ca_key_der_item; | |
158 trusted_ca_key_der_item.type = siDERCertBuffer; | |
159 trusted_ca_key_der_item.data = const_cast<unsigned char*>(kCAPublicKeyDER); | |
160 trusted_ca_key_der_item.len = sizeof(kCAPublicKeyDER); | |
161 crypto::ScopedSECKEYPublicKey ca_public_key( | |
162 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA)); | |
163 SECStatus verified = CERT_VerifySignedDataWithPublicKey( | |
164 &cert->signatureWrap, ca_public_key.get(), NULL); | |
165 if (verified != SECSuccess) { | |
166 DVLOG(1)<< "Cert not signed by trusted CA"; | |
167 return false; | |
168 } | |
169 | |
170 // Verify that the |signature| matches |data|. | |
171 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); | |
172 if (!public_key.get()) { | |
173 DVLOG(1) << "Unable to extract public key from certificate."; | |
174 return false; | |
175 } | |
176 SECItem signature_item; | |
177 signature_item.type = siBuffer; | |
178 signature_item.data = reinterpret_cast<unsigned char*>( | |
179 const_cast<char*>(signature.data())); | |
180 signature_item.len = signature.length(); | |
181 verified = VFY_VerifyDataDirect( | |
182 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), | |
183 data.size(), | |
184 public_key.get(), | |
185 &signature_item, | |
186 SEC_OID_PKCS1_RSA_ENCRYPTION, | |
187 SEC_OID_SHA1, NULL, NULL); | |
188 | |
189 if (verified != SECSuccess) { | |
190 DVLOG(1) << "Signed blobs did not match."; | |
191 return false; | |
192 } | |
193 return true; | |
194 } | |
195 | |
196 } // namespace | |
197 | |
198 namespace extensions { | |
199 namespace api { | |
200 namespace cast_channel { | |
201 | |
202 bool AuthenticateChallengeReply(const CastMessage& challenge_reply, | |
203 const std::string& peer_cert) { | |
204 if (peer_cert.empty()) | |
205 return false; | |
206 | |
207 DVLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); | |
208 DeviceAuthMessage auth_message; | |
209 if (!ParseAuthMessage(challenge_reply, &auth_message)) | |
210 return false; | |
211 | |
212 const AuthResponse& response = auth_message.response(); | |
213 return VerifyCredentials(response.client_auth_certificate(), | |
214 response.signature(), | |
215 peer_cert); | |
216 } | |
217 | |
218 } // namespace cast_channel | |
219 } // namespace api | |
220 } // namespace extensions | |
OLD | NEW |