| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/cert/jwk_serializer.h" | 5 #include "net/cert/jwk_serializer.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 // This is the ASN.1 prefix for a P-256 public key. Specifically it's: | 13 // This is the ASN.1 prefix for a P-256 public key. Specifically it's: |
| 14 // SEQUENCE | 14 // SEQUENCE |
| 15 // SEQUENCE | 15 // SEQUENCE |
| 16 // OID id-ecPublicKey | 16 // OID id-ecPublicKey |
| 17 // OID prime256v1 | 17 // OID prime256v1 |
| 18 // BIT STRING, length 66, 0 trailing bits: 0x04 | 18 // BIT STRING, length 66, 0 trailing bits: 0x04 |
| 19 // | 19 // |
| 20 // The 0x04 in the BIT STRING is the prefix for an uncompressed, X9.62 | 20 // The 0x04 in the BIT STRING is the prefix for an uncompressed, X9.62 |
| 21 // public key. Following that are the two field elements as 32-byte, | 21 // public key. Following that are the two field elements as 32-byte, |
| 22 // big-endian numbers, as required by the Channel ID. | 22 // big-endian numbers, as required by the Channel ID. |
| 23 static const unsigned char kP256SpkiPrefix[] = { | 23 // static const unsigned char kP256SpkiPrefix[] = { |
| 24 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 24 // 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
| 25 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, | 25 // 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
| 26 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | 26 // 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
| 27 0x42, 0x00, 0x04 | 27 // 0x42, 0x00, 0x04 |
| 28 }; | 28 //}; |
| 29 static const unsigned int kP256SpkiPrefixSize = 27U; |
| 29 static const unsigned int kEcCoordinateSize = 32U; | 30 static const unsigned int kEcCoordinateSize = 32U; |
| 30 | 31 |
| 31 // This is a valid P-256 public key. | 32 // This is a valid P-256 public key. |
| 32 static const unsigned char kSpkiEc[] = { | 33 static const unsigned char kSpkiEc[] = { |
| 33 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 34 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
| 34 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, | 35 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
| 35 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | 36 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
| 36 0x42, 0x00, 0x04, | 37 0x42, 0x00, 0x04, |
| 37 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea, | 38 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea, |
| 38 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0, | 39 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 std::string string_value; | 75 std::string string_value; |
| 75 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value)); | 76 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value)); |
| 76 EXPECT_STREQ("EC", string_value.c_str()); | 77 EXPECT_STREQ("EC", string_value.c_str()); |
| 77 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); | 78 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); |
| 78 EXPECT_STREQ("P-256", string_value.c_str()); | 79 EXPECT_STREQ("P-256", string_value.c_str()); |
| 79 | 80 |
| 80 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); | 81 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); |
| 81 std::string decoded_coordinate; | 82 std::string decoded_coordinate; |
| 82 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 83 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
| 83 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 84 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
| 84 EXPECT_EQ(0, | 85 EXPECT_EQ(0, memcmp(decoded_coordinate.data(), kSpkiEc + kP256SpkiPrefixSize, |
| 85 memcmp(decoded_coordinate.data(), | 86 kEcCoordinateSize)); |
| 86 kSpkiEc + sizeof(kP256SpkiPrefix), | |
| 87 kEcCoordinateSize)); | |
| 88 | 87 |
| 89 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); | 88 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); |
| 90 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 89 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
| 91 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 90 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
| 92 EXPECT_EQ(0, | 91 EXPECT_EQ(0, memcmp(decoded_coordinate.data(), |
| 93 memcmp(decoded_coordinate.data(), | 92 kSpkiEc + kP256SpkiPrefixSize + kEcCoordinateSize, |
| 94 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcCoordinateSize, | 93 kEcCoordinateSize)); |
| 95 kEcCoordinateSize)); | |
| 96 | 94 |
| 97 // Test the result of a corner case: leading 0s in the x, y coordinates are | 95 // Test the result of a corner case: leading 0s in the x, y coordinates are |
| 98 // not trimmed, but the point is fixed-length encoded. | 96 // not trimmed, but the point is fixed-length encoded. |
| 99 spki.set(reinterpret_cast<const char*>(kSpkiEcWithLeadingZero), | 97 spki.set(reinterpret_cast<const char*>(kSpkiEcWithLeadingZero), |
| 100 sizeof(kSpkiEcWithLeadingZero)); | 98 sizeof(kSpkiEcWithLeadingZero)); |
| 101 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); | 99 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); |
| 102 | 100 |
| 103 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value)); | 101 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value)); |
| 104 EXPECT_STREQ("EC", string_value.c_str()); | 102 EXPECT_STREQ("EC", string_value.c_str()); |
| 105 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); | 103 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); |
| 106 EXPECT_STREQ("P-256", string_value.c_str()); | 104 EXPECT_STREQ("P-256", string_value.c_str()); |
| 107 | 105 |
| 108 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); | 106 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); |
| 109 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 107 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
| 110 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 108 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
| 111 EXPECT_EQ(0, | 109 EXPECT_EQ(0, memcmp(decoded_coordinate.data(), |
| 112 memcmp(decoded_coordinate.data(), | 110 kSpkiEcWithLeadingZero + kP256SpkiPrefixSize, |
| 113 kSpkiEcWithLeadingZero + sizeof(kP256SpkiPrefix), | 111 kEcCoordinateSize)); |
| 114 kEcCoordinateSize)); | |
| 115 | 112 |
| 116 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); | 113 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); |
| 117 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 114 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
| 118 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 115 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
| 119 EXPECT_EQ(0, memcmp( | 116 EXPECT_EQ(0, memcmp(decoded_coordinate.data(), |
| 120 decoded_coordinate.data(), | 117 kSpkiEcWithLeadingZero + kP256SpkiPrefixSize + |
| 121 kSpkiEcWithLeadingZero + sizeof(kP256SpkiPrefix) + kEcCoordinateSize, | 118 kEcCoordinateSize, |
| 122 kEcCoordinateSize)); | 119 kEcCoordinateSize)); |
| 123 } | 120 } |
| 124 | 121 |
| 125 } // namespace net | 122 } // namespace net |
| OLD | NEW |