| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "net/tools/domain_security_preload_generator/cert_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/base64.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "net/tools/domain_security_preload_generator/spki_hash.h" |
| 14 #include "third_party/boringssl/src/include/openssl/crypto.h" |
| 15 #include "third_party/boringssl/src/include/openssl/x509v3.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 static const char* kPEMBeginBlock = "-----BEGIN %s-----"; |
| 20 static const char* kPEMEndBlock = "-----END %s-----"; |
| 21 |
| 22 // Tries to extract the BASE64 encoded DER structure from |pem_input| by looking |
| 23 // for the block type in |expected_block_type|. Only attempts the locate the |
| 24 // first matching block. Other blocks are ignored. Returns true on success and |
| 25 // copies the der structure to der_output. Returns false on error. |
| 26 bool ParsePEM(base::StringPiece pem_input, |
| 27 base::StringPiece expected_block_type, |
| 28 std::string* der_output) { |
| 29 const std::string& block_start = |
| 30 base::StringPrintf(kPEMBeginBlock, expected_block_type.data()); |
| 31 const std::string& block_end = |
| 32 base::StringPrintf(kPEMEndBlock, expected_block_type.data()); |
| 33 |
| 34 size_t block_start_pos = pem_input.find(block_start); |
| 35 if (block_start_pos == std::string::npos) |
| 36 return false; |
| 37 size_t base64_start_pos = block_start_pos + block_start.size(); |
| 38 |
| 39 size_t block_end_pos = pem_input.find(block_end, base64_start_pos); |
| 40 if (block_end_pos == std::string::npos) |
| 41 return false; |
| 42 |
| 43 base::StringPiece base64_encoded = |
| 44 pem_input.substr(base64_start_pos, block_end_pos - base64_start_pos); |
| 45 |
| 46 if (!base::Base64Decode( |
| 47 base::CollapseWhitespaceASCII(base64_encoded.as_string(), true), |
| 48 der_output)) { |
| 49 return false; |
| 50 } |
| 51 |
| 52 return true; |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 bool CalculateSPKIHashFromCertificate(base::StringPiece pem_certificate, |
| 58 net::SPKIHash* out_hash) { |
| 59 std::string der; |
| 60 bool result = ParsePEM(pem_certificate, "CERTIFICATE", &der); |
| 61 if (!result) { |
| 62 return false; |
| 63 } |
| 64 |
| 65 const unsigned char* der_data = |
| 66 reinterpret_cast<const unsigned char*>(der.c_str()); |
| 67 X509* cert = d2i_X509(NULL, &der_data, base::checked_cast<long>(der.size())); |
| 68 if (!cert) { |
| 69 return false; |
| 70 } |
| 71 |
| 72 bssl::UniquePtr<EVP_PKEY> key(X509_get_pubkey(cert)); |
| 73 if (!key) { |
| 74 return false; |
| 75 } |
| 76 |
| 77 uint8_t* spki_der; |
| 78 size_t spki_der_len; |
| 79 bssl::ScopedCBB cbb; |
| 80 if (!CBB_init(cbb.get(), 0) || |
| 81 !EVP_marshal_public_key(cbb.get(), key.get()) || |
| 82 !CBB_finish(cbb.get(), &spki_der, &spki_der_len)) { |
| 83 return false; |
| 84 } |
| 85 |
| 86 out_hash->CalculateFromBytes(spki_der, spki_der_len); |
| 87 OPENSSL_free(spki_der); |
| 88 return result; |
| 89 } |
| 90 |
| 91 bool CalculateSPKIHashFromKey(base::StringPiece pem_key, |
| 92 net::SPKIHash* out_hash) { |
| 93 std::string der; |
| 94 bool result = ParsePEM(pem_key, "PUBLIC KEY", &der); |
| 95 if (!result) { |
| 96 return false; |
| 97 } |
| 98 |
| 99 return out_hash->CalculateFromBytes( |
| 100 reinterpret_cast<const uint8_t*>(der.data()), der.size()); |
| 101 } |
| 102 |
| 103 void InitializeCryptoLibrary() { |
| 104 CRYPTO_library_init(); |
| 105 } |
| OLD | NEW |