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/numerics/safe_conversions.h" | 7 #include "base/numerics/safe_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 TEST(SignatureVerifierTest, BasicTest) { | 10 TEST(SignatureVerifierTest, BasicTest) { |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 while (in[0] != '\0') { | 993 while (in[0] != '\0') { |
994 if (!isxdigit(in[0]) || !isxdigit(in[1]) || in[2] != ' ') | 994 if (!isxdigit(in[0]) || !isxdigit(in[1]) || in[2] != ' ') |
995 return false; | 995 return false; |
996 uint8 octet = HexDigitValue(in[0]) * 16 + HexDigitValue(in[1]); | 996 uint8 octet = HexDigitValue(in[0]) * 16 + HexDigitValue(in[1]); |
997 out->push_back(octet); | 997 out->push_back(octet); |
998 in += 3; | 998 in += 3; |
999 } | 999 } |
1000 return true; | 1000 return true; |
1001 } | 1001 } |
1002 | 1002 |
| 1003 // PrependASN1Length prepends an ASN.1 serialized length to the beginning of |
| 1004 // |out|. |
| 1005 static void PrependASN1Length(std::vector<uint8>* out, size_t len) { |
| 1006 if (len < 128) { |
| 1007 out->insert(out->begin(), static_cast<uint8>(len)); |
| 1008 } else if (len < 256) { |
| 1009 out->insert(out->begin(), static_cast<uint8>(len)); |
| 1010 out->insert(out->begin(), 0x81); |
| 1011 } else if (len < 0x10000) { |
| 1012 out->insert(out->begin(), static_cast<uint8>(len)); |
| 1013 out->insert(out->begin(), static_cast<uint8>(len >> 8)); |
| 1014 out->insert(out->begin(), 0x82); |
| 1015 } else { |
| 1016 CHECK(false) << "ASN.1 length not handled: " << len; |
| 1017 } |
| 1018 } |
| 1019 |
1003 static bool EncodeRSAPublicKey(const std::vector<uint8>& modulus_n, | 1020 static bool EncodeRSAPublicKey(const std::vector<uint8>& modulus_n, |
1004 const std::vector<uint8>& public_exponent_e, | 1021 const std::vector<uint8>& public_exponent_e, |
1005 std::vector<uint8>* public_key_info) { | 1022 std::vector<uint8>* public_key_info) { |
1006 // The public key is specified as the following ASN.1 structure: | 1023 // The public key is specified as the following ASN.1 structure: |
1007 // SubjectPublicKeyInfo ::= SEQUENCE { | 1024 // SubjectPublicKeyInfo ::= SEQUENCE { |
1008 // algorithm AlgorithmIdentifier, | 1025 // algorithm AlgorithmIdentifier, |
1009 // subjectPublicKey BIT STRING } | 1026 // subjectPublicKey BIT STRING } |
1010 // | 1027 // |
1011 // The signature algorithm is specified as the following ASN.1 structure: | 1028 // The signature algorithm is specified as the following ASN.1 structure: |
1012 // AlgorithmIdentifier ::= SEQUENCE { | 1029 // AlgorithmIdentifier ::= SEQUENCE { |
1013 // algorithm OBJECT IDENTIFIER, | 1030 // algorithm OBJECT IDENTIFIER, |
1014 // parameters ANY DEFINED BY algorithm OPTIONAL } | 1031 // parameters ANY DEFINED BY algorithm OPTIONAL } |
1015 // | 1032 // |
1016 // An RSA public key is specified as the following ASN.1 structure: | 1033 // An RSA public key is specified as the following ASN.1 structure: |
1017 // RSAPublicKey ::= SEQUENCE { | 1034 // RSAPublicKey ::= SEQUENCE { |
1018 // modulus INTEGER, -- n | 1035 // modulus INTEGER, -- n |
1019 // publicExponent INTEGER -- e | 1036 // publicExponent INTEGER -- e |
1020 // } | 1037 // } |
1021 static const uint8 kIntegerTag = 0x02; | 1038 static const uint8 kIntegerTag = 0x02; |
1022 static const uint8 kBitStringTag = 0x03; | 1039 static const uint8 kBitStringTag = 0x03; |
1023 static const uint8 kSequenceTag = 0x30; | 1040 static const uint8 kSequenceTag = 0x30; |
1024 public_key_info->clear(); | 1041 public_key_info->clear(); |
1025 | 1042 |
1026 // Encode the public exponent e as an INTEGER. | 1043 // Encode the public exponent e as an INTEGER. |
1027 public_key_info->insert(public_key_info->begin(), | 1044 public_key_info->insert(public_key_info->begin(), |
1028 public_exponent_e.begin(), | 1045 public_exponent_e.begin(), |
1029 public_exponent_e.end()); | 1046 public_exponent_e.end()); |
1030 uint8 exponent_size = base::checked_cast<uint8>(public_exponent_e.size()); | 1047 PrependASN1Length(public_key_info, public_exponent_e.size()); |
1031 public_key_info->insert(public_key_info->begin(), exponent_size); | |
1032 public_key_info->insert(public_key_info->begin(), kIntegerTag); | 1048 public_key_info->insert(public_key_info->begin(), kIntegerTag); |
1033 | 1049 |
1034 // Encode the modulus n as an INTEGER. | 1050 // Encode the modulus n as an INTEGER. |
1035 public_key_info->insert(public_key_info->begin(), | 1051 public_key_info->insert(public_key_info->begin(), |
1036 modulus_n.begin(), modulus_n.end()); | 1052 modulus_n.begin(), modulus_n.end()); |
1037 uint16 modulus_size = base::checked_cast<uint16>(modulus_n.size()); | 1053 size_t modulus_size = modulus_n.size(); |
1038 if (modulus_n[0] & 0x80) { | 1054 if (modulus_n[0] & 0x80) { |
1039 public_key_info->insert(public_key_info->begin(), 0x00); | 1055 public_key_info->insert(public_key_info->begin(), 0x00); |
1040 modulus_size++; | 1056 modulus_size++; |
1041 } | 1057 } |
1042 public_key_info->insert(public_key_info->begin(), modulus_size & 0xff); | 1058 PrependASN1Length(public_key_info, modulus_size); |
1043 public_key_info->insert(public_key_info->begin(), (modulus_size >> 8) & 0xff); | |
1044 public_key_info->insert(public_key_info->begin(), 0x82); | |
1045 public_key_info->insert(public_key_info->begin(), kIntegerTag); | 1059 public_key_info->insert(public_key_info->begin(), kIntegerTag); |
1046 | 1060 |
1047 // Encode the RSAPublicKey SEQUENCE. | 1061 // Encode the RSAPublicKey SEQUENCE. |
1048 uint16 info_size = base::checked_cast<uint16>(public_key_info->size()); | 1062 PrependASN1Length(public_key_info, public_key_info->size()); |
1049 public_key_info->insert(public_key_info->begin(), info_size & 0xff); | |
1050 public_key_info->insert(public_key_info->begin(), (info_size >> 8) & 0xff); | |
1051 public_key_info->insert(public_key_info->begin(), 0x82); | |
1052 public_key_info->insert(public_key_info->begin(), kSequenceTag); | 1063 public_key_info->insert(public_key_info->begin(), kSequenceTag); |
1053 | 1064 |
1054 // Encode the BIT STRING. | 1065 // Encode the BIT STRING. |
1055 // Number of unused bits. | 1066 // Number of unused bits. |
1056 public_key_info->insert(public_key_info->begin(), 0x00); | 1067 public_key_info->insert(public_key_info->begin(), 0x00); |
1057 info_size = base::checked_cast<uint16>(public_key_info->size()); | 1068 PrependASN1Length(public_key_info, public_key_info->size()); |
1058 public_key_info->insert(public_key_info->begin(), info_size & 0xff); | |
1059 public_key_info->insert(public_key_info->begin(), (info_size >> 8) & 0xff); | |
1060 public_key_info->insert(public_key_info->begin(), 0x82); | |
1061 public_key_info->insert(public_key_info->begin(), kBitStringTag); | 1069 public_key_info->insert(public_key_info->begin(), kBitStringTag); |
1062 | 1070 |
1063 // Encode the AlgorithmIdentifier. | 1071 // Encode the AlgorithmIdentifier. |
1064 static const uint8 algorithm[] = { | 1072 static const uint8 algorithm[] = { |
1065 0x30, 0x0d, // a SEQUENCE of length 13 | 1073 0x30, 0x0d, // a SEQUENCE of length 13 |
1066 0x06, 0x09, // an OBJECT IDENTIFIER of length 9 | 1074 0x06, 0x09, // an OBJECT IDENTIFIER of length 9 |
1067 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, | 1075 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, |
1068 0x05, 0x00, | 1076 0x05, 0x00, |
1069 }; | 1077 }; |
1070 public_key_info->insert(public_key_info->begin(), | 1078 public_key_info->insert(public_key_info->begin(), |
1071 algorithm, algorithm + sizeof(algorithm)); | 1079 algorithm, algorithm + sizeof(algorithm)); |
1072 | 1080 |
1073 // Encode the outermost SEQUENCE. | 1081 // Encode the outermost SEQUENCE. |
1074 info_size = base::checked_cast<uint16>(public_key_info->size()); | 1082 PrependASN1Length(public_key_info, public_key_info->size()); |
1075 public_key_info->insert(public_key_info->begin(), info_size & 0xff); | |
1076 public_key_info->insert(public_key_info->begin(), (info_size >> 8) & 0xff); | |
1077 public_key_info->insert(public_key_info->begin(), 0x82); | |
1078 public_key_info->insert(public_key_info->begin(), kSequenceTag); | 1083 public_key_info->insert(public_key_info->begin(), kSequenceTag); |
1079 | 1084 |
1080 return true; | 1085 return true; |
1081 } | 1086 } |
1082 | 1087 |
1083 TEST(SignatureVerifierTest, VerifyRSAPSS) { | 1088 TEST(SignatureVerifierTest, VerifyRSAPSS) { |
1084 for (unsigned int i = 0; i < arraysize(pss_test); i++) { | 1089 for (unsigned int i = 0; i < arraysize(pss_test); i++) { |
| 1090 SCOPED_TRACE(i); |
1085 std::vector<uint8> modulus_n; | 1091 std::vector<uint8> modulus_n; |
1086 std::vector<uint8> public_exponent_e; | 1092 std::vector<uint8> public_exponent_e; |
1087 ASSERT_TRUE(DecodeTestInput(pss_test[i].modulus_n, &modulus_n)); | 1093 ASSERT_TRUE(DecodeTestInput(pss_test[i].modulus_n, &modulus_n)); |
1088 ASSERT_TRUE(DecodeTestInput(pss_test[i].public_exponent_e, | 1094 ASSERT_TRUE(DecodeTestInput(pss_test[i].public_exponent_e, |
1089 &public_exponent_e)); | 1095 &public_exponent_e)); |
1090 std::vector<uint8> public_key_info; | 1096 std::vector<uint8> public_key_info; |
1091 ASSERT_TRUE(EncodeRSAPublicKey(modulus_n, public_exponent_e, | 1097 ASSERT_TRUE(EncodeRSAPublicKey(modulus_n, public_exponent_e, |
1092 &public_key_info)); | 1098 &public_key_info)); |
1093 | 1099 |
1094 for (unsigned int j = 0; j < arraysize(pss_test[i].example); j++) { | 1100 for (unsigned int j = 0; j < arraysize(pss_test[i].example); j++) { |
| 1101 SCOPED_TRACE(j); |
1095 std::vector<uint8> message; | 1102 std::vector<uint8> message; |
1096 std::vector<uint8> salt; | 1103 std::vector<uint8> salt; |
1097 std::vector<uint8> signature; | 1104 std::vector<uint8> signature; |
1098 ASSERT_TRUE(DecodeTestInput(pss_test[i].example[j].message, &message)); | 1105 ASSERT_TRUE(DecodeTestInput(pss_test[i].example[j].message, &message)); |
1099 ASSERT_TRUE(DecodeTestInput(pss_test[i].example[j].salt, &salt)); | 1106 ASSERT_TRUE(DecodeTestInput(pss_test[i].example[j].salt, &salt)); |
1100 ASSERT_TRUE(DecodeTestInput(pss_test[i].example[j].signature, | 1107 ASSERT_TRUE(DecodeTestInput(pss_test[i].example[j].signature, |
1101 &signature)); | 1108 &signature)); |
1102 | 1109 |
1103 crypto::SignatureVerifier verifier; | 1110 crypto::SignatureVerifier verifier; |
1104 bool ok; | 1111 bool ok; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 &public_key_info[0], | 1158 &public_key_info[0], |
1152 public_key_info.size()); | 1159 public_key_info.size()); |
1153 signature[0] -= 1; | 1160 signature[0] -= 1; |
1154 ASSERT_TRUE(ok); | 1161 ASSERT_TRUE(ok); |
1155 verifier.VerifyUpdate(&message[0], message.size()); | 1162 verifier.VerifyUpdate(&message[0], message.size()); |
1156 ok = verifier.VerifyFinal(); | 1163 ok = verifier.VerifyFinal(); |
1157 EXPECT_FALSE(ok); | 1164 EXPECT_FALSE(ok); |
1158 } | 1165 } |
1159 } | 1166 } |
1160 } | 1167 } |
OLD | NEW |