| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 TEST(SignatureVerifierTest, BasicTest) { | 14 TEST(SignatureVerifierTest, BasicTest) { |
| 15 // The input data in this test comes from real certificates. | 15 // The input data in this test comes from real certificates. |
| 16 // | 16 // |
| 17 // tbs_certificate ("to-be-signed certificate", the part of a certificate | 17 // tbs_certificate ("to-be-signed certificate", the part of a certificate that |
| 18 // that is signed), signature_algorithm, and algorithm come from the | 18 // is signed), signature, and algorithm come from the certificate of |
| 19 // certificate of bugs.webkit.org. | 19 // bugs.webkit.org. |
| 20 // | 20 // |
| 21 // public_key_info comes from the certificate of the issuer, Go Daddy Secure | 21 // public_key_info comes from the certificate of the issuer, Go Daddy Secure |
| 22 // Certification Authority. | 22 // Certification Authority. |
| 23 // | 23 // |
| 24 // The bytes in the array initializers are formatted to expose the DER | 24 // The bytes in the array initializers are formatted to expose the DER |
| 25 // encoding of the ASN.1 structures. | 25 // encoding of the ASN.1 structures. |
| 26 | 26 |
| 27 // The data that is signed is the following ASN.1 structure: | 27 // The data that is signed is the following ASN.1 structure: |
| 28 // TBSCertificate ::= SEQUENCE { | 28 // TBSCertificate ::= SEQUENCE { |
| 29 // ... -- omitted, not important | 29 // ... -- omitted, not important |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x48, | 109 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x48, |
| 110 0xdf, 0x60, 0x32, 0xcc, 0x89, 0x01, 0xb6, 0xdc, 0x2f, 0xe3, 0x73, 0xb5, | 110 0xdf, 0x60, 0x32, 0xcc, 0x89, 0x01, 0xb6, 0xdc, 0x2f, 0xe3, 0x73, 0xb5, |
| 111 0x9c, 0x16, 0x58, 0x32, 0x68, 0xa9, 0xc3, 0x30, 0x1f, 0x06, 0x03, 0x55, | 111 0x9c, 0x16, 0x58, 0x32, 0x68, 0xa9, 0xc3, 0x30, 0x1f, 0x06, 0x03, 0x55, |
| 112 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xfd, 0xac, 0x61, 0x32, | 112 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xfd, 0xac, 0x61, 0x32, |
| 113 0x93, 0x6c, 0x45, 0xd6, 0xe2, 0xee, 0x85, 0x5f, 0x9a, 0xba, 0xe7, 0x76, | 113 0x93, 0x6c, 0x45, 0xd6, 0xe2, 0xee, 0x85, 0x5f, 0x9a, 0xba, 0xe7, 0x76, |
| 114 0x99, 0x68, 0xcc, 0xe7, 0x30, 0x23, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, | 114 0x99, 0x68, 0xcc, 0xe7, 0x30, 0x23, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, |
| 115 0x1c, 0x30, 0x1a, 0x82, 0x0c, 0x2a, 0x2e, 0x77, 0x65, 0x62, 0x6b, 0x69, | 115 0x1c, 0x30, 0x1a, 0x82, 0x0c, 0x2a, 0x2e, 0x77, 0x65, 0x62, 0x6b, 0x69, |
| 116 0x74, 0x2e, 0x6f, 0x72, 0x67, 0x82, 0x0a, 0x77, 0x65, 0x62, 0x6b, 0x69, | 116 0x74, 0x2e, 0x6f, 0x72, 0x67, 0x82, 0x0a, 0x77, 0x65, 0x62, 0x6b, 0x69, |
| 117 0x74, 0x2e, 0x6f, 0x72, 0x67}; | 117 0x74, 0x2e, 0x6f, 0x72, 0x67}; |
| 118 | 118 |
| 119 // The signature algorithm is specified as the following ASN.1 structure: | |
| 120 // AlgorithmIdentifier ::= SEQUENCE { | |
| 121 // algorithm OBJECT IDENTIFIER, | |
| 122 // parameters ANY DEFINED BY algorithm OPTIONAL } | |
| 123 // | |
| 124 const uint8_t signature_algorithm[15] = { | |
| 125 0x30, 0x0d, // a SEQUENCE of length 13 (0xd) | |
| 126 0x06, 0x09, // an OBJECT IDENTIFIER of length 9 | |
| 127 // 1.2.840.113549.1.1.5 - sha1WithRSAEncryption | |
| 128 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, | |
| 129 0x00, // a NULL of length 0 | |
| 130 }; | |
| 131 | |
| 132 // RSA signature, a big integer in the big-endian byte order. | 119 // RSA signature, a big integer in the big-endian byte order. |
| 133 const uint8_t signature[256] = { | 120 const uint8_t signature[256] = { |
| 134 0x1e, 0x6a, 0xe7, 0xe0, 0x4f, 0xe7, 0x4d, 0xd0, 0x69, 0x7c, 0xf8, 0x8f, | 121 0x1e, 0x6a, 0xe7, 0xe0, 0x4f, 0xe7, 0x4d, 0xd0, 0x69, 0x7c, 0xf8, 0x8f, |
| 135 0x99, 0xb4, 0x18, 0x95, 0x36, 0x24, 0x0f, 0x0e, 0xa3, 0xea, 0x34, 0x37, | 122 0x99, 0xb4, 0x18, 0x95, 0x36, 0x24, 0x0f, 0x0e, 0xa3, 0xea, 0x34, 0x37, |
| 136 0xf4, 0x7d, 0xd5, 0x92, 0x35, 0x53, 0x72, 0x76, 0x3f, 0x69, 0xf0, 0x82, | 123 0xf4, 0x7d, 0xd5, 0x92, 0x35, 0x53, 0x72, 0x76, 0x3f, 0x69, 0xf0, 0x82, |
| 137 0x56, 0xe3, 0x94, 0x7a, 0x1d, 0x1a, 0x81, 0xaf, 0x9f, 0xc7, 0x43, 0x01, | 124 0x56, 0xe3, 0x94, 0x7a, 0x1d, 0x1a, 0x81, 0xaf, 0x9f, 0xc7, 0x43, 0x01, |
| 138 0x64, 0xd3, 0x7c, 0x0d, 0xc8, 0x11, 0x4e, 0x4a, 0xe6, 0x1a, 0xc3, 0x01, | 125 0x64, 0xd3, 0x7c, 0x0d, 0xc8, 0x11, 0x4e, 0x4a, 0xe6, 0x1a, 0xc3, 0x01, |
| 139 0x74, 0xe8, 0x35, 0x87, 0x5c, 0x61, 0xaa, 0x8a, 0x46, 0x06, 0xbe, 0x98, | 126 0x74, 0xe8, 0x35, 0x87, 0x5c, 0x61, 0xaa, 0x8a, 0x46, 0x06, 0xbe, 0x98, |
| 140 0x95, 0x24, 0x9e, 0x01, 0xe3, 0xe6, 0xa0, 0x98, 0xee, 0x36, 0x44, 0x56, | 127 0x95, 0x24, 0x9e, 0x01, 0xe3, 0xe6, 0xa0, 0x98, 0xee, 0x36, 0x44, 0x56, |
| 141 0x8d, 0x23, 0x9c, 0x65, 0xea, 0x55, 0x6a, 0xdf, 0x66, 0xee, 0x45, 0xe8, | 128 0x8d, 0x23, 0x9c, 0x65, 0xea, 0x55, 0x6a, 0xdf, 0x66, 0xee, 0x45, 0xe8, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 0xa3, 0x3f, 0x84, 0x0d, 0x4f, | 182 0xa3, 0x3f, 0x84, 0x0d, 0x4f, |
| 196 // public exponent | 183 // public exponent |
| 197 0x02, 0x03, // an INTEGER of length 3 | 184 0x02, 0x03, // an INTEGER of length 3 |
| 198 0x01, 0x00, 0x01}; | 185 0x01, 0x00, 0x01}; |
| 199 | 186 |
| 200 // We use the signature verifier to perform four signature verification | 187 // We use the signature verifier to perform four signature verification |
| 201 // tests. | 188 // tests. |
| 202 crypto::SignatureVerifier verifier; | 189 crypto::SignatureVerifier verifier; |
| 203 bool ok; | 190 bool ok; |
| 204 | 191 |
| 205 // Test 1: feed all of the data to the verifier at once (a single | 192 // Test 1: feed all of the data to the verifier at once (a single |
| 206 // VerifyUpdate call). | 193 // VerifyUpdate call). |
| 207 ok = verifier.VerifyInit(signature_algorithm, | 194 ok = verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, signature, |
| 208 sizeof(signature_algorithm), | 195 sizeof(signature), public_key_info, |
| 209 signature, sizeof(signature), | 196 sizeof(public_key_info)); |
| 210 public_key_info, sizeof(public_key_info)); | |
| 211 EXPECT_TRUE(ok); | 197 EXPECT_TRUE(ok); |
| 212 verifier.VerifyUpdate(tbs_certificate, sizeof(tbs_certificate)); | 198 verifier.VerifyUpdate(tbs_certificate, sizeof(tbs_certificate)); |
| 213 ok = verifier.VerifyFinal(); | 199 ok = verifier.VerifyFinal(); |
| 214 EXPECT_TRUE(ok); | 200 EXPECT_TRUE(ok); |
| 215 | 201 |
| 216 // Test 2: feed the data to the verifier in three parts (three VerifyUpdate | 202 // Test 2: feed the data to the verifier in three parts (three VerifyUpdate |
| 217 // calls). | 203 // calls). |
| 218 ok = verifier.VerifyInit(signature_algorithm, | 204 ok = verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, signature, |
| 219 sizeof(signature_algorithm), | 205 sizeof(signature), public_key_info, |
| 220 signature, sizeof(signature), | 206 sizeof(public_key_info)); |
| 221 public_key_info, sizeof(public_key_info)); | |
| 222 EXPECT_TRUE(ok); | 207 EXPECT_TRUE(ok); |
| 223 verifier.VerifyUpdate(tbs_certificate, 256); | 208 verifier.VerifyUpdate(tbs_certificate, 256); |
| 224 verifier.VerifyUpdate(tbs_certificate + 256, 256); | 209 verifier.VerifyUpdate(tbs_certificate + 256, 256); |
| 225 verifier.VerifyUpdate(tbs_certificate + 512, sizeof(tbs_certificate) - 512); | 210 verifier.VerifyUpdate(tbs_certificate + 512, sizeof(tbs_certificate) - 512); |
| 226 ok = verifier.VerifyFinal(); | 211 ok = verifier.VerifyFinal(); |
| 227 EXPECT_TRUE(ok); | 212 EXPECT_TRUE(ok); |
| 228 | 213 |
| 229 // Test 3: verify the signature with incorrect data. | 214 // Test 3: verify the signature with incorrect data. |
| 230 uint8_t bad_tbs_certificate[sizeof(tbs_certificate)]; | 215 uint8_t bad_tbs_certificate[sizeof(tbs_certificate)]; |
| 231 memcpy(bad_tbs_certificate, tbs_certificate, sizeof(tbs_certificate)); | 216 memcpy(bad_tbs_certificate, tbs_certificate, sizeof(tbs_certificate)); |
| 232 bad_tbs_certificate[10] += 1; // Corrupt one byte of the data. | 217 bad_tbs_certificate[10] += 1; // Corrupt one byte of the data. |
| 233 ok = verifier.VerifyInit(signature_algorithm, | 218 ok = verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, signature, |
| 234 sizeof(signature_algorithm), | 219 sizeof(signature), public_key_info, |
| 235 signature, sizeof(signature), | 220 sizeof(public_key_info)); |
| 236 public_key_info, sizeof(public_key_info)); | |
| 237 EXPECT_TRUE(ok); | 221 EXPECT_TRUE(ok); |
| 238 verifier.VerifyUpdate(bad_tbs_certificate, sizeof(bad_tbs_certificate)); | 222 verifier.VerifyUpdate(bad_tbs_certificate, sizeof(bad_tbs_certificate)); |
| 239 ok = verifier.VerifyFinal(); | 223 ok = verifier.VerifyFinal(); |
| 240 EXPECT_FALSE(ok); | 224 EXPECT_FALSE(ok); |
| 241 | 225 |
| 242 // Test 4: verify a bad signature. | 226 // Test 4: verify a bad signature. |
| 243 uint8_t bad_signature[sizeof(signature)]; | 227 uint8_t bad_signature[sizeof(signature)]; |
| 244 memcpy(bad_signature, signature, sizeof(signature)); | 228 memcpy(bad_signature, signature, sizeof(signature)); |
| 245 bad_signature[10] += 1; // Corrupt one byte of the signature. | 229 bad_signature[10] += 1; // Corrupt one byte of the signature. |
| 246 ok = verifier.VerifyInit(signature_algorithm, | 230 ok = verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, |
| 247 sizeof(signature_algorithm), | |
| 248 bad_signature, sizeof(bad_signature), | 231 bad_signature, sizeof(bad_signature), |
| 249 public_key_info, sizeof(public_key_info)); | 232 public_key_info, sizeof(public_key_info)); |
| 250 | 233 |
| 251 // A crypto library (e.g., NSS) may detect that the signature is corrupted | 234 // A crypto library (e.g., NSS) may detect that the signature is corrupted |
| 252 // and cause VerifyInit to return false, so it is fine for 'ok' to be false. | 235 // and cause VerifyInit to return false, so it is fine for 'ok' to be false. |
| 253 if (ok) { | 236 if (ok) { |
| 254 verifier.VerifyUpdate(tbs_certificate, sizeof(tbs_certificate)); | 237 verifier.VerifyUpdate(tbs_certificate, sizeof(tbs_certificate)); |
| 255 ok = verifier.VerifyFinal(); | 238 ok = verifier.VerifyFinal(); |
| 256 EXPECT_FALSE(ok); | 239 EXPECT_FALSE(ok); |
| 257 } | 240 } |
| 258 | 241 |
| 259 // Test 5: import an invalid key. | 242 // Test 5: import an invalid key. |
| 260 uint8_t bad_public_key_info[sizeof(public_key_info)]; | 243 uint8_t bad_public_key_info[sizeof(public_key_info)]; |
| 261 memcpy(bad_public_key_info, public_key_info, sizeof(public_key_info)); | 244 memcpy(bad_public_key_info, public_key_info, sizeof(public_key_info)); |
| 262 bad_public_key_info[0] += 1; // Corrupt part of the SPKI syntax. | 245 bad_public_key_info[0] += 1; // Corrupt part of the SPKI syntax. |
| 263 ok = verifier.VerifyInit(signature_algorithm, | 246 ok = verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, signature, |
| 264 sizeof(signature_algorithm), | 247 sizeof(signature), bad_public_key_info, |
| 265 signature, sizeof(signature), | 248 sizeof(bad_public_key_info)); |
| 266 bad_public_key_info, sizeof(bad_public_key_info)); | |
| 267 EXPECT_FALSE(ok); | 249 EXPECT_FALSE(ok); |
| 268 | 250 |
| 269 // Test 6: import a key with extra data. | 251 // Test 6: import a key with extra data. |
| 270 uint8_t long_public_key_info[sizeof(public_key_info) + 5]; | 252 uint8_t long_public_key_info[sizeof(public_key_info) + 5]; |
| 271 memset(long_public_key_info, 0, sizeof(long_public_key_info)); | 253 memset(long_public_key_info, 0, sizeof(long_public_key_info)); |
| 272 memcpy(long_public_key_info, public_key_info, sizeof(public_key_info)); | 254 memcpy(long_public_key_info, public_key_info, sizeof(public_key_info)); |
| 273 ok = verifier.VerifyInit(signature_algorithm, | 255 ok = verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, signature, |
| 274 sizeof(signature_algorithm), | 256 sizeof(signature), long_public_key_info, |
| 275 signature, sizeof(signature), | 257 sizeof(long_public_key_info)); |
| 276 long_public_key_info, sizeof(long_public_key_info)); | |
| 277 EXPECT_FALSE(ok); | 258 EXPECT_FALSE(ok); |
| 278 } | 259 } |
| 279 | 260 |
| 280 ////////////////////////////////////////////////////////////////////// | 261 ////////////////////////////////////////////////////////////////////// |
| 281 // | 262 // |
| 282 // RSA-PSS signature verification known answer test | 263 // RSA-PSS signature verification known answer test |
| 283 // | 264 // |
| 284 ////////////////////////////////////////////////////////////////////// | 265 ////////////////////////////////////////////////////////////////////// |
| 285 | 266 |
| 286 // The following RSA-PSS signature test vectors come from the pss-vect.txt | 267 // The following RSA-PSS signature test vectors come from the pss-vect.txt |
| (...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 } | 996 } |
| 1016 | 997 |
| 1017 static bool EncodeRSAPublicKey(const std::vector<uint8_t>& modulus_n, | 998 static bool EncodeRSAPublicKey(const std::vector<uint8_t>& modulus_n, |
| 1018 const std::vector<uint8_t>& public_exponent_e, | 999 const std::vector<uint8_t>& public_exponent_e, |
| 1019 std::vector<uint8_t>* public_key_info) { | 1000 std::vector<uint8_t>* public_key_info) { |
| 1020 // The public key is specified as the following ASN.1 structure: | 1001 // The public key is specified as the following ASN.1 structure: |
| 1021 // SubjectPublicKeyInfo ::= SEQUENCE { | 1002 // SubjectPublicKeyInfo ::= SEQUENCE { |
| 1022 // algorithm AlgorithmIdentifier, | 1003 // algorithm AlgorithmIdentifier, |
| 1023 // subjectPublicKey BIT STRING } | 1004 // subjectPublicKey BIT STRING } |
| 1024 // | 1005 // |
| 1025 // The signature algorithm is specified as the following ASN.1 structure: | 1006 // The algorithm is specified as the following ASN.1 structure: |
| 1026 // AlgorithmIdentifier ::= SEQUENCE { | 1007 // AlgorithmIdentifier ::= SEQUENCE { |
| 1027 // algorithm OBJECT IDENTIFIER, | 1008 // algorithm OBJECT IDENTIFIER, |
| 1028 // parameters ANY DEFINED BY algorithm OPTIONAL } | 1009 // parameters ANY DEFINED BY algorithm OPTIONAL } |
| 1029 // | 1010 // |
| 1030 // An RSA public key is specified as the following ASN.1 structure: | 1011 // An RSA public key is specified as the following ASN.1 structure: |
| 1031 // RSAPublicKey ::= SEQUENCE { | 1012 // RSAPublicKey ::= SEQUENCE { |
| 1032 // modulus INTEGER, -- n | 1013 // modulus INTEGER, -- n |
| 1033 // publicExponent INTEGER -- e | 1014 // publicExponent INTEGER -- e |
| 1034 // } | 1015 // } |
| 1035 static const uint8_t kIntegerTag = 0x02; | 1016 static const uint8_t kIntegerTag = 0x02; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 &public_key_info[0], | 1135 &public_key_info[0], |
| 1155 public_key_info.size()); | 1136 public_key_info.size()); |
| 1156 signature[0] -= 1; | 1137 signature[0] -= 1; |
| 1157 ASSERT_TRUE(ok); | 1138 ASSERT_TRUE(ok); |
| 1158 verifier.VerifyUpdate(&message[0], message.size()); | 1139 verifier.VerifyUpdate(&message[0], message.size()); |
| 1159 ok = verifier.VerifyFinal(); | 1140 ok = verifier.VerifyFinal(); |
| 1160 EXPECT_FALSE(ok); | 1141 EXPECT_FALSE(ok); |
| 1161 } | 1142 } |
| 1162 } | 1143 } |
| 1163 } | 1144 } |
| OLD | NEW |