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

Side by Side Diff: net/cert/jwk_serializer_nss.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: More nits addressed 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.
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 <cert.h>
8 #include <keyhi.h>
9 #include <nss.h>
10
11 #include "base/base64.h"
12 #include "base/values.h"
13 #include "crypto/scoped_nss_types.h"
14
15 namespace net {
16
17 namespace JwkSerializer {
18
19 namespace {
20
21 bool ConvertEcPrime256v1PublicKeyInfoToJwk(
22 CERTSubjectPublicKeyInfo* spki,
23 base::DictionaryValue* public_key_jwk) {
24 static const int kPrime256v1EncodingType = 4;
25 static const int kPrime256v1PublicKeyLength = 64;
26 // The public key value is encoded as 0x04 + 64 bytes of public key.
27 // NSS gives the length as the bit length.
28 if (spki->subjectPublicKey.len != (kPrime256v1PublicKeyLength + 1) * 8 ||
29 spki->subjectPublicKey.data[0] != kPrime256v1EncodingType)
30 return false;
31
32 public_key_jwk->SetString("alg", "EC");
33 public_key_jwk->SetString("crv", "P-256");
34
35 base::StringPiece x(
36 reinterpret_cast<char*>(spki->subjectPublicKey.data + 1),
37 kPrime256v1PublicKeyLength / 2);
38 std::string x_b64;
39 base::Base64Encode(x, &x_b64);
40 public_key_jwk->SetString("x", x_b64);
41
42 base::StringPiece y(
43 reinterpret_cast<char*>(spki->subjectPublicKey.data + 1 +
44 kPrime256v1PublicKeyLength / 2),
45 kPrime256v1PublicKeyLength / 2);
46 std::string y_b64;
47 base::Base64Encode(y, &y_b64);
48 public_key_jwk->SetString("y", y_b64);
49 return true;
50 }
51
52 bool ConvertEcPublicKeyInfoToJwk(
53 CERTSubjectPublicKeyInfo* spki,
54 base::DictionaryValue* public_key_jwk) {
55 // 1.2.840.10045.3.1.7
56 // (iso.member-body.us.ansi-x9-62.ellipticCurve.primeCurve.prime256v1)
57 // (This includes the DER-encoded type (OID) and length: parameters can be
58 // anything, so the DER type isn't implied, and NSS includes it.)
59 static const uint8_t kPrime256v1[] = {
60 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
61 };
62 if (spki->algorithm.parameters.len == sizeof(kPrime256v1) &&
63 !memcmp(spki->algorithm.parameters.data, kPrime256v1,
64 sizeof(kPrime256v1))) {
65 return ConvertEcPrime256v1PublicKeyInfoToJwk(spki, public_key_jwk);
66 }
67 // TODO(juanlang): other curves
68 return false;
69 }
70
71 } // namespace
72
73 bool ConvertSPKIFromDERToJWK(
74 const base::StringPiece& spki_der,
75 base::DictionaryValue* public_key_jwk) {
Ryan Sleevi 2013/08/06 21:59:42 BUG? Missing crypto::EnsureNSSInit() ?
juanlang 2013/08/06 23:32:17 Yikes, thanks. Done.
76 public_key_jwk->Clear();
77
78 typedef scoped_ptr_malloc<
79 CERTSubjectPublicKeyInfo,
80 crypto::NSSDestroyer<CERTSubjectPublicKeyInfo,
81 SECKEY_DestroySubjectPublicKeyInfo> >
82 ScopedCERTSubjectPublicKeyInfo;
Ryan Sleevi 2013/08/06 21:59:42 nit: I'd suggest moving this into the unnamed name
juanlang 2013/08/06 23:32:17 Done.
83
84 SECItem sec_item;
85 sec_item.data = const_cast<unsigned char*>(
86 reinterpret_cast<const unsigned char*>(spki_der.data()));
87 sec_item.len = spki_der.size();
88 ScopedCERTSubjectPublicKeyInfo spki(
89 SECKEY_DecodeDERSubjectPublicKeyInfo(&sec_item));
90 if (!spki)
91 return false;
92
93 // 1.2.840.10045.2
94 // (iso.member-body.us.ansi-x9-62.id-ecPublicKey)
95 // (This omits the ASN.1 encoding of the type (OID) and length: the fact that
96 // this is an OID is already clear, and NSS omits it here.)
97 static const uint8 kIdEcPublicKey[] = {
98 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01
99 };
100 bool rv = false;
101 if (spki->algorithm.algorithm.len == sizeof(kIdEcPublicKey) &&
102 !memcmp(spki->algorithm.algorithm.data, kIdEcPublicKey,
103 sizeof(kIdEcPublicKey))) {
104 rv = ConvertEcPublicKeyInfoToJwk(spki.get(), public_key_jwk);
105 }
106 // TODO(juanlang): other algorithms
107 return rv;
108 }
109
110 } // namespace JwkSerializer
111
112 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698