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/x509_certificate.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 // Public key of the certificate with which the peer cert should be signed. | 23 // Fingerprints and public keys of the allowed / trusted ICAs. |
22 static const unsigned char kCAPublicKeyDER[] = { | 24 static const net::SHA1HashValue kFingerprintICA1 = { { |
23 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, | 25 0x57,0x16,0xE2,0xAD,0x73,0x2E,0xBE,0xDA,0xEB,0x18, |
24 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, | 26 0xE8,0x47,0x15,0xA8,0xDE,0x90,0x3B,0x5E,0x2A,0xF4 |
25 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, | 27 } }; |
26 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf, | 28 static const unsigned char kPublicKeyICA1[] = { |
27 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80, | 29 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, | 30 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, | 31 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, | 32 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, | 33 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, | 34 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, | 35 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, | 36 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, | 37 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, | 38 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, | 39 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, | 40 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, | 41 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, | 42 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, | 43 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, | 44 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, | 45 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, | 46 0x9C,0xAD,0x18,0xE7,0x93,0x05,0x8C,0xAC,0x52,0x9A,0xF7,0x4C, |
45 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01 }; | 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 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 }; |
46 | 169 |
47 typedef scoped_ptr< | 170 typedef scoped_ptr< |
48 CERTCertificate, | 171 CERTCertificate, |
49 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > | 172 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > |
50 ScopedCERTCertificate; | 173 ScopedCERTCertificate; |
51 | 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 (fingerprint.Equals(kAllowedICAs[i].fingerprint)) |
| 180 return static_cast<int>(i); |
| 181 } |
| 182 return -1; |
| 183 } |
| 184 |
52 // Parses out DeviceAuthMessage from CastMessage | 185 // Parses out DeviceAuthMessage from CastMessage |
53 static bool ParseAuthMessage( | 186 static bool ParseAuthMessage( |
54 const extensions::api::cast_channel::CastMessage& challenge_reply, | 187 const extensions::api::cast_channel::CastMessage& challenge_reply, |
55 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { | 188 extensions::api::cast_channel::DeviceAuthMessage* auth_message) { |
56 if (challenge_reply.payload_type() != | 189 if (challenge_reply.payload_type() != |
57 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { | 190 extensions::api::cast_channel::CastMessage_PayloadType_BINARY) { |
58 VLOG(1) << "Wrong payload type in challenge reply"; | 191 VLOG(1) << "Wrong payload type in challenge reply"; |
59 return false; | 192 return false; |
60 } | 193 } |
61 if (!challenge_reply.has_payload_binary()) { | 194 if (!challenge_reply.has_payload_binary()) { |
(...skipping 12 matching lines...) Expand all Loading... |
74 if (!auth_message->has_response()) { | 207 if (!auth_message->has_response()) { |
75 VLOG(1) << "Auth message has no response field"; | 208 VLOG(1) << "Auth message has no response field"; |
76 return false; | 209 return false; |
77 } | 210 } |
78 return true; | 211 return true; |
79 } | 212 } |
80 | 213 |
81 // Authenticates the given credentials: | 214 // Authenticates the given credentials: |
82 // 1. |signature| verification of |data| using |certificate|. | 215 // 1. |signature| verification of |data| using |certificate|. |
83 // 2. |certificate| is signed by a trusted CA. | 216 // 2. |certificate| is signed by a trusted CA. |
84 bool VerifyCredentials(const std::string& certificate, | 217 bool VerifyCredentials( |
85 const std::string& signature, | 218 const extensions::api::cast_channel::AuthResponse& response, |
86 const std::string& data) { | 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 |
87 crypto::EnsureNSSInit(); | 245 crypto::EnsureNSSInit(); |
88 SECItem der_cert; | 246 SECItem der_cert; |
89 der_cert.type = siDERCertBuffer; | 247 der_cert.type = siDERCertBuffer; |
90 // Make a copy of certificate string so it is safe to type cast. | 248 // Make a copy of certificate string so it is safe to type cast. |
91 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( | 249 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>( |
92 certificate.data())); | 250 certificate.data())); |
93 der_cert.len = certificate.length(); | 251 der_cert.len = certificate.length(); |
94 | 252 |
95 // Parse into a certificate structure. | 253 // Parse into a certificate structure. |
96 ScopedCERTCertificate cert(CERT_NewTempCertificate( | 254 ScopedCERTCertificate cert(CERT_NewTempCertificate( |
97 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); | 255 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); |
98 if (!cert.get()) { | 256 if (!cert.get()) { |
99 VLOG(1) << "Failed to parse certificate."; | 257 VLOG(1) << "Failed to parse certificate."; |
100 return false; | 258 return false; |
101 } | 259 } |
102 | 260 |
103 // Check that the certificate is signed by trusted CA. | 261 // Check that the certificate is signed by trusted CA. |
104 SECItem trusted_ca_key_der_item; | 262 // NOTE: We const_cast trusted_ca_key_der since on some platforms |
105 trusted_ca_key_der_item.type = siDERCertBuffer; | 263 // SECKEY_ImportDERPublicKey API takes in SECItem* and not const |
106 trusted_ca_key_der_item.data = const_cast<unsigned char*>(kCAPublicKeyDER); | 264 // SECItem*. |
107 trusted_ca_key_der_item.len = sizeof(kCAPublicKeyDER); | |
108 crypto::ScopedSECKEYPublicKey ca_public_key( | 265 crypto::ScopedSECKEYPublicKey ca_public_key( |
109 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA)); | 266 SECKEY_ImportDERPublicKey( |
| 267 const_cast<SECItem*>(trusted_ca_key_der), CKK_RSA)); |
110 SECStatus verified = CERT_VerifySignedDataWithPublicKey( | 268 SECStatus verified = CERT_VerifySignedDataWithPublicKey( |
111 &cert->signatureWrap, ca_public_key.get(), NULL); | 269 &cert->signatureWrap, ca_public_key.get(), NULL); |
112 if (verified != SECSuccess) { | 270 if (verified != SECSuccess) { |
113 VLOG(1)<< "Cert not signed by trusted CA"; | 271 VLOG(1)<< "Cert not signed by trusted CA"; |
114 return false; | 272 return false; |
115 } | 273 } |
| 274 VLOG(1) << "Cert signed by trusted CA"; |
116 | 275 |
117 // Verify that the |signature| matches |data|. | 276 // Verify that the |signature| matches |data|. |
118 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); | 277 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); |
119 if (!public_key.get()) { | 278 if (!public_key.get()) { |
120 VLOG(1) << "Unable to extract public key from certificate."; | 279 VLOG(1) << "Unable to extract public key from certificate."; |
121 return false; | 280 return false; |
122 } | 281 } |
123 SECItem signature_item; | 282 SECItem signature_item; |
124 signature_item.type = siBuffer; | 283 signature_item.type = siBuffer; |
125 signature_item.data = reinterpret_cast<unsigned char*>( | 284 signature_item.data = reinterpret_cast<unsigned char*>( |
126 const_cast<char*>(signature.data())); | 285 const_cast<char*>(signature.data())); |
127 signature_item.len = signature.length(); | 286 signature_item.len = signature.length(); |
128 verified = VFY_VerifyDataDirect( | 287 verified = VFY_VerifyDataDirect( |
129 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), | 288 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), |
130 data.size(), | 289 data.size(), |
131 public_key.get(), | 290 public_key.get(), |
132 &signature_item, | 291 &signature_item, |
133 SEC_OID_PKCS1_RSA_ENCRYPTION, | 292 SEC_OID_PKCS1_RSA_ENCRYPTION, |
134 SEC_OID_SHA1, NULL, NULL); | 293 SEC_OID_SHA1, NULL, NULL); |
135 | 294 |
136 if (verified != SECSuccess) { | 295 if (verified != SECSuccess) { |
137 VLOG(1) << "Signed blobs did not match."; | 296 VLOG(1) << "Signed blobs did not match."; |
138 return false; | 297 return false; |
139 } | 298 } |
| 299 VLOG(1) << "Signature verification succeeded"; |
140 return true; | 300 return true; |
141 } | 301 } |
142 | 302 |
143 } // namespace | 303 } // namespace |
144 | 304 |
145 namespace extensions { | 305 namespace extensions { |
146 namespace api { | 306 namespace api { |
147 namespace cast_channel { | 307 namespace cast_channel { |
148 | 308 |
149 bool AuthenticateChallengeReply(const CastMessage& challenge_reply, | 309 bool AuthenticateChallengeReply(const CastMessage& challenge_reply, |
150 const std::string& peer_cert) { | 310 const std::string& peer_cert) { |
151 if (peer_cert.empty()) | 311 if (peer_cert.empty()) |
152 return false; | 312 return false; |
153 | 313 |
154 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); | 314 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); |
155 DeviceAuthMessage auth_message; | 315 DeviceAuthMessage auth_message; |
156 if (!ParseAuthMessage(challenge_reply, &auth_message)) | 316 if (!ParseAuthMessage(challenge_reply, &auth_message)) |
157 return false; | 317 return false; |
158 | 318 |
159 const AuthResponse& response = auth_message.response(); | 319 const AuthResponse& response = auth_message.response(); |
160 return VerifyCredentials(response.client_auth_certificate(), | 320 return VerifyCredentials(response, peer_cert); |
161 response.signature(), | |
162 peer_cert); | |
163 } | 321 } |
164 | 322 |
165 } // namespace cast_channel | 323 } // namespace cast_channel |
166 } // namespace api | 324 } // namespace api |
167 } // namespace extensions | 325 } // namespace extensions |
OLD | NEW |