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 // Fingerprints and public keys of the allowed / trusted ICAs. |
22 static const unsigned char kCAPublicKeyDER[] = { | 25 static const net::SHA1HashValue kFingerprintICA1 = { { |
23 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, | 26 0x57,0x16,0xE2,0xAD,0x73,0x2E,0xBE,0xDA,0xEB,0x18, |
24 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, | 27 0xE8,0x47,0x15,0xA8,0xDE,0x90,0x3B,0x5E,0x2A,0xF4 |
25 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, | 28 } }; |
26 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf, | 29 static const unsigned char kPublicKeyICA1[] = { |
27 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80, | 30 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xBC,0x22,0x80, |
28 0xa4, 0x47, 0x15, 0xc1, 0x20, 0x99, 0x50, 0xcd, 0xec, 0x14, 0x62, 0x09, | 31 0xBD,0x80,0xF6,0x3A,0x21,0x00,0x3B,0xAE,0x76,0x5E,0x35,0x7F, |
29 0x5b, 0xa4, 0x98, 0xcd, 0xd2, 0x41, 0xb6, 0x36, 0x4e, 0xff, 0xe8, 0x2e, | 32 0x3D,0xC3,0x64,0x5C,0x55,0x94,0x86,0x34,0x2F,0x05,0x87,0x28, |
30 0x32, 0x30, 0x4a, 0x81, 0xa8, 0x42, 0xa3, 0x6c, 0x9b, 0x33, 0x6e, 0xca, | 33 0xCD,0xF7,0x69,0x8C,0x17,0xB3,0x50,0xA7,0xB8,0x82,0xFA,0xDF, |
31 0xb2, 0xf5, 0x53, 0x66, 0xe0, 0x27, 0x53, 0x86, 0x1a, 0x85, 0x1e, 0xa7, | 34 0xC7,0x43,0x2D,0xD6,0x7E,0xAB,0xA0,0x6F,0xB7,0x13,0x72,0x80, |
32 0x39, 0x3f, 0x4a, 0x77, 0x8e, 0xfb, 0x54, 0x66, 0x66, 0xfb, 0x58, 0x54, | 35 0xA4,0x47,0x15,0xC1,0x20,0x99,0x50,0xCD,0xEC,0x14,0x62,0x09, |
33 0xc0, 0x5e, 0x39, 0xc7, 0xf5, 0x50, 0x06, 0x0b, 0xe0, 0x8a, 0xd4, 0xce, | 36 0x5B,0xA4,0x98,0xCD,0xD2,0x41,0xB6,0x36,0x4E,0xFF,0xE8,0x2E, |
34 0xe1, 0x6a, 0x55, 0x1f, 0x8b, 0x17, 0x00, 0xe6, 0x69, 0xa3, 0x27, 0xe6, | 37 0x32,0x30,0x4A,0x81,0xA8,0x42,0xA3,0x6C,0x9B,0x33,0x6E,0xCA, |
35 0x08, 0x25, 0x69, 0x3c, 0x12, 0x9d, 0x8d, 0x05, 0x2c, 0xd6, 0x2e, 0xa2, | 38 0xB2,0xF5,0x53,0x66,0xE0,0x27,0x53,0x86,0x1A,0x85,0x1E,0xA7, |
36 0x31, 0xde, 0xb4, 0x52, 0x50, 0xd6, 0x20, 0x49, 0xde, 0x71, 0xa0, 0xf9, | 39 0x39,0x3F,0x4A,0x77,0x8E,0xFB,0x54,0x66,0x66,0xFB,0x58,0x54, |
37 0xad, 0x20, 0x40, 0x12, 0xf1, 0xdd, 0x25, 0xeb, 0xd5, 0xe6, 0xb8, 0x36, | 40 0xC0,0x5E,0x39,0xC7,0xF5,0x50,0x06,0x0B,0xE0,0x8A,0xD4,0xCE, |
38 0xf4, 0xd6, 0x8f, 0x7f, 0xca, 0x43, 0xdc, 0xd7, 0x10, 0x5b, 0xe6, 0x3f, | 41 0xE1,0x6A,0x55,0x1F,0x8B,0x17,0x00,0xE6,0x69,0xA3,0x27,0xE6, |
39 0x51, 0x8a, 0x85, 0xb3, 0xf3, 0xff, 0xf6, 0x03, 0x2d, 0xcb, 0x23, 0x4f, | 42 0x08,0x25,0x69,0x3C,0x12,0x9D,0x8D,0x05,0x2C,0xD6,0x2E,0xA2, |
40 0x9c, 0xad, 0x18, 0xe7, 0x93, 0x05, 0x8c, 0xac, 0x52, 0x9a, 0xf7, 0x4c, | 43 0x31,0xDE,0xB4,0x52,0x50,0xD6,0x20,0x49,0xDE,0x71,0xA0,0xF9, |
41 0xe9, 0x99, 0x7a, 0xbe, 0x6e, 0x7e, 0x4d, 0x0a, 0xe3, 0xc6, 0x1c, 0xa9, | 44 0xAD,0x20,0x40,0x12,0xF1,0xDD,0x25,0xEB,0xD5,0xE6,0xB8,0x36, |
42 0x93, 0xfa, 0x3a, 0xa5, 0x91, 0x5d, 0x1c, 0xbd, 0x66, 0xeb, 0xcc, 0x60, | 45 0xF4,0xD6,0x8F,0x7F,0xCA,0x43,0xDC,0xD7,0x10,0x5B,0xE6,0x3F, |
43 0xdc, 0x86, 0x74, 0xca, 0xcf, 0xf8, 0x92, 0x1c, 0x98, 0x7d, 0x57, 0xfa, | 46 0x51,0x8A,0x85,0xB3,0xF3,0xFF,0xF6,0x03,0x2D,0xCB,0x23,0x4F, |
44 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, 0x80, 0x2a, 0x92, 0xc5, | 47 0x9C,0xAD,0x18,0xE7,0x93,0x05,0x8C,0xAC,0x52,0x9A,0xF7,0x4C, |
45 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01 }; | 48 0xE9,0x99,0x7A,0xBE,0x6E,0x7E,0x4D,0x0A,0xE3,0xC6,0x1C,0xA9, |
49 0x93,0xFA,0x3A,0xA5,0x91,0x5D,0x1C,0xBD,0x66,0xEB,0xCC,0x60, | |
50 0xDC,0x86,0x74,0xCA,0xCF,0xF8,0x92,0x1C,0x98,0x7D,0x57,0xFA, | |
51 0x61,0x47,0x9E,0xAB,0x80,0xB7,0xE4,0x48,0x80,0x2A,0x92,0xC5, | |
52 0x1B,0x02,0x03,0x01,0x00,0x01 | |
53 }; | |
54 | |
55 static const net::SHA1HashValue kFingerprintICA2 = { { | |
56 0x1B,0xA2,0x9E,0xC9,0x8E,0x4E,0xB3,0x80,0xEE,0x55, | |
57 0xB2,0x97,0xFD,0x2E,0x2B,0x2C,0xB6,0x8E,0x0B,0x2F | |
58 } }; | |
59 static const unsigned char kPublicKeyICA2[] = { | |
60 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xBC,0x22,0x80, | |
61 0xBD,0x80,0xF6,0x3A,0x21,0x00,0x3B,0xAE,0x76,0x5E,0x35,0x7F, | |
62 0x3D,0xC3,0x64,0x5C,0x55,0x94,0x86,0x34,0x2F,0x05,0x87,0x28, | |
63 0xCD,0xF7,0x69,0x8C,0x17,0xB3,0x50,0xA7,0xB8,0x82,0xFA,0xDF, | |
64 0xC7,0x43,0x2D,0xD6,0x7E,0xAB,0xA0,0x6F,0xB7,0x13,0x72,0x80, | |
65 0xA4,0x47,0x15,0xC1,0x20,0x99,0x50,0xCD,0xEC,0x14,0x62,0x09, | |
66 0x5B,0xA4,0x98,0xCD,0xD2,0x41,0xB6,0x36,0x4E,0xFF,0xE8,0x2E, | |
67 0x32,0x30,0x4A,0x81,0xA8,0x42,0xA3,0x6C,0x9B,0x33,0x6E,0xCA, | |
68 0xB2,0xF5,0x53,0x66,0xE0,0x27,0x53,0x86,0x1A,0x85,0x1E,0xA7, | |
69 0x39,0x3F,0x4A,0x77,0x8E,0xFB,0x54,0x66,0x66,0xFB,0x58,0x54, | |
70 0xC0,0x5E,0x39,0xC7,0xF5,0x50,0x06,0x0B,0xE0,0x8A,0xD4,0xCE, | |
71 0xE1,0x6A,0x55,0x1F,0x8B,0x17,0x00,0xE6,0x69,0xA3,0x27,0xE6, | |
72 0x08,0x25,0x69,0x3C,0x12,0x9D,0x8D,0x05,0x2C,0xD6,0x2E,0xA2, | |
73 0x31,0xDE,0xB4,0x52,0x50,0xD6,0x20,0x49,0xDE,0x71,0xA0,0xF9, | |
74 0xAD,0x20,0x40,0x12,0xF1,0xDD,0x25,0xEB,0xD5,0xE6,0xB8,0x36, | |
75 0xF4,0xD6,0x8F,0x7F,0xCA,0x43,0xDC,0xD7,0x10,0x5B,0xE6,0x3F, | |
76 0x51,0x8A,0x85,0xB3,0xF3,0xFF,0xF6,0x03,0x2D,0xCB,0x23,0x4F, | |
77 0x9C,0xAD,0x18,0xE7,0x93,0x05,0x8C,0xAC,0x52,0x9A,0xF7,0x4C, | |
78 0xE9,0x99,0x7A,0xBE,0x6E,0x7E,0x4D,0x0A,0xE3,0xC6,0x1C,0xA9, | |
79 0x93,0xFA,0x3A,0xA5,0x91,0x5D,0x1C,0xBD,0x66,0xEB,0xCC,0x60, | |
80 0xDC,0x86,0x74,0xCA,0xCF,0xF8,0x92,0x1C,0x98,0x7D,0x57,0xFA, | |
81 0x61,0x47,0x9E,0xAB,0x80,0xB7,0xE4,0x48,0x80,0x2A,0x92,0xC5, | |
82 0x1B,0x02,0x03,0x01,0x00,0x01 | |
83 }; | |
84 | |
85 static const net::SHA1HashValue kFingerprintICA3 = { { | |
86 0x97,0x05,0xCE,0xF6,0x3F,0xA9,0x5E,0x0F,0xE7,0x61, | |
87 0xFB,0x08,0x44,0x31,0xBE,0xDE,0x01,0xB8,0xFB,0xEB | |
88 } }; | |
89 static const unsigned char kPublicKeyICA3[] = { | |
90 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xB7,0xE8,0xC3, | |
91 0xE4,0x2C,0xDE,0x74,0x53,0xF2,0x49,0x95,0x6D,0xD1,0xDA,0x69, | |
92 0x57,0x0D,0x86,0xE5,0xED,0xB4,0xB9,0xE6,0x73,0x9F,0x6C,0xAD, | |
93 0x3B,0x64,0x85,0x03,0x0D,0x08,0x44,0xAF,0x18,0x69,0x82,0xAD, | |
94 0xA9,0x74,0x64,0x37,0x47,0xE1,0xE7,0x26,0x19,0x33,0x3C,0xE2, | |
95 0xD0,0xB5,0x84,0x3C,0xD7,0xAC,0x63,0xAE,0xC4,0x32,0x23,0xF6, | |
96 0xDC,0x14,0x10,0x4B,0x95,0x7F,0xE8,0x98,0xD7,0x7A,0x9E,0x43, | |
97 0x3D,0x68,0x8B,0x2A,0x70,0xF7,0x1E,0x43,0x70,0xBA,0xA5,0xA5, | |
98 0x93,0xAD,0x8A,0xD4,0x9F,0xAC,0x83,0x16,0xF3,0x48,0x5F,0xC5, | |
99 0xE0,0xA5,0x44,0xB8,0x4F,0xD9,0xD8,0x75,0x90,0x25,0x8B,0xE3, | |
100 0x1C,0x6C,0xDA,0x88,0xFF,0x09,0x2B,0xCA,0x1E,0x48,0xDD,0x76, | |
101 0x0F,0x68,0x56,0x7B,0x15,0x9D,0xCA,0x6B,0x1C,0xF7,0x48,0xC2, | |
102 0x89,0xC6,0x93,0x0A,0x31,0xF2,0x78,0x27,0x45,0x3D,0xF1,0x0D, | |
103 0x5B,0x6E,0x55,0x32,0xEF,0x49,0xA0,0xD6,0xAF,0xA6,0x30,0x91, | |
104 0xF2,0x21,0x2F,0xDB,0xA4,0x29,0xB9,0x9B,0x22,0xBC,0xCD,0x0B, | |
105 0xA6,0x8B,0xA6,0x22,0x79,0xFD,0xCF,0x95,0x93,0x96,0xB3,0x23, | |
106 0xC9,0xC6,0x30,0x8E,0xC0,0xE9,0x1F,0xEC,0xFB,0xF5,0x88,0xDD, | |
107 0x97,0x72,0x16,0x29,0x08,0xFA,0x42,0xE7,0x4F,0xCA,0xAE,0xD7, | |
108 0x0F,0x23,0x48,0x9B,0x82,0xA7,0x37,0x4A,0xDD,0x60,0x04,0x75, | |
109 0xDC,0xDE,0x09,0x98,0xD2,0x16,0x23,0x04,0x70,0x4D,0x99,0x9F, | |
110 0x4A,0x82,0x28,0xE6,0xBE,0x8F,0x9D,0xBF,0xA1,0x4B,0xA2,0xBA, | |
111 0xF5,0xB2,0x51,0x1E,0x4E,0xE7,0x80,0x9E,0x7A,0x38,0xA1,0xC7, | |
112 0x09,0x02,0x03,0x01,0x00,0x01 | |
113 }; | |
114 | |
115 static const net::SHA1HashValue kFingerprintICA4 = { { | |
116 0x01,0xF5,0x28,0x56,0x33,0x80,0x9B,0x31,0xE7,0xD9, | |
117 0xF7,0x4E,0xAA,0xDD,0x97,0x37,0xA0,0x28,0xE7,0x24 | |
118 } }; | |
119 static const unsigned char kPublicKeyICA4[] = { | |
120 0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xB0,0x0E,0x5E, | |
121 0x07,0x3A,0xDF,0xA4,0x5F,0x68,0xF7,0x21,0xC7,0x64,0xDB,0xB6, | |
122 0x76,0xEF,0xEE,0x8B,0x93,0xF8,0xF6,0x1B,0x88,0xE1,0x93,0xB7, | |
123 0x17,0xF0,0x15,0x1E,0x7E,0x52,0x55,0x77,0x3C,0x02,0x8D,0x7B, | |
124 0x4A,0x6C,0xD3,0xBD,0xD6,0xC1,0x9C,0x72,0xC8,0xB3,0x15,0xCF, | |
125 0x11,0xC1,0xF5,0x46,0xC4,0xD5,0x20,0x47,0xFB,0x30,0xF4,0xE4, | |
126 0x61,0x0C,0x68,0xF0,0x5E,0xAB,0x37,0x8E,0x9B,0xE1,0xBC,0x81, | |
127 0xC3,0x70,0x8A,0x78,0xD6,0x83,0x34,0x32,0x9C,0x19,0x62,0xEB, | |
128 0xE4,0x9C,0xED,0xE3,0x64,0x6C,0x41,0x1D,0x9C,0xD2,0x8B,0x48, | |
129 0x4C,0x23,0x90,0x95,0xB3,0xE7,0x52,0xEA,0x05,0x57,0xCC,0x60, | |
130 0xB3,0xBA,0x14,0xE4,0xBA,0x00,0x39,0xE4,0x46,0x55,0x74,0xCE, | |
131 0x5A,0x8E,0x7A,0x67,0x23,0xDA,0x68,0x0A,0xFA,0xC4,0x84,0x1E, | |
132 0xB4,0xC5,0xA1,0xA2,0x6A,0x73,0x1F,0x6E,0xC8,0x2E,0x2F,0x9A, | |
133 0x9E,0xA8,0xB1,0x0E,0xFD,0x87,0xA6,0x8F,0x4D,0x3D,0x4B,0x05, | |
134 0xD5,0x35,0x5A,0x74,0x4D,0xBC,0x8E,0x82,0x44,0x96,0xF4,0xB5, | |
135 0x95,0x60,0x4E,0xA5,0xDF,0x27,0x3D,0x41,0x5C,0x07,0xA3,0xB4, | |
136 0x35,0x5A,0xB3,0x9E,0xF2,0x05,0x24,0xCA,0xCD,0x31,0x5A,0x0D, | |
137 0x26,0x4C,0xD4,0xD3,0xFD,0x50,0xE1,0x34,0xE9,0x4C,0x81,0x58, | |
138 0x30,0xB2,0xC7,0x7A,0xDD,0x81,0x89,0xA6,0xD4,0x3A,0x38,0x84, | |
139 0x03,0xB7,0x34,0x9E,0x77,0x3F,0xFF,0x78,0x07,0x5B,0x99,0xC1, | |
140 0xB2,0x1F,0x35,0x56,0x6E,0x3A,0x3C,0x0C,0x25,0xE1,0x57,0xF6, | |
141 0x8A,0x7E,0x49,0xC0,0xCC,0x83,0x11,0x35,0xE7,0x91,0x6D,0x2E, | |
142 0x65,0x02,0x03,0x01,0x00,0x01 | |
143 }; | |
144 | |
145 // Info for trusted ICA certs. | |
146 struct ICACertInfo { | |
147 net::SHA1HashValue fingerprint; | |
148 SECItem public_key; | |
149 }; | |
150 | |
151 // List of allowed / trusted ICAs. | |
152 static const ICACertInfo kAllowedICAs[] = { | |
153 { kFingerprintICA1, | |
154 { siDERCertBuffer, | |
155 const_cast<unsigned char*>(kPublicKeyICA1), | |
156 sizeof(kPublicKeyICA1) } }, | |
157 { kFingerprintICA2, | |
158 { siDERCertBuffer, | |
159 const_cast<unsigned char*>(kPublicKeyICA2), | |
160 sizeof(kPublicKeyICA2) } }, | |
161 { kFingerprintICA3, | |
162 { siDERCertBuffer, | |
163 const_cast<unsigned char*>(kPublicKeyICA3), | |
164 sizeof(kPublicKeyICA3) } }, | |
165 { kFingerprintICA4, | |
166 { siDERCertBuffer, | |
167 const_cast<unsigned char*>(kPublicKeyICA4), | |
168 sizeof(kPublicKeyICA4) } }, | |
169 }; | |
46 | 170 |
47 typedef scoped_ptr< | 171 typedef scoped_ptr< |
48 CERTCertificate, | 172 CERTCertificate, |
49 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > | 173 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > |
50 ScopedCERTCertificate; | 174 ScopedCERTCertificate; |
51 | 175 |
176 // Returns the index of the ICA whose fingerprint matches |fingerprint|. | |
177 // Returns -1, if no such ICA is found. | |
178 static int GetICAWithFingerprint(const net::SHA1HashValue& fingerprint) { | |
179 for (size_t i = 0; i < arraysize(kAllowedICAs); ++i) { | |
180 if (fingerprint.Equals(kAllowedICAs[i].fingerprint)) | |
181 return i; | |
Ryan Sleevi
2014/05/07 23:34:49
TYPE-CASTING: Downcasting a size_t to an int has b
mark a. foltz
2014/05/07 23:48:31
Also see checked_cast in safe_conversions.h.
http
Munjal (Google)
2014/05/08 16:54:00
Done. I chose to use static_cast since I know that
| |
182 } | |
183 return -1; | |
184 } | |
185 | |
186 // Check whether the public key in the given cert is the same as |pk|. | |
187 static bool CheckCertPublicKey(const std::string& ica, const SECItem& pk) { | |
188 base::StringPiece cert_public_key; | |
189 if (!net::asn1::ExtractSPKIFromDERCert(ica, &cert_public_key)) { | |
190 VLOG(1) << "Could not extract public key from cert"; | |
191 return false; | |
192 } | |
193 base::StringPiece public_key( | |
194 reinterpret_cast<const char*>(pk.data), pk.len); | |
195 return cert_public_key.ends_with(public_key); | |
196 } | |
197 | |
52 // Parses out DeviceAuthMessage from CastMessage | 198 // Parses out DeviceAuthMessage from CastMessage |
53 static bool ParseAuthMessage( | 199 static bool ParseAuthMessage( |
54 const extensions::api::cast_channel::CastMessage& challenge_reply, | 200 const extensions::api::cast_channel::CastMessage& challenge_reply, |
55 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { | 201 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { |
56 if (challenge_reply.payload_type() != | 202 if (challenge_reply.payload_type() != |
57 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { | 203 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { |
58 VLOG(1) << "Wrong payload type in challenge reply"; | 204 VLOG(1) << "Wrong payload type in challenge reply"; |
59 return false; | 205 return false; |
60 } | 206 } |
61 if (!challenge_reply.has_payload_binary()) { | 207 if (!challenge_reply.has_payload_binary()) { |
(...skipping 12 matching lines...) Expand all Loading... | |
74 if (!auth_message->has_response()) { | 220 if (!auth_message->has_response()) { |
75 VLOG(1) << "Auth message has no response field"; | 221 VLOG(1) << "Auth message has no response field"; |
76 return false; | 222 return false; |
77 } | 223 } |
78 return true; | 224 return true; |
79 } | 225 } |
80 | 226 |
81 // Authenticates the given credentials: | 227 // Authenticates the given credentials: |
82 // 1. |signature| verification of |data| using |certificate|. | 228 // 1. |signature| verification of |data| using |certificate|. |
83 // 2. |certificate| is signed by a trusted CA. | 229 // 2. |certificate| is signed by a trusted CA. |
84 bool VerifyCredentials(const std::string& certificate, | 230 bool VerifyCredentials( |
85 const std::string& signature, | 231 const extensions::api::cast_channel::AuthResponse& response, |
86 const std::string& data) { | 232 const std::string& data) { |
233 const std::string& certificate = response.client_auth_certificate(); | |
234 const std::string& signature = response.signature(); | |
235 | |
236 const SECItem* trusted_ca_key_der; | |
237 | |
238 // If the list of intermediates is empty then use kPublicKeyICA1 as | |
239 // the trusted CA (legacy case). | |
240 // Otherwise, use the first intermediate in the list as long as it | |
241 // is in the allowed list of intermediates. | |
242 int num_intermediates = response.intermediate_certificate_size(); | |
243 VLOG(1) << "Response has " << num_intermediates << " intermediates"; | |
244 if (num_intermediates <= 0) { | |
245 trusted_ca_key_der = &kAllowedICAs[0].public_key; | |
246 } else { | |
247 // If the list of intermediates is not empty, then the fingerprint of | |
248 // the first intermediate should be one of the hard-coded list of | |
249 // fingerprints. | |
250 if (response.intermediate_certificate_size() > 0) { | |
Ryan Sleevi
2014/05/07 23:34:49
This condition is unnecessary (it's handled by lin
Munjal (Google)
2014/05/08 16:54:00
Good catch. Residue of a bit of refactoring.
| |
251 const std::string& ica = response.intermediate_certificate(0); | |
252 scoped_refptr<net::X509Certificate> ica_cert | |
253 = net::X509Certificate::CreateFromBytes(ica.data(), ica.length()); | |
254 int index = GetICAWithFingerprint(ica_cert->fingerprint()); | |
255 if (index == -1) { | |
256 VLOG(1) << "Disallowed intermdiate cert"; | |
257 return false; | |
258 } | |
259 trusted_ca_key_der = &kAllowedICAs[index].public_key; | |
260 | |
261 // Sanity check that the public key in the response is the same as the | |
262 // hard-coded public key. | |
263 if (!CheckCertPublicKey(ica, *trusted_ca_key_der)) { | |
Ryan Sleevi
2014/05/07 23:34:49
It's not necessary to perform this check, since yo
Munjal (Google)
2014/05/08 16:54:00
Removed.
| |
264 VLOG(1) << "Unexpected public key in intermediate cert"; | |
265 return false; | |
266 } | |
267 } | |
268 } | |
269 | |
87 crypto::EnsureNSSInit(); | 270 crypto::EnsureNSSInit(); |
88 SECItem der_cert; | 271 SECItem der_cert; |
89 der_cert.type = siDERCertBuffer; | 272 der_cert.type = siDERCertBuffer; |
90 // Make a copy of certificate string so it is safe to type cast. | 273 // Make a copy of certificate string so it is safe to type cast. |
91 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( | 274 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( |
92 certificate.data())); | 275 certificate.data())); |
93 der_cert.len = certificate.length(); | 276 der_cert.len = certificate.length(); |
94 | 277 |
95 // Parse into a certificate structure. | 278 // Parse into a certificate structure. |
96 ScopedCERTCertificate cert(CERT_NewTempCertificate( | 279 ScopedCERTCertificate cert(CERT_NewTempCertificate( |
97 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); | 280 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); |
98 if (!cert.get()) { | 281 if (!cert.get()) { |
99 VLOG(1) << "Failed to parse certificate."; | 282 VLOG(1) << "Failed to parse certificate."; |
100 return false; | 283 return false; |
101 } | 284 } |
102 | 285 |
103 // Check that the certificate is signed by trusted CA. | 286 // Check that the certificate is signed by trusted CA. |
104 SECItem trusted_ca_key_der_item; | |
105 trusted_ca_key_der_item.type = siDERCertBuffer; | |
106 trusted_ca_key_der_item.data = const_cast<unsigned char*>(kCAPublicKeyDER); | |
107 trusted_ca_key_der_item.len = sizeof(kCAPublicKeyDER); | |
108 crypto::ScopedSECKEYPublicKey ca_public_key( | 287 crypto::ScopedSECKEYPublicKey ca_public_key( |
109 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA)); | 288 SECKEY_ImportDERPublicKey(trusted_ca_key_der, CKK_RSA)); |
110 SECStatus verified = CERT_VerifySignedDataWithPublicKey( | 289 SECStatus verified = CERT_VerifySignedDataWithPublicKey( |
111 &cert->signatureWrap, ca_public_key.get(), NULL); | 290 &cert->signatureWrap, ca_public_key.get(), NULL); |
112 if (verified != SECSuccess) { | 291 if (verified != SECSuccess) { |
113 VLOG(1)<< "Cert not signed by trusted CA"; | 292 VLOG(1)<< "Cert not signed by trusted CA"; |
114 return false; | 293 return false; |
115 } | 294 } |
295 VLOG(1) << "Cert signed by trusted CA"; | |
116 | 296 |
117 // Verify that the |signature| matches |data|. | 297 // Verify that the |signature| matches |data|. |
118 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); | 298 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); |
119 if (!public_key.get()) { | 299 if (!public_key.get()) { |
120 VLOG(1) << "Unable to extract public key from certificate."; | 300 VLOG(1) << "Unable to extract public key from certificate."; |
121 return false; | 301 return false; |
122 } | 302 } |
123 SECItem signature_item; | 303 SECItem signature_item; |
124 signature_item.type = siBuffer; | 304 signature_item.type = siBuffer; |
125 signature_item.data = reinterpret_cast<unsigned char*>( | 305 signature_item.data = reinterpret_cast<unsigned char*>( |
126 const_cast<char*>(signature.data())); | 306 const_cast<char*>(signature.data())); |
127 signature_item.len = signature.length(); | 307 signature_item.len = signature.length(); |
128 verified = VFY_VerifyDataDirect( | 308 verified = VFY_VerifyDataDirect( |
129 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), | 309 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), |
130 data.size(), | 310 data.size(), |
131 public_key.get(), | 311 public_key.get(), |
132 &signature_item, | 312 &signature_item, |
133 SEC_OID_PKCS1_RSA_ENCRYPTION, | 313 SEC_OID_PKCS1_RSA_ENCRYPTION, |
134 SEC_OID_SHA1, NULL, NULL); | 314 SEC_OID_SHA1, NULL, NULL); |
135 | 315 |
136 if (verified != SECSuccess) { | 316 if (verified != SECSuccess) { |
137 VLOG(1) << "Signed blobs did not match."; | 317 VLOG(1) << "Signed blobs did not match."; |
138 return false; | 318 return false; |
139 } | 319 } |
320 VLOG(1) << "Signature verification succeeded"; | |
140 return true; | 321 return true; |
141 } | 322 } |
142 | 323 |
143 } // namespace | 324 } // namespace |
144 | 325 |
145 namespace extensions { | 326 namespace extensions { |
146 namespace api { | 327 namespace api { |
147 namespace cast_channel { | 328 namespace cast_channel { |
148 | 329 |
149 bool AuthenticateChallengeReply(const CastMessage& challenge_reply, | 330 bool AuthenticateChallengeReply(const CastMessage& challenge_reply, |
150 const std::string& peer_cert) { | 331 const std::string& peer_cert) { |
151 if (peer_cert.empty()) | 332 if (peer_cert.empty()) |
152 return false; | 333 return false; |
153 | 334 |
154 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); | 335 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); |
155 DeviceAuthMessage auth_message; | 336 DeviceAuthMessage auth_message; |
156 if (!ParseAuthMessage(challenge_reply, &auth_message)) | 337 if (!ParseAuthMessage(challenge_reply, &auth_message)) |
157 return false; | 338 return false; |
158 | 339 |
159 const AuthResponse& response = auth_message.response(); | 340 const AuthResponse& response = auth_message.response(); |
160 return VerifyCredentials(response.client_auth_certificate(), | 341 return VerifyCredentials(response, peer_cert); |
161 response.signature(), | |
162 peer_cert); | |
163 } | 342 } |
164 | 343 |
165 } // namespace cast_channel | 344 } // namespace cast_channel |
166 } // namespace api | 345 } // namespace api |
167 } // namespace extensions | 346 } // namespace extensions |
OLD | NEW |