We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / cert / jwk_serializer_unittest.cc
blob6fa318f3637e91cc90d5f2da2728f67609a13018
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
3 // found in the LICENSE file.
5 #include "net/cert/jwk_serializer.h"
7 #include "base/base64.h"
8 #include "base/values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace net {
13 // This is the ASN.1 prefix for a P-256 public key. Specifically it's:
14 // SEQUENCE
15 // SEQUENCE
16 // OID id-ecPublicKey
17 // OID prime256v1
18 // BIT STRING, length 66, 0 trailing bits: 0x04
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,
22 // big-endian numbers, as required by the Channel ID.
23 static const unsigned char kP256SpkiPrefix[] = {
24 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
25 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
26 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
27 0x42, 0x00, 0x04
29 static const unsigned int kEcCoordinateSize = 32U;
31 // This is a valid P-256 public key.
32 static const unsigned char kSpkiEc[] = {
33 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
34 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
35 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
36 0x42, 0x00, 0x04,
37 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea,
38 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0,
39 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98,
40 0xbd, 0x46, 0x0d, 0xf6, 0xd4, 0x5a, 0xdc, 0x8a,
41 0x1f, 0x8a, 0xb2, 0x20, 0x51, 0xb7, 0xd2, 0x87,
42 0x0d, 0x53, 0x7e, 0x5d, 0x94, 0xa3, 0xe0, 0x34,
43 0x16, 0xa1, 0xcc, 0x10, 0x48, 0xcd, 0x70, 0x9c,
44 0x05, 0xd3, 0xd2, 0xca, 0xdf, 0x44, 0x2f, 0xf4
47 // This is a P-256 public key with a leading 0.
48 static const unsigned char kSpkiEcWithLeadingZero[] = {
49 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
50 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
51 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
52 0x42, 0x00, 0x04,
53 0x00, 0x8e, 0xd5, 0x49, 0x51, 0xad, 0x7d, 0xd3,
54 0x3a, 0x59, 0x86, 0xd1, 0x2c, 0xba, 0x05, 0xd3,
55 0xa6, 0x3c, 0x5d, 0x6d, 0x28, 0xde, 0x8f, 0xdd,
56 0xa5, 0x3d, 0x30, 0x18, 0x05, 0x86, 0x76, 0x9c,
57 0x7c, 0xa7, 0xba, 0x58, 0xea, 0x1a, 0x84, 0x19,
58 0x29, 0x0a, 0x15, 0x30, 0x7d, 0x6b, 0x00, 0x41,
59 0x64, 0x56, 0x84, 0x19, 0x54, 0x3e, 0x26, 0x13,
60 0xc9, 0x1e, 0x31, 0x89, 0xe2, 0x62, 0xcb, 0x3f
63 TEST(JwkSerializerTest, ConvertSpkiFromDerToJwkEc) {
64 base::StringPiece spki;
65 base::DictionaryValue public_key_jwk;
67 EXPECT_FALSE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk));
68 EXPECT_TRUE(public_key_jwk.empty());
70 // Test the result of a "normal" point on this curve.
71 spki.set(reinterpret_cast<const char*>(kSpkiEc), sizeof(kSpkiEc));
72 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk));
74 std::string string_value;
75 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value));
76 EXPECT_STREQ("EC", string_value.c_str());
77 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value));
78 EXPECT_STREQ("P-256", string_value.c_str());
80 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value));
81 std::string decoded_coordinate;
82 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
83 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
84 EXPECT_EQ(0,
85 memcmp(decoded_coordinate.data(),
86 kSpkiEc + sizeof(kP256SpkiPrefix),
87 kEcCoordinateSize));
89 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value));
90 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
91 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
92 EXPECT_EQ(0,
93 memcmp(decoded_coordinate.data(),
94 kSpkiEc + sizeof(kP256SpkiPrefix) + kEcCoordinateSize,
95 kEcCoordinateSize));
97 // 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.
99 spki.set(reinterpret_cast<const char*>(kSpkiEcWithLeadingZero),
100 sizeof(kSpkiEcWithLeadingZero));
101 EXPECT_TRUE(JwkSerializer::ConvertSpkiFromDerToJwk(spki, &public_key_jwk));
103 EXPECT_TRUE(public_key_jwk.GetString("kty", &string_value));
104 EXPECT_STREQ("EC", string_value.c_str());
105 EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value));
106 EXPECT_STREQ("P-256", string_value.c_str());
108 EXPECT_TRUE(public_key_jwk.GetString("x", &string_value));
109 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
110 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
111 EXPECT_EQ(0,
112 memcmp(decoded_coordinate.data(),
113 kSpkiEcWithLeadingZero + sizeof(kP256SpkiPrefix),
114 kEcCoordinateSize));
116 EXPECT_TRUE(public_key_jwk.GetString("y", &string_value));
117 EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate));
118 EXPECT_EQ(kEcCoordinateSize, decoded_coordinate.size());
119 EXPECT_EQ(0, memcmp(
120 decoded_coordinate.data(),
121 kSpkiEcWithLeadingZero + sizeof(kP256SpkiPrefix) + kEcCoordinateSize,
122 kEcCoordinateSize));
125 } // namespace net