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 "base/logging.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" | |
16 #include "crypto/nss_util.h" | |
17 #include "crypto/scoped_nss_types.h" | |
18 #include "net/base/hash_value.h" | |
19 #include "net/cert/x509_certificate.h" | |
20 | |
21 namespace { | |
22 | |
23 // Fingerprints and public keys of the allowed / trusted ICAs. | |
24 static const net::SHA1HashValue kFingerprintICA1 = { { | |
25 0x57,0x16,0xE2,0xAD,0x73,0x2E,0xBE,0xDA,0xEB,0x18, | |
26 0xE8,0x47,0x15,0xA8,0xDE,0x90,0x3B,0x5E,0x2A,0xF4 | |
27 } }; | |
28 static const unsigned char kPublicKeyICA1[] = { | |
29 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xBC,0x22,0x80, | |
30 0xBD,0x80,0xF6,0x3A,0x21,0x00,0x3B,0xAE,0x76,0x5E,0x35,0x7F, | |
31 0x3D,0xC3,0x64,0x5C,0x55,0x94,0x86,0x34,0x2F,0x05,0x87,0x28, | |
32 0xCD,0xF7,0x69,0x8C,0x17,0xB3,0x50,0xA7,0xB8,0x82,0xFA,0xDF, | |
33 0xC7,0x43,0x2D,0xD6,0x7E,0xAB,0xA0,0x6F,0xB7,0x13,0x72,0x80, | |
34 0xA4,0x47,0x15,0xC1,0x20,0x99,0x50,0xCD,0xEC,0x14,0x62,0x09, | |
35 0x5B,0xA4,0x98,0xCD,0xD2,0x41,0xB6,0x36,0x4E,0xFF,0xE8,0x2E, | |
36 0x32,0x30,0x4A,0x81,0xA8,0x42,0xA3,0x6C,0x9B,0x33,0x6E,0xCA, | |
37 0xB2,0xF5,0x53,0x66,0xE0,0x27,0x53,0x86,0x1A,0x85,0x1E,0xA7, | |
38 0x39,0x3F,0x4A,0x77,0x8E,0xFB,0x54,0x66,0x66,0xFB,0x58,0x54, | |
39 0xC0,0x5E,0x39,0xC7,0xF5,0x50,0x06,0x0B,0xE0,0x8A,0xD4,0xCE, | |
40 0xE1,0x6A,0x55,0x1F,0x8B,0x17,0x00,0xE6,0x69,0xA3,0x27,0xE6, | |
41 0x08,0x25,0x69,0x3C,0x12,0x9D,0x8D,0x05,0x2C,0xD6,0x2E,0xA2, | |
42 0x31,0xDE,0xB4,0x52,0x50,0xD6,0x20,0x49,0xDE,0x71,0xA0,0xF9, | |
43 0xAD,0x20,0x40,0x12,0xF1,0xDD,0x25,0xEB,0xD5,0xE6,0xB8,0x36, | |
44 0xF4,0xD6,0x8F,0x7F,0xCA,0x43,0xDC,0xD7,0x10,0x5B,0xE6,0x3F, | |
45 0x51,0x8A,0x85,0xB3,0xF3,0xFF,0xF6,0x03,0x2D,0xCB,0x23,0x4F, | |
46 0x9C,0xAD,0x18,0xE7,0x93,0x05,0x8C,0xAC,0x52,0x9A,0xF7,0x4C, | |
47 0xE9,0x99,0x7A,0xBE,0x6E,0x7E,0x4D,0x0A,0xE3,0xC6,0x1C,0xA9, | |
48 0x93,0xFA,0x3A,0xA5,0x91,0x5D,0x1C,0xBD,0x66,0xEB,0xCC,0x60, | |
49 0xDC,0x86,0x74,0xCA,0xCF,0xF8,0x92,0x1C,0x98,0x7D,0x57,0xFA, | |
50 0x61,0x47,0x9E,0xAB,0x80,0xB7,0xE4,0x48,0x80,0x2A,0x92,0xC5, | |
51 0x1B,0x02,0x03,0x01,0x00,0x01 | |
52 }; | |
53 | |
54 static const net::SHA1HashValue kFingerprintICA2 = { { | |
55 0x1B,0xA2,0x9E,0xC9,0x8E,0x4E,0xB3,0x80,0xEE,0x55, | |
56 0xB2,0x97,0xFD,0x2E,0x2B,0x2C,0xB6,0x8E,0x0B,0x2F | |
57 } }; | |
58 static const unsigned char kPublicKeyICA2[] = { | |
59 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xBC,0x22,0x80, | |
60 0xBD,0x80,0xF6,0x3A,0x21,0x00,0x3B,0xAE,0x76,0x5E,0x35,0x7F, | |
61 0x3D,0xC3,0x64,0x5C,0x55,0x94,0x86,0x34,0x2F,0x05,0x87,0x28, | |
62 0xCD,0xF7,0x69,0x8C,0x17,0xB3,0x50,0xA7,0xB8,0x82,0xFA,0xDF, | |
63 0xC7,0x43,0x2D,0xD6,0x7E,0xAB,0xA0,0x6F,0xB7,0x13,0x72,0x80, | |
64 0xA4,0x47,0x15,0xC1,0x20,0x99,0x50,0xCD,0xEC,0x14,0x62,0x09, | |
65 0x5B,0xA4,0x98,0xCD,0xD2,0x41,0xB6,0x36,0x4E,0xFF,0xE8,0x2E, | |
66 0x32,0x30,0x4A,0x81,0xA8,0x42,0xA3,0x6C,0x9B,0x33,0x6E,0xCA, | |
67 0xB2,0xF5,0x53,0x66,0xE0,0x27,0x53,0x86,0x1A,0x85,0x1E,0xA7, | |
68 0x39,0x3F,0x4A,0x77,0x8E,0xFB,0x54,0x66,0x66,0xFB,0x58,0x54, | |
69 0xC0,0x5E,0x39,0xC7,0xF5,0x50,0x06,0x0B,0xE0,0x8A,0xD4,0xCE, | |
70 0xE1,0x6A,0x55,0x1F,0x8B,0x17,0x00,0xE6,0x69,0xA3,0x27,0xE6, | |
71 0x08,0x25,0x69,0x3C,0x12,0x9D,0x8D,0x05,0x2C,0xD6,0x2E,0xA2, | |
72 0x31,0xDE,0xB4,0x52,0x50,0xD6,0x20,0x49,0xDE,0x71,0xA0,0xF9, | |
73 0xAD,0x20,0x40,0x12,0xF1,0xDD,0x25,0xEB,0xD5,0xE6,0xB8,0x36, | |
74 0xF4,0xD6,0x8F,0x7F,0xCA,0x43,0xDC,0xD7,0x10,0x5B,0xE6,0x3F, | |
75 0x51,0x8A,0x85,0xB3,0xF3,0xFF,0xF6,0x03,0x2D,0xCB,0x23,0x4F, | |
76 0x9C,0xAD,0x18,0xE7,0x93,0x05,0x8C,0xAC,0x52,0x9A,0xF7,0x4C, | |
77 0xE9,0x99,0x7A,0xBE,0x6E,0x7E,0x4D,0x0A,0xE3,0xC6,0x1C,0xA9, | |
78 0x93,0xFA,0x3A,0xA5,0x91,0x5D,0x1C,0xBD,0x66,0xEB,0xCC,0x60, | |
79 0xDC,0x86,0x74,0xCA,0xCF,0xF8,0x92,0x1C,0x98,0x7D,0x57,0xFA, | |
80 0x61,0x47,0x9E,0xAB,0x80,0xB7,0xE4,0x48,0x80,0x2A,0x92,0xC5, | |
81 0x1B,0x02,0x03,0x01,0x00,0x01 | |
82 }; | |
83 | |
84 static const net::SHA1HashValue kFingerprintICA3 = { { | |
85 0x97,0x05,0xCE,0xF6,0x3F,0xA9,0x5E,0x0F,0xE7,0x61, | |
86 0xFB,0x08,0x44,0x31,0xBE,0xDE,0x01,0xB8,0xFB,0xEB | |
87 } }; | |
88 static const unsigned char kPublicKeyICA3[] = { | |
89 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xB7,0xE8,0xC3, | |
90 0xE4,0x2C,0xDE,0x74,0x53,0xF2,0x49,0x95,0x6D,0xD1,0xDA,0x69, | |
91 0x57,0x0D,0x86,0xE5,0xED,0xB4,0xB9,0xE6,0x73,0x9F,0x6C,0xAD, | |
92 0x3B,0x64,0x85,0x03,0x0D,0x08,0x44,0xAF,0x18,0x69,0x82,0xAD, | |
93 0xA9,0x74,0x64,0x37,0x47,0xE1,0xE7,0x26,0x19,0x33,0x3C,0xE2, | |
94 0xD0,0xB5,0x84,0x3C,0xD7,0xAC,0x63,0xAE,0xC4,0x32,0x23,0xF6, | |
95 0xDC,0x14,0x10,0x4B,0x95,0x7F,0xE8,0x98,0xD7,0x7A,0x9E,0x43, | |
96 0x3D,0x68,0x8B,0x2A,0x70,0xF7,0x1E,0x43,0x70,0xBA,0xA5,0xA5, | |
97 0x93,0xAD,0x8A,0xD4,0x9F,0xAC,0x83,0x16,0xF3,0x48,0x5F,0xC5, | |
98 0xE0,0xA5,0x44,0xB8,0x4F,0xD9,0xD8,0x75,0x90,0x25,0x8B,0xE3, | |
99 0x1C,0x6C,0xDA,0x88,0xFF,0x09,0x2B,0xCA,0x1E,0x48,0xDD,0x76, | |
100 0x0F,0x68,0x56,0x7B,0x15,0x9D,0xCA,0x6B,0x1C,0xF7,0x48,0xC2, | |
101 0x89,0xC6,0x93,0x0A,0x31,0xF2,0x78,0x27,0x45,0x3D,0xF1,0x0D, | |
102 0x5B,0x6E,0x55,0x32,0xEF,0x49,0xA0,0xD6,0xAF,0xA6,0x30,0x91, | |
103 0xF2,0x21,0x2F,0xDB,0xA4,0x29,0xB9,0x9B,0x22,0xBC,0xCD,0x0B, | |
104 0xA6,0x8B,0xA6,0x22,0x79,0xFD,0xCF,0x95,0x93,0x96,0xB3,0x23, | |
105 0xC9,0xC6,0x30,0x8E,0xC0,0xE9,0x1F,0xEC,0xFB,0xF5,0x88,0xDD, | |
106 0x97,0x72,0x16,0x29,0x08,0xFA,0x42,0xE7,0x4F,0xCA,0xAE,0xD7, | |
107 0x0F,0x23,0x48,0x9B,0x82,0xA7,0x37,0x4A,0xDD,0x60,0x04,0x75, | |
108 0xDC,0xDE,0x09,0x98,0xD2,0x16,0x23,0x04,0x70,0x4D,0x99,0x9F, | |
109 0x4A,0x82,0x28,0xE6,0xBE,0x8F,0x9D,0xBF,0xA1,0x4B,0xA2,0xBA, | |
110 0xF5,0xB2,0x51,0x1E,0x4E,0xE7,0x80,0x9E,0x7A,0x38,0xA1,0xC7, | |
111 0x09,0x02,0x03,0x01,0x00,0x01 | |
112 }; | |
113 | |
114 static const net::SHA1HashValue kFingerprintICA4 = { { | |
115 0x01,0xF5,0x28,0x56,0x33,0x80,0x9B,0x31,0xE7,0xD9, | |
116 0xF7,0x4E,0xAA,0xDD,0x97,0x37,0xA0,0x28,0xE7,0x24 | |
117 } }; | |
118 static const unsigned char kPublicKeyICA4[] = { | |
119 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xB0,0x0E,0x5E, | |
120 0x07,0x3A,0xDF,0xA4,0x5F,0x68,0xF7,0x21,0xC7,0x64,0xDB,0xB6, | |
121 0x76,0xEF,0xEE,0x8B,0x93,0xF8,0xF6,0x1B,0x88,0xE1,0x93,0xB7, | |
122 0x17,0xF0,0x15,0x1E,0x7E,0x52,0x55,0x77,0x3C,0x02,0x8D,0x7B, | |
123 0x4A,0x6C,0xD3,0xBD,0xD6,0xC1,0x9C,0x72,0xC8,0xB3,0x15,0xCF, | |
124 0x11,0xC1,0xF5,0x46,0xC4,0xD5,0x20,0x47,0xFB,0x30,0xF4,0xE4, | |
125 0x61,0x0C,0x68,0xF0,0x5E,0xAB,0x37,0x8E,0x9B,0xE1,0xBC,0x81, | |
126 0xC3,0x70,0x8A,0x78,0xD6,0x83,0x34,0x32,0x9C,0x19,0x62,0xEB, | |
127 0xE4,0x9C,0xED,0xE3,0x64,0x6C,0x41,0x1D,0x9C,0xD2,0x8B,0x48, | |
128 0x4C,0x23,0x90,0x95,0xB3,0xE7,0x52,0xEA,0x05,0x57,0xCC,0x60, | |
129 0xB3,0xBA,0x14,0xE4,0xBA,0x00,0x39,0xE4,0x46,0x55,0x74,0xCE, | |
130 0x5A,0x8E,0x7A,0x67,0x23,0xDA,0x68,0x0A,0xFA,0xC4,0x84,0x1E, | |
131 0xB4,0xC5,0xA1,0xA2,0x6A,0x73,0x1F,0x6E,0xC8,0x2E,0x2F,0x9A, | |
132 0x9E,0xA8,0xB1,0x0E,0xFD,0x87,0xA6,0x8F,0x4D,0x3D,0x4B,0x05, | |
133 0xD5,0x35,0x5A,0x74,0x4D,0xBC,0x8E,0x82,0x44,0x96,0xF4,0xB5, | |
134 0x95,0x60,0x4E,0xA5,0xDF,0x27,0x3D,0x41,0x5C,0x07,0xA3,0xB4, | |
135 0x35,0x5A,0xB3,0x9E,0xF2,0x05,0x24,0xCA,0xCD,0x31,0x5A,0x0D, | |
136 0x26,0x4C,0xD4,0xD3,0xFD,0x50,0xE1,0x34,0xE9,0x4C,0x81,0x58, | |
137 0x30,0xB2,0xC7,0x7A,0xDD,0x81,0x89,0xA6,0xD4,0x3A,0x38,0x84, | |
138 0x03,0xB7,0x34,0x9E,0x77,0x3F,0xFF,0x78,0x07,0x5B,0x99,0xC1, | |
139 0xB2,0x1F,0x35,0x56,0x6E,0x3A,0x3C,0x0C,0x25,0xE1,0x57,0xF6, | |
140 0x8A,0x7E,0x49,0xC0,0xCC,0x83,0x11,0x35,0xE7,0x91,0x6D,0x2E, | |
141 0x65,0x02,0x03,0x01,0x00,0x01 | |
142 }; | |
143 | |
144 // Info for trusted ICA certs. | |
145 struct ICACertInfo { | |
146 const net::SHA1HashValue* fingerprint; | |
147 SECItem public_key; | |
148 }; | |
149 | |
150 // List of allowed / trusted ICAs. | |
151 static const ICACertInfo kAllowedICAs[] = { | |
152 { &kFingerprintICA1, | |
153 { siDERCertBuffer, | |
154 const_cast<unsigned char*>(kPublicKeyICA1), | |
155 sizeof(kPublicKeyICA1) } }, | |
156 { &kFingerprintICA2, | |
157 { siDERCertBuffer, | |
158 const_cast<unsigned char*>(kPublicKeyICA2), | |
159 sizeof(kPublicKeyICA2) } }, | |
160 { &kFingerprintICA3, | |
161 { siDERCertBuffer, | |
162 const_cast<unsigned char*>(kPublicKeyICA3), | |
163 sizeof(kPublicKeyICA3) } }, | |
164 { &kFingerprintICA4, | |
165 { siDERCertBuffer, | |
166 const_cast<unsigned char*>(kPublicKeyICA4), | |
167 sizeof(kPublicKeyICA4) } }, | |
168 }; | |
169 | |
170 typedef scoped_ptr< | |
171 CERTCertificate, | |
172 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > | |
173 ScopedCERTCertificate; | |
174 | |
175 // Returns the index of the ICA whose fingerprint matches |fingerprint|. | |
176 // Returns -1, if no such ICA is found. | |
177 static int GetICAWithFingerprint(const net::SHA1HashValue& fingerprint) { | |
178 for (size_t i = 0; i < arraysize(kAllowedICAs); ++i) { | |
179 if (kAllowedICAs[i].fingerprint->Equals(fingerprint)) | |
180 return static_cast<int>(i); | |
181 } | |
182 return -1; | |
183 } | |
184 | |
185 // Parses out DeviceAuthMessage from CastMessage | |
186 static bool ParseAuthMessage( | |
187 const extensions::api::cast_channel::CastMessage& challenge_reply, | |
188 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { | |
189 if (challenge_reply.payload_type() != | |
190 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { | |
191 VLOG(1) << "Wrong payload type in challenge reply"; | |
192 return false; | |
193 } | |
194 if (!challenge_reply.has_payload_binary()) { | |
195 VLOG(1) << "Payload type is binary but payload_binary field not set"; | |
196 return false; | |
197 } | |
198 if (!auth_message->ParseFromString(challenge_reply.payload_binary())) { | |
199 VLOG(1) << "Cannot parse binary payload into DeviceAuthMessage"; | |
200 return false; | |
201 } | |
202 VLOG(1) << "Auth message: " << AuthMessageToString(*auth_message); | |
203 if (auth_message->has_error()) { | |
204 VLOG(1) << "Auth message error: " << auth_message->error().error_type(); | |
205 return false; | |
206 } | |
207 if (!auth_message->has_response()) { | |
208 VLOG(1) << "Auth message has no response field"; | |
209 return false; | |
210 } | |
211 return true; | |
212 } | |
213 | |
214 // Authenticates the given credentials: | |
215 // 1. |signature| verification of |data| using |certificate|. | |
216 // 2. |certificate| is signed by a trusted CA. | |
217 bool VerifyCredentials( | |
218 const extensions::api::cast_channel::AuthResponse& response, | |
219 const std::string& data) { | |
220 const std::string& certificate = response.client_auth_certificate(); | |
221 const std::string& signature = response.signature(); | |
222 | |
223 const SECItem* trusted_ca_key_der; | |
224 | |
225 // If the list of intermediates is empty then use kPublicKeyICA1 as | |
226 // the trusted CA (legacy case). | |
227 // Otherwise, use the first intermediate in the list as long as it | |
228 // is in the allowed list of intermediates. | |
229 int num_intermediates = response.intermediate_certificate_size(); | |
230 VLOG(1) << "Response has " << num_intermediates << " intermediates"; | |
231 if (num_intermediates <= 0) { | |
232 trusted_ca_key_der = &kAllowedICAs[0].public_key; | |
233 } else { | |
234 const std::string& ica = response.intermediate_certificate(0); | |
235 scoped_refptr<net::X509Certificate> ica_cert | |
236 = net::X509Certificate::CreateFromBytes(ica.data(), ica.length()); | |
237 int index = GetICAWithFingerprint(ica_cert->fingerprint()); | |
238 if (index == -1) { | |
239 VLOG(1) << "Disallowed intermdiate cert"; | |
240 return false; | |
241 } | |
242 trusted_ca_key_der = &kAllowedICAs[index].public_key; | |
243 } | |
244 | |
245 crypto::EnsureNSSInit(); | |
246 SECItem der_cert; | |
247 der_cert.type = siDERCertBuffer; | |
248 // Make a copy of certificate string so it is safe to type cast. | |
249 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( | |
250 certificate.data())); | |
251 der_cert.len = certificate.length(); | |
252 | |
253 // Parse into a certificate structure. | |
254 ScopedCERTCertificate cert(CERT_NewTempCertificate( | |
255 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); | |
256 if (!cert.get()) { | |
257 VLOG(1) << "Failed to parse certificate."; | |
258 return false; | |
259 } | |
260 | |
261 // Check that the certificate is signed by trusted CA. | |
262 // NOTE: We const_cast trusted_ca_key_der since on some platforms | |
263 // SECKEY_ImportDERPublicKey API takes in SECItem* and not const | |
264 // SECItem*. | |
265 crypto::ScopedSECKEYPublicKey ca_public_key( | |
266 SECKEY_ImportDERPublicKey( | |
267 const_cast<SECItem*>(trusted_ca_key_der), CKK_RSA)); | |
268 SECStatus verified = CERT_VerifySignedDataWithPublicKey( | |
269 &cert->signatureWrap, ca_public_key.get(), NULL); | |
270 if (verified != SECSuccess) { | |
271 VLOG(1)<< "Cert not signed by trusted CA"; | |
272 return false; | |
273 } | |
274 VLOG(1) << "Cert signed by trusted CA"; | |
275 | |
276 // Verify that the |signature| matches |data|. | |
277 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); | |
278 if (!public_key.get()) { | |
279 VLOG(1) << "Unable to extract public key from certificate."; | |
280 return false; | |
281 } | |
282 SECItem signature_item; | |
283 signature_item.type = siBuffer; | |
284 signature_item.data = reinterpret_cast<unsigned char*>( | |
285 const_cast<char*>(signature.data())); | |
286 signature_item.len = signature.length(); | |
287 verified = VFY_VerifyDataDirect( | |
288 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), | |
289 data.size(), | |
290 public_key.get(), | |
291 &signature_item, | |
292 SEC_OID_PKCS1_RSA_ENCRYPTION, | |
293 SEC_OID_SHA1, NULL, NULL); | |
294 | |
295 if (verified != SECSuccess) { | |
296 VLOG(1) << "Signed blobs did not match."; | |
297 return false; | |
298 } | |
299 VLOG(1) << "Signature verification succeeded"; | |
300 return true; | |
301 } | |
302 | |
303 } // namespace | |
304 | |
305 namespace extensions { | |
306 namespace api { | |
307 namespace cast_channel { | |
308 | |
309 bool AuthenticateChallengeReply(const CastMessage& challenge_reply, | |
310 const std::string& peer_cert) { | |
311 if (peer_cert.empty()) | |
312 return false; | |
313 | |
314 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); | |
315 DeviceAuthMessage auth_message; | |
316 if (!ParseAuthMessage(challenge_reply, &auth_message)) | |
317 return false; | |
318 | |
319 const AuthResponse& response = auth_message.response(); | |
320 return VerifyCredentials(response, peer_cert); | |
321 } | |
322 | |
323 } // namespace cast_channel | |
324 } // namespace api | |
325 } // namespace extensions | |
OLD | NEW |