| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/signature_verifier.h" | 5 #include "crypto/signature_verifier.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "crypto/capi_util.h" |
| 8 | 9 |
| 9 #pragma comment(lib, "crypt32.lib") | 10 #pragma comment(lib, "crypt32.lib") |
| 10 | 11 |
| 11 namespace { | |
| 12 | |
| 13 // Wrappers of malloc and free for CRYPT_DECODE_PARA, which requires the | |
| 14 // WINAPI calling convention. | |
| 15 void* WINAPI MyCryptAlloc(size_t size) { | |
| 16 return malloc(size); | |
| 17 } | |
| 18 | |
| 19 void WINAPI MyCryptFree(void* p) { | |
| 20 free(p); | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace crypto { | 12 namespace crypto { |
| 26 | 13 |
| 27 SignatureVerifier::SignatureVerifier() : hash_object_(0), public_key_(0) { | 14 SignatureVerifier::SignatureVerifier() : hash_object_(0), public_key_(0) { |
| 28 if (!CryptAcquireContext(provider_.receive(), NULL, NULL, | 15 if (!CryptAcquireContext(provider_.receive(), NULL, NULL, |
| 29 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) | 16 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 30 provider_.reset(); | 17 provider_.reset(); |
| 31 } | 18 } |
| 32 | 19 |
| 33 SignatureVerifier::~SignatureVerifier() { | 20 SignatureVerifier::~SignatureVerifier() { |
| 34 } | 21 } |
| 35 | 22 |
| 36 bool SignatureVerifier::VerifyInit(const uint8* signature_algorithm, | 23 bool SignatureVerifier::VerifyInit(const uint8* signature_algorithm, |
| 37 int signature_algorithm_len, | 24 int signature_algorithm_len, |
| 38 const uint8* signature, | 25 const uint8* signature, |
| 39 int signature_len, | 26 int signature_len, |
| 40 const uint8* public_key_info, | 27 const uint8* public_key_info, |
| 41 int public_key_info_len) { | 28 int public_key_info_len) { |
| 42 signature_.reserve(signature_len); | 29 signature_.reserve(signature_len); |
| 43 // CryptoAPI uses big integers in the little-endian byte order, so we need | 30 // CryptoAPI uses big integers in the little-endian byte order, so we need |
| 44 // to first swap the order of signature bytes. | 31 // to first swap the order of signature bytes. |
| 45 for (int i = signature_len - 1; i >= 0; --i) | 32 for (int i = signature_len - 1; i >= 0; --i) |
| 46 signature_.push_back(signature[i]); | 33 signature_.push_back(signature[i]); |
| 47 | 34 |
| 48 CRYPT_DECODE_PARA decode_para; | 35 CRYPT_DECODE_PARA decode_para; |
| 49 decode_para.cbSize = sizeof(decode_para); | 36 decode_para.cbSize = sizeof(decode_para); |
| 50 decode_para.pfnAlloc = MyCryptAlloc; | 37 decode_para.pfnAlloc = crypto::CryptAlloc; |
| 51 decode_para.pfnFree = MyCryptFree; | 38 decode_para.pfnFree = crypto::CryptFree; |
| 52 CERT_PUBLIC_KEY_INFO* cert_public_key_info = NULL; | 39 CERT_PUBLIC_KEY_INFO* cert_public_key_info = NULL; |
| 53 DWORD struct_len = 0; | 40 DWORD struct_len = 0; |
| 54 BOOL ok; | 41 BOOL ok; |
| 55 ok = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | 42 ok = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 56 X509_PUBLIC_KEY_INFO, | 43 X509_PUBLIC_KEY_INFO, |
| 57 public_key_info, | 44 public_key_info, |
| 58 public_key_info_len, | 45 public_key_info_len, |
| 59 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, | 46 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, |
| 60 &decode_para, | 47 &decode_para, |
| 61 &cert_public_key_info, | 48 &cert_public_key_info, |
| 62 &struct_len); | 49 &struct_len); |
| 63 if (!ok) | 50 if (!ok) |
| 64 return false; | 51 return false; |
| 65 | 52 |
| 66 ok = CryptImportPublicKeyInfo(provider_, | 53 ok = CryptImportPublicKeyInfo(provider_, |
| 67 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | 54 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 68 cert_public_key_info, public_key_.receive()); | 55 cert_public_key_info, public_key_.receive()); |
| 69 free(cert_public_key_info); | 56 crypto::CryptFree(cert_public_key_info); |
| 70 if (!ok) | 57 if (!ok) |
| 71 return false; | 58 return false; |
| 72 | 59 |
| 73 CRYPT_ALGORITHM_IDENTIFIER* signature_algorithm_id; | 60 CRYPT_ALGORITHM_IDENTIFIER* signature_algorithm_id; |
| 74 struct_len = 0; | 61 struct_len = 0; |
| 75 ok = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | 62 ok = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 76 X509_ALGORITHM_IDENTIFIER, | 63 X509_ALGORITHM_IDENTIFIER, |
| 77 signature_algorithm, | 64 signature_algorithm, |
| 78 signature_algorithm_len, | 65 signature_algorithm_len, |
| 79 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, | 66 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, |
| 80 &decode_para, | 67 &decode_para, |
| 81 &signature_algorithm_id, | 68 &signature_algorithm_id, |
| 82 &struct_len); | 69 &struct_len); |
| 83 DCHECK(ok || GetLastError() == ERROR_FILE_NOT_FOUND); | 70 DCHECK(ok || GetLastError() == ERROR_FILE_NOT_FOUND); |
| 84 ALG_ID hash_alg_id; | 71 ALG_ID hash_alg_id; |
| 85 if (ok) { | 72 if (ok) { |
| 86 hash_alg_id = CALG_MD4; // Initialize to a weak hash algorithm that we | 73 hash_alg_id = CALG_MD4; // Initialize to a weak hash algorithm that we |
| 87 // don't support. | 74 // don't support. |
| 88 if (!strcmp(signature_algorithm_id->pszObjId, szOID_RSA_SHA1RSA)) | 75 if (!strcmp(signature_algorithm_id->pszObjId, szOID_RSA_SHA1RSA)) |
| 89 hash_alg_id = CALG_SHA1; | 76 hash_alg_id = CALG_SHA1; |
| 90 else if (!strcmp(signature_algorithm_id->pszObjId, szOID_RSA_MD5RSA)) | 77 else if (!strcmp(signature_algorithm_id->pszObjId, szOID_RSA_MD5RSA)) |
| 91 hash_alg_id = CALG_MD5; | 78 hash_alg_id = CALG_MD5; |
| 92 free(signature_algorithm_id); | 79 crypto::CryptFree(signature_algorithm_id); |
| 93 DCHECK_NE(static_cast<ALG_ID>(CALG_MD4), hash_alg_id); | 80 DCHECK_NE(static_cast<ALG_ID>(CALG_MD4), hash_alg_id); |
| 94 if (hash_alg_id == CALG_MD4) | 81 if (hash_alg_id == CALG_MD4) |
| 95 return false; // Unsupported hash algorithm. | 82 return false; // Unsupported hash algorithm. |
| 96 } else if (GetLastError() == ERROR_FILE_NOT_FOUND) { | 83 } else if (GetLastError() == ERROR_FILE_NOT_FOUND) { |
| 97 // TODO(wtc): X509_ALGORITHM_IDENTIFIER isn't supported on XP SP2. We | 84 // TODO(wtc): X509_ALGORITHM_IDENTIFIER isn't supported on XP SP2. We |
| 98 // may be able to encapsulate signature_algorithm in a dummy SignedContent | 85 // may be able to encapsulate signature_algorithm in a dummy SignedContent |
| 99 // and decode it with X509_CERT into a CERT_SIGNED_CONTENT_INFO. For now, | 86 // and decode it with X509_CERT into a CERT_SIGNED_CONTENT_INFO. For now, |
| 100 // just hardcode the hash algorithm to be SHA-1. | 87 // just hardcode the hash algorithm to be SHA-1. |
| 101 hash_alg_id = CALG_SHA1; | 88 hash_alg_id = CALG_SHA1; |
| 102 } else { | 89 } else { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 125 } | 112 } |
| 126 | 113 |
| 127 void SignatureVerifier::Reset() { | 114 void SignatureVerifier::Reset() { |
| 128 hash_object_.reset(); | 115 hash_object_.reset(); |
| 129 public_key_.reset(); | 116 public_key_.reset(); |
| 130 signature_.clear(); | 117 signature_.clear(); |
| 131 } | 118 } |
| 132 | 119 |
| 133 } // namespace crypto | 120 } // namespace crypto |
| 134 | 121 |
| OLD | NEW |