Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: net/cert/jwk_serializer_unittest.cc

Issue 22731002: Improve style of JWK serializer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cert/jwk_serializer_openssl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 kEcPointSize = 32U; 29 static const unsigned int kEcCoordinateSize = 32U;
30 30
31 // This is a valid P-256 public key. 31 // This is a valid P-256 public key.
32 static const unsigned char kSpkiEc[] = { 32 static const unsigned char kSpkiEc[] = {
33 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 33 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
34 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 34 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
35 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 35 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
36 0x42, 0x00, 0x04, 36 0x42, 0x00, 0x04,
37 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea, 37 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea,
38 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0, 38 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0,
39 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98, 39 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 75
76 std::string string_value; 76 std::string string_value;
77 EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value)); 77 EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value));
78 EXPECT_STREQ("EC", string_value.c_str()); 78 EXPECT_STREQ("EC", string_value.c_str());
79 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); 79 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value));
80 EXPECT_STREQ("P-256", string_value.c_str()); 80 EXPECT_STREQ("P-256", string_value.c_str());
81 81
82 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); 82 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value));
83 std::string decoded_coordinate; 83 std::string decoded_coordinate;
84 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); 84 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
85 EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); 85 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
86 EXPECT_EQ(0, 86 EXPECT_EQ(0,
87 memcmp(decoded_coordinate.data(), 87 memcmp(decoded_coordinate.data(),
88 kSpkiEc + sizeof(kP256SpkiPrefix), 88 kSpkiEc + sizeof(kP256SpkiPrefix),
89 kEcPointSize)); 89 kEcCoordinateSize));
90 90
91 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); 91 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value));
92 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); 92 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
93 EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); 93 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
94 EXPECT_EQ(0, 94 EXPECT_EQ(0,
95 memcmp(decoded_coordinate.data(), 95 memcmp(decoded_coordinate.data(),
96 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcPointSize, 96 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcCoordinateSize,
97 kEcPointSize)); 97 kEcCoordinateSize));
98 98
99 // Test the result of a corner case: leading 0s in the x, y coordinates are 99 // Test the result of a corner case: leading 0s in the x, y coordinates are
100 // not trimmed, but the point is fixed-length encoded. 100 // not trimmed, but the point is fixed-length encoded.
101 spki.set(reinterpret_cast<const char*>(kSpkiEcWithZeroXY), 101 spki.set(reinterpret_cast<const char*>(kSpkiEcWithZeroXY),
102 sizeof(kSpkiEcWithZeroXY)); 102 sizeof(kSpkiEcWithZeroXY));
103 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); 103 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk));
104 104
105 EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value)); 105 EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value));
106 EXPECT_STREQ("EC", string_value.c_str()); 106 EXPECT_STREQ("EC", string_value.c_str());
107 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); 107 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value));
108 EXPECT_STREQ("P-256", string_value.c_str()); 108 EXPECT_STREQ("P-256", string_value.c_str());
109 109
110 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); 110 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value));
111 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); 111 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
112 EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); 112 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
113 EXPECT_EQ(0, 113 EXPECT_EQ(0,
114 memcmp(decoded_coordinate.data(), 114 memcmp(decoded_coordinate.data(),
115 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix), 115 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix),
116 kEcPointSize)); 116 kEcCoordinateSize));
117 117
118 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); 118 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value));
119 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); 119 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
120 EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); 120 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
121 EXPECT_EQ(0, 121 EXPECT_EQ(0,
122 memcmp(decoded_coordinate.data(), 122 memcmp(decoded_coordinate.data(),
123 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix) + kEcPointSize, 123 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix) + kEcCoordinateSize,
124 kEcPointSize)); 124 kEcCoordinateSize));
125 } 125 }
126 126
127 #else 127 #else
128 128
129 // For OpenSSL, JwkSerializer::ConvertSpkiFromDerToJwk() is not yet implemented 129 // For OpenSSL, JwkSerializer::ConvertSpkiFromDerToJwk() is not yet implemented
130 // and should return false. This unit test ensures that a stub implementation 130 // and should return false. This unit test ensures that a stub implementation
131 // is present. 131 // is present.
132 TEST(JwkSerializerOpenSSLTest, ConvertSpkiFromDerToJwkNotImplemented) { 132 TEST(JwkSerializerOpenSSLTest, ConvertSpkiFromDerToJwkNotImplemented) {
133 base::StringPiece spki; 133 base::StringPiece spki;
134 base::DictionaryValue public_key_jwk; 134 base::DictionaryValue public_key_jwk;
135 135
136 // The empty SPKI is trivially non-convertible... 136 // The empty SPKI is trivially non-convertible...
137 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); 137 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk));
138 EXPECT_TRUE(public_key_jwk.empty()); 138 EXPECT_TRUE(public_key_jwk.empty());
139 // but even a valid SPKI is non-convertible via the stub OpenSSL 139 // but even a valid SPKI is non-convertible via the stub OpenSSL
140 // implementation. 140 // implementation.
141 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc)); 141 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc));
142 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk)); 142 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk));
143 EXPECT_TRUE(public_key_jwk.empty()); 143 EXPECT_TRUE(public_key_jwk.empty());
144 } 144 }
145 145
146 #endif // !defined(USE_OPENSSL) 146 #endif // !defined(USE_OPENSSL)
147 147
148 } // namespace net 148 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/jwk_serializer_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698