| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CRYPTO_SIGNATURE_VERIFIER_H_ | 5 #ifndef CRYPTO_SIGNATURE_VERIFIER_H_ |
| 6 #define CRYPTO_SIGNATURE_VERIFIER_H_ | 6 #define CRYPTO_SIGNATURE_VERIFIER_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "crypto/crypto_export.h" | 13 #include "crypto/crypto_export.h" |
| 14 | 14 |
| 15 #if !defined(USE_OPENSSL) | 15 #if defined(USE_OPENSSL) |
| 16 typedef struct env_md_st EVP_MD; |
| 17 typedef struct evp_pkey_ctx_st EVP_PKEY_CTX; |
| 18 #else |
| 19 typedef struct HASHContextStr HASHContext; |
| 20 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; |
| 16 typedef struct VFYContextStr VFYContext; | 21 typedef struct VFYContextStr VFYContext; |
| 17 #endif | 22 #endif |
| 18 | 23 |
| 19 namespace crypto { | 24 namespace crypto { |
| 20 | 25 |
| 21 // The SignatureVerifier class verifies a signature using a bare public key | 26 // The SignatureVerifier class verifies a signature using a bare public key |
| 22 // (as opposed to a certificate). | 27 // (as opposed to a certificate). |
| 23 class CRYPTO_EXPORT SignatureVerifier { | 28 class CRYPTO_EXPORT SignatureVerifier { |
| 24 public: | 29 public: |
| 30 // The set of supported hash functions. Extend as required. |
| 31 enum HashAlgorithm { |
| 32 SHA1, |
| 33 SHA256, |
| 34 }; |
| 35 |
| 25 SignatureVerifier(); | 36 SignatureVerifier(); |
| 26 ~SignatureVerifier(); | 37 ~SignatureVerifier(); |
| 27 | 38 |
| 28 // Streaming interface: | 39 // Streaming interface: |
| 29 | 40 |
| 30 // Initiates a signature verification operation. This should be followed | 41 // Initiates a signature verification operation. This should be followed |
| 31 // by one or more VerifyUpdate calls and a VerifyFinal call. | 42 // by one or more VerifyUpdate calls and a VerifyFinal call. |
| 43 // NOTE: for RSA-PSS signatures, use VerifyInitRSAPSS instead. |
| 32 // | 44 // |
| 33 // The signature algorithm is specified as a DER encoded ASN.1 | 45 // The signature algorithm is specified as a DER encoded ASN.1 |
| 34 // AlgorithmIdentifier structure: | 46 // AlgorithmIdentifier structure: |
| 35 // AlgorithmIdentifier ::= SEQUENCE { | 47 // AlgorithmIdentifier ::= SEQUENCE { |
| 36 // algorithm OBJECT IDENTIFIER, | 48 // algorithm OBJECT IDENTIFIER, |
| 37 // parameters ANY DEFINED BY algorithm OPTIONAL } | 49 // parameters ANY DEFINED BY algorithm OPTIONAL } |
| 38 // | 50 // |
| 39 // The signature is encoded according to the signature algorithm, but it | 51 // The signature is encoded according to the signature algorithm, but it |
| 40 // must not be further encoded in an ASN.1 BIT STRING. | 52 // must not be further encoded in an ASN.1 BIT STRING. |
| 41 // Note: An RSA signatures is actually a big integer. It must be in the | 53 // Note: An RSA signature is actually a big integer. It must be in |
| 42 // big-endian byte order. | 54 // big-endian byte order. |
| 43 // | 55 // |
| 44 // The public key is specified as a DER encoded ASN.1 SubjectPublicKeyInfo | 56 // The public key is specified as a DER encoded ASN.1 SubjectPublicKeyInfo |
| 45 // structure, which contains not only the public key but also its type | 57 // structure, which contains not only the public key but also its type |
| 46 // (algorithm): | 58 // (algorithm): |
| 47 // SubjectPublicKeyInfo ::= SEQUENCE { | 59 // SubjectPublicKeyInfo ::= SEQUENCE { |
| 48 // algorithm AlgorithmIdentifier, | 60 // algorithm AlgorithmIdentifier, |
| 49 // subjectPublicKey BIT STRING } | 61 // subjectPublicKey BIT STRING } |
| 50 bool VerifyInit(const uint8* signature_algorithm, | 62 bool VerifyInit(const uint8* signature_algorithm, |
| 51 int signature_algorithm_len, | 63 int signature_algorithm_len, |
| 52 const uint8* signature, | 64 const uint8* signature, |
| 53 int signature_len, | 65 int signature_len, |
| 54 const uint8* public_key_info, | 66 const uint8* public_key_info, |
| 55 int public_key_info_len); | 67 int public_key_info_len); |
| 56 | 68 |
| 69 // Initiates a RSA-PSS signature verification operation. This should be |
| 70 // followed by one or more VerifyUpdate calls and a VerifyFinal call. |
| 71 // |
| 72 // The RSA-PSS signature algorithm parameters are specified with the |
| 73 // |hash_alg|, |mask_hash_alg|, and |salt_len| arguments. |
| 74 // |
| 75 // An RSA-PSS signature is a nonnegative integer encoded as a byte string |
| 76 // (of the same length as the RSA modulus) in big-endian byte order. It |
| 77 // must not be further encoded in an ASN.1 BIT STRING. |
| 78 // |
| 79 // The public key is specified as a DER encoded ASN.1 SubjectPublicKeyInfo |
| 80 // structure, which contains not only the public key but also its type |
| 81 // (algorithm): |
| 82 // SubjectPublicKeyInfo ::= SEQUENCE { |
| 83 // algorithm AlgorithmIdentifier, |
| 84 // subjectPublicKey BIT STRING } |
| 85 bool VerifyInitRSAPSS(HashAlgorithm hash_alg, |
| 86 HashAlgorithm mask_hash_alg, |
| 87 int salt_len, |
| 88 const uint8* signature, |
| 89 int signature_len, |
| 90 const uint8* public_key_info, |
| 91 int public_key_info_len); |
| 92 |
| 57 // Feeds a piece of the data to the signature verifier. | 93 // Feeds a piece of the data to the signature verifier. |
| 58 void VerifyUpdate(const uint8* data_part, int data_part_len); | 94 void VerifyUpdate(const uint8* data_part, int data_part_len); |
| 59 | 95 |
| 60 // Concludes a signature verification operation. Returns true if the | 96 // Concludes a signature verification operation. Returns true if the |
| 61 // signature is valid. Returns false if the signature is invalid or an | 97 // signature is valid. Returns false if the signature is invalid or an |
| 62 // error occurred. | 98 // error occurred. |
| 63 bool VerifyFinal(); | 99 bool VerifyFinal(); |
| 64 | 100 |
| 65 // Note: we can provide a one-shot interface if there is interest: | 101 // Note: we can provide a one-shot interface if there is interest: |
| 66 // bool Verify(const uint8* data, | 102 // bool Verify(const uint8* data, |
| 67 // int data_len, | 103 // int data_len, |
| 68 // const uint8* signature_algorithm, | 104 // const uint8* signature_algorithm, |
| 69 // int signature_algorithm_len, | 105 // int signature_algorithm_len, |
| 70 // const uint8* signature, | 106 // const uint8* signature, |
| 71 // int signature_len, | 107 // int signature_len, |
| 72 // const uint8* public_key_info, | 108 // const uint8* public_key_info, |
| 73 // int public_key_info_len); | 109 // int public_key_info_len); |
| 74 | 110 |
| 75 private: | 111 private: |
| 112 #if defined(USE_OPENSSL) |
| 113 bool CommonInit(const EVP_MD* digest, |
| 114 const uint8* signature, |
| 115 int signature_len, |
| 116 const uint8* public_key_info, |
| 117 int public_key_info_len, |
| 118 EVP_PKEY_CTX** pkey_ctx); |
| 119 #else |
| 120 static SECKEYPublicKey* DecodePublicKeyInfo(const uint8* public_key_info, |
| 121 int public_key_info_len); |
| 122 #endif |
| 123 |
| 76 void Reset(); | 124 void Reset(); |
| 77 | 125 |
| 78 std::vector<uint8> signature_; | 126 std::vector<uint8> signature_; |
| 79 | 127 |
| 80 #if defined(USE_OPENSSL) | 128 #if defined(USE_OPENSSL) |
| 81 struct VerifyContext; | 129 struct VerifyContext; |
| 82 VerifyContext* verify_context_; | 130 VerifyContext* verify_context_; |
| 83 #else | 131 #else |
| 132 // Used for all signature types except RSA-PSS. |
| 84 VFYContext* vfy_context_; | 133 VFYContext* vfy_context_; |
| 134 |
| 135 // Used for RSA-PSS signatures. |
| 136 HashAlgorithm hash_alg_; |
| 137 HashAlgorithm mask_hash_alg_; |
| 138 unsigned int salt_len_; |
| 139 SECKEYPublicKey* public_key_; |
| 140 HASHContext* hash_context_; |
| 85 #endif | 141 #endif |
| 86 }; | 142 }; |
| 87 | 143 |
| 88 } // namespace crypto | 144 } // namespace crypto |
| 89 | 145 |
| 90 #endif // CRYPTO_SIGNATURE_VERIFIER_H_ | 146 #endif // CRYPTO_SIGNATURE_VERIFIER_H_ |
| OLD | NEW |