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 #if !defined(USE_OPENSSL) | |
14 // 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: |
15 // SEQUENCE | 14 // SEQUENCE |
16 // SEQUENCE | 15 // SEQUENCE |
17 // OID id-ecPublicKey | 16 // OID id-ecPublicKey |
18 // OID prime256v1 | 17 // OID prime256v1 |
19 // BIT STRING, length 66, 0 trailing bits: 0x04 | 18 // BIT STRING, length 66, 0 trailing bits: 0x04 |
20 // | 19 // |
21 // 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 |
22 // 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, |
23 // big-endian numbers, as required by the Channel ID. | 22 // big-endian numbers, as required by the Channel ID. |
24 static const unsigned char kP256SpkiPrefix[] = { | 23 static const unsigned char kP256SpkiPrefix[] = { |
25 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 24 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
26 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, | 25 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
27 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | 26 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
28 0x42, 0x00, 0x04 | 27 0x42, 0x00, 0x04 |
29 }; | 28 }; |
30 static const unsigned int kEcCoordinateSize = 32U; | 29 static const unsigned int kEcCoordinateSize = 32U; |
31 #endif | |
32 | 30 |
33 // This is a valid P-256 public key. | 31 // This is a valid P-256 public key. |
34 static const unsigned char kSpkiEc[] = { | 32 static const unsigned char kSpkiEc[] = { |
35 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 33 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
36 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, | 34 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
37 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | 35 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
38 0x42, 0x00, 0x04, | 36 0x42, 0x00, 0x04, |
39 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea, | 37 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea, |
40 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0, | 38 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0, |
41 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98, | 39 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98, |
42 0xbd, 0x46, 0x0d, 0xf6, 0xd4, 0x5a, 0xdc, 0x8a, | 40 0xbd, 0x46, 0x0d, 0xf6, 0xd4, 0x5a, 0xdc, 0x8a, |
43 0x1f, 0x8a, 0xb2, 0x20, 0x51, 0xb7, 0xd2, 0x87, | 41 0x1f, 0x8a, 0xb2, 0x20, 0x51, 0xb7, 0xd2, 0x87, |
44 0x0d, 0x53, 0x7e, 0x5d, 0x94, 0xa3, 0xe0, 0x34, | 42 0x0d, 0x53, 0x7e, 0x5d, 0x94, 0xa3, 0xe0, 0x34, |
45 0x16, 0xa1, 0xcc, 0x10, 0x48, 0xcd, 0x70, 0x9c, | 43 0x16, 0xa1, 0xcc, 0x10, 0x48, 0xcd, 0x70, 0x9c, |
46 0x05, 0xd3, 0xd2, 0xca, 0xdf, 0x44, 0x2f, 0xf4 | 44 0x05, 0xd3, 0xd2, 0xca, 0xdf, 0x44, 0x2f, 0xf4 |
47 }; | 45 }; |
48 | 46 |
49 #if !defined(USE_OPENSSL) | 47 // This is a P-256 public key with a leading 0. |
50 // This is a P-256 public key with 0 X and Y values. | 48 static const unsigned char kSpkiEcWithLeadingZero[] = { |
51 static const unsigned char kSpkiEcWithZeroXY[] = { | |
52 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 49 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
53 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, | 50 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
54 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | 51 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
55 0x42, 0x00, 0x04, | 52 0x42, 0x00, 0x04, |
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 53 0x00, 0x8e, 0xd5, 0x49, 0x51, 0xad, 0x7d, 0xd3, |
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 54 0x3a, 0x59, 0x86, 0xd1, 0x2c, 0xba, 0x05, 0xd3, |
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 55 0xa6, 0x3c, 0x5d, 0x6d, 0x28, 0xde, 0x8f, 0xdd, |
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 56 0xa5, 0x3d, 0x30, 0x18, 0x05, 0x86, 0x76, 0x9c, |
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 57 0x7c, 0xa7, 0xba, 0x58, 0xea, 0x1a, 0x84, 0x19, |
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 58 0x29, 0x0a, 0x15, 0x30, 0x7d, 0x6b, 0x00, 0x41, |
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 59 0x64, 0x56, 0x84, 0x19, 0x54, 0x3e, 0x26, 0x13, |
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | 60 0xc9, 0x1e, 0x31, 0x89, 0xe2, 0x62, 0xcb, 0x3f |
64 }; | 61 }; |
65 | 62 |
66 TEST(JwkSerializerNSSTest, ConvertSpkiFromDerToJwkEc) { | 63 TEST(JwkSerializerTest, ConvertSpkiFromDerToJwkEc) { |
67 base::StringPiece spki; | 64 base::StringPiece spki; |
68 base::DictionaryValue public_key_jwk; | 65 base::DictionaryValue public_key_jwk; |
69 | 66 |
70 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); | 67 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); |
71 EXPECT_TRUE(public_key_jwk.empty()); | 68 EXPECT_TRUE(public_key_jwk.empty()); |
72 | 69 |
73 // Test the result of a "normal" point on this curve. | 70 // Test the result of a "normal" point on this curve. |
74 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc)); | 71 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc)); |
75 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); | 72 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); |
76 | 73 |
(...skipping 15 matching lines...) Expand all Loading... |
92 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); | 89 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); |
93 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 90 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
94 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 91 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
95 EXPECT_EQ(0, | 92 EXPECT_EQ(0, |
96 memcmp(decoded_coordinate.data(), | 93 memcmp(decoded_coordinate.data(), |
97 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcCoordinateSize, | 94 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcCoordinateSize, |
98 kEcCoordinateSize)); | 95 kEcCoordinateSize)); |
99 | 96 |
100 // Test the result of a corner case: leading 0s in the x, y coordinates are | 97 // Test the result of a corner case: leading 0s in the x, y coordinates are |
101 // not trimmed, but the point is fixed-length encoded. | 98 // not trimmed, but the point is fixed-length encoded. |
102 spki.set(reinterpret_cast<const char*>(kSpkiEcWithZeroXY), | 99 spki.set(reinterpret_cast<const char*>(kSpkiEcWithLeadingZero), |
103 sizeof(kSpkiEcWithZeroXY)); | 100 sizeof(kSpkiEcWithLeadingZero)); |
104 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); | 101 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); |
105 | 102 |
106 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value)); | 103 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value)); |
107 EXPECT_STREQ("EC", string_value.c_str()); | 104 EXPECT_STREQ("EC", string_value.c_str()); |
108 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); | 105 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); |
109 EXPECT_STREQ("P-256", string_value.c_str()); | 106 EXPECT_STREQ("P-256", string_value.c_str()); |
110 | 107 |
111 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); | 108 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); |
112 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 109 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
113 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 110 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
114 EXPECT_EQ(0, | 111 EXPECT_EQ(0, |
115 memcmp(decoded_coordinate.data(), | 112 memcmp(decoded_coordinate.data(), |
116 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix), | 113 kSpkiEcWithLeadingZero + sizeof(kP256SpkiPrefix), |
117 kEcCoordinateSize)); | 114 kEcCoordinateSize)); |
118 | 115 |
119 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); | 116 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); |
120 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); | 117 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
121 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); | 118 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size()); |
122 EXPECT_EQ(0, | 119 EXPECT_EQ(0, memcmp( |
123 memcmp(decoded_coordinate.data(), | 120 decoded_coordinate.data(), |
124 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix) + kEcCoordinateSize, | 121 kSpkiEcWithLeadingZero + sizeof(kP256SpkiPrefix) + kEcCoordinateSize, |
125 kEcCoordinateSize)); | 122 kEcCoordinateSize)); |
126 } | 123 } |
127 | 124 |
128 #else | |
129 | |
130 // For OpenSSL, JwkSerializer::ConvertSpkiFromDerToJwk() is not yet implemented | |
131 // and should return false. This unit test ensures that a stub implementation | |
132 // is present. | |
133 TEST(JwkSerializerOpenSSLTest, ConvertSpkiFromDerToJwkNotImplemented) { | |
134 base::StringPiece spki; | |
135 base::DictionaryValue public_key_jwk; | |
136 | |
137 // The empty SPKI is trivially non-convertible... | |
138 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); | |
139 EXPECT_TRUE(public_key_jwk.empty()); | |
140 // but even a valid SPKI is non-convertible via the stub OpenSSL | |
141 // implementation. | |
142 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc)); | |
143 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); | |
144 EXPECT_TRUE(public_key_jwk.empty()); | |
145 } | |
146 | |
147 #endif // !defined(USE_OPENSSL) | |
148 | |
149 } // namespace net | 125 } // namespace net |
OLD | NEW |