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

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

Issue 21561003: Add a utility method to convert SPKI from DER to JWK, so far implemented only for EC P256v1 (which … (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@domain-bound-public-key
Patch Set: Move to jwk_serializer 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
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
Ryan Sleevi 2013/08/06 19:50:38 Rather than define an NSS-only unittest, just call
juanlang 2013/08/06 21:32:59 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/cert/jwk_serializer.h"
6
7 #include "base/base64.h"
8 #include "base/values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12
13 static const unsigned char kP256SpkiPrefix[] = {
Ryan Sleevi 2013/08/06 19:50:38 nit: Comment explaining what this prefix is (I can
juanlang 2013/08/06 21:32:59 Done.
14 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
15 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
16 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
17 0x42, 0x00, 0x04
18 };
19 static const unsigned int kEcPointSize = 32U;
20
21 static const unsigned char kSpkiEc[] = {
Ryan Sleevi 2013/08/06 19:50:38 nit: comments
juanlang 2013/08/06 21:32:59 Done.
22 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
23 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
24 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
25 0x42, 0x00, 0x04,
26 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea,
27 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0,
28 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98,
29 0xbd, 0x46, 0x0d, 0xf6, 0xd4, 0x5a, 0xdc, 0x8a,
30 0x1f, 0x8a, 0xb2, 0x20, 0x51, 0xb7, 0xd2, 0x87,
31 0x0d, 0x53, 0x7e, 0x5d, 0x94, 0xa3, 0xe0, 0x34,
32 0x16, 0xa1, 0xcc, 0x10, 0x48, 0xcd, 0x70, 0x9c,
33 0x05, 0xd3, 0xd2, 0xca, 0xdf, 0x44, 0x2f, 0xf4
34 };
35
36 static const unsigned char kSpkiEcWithZeroXY[] = {
Ryan Sleevi 2013/08/06 19:50:38 nit: comments
juanlang 2013/08/06 21:32:59 Done.
37 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
38 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
39 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
40 0x42, 0x00, 0x04,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
49 };
50
51 TEST(JwkSerializerNSSTest, ConvertSPKIFromDERToJwkEC) {
52 base::StringPiece spki;
53 base::DictionaryValue public_key_jwk;
54
55 EXPECT_FALSE(JwkSerializer::ConvertSPKIFromDERToJWK(spki, &public_key_jwk));
56 EXPECT_TRUE(public_key_jwk.empty());
57
58 // Test the result of a "normal" point on this curve.
59 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc));
60 EXPECT_TRUE(JwkSerializer::ConvertSPKIFromDERToJWK(spki, &public_key_jwk));
61
62 std::string string_value;
63 EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value));
64 EXPECT_STREQ("EC", string_value.c_str());
65 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value));
66 EXPECT_STREQ("P-256", string_value.c_str());
67
68 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value));
69 std::string decoded_coordinate;
70 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
71 EXPECT_EQ(kEcPointSize, decoded_coordinate.size());
72 EXPECT_EQ(0,
73 memcmp(decoded_coordinate.data(),
74 kSpkiEc + sizeof(kP256SpkiPrefix),
75 kEcPointSize));
76
77 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value));
78 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
79 EXPECT_EQ(kEcPointSize, decoded_coordinate.size());
80 EXPECT_EQ(0,
81 memcmp(decoded_coordinate.data(),
82 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcPointSize,
83 kEcPointSize));
84
85 // Test the result of a corner case: leading 0s in the x, y coordinates are
86 // not trimmed, but the point is fixed-length encoded.
87 spki.set(reinterpret_cast<const char*>(kSpkiEcWithZeroXY),
88 sizeof(kSpkiEcWithZeroXY));
89 EXPECT_TRUE(JwkSerializer::ConvertSPKIFromDERToJWK(spki, &public_key_jwk));
90
91 EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value));
92 EXPECT_STREQ("EC", string_value.c_str());
93 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value));
94 EXPECT_STREQ("P-256", string_value.c_str());
95
96 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value));
97 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
98 EXPECT_EQ(kEcPointSize, decoded_coordinate.size());
99 EXPECT_EQ(0,
100 memcmp(decoded_coordinate.data(),
101 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix),
102 kEcPointSize));
103
104 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value));
105 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
106 EXPECT_EQ(kEcPointSize, decoded_coordinate.size());
107 EXPECT_EQ(0,
108 memcmp(decoded_coordinate.data(),
109 kSpkiEcWithZeroXY + sizeof(kP256SpkiPrefix) + kEcPointSize,
110 kEcPointSize));
111 }
112
113 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698