OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/common/extensions/api/networking_private/networking_private_cry pto.h" | 5 #include "chrome/common/extensions/api/networking_private/networking_private_cry pto.h" |
6 | 6 |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "extensions/common/cast/cast_cert_validator.h" | |
11 #include "net/cert/pem_tokenizer.h" | |
12 | |
13 namespace { | |
14 | |
15 namespace cast_crypto = ::extensions::core_api::cast_crypto; | |
16 | |
17 } // namespace | |
18 | |
7 namespace networking_private_crypto { | 19 namespace networking_private_crypto { |
8 | 20 |
9 const uint8 kTrustedCAPublicKeyDER[] = { | 21 bool VerifyCredentials( |
10 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, | 22 const std::string& certificate, |
11 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, | 23 const std::vector<std::string>& intermediate_certificates, |
12 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, | 24 const std::string& signature, |
13 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf, | 25 const std::string& data, |
14 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80, | 26 const std::string& connected_mac) { |
15 0xa4, 0x47, 0x15, 0xc1, 0x20, 0x99, 0x50, 0xcd, 0xec, 0x14, 0x62, 0x09, | 27 std::vector<std::string> headers; |
16 0x5b, 0xa4, 0x98, 0xcd, 0xd2, 0x41, 0xb6, 0x36, 0x4e, 0xff, 0xe8, 0x2e, | 28 headers.push_back("CERTIFICATE"); |
17 0x32, 0x30, 0x4a, 0x81, 0xa8, 0x42, 0xa3, 0x6c, 0x9b, 0x33, 0x6e, 0xca, | |
18 0xb2, 0xf5, 0x53, 0x66, 0xe0, 0x27, 0x53, 0x86, 0x1a, 0x85, 0x1e, 0xa7, | |
19 0x39, 0x3f, 0x4a, 0x77, 0x8e, 0xfb, 0x54, 0x66, 0x66, 0xfb, 0x58, 0x54, | |
20 0xc0, 0x5e, 0x39, 0xc7, 0xf5, 0x50, 0x06, 0x0b, 0xe0, 0x8a, 0xd4, 0xce, | |
21 0xe1, 0x6a, 0x55, 0x1f, 0x8b, 0x17, 0x00, 0xe6, 0x69, 0xa3, 0x27, 0xe6, | |
22 0x08, 0x25, 0x69, 0x3c, 0x12, 0x9d, 0x8d, 0x05, 0x2c, 0xd6, 0x2e, 0xa2, | |
23 0x31, 0xde, 0xb4, 0x52, 0x50, 0xd6, 0x20, 0x49, 0xde, 0x71, 0xa0, 0xf9, | |
24 0xad, 0x20, 0x40, 0x12, 0xf1, 0xdd, 0x25, 0xeb, 0xd5, 0xe6, 0xb8, 0x36, | |
25 0xf4, 0xd6, 0x8f, 0x7f, 0xca, 0x43, 0xdc, 0xd7, 0x10, 0x5b, 0xe6, 0x3f, | |
26 0x51, 0x8a, 0x85, 0xb3, 0xf3, 0xff, 0xf6, 0x03, 0x2d, 0xcb, 0x23, 0x4f, | |
27 0x9c, 0xad, 0x18, 0xe7, 0x93, 0x05, 0x8c, 0xac, 0x52, 0x9a, 0xf7, 0x4c, | |
28 0xe9, 0x99, 0x7a, 0xbe, 0x6e, 0x7e, 0x4d, 0x0a, 0xe3, 0xc6, 0x1c, 0xa9, | |
29 0x93, 0xfa, 0x3a, 0xa5, 0x91, 0x5d, 0x1c, 0xbd, 0x66, 0xeb, 0xcc, 0x60, | |
30 0xdc, 0x86, 0x74, 0xca, 0xcf, 0xf8, 0x92, 0x1c, 0x98, 0x7d, 0x57, 0xfa, | |
31 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, 0x80, 0x2a, 0x92, 0xc5, | |
32 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01}; | |
33 | 29 |
34 const size_t kTrustedCAPublicKeyDERLength = sizeof(kTrustedCAPublicKeyDER); | 30 // Convert certificate from PEM to raw DER |
31 net::PEMTokenizer pem_tok(certificate, headers); | |
32 if (!pem_tok.GetNext()) { | |
33 LOG(ERROR) << "Failed to parse certificate."; | |
34 return false; | |
35 } | |
36 std::string der_certificate = pem_tok.data(); | |
37 | |
38 // Convert intermediate certificates from PEM to raw DER | |
39 std::vector<std::string> der_intermediate_certificates; | |
40 for (size_t idx = 0; idx < intermediate_certificates.size(); ++idx) { | |
41 net::PEMTokenizer ica_pem_tok(intermediate_certificates[idx], headers); | |
42 if (ica_pem_tok.GetNext()) | |
43 der_intermediate_certificates.push_back(ica_pem_tok.data()); | |
44 else | |
45 LOG(ERROR) << "Failed to parse certificate: "; | |
Ryan Sleevi
2014/12/15 21:42:59
WARNING: Include braces around single line LOG sta
sheretov
2014/12/16 08:44:20
Done.
| |
46 } | |
47 | |
48 // Verify the certificate | |
49 cast_crypto::CertVerificationContext* verification_context = NULL; | |
50 | |
51 cast_crypto::VerificationResult verification_result = cast_crypto::VerifyCert( | |
52 der_certificate, der_intermediate_certificates, &verification_context); | |
53 | |
54 scoped_ptr<cast_crypto::CertVerificationContext> verification_context_ptr( | |
55 verification_context); | |
56 | |
57 if (verification_result.Failure()) | |
58 return false; | |
59 | |
60 // Check that the device listed in the certificate is correct. | |
61 // Something like evt_e161 001a11ffacdf | |
62 std::string common_name = verification_context->getCommonName(); | |
63 std::string translated_mac; | |
64 base::RemoveChars(connected_mac, ":", &translated_mac); | |
65 if (!EndsWith(common_name, translated_mac, false)) { | |
66 LOG(ERROR) << "MAC addresses don't match."; | |
67 return false; | |
68 } | |
69 | |
70 // Make sure that the certificate matches the unsigned data presented. | |
71 // Verify that the |signature| matches |data|. | |
72 return verification_context->VerifySignatureOverData(signature, data) | |
73 .Success(); | |
Ryan Sleevi
2014/12/15 21:42:59
nit: Did clang-format do this? A modern one? I tho
sheretov
2014/12/16 08:44:20
Sure did. I'm not a big fan myself, but I'm train
| |
74 } | |
35 | 75 |
36 } // namespace networking_private_crypto | 76 } // namespace networking_private_crypto |
OLD | NEW |