Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace JwkSerializer { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 bool ConvertEcPrime256v1PublicKeyInfoToJwk( | |
| 21 CERTSubjectPublicKeyInfo* spki, | |
| 22 base::DictionaryValue* public_key_jwk) { | |
| 23 static const int kPrime256v1EncodingType = 4; | |
| 24 static const int kPrime256v1PublicKeyLength = 64; | |
| 25 // The public key value is encoded as 0x04 + 64 bytes of public key. | |
| 26 // NSS gives the length as the bit length. | |
| 27 // | |
|
Ryan Sleevi
2013/08/06 19:50:38
nit: extraneous //
juanlang
2013/08/06 21:32:59
Done.
| |
| 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) { | |
| 76 public_key_jwk->Clear(); | |
| 77 | |
| 78 SECItem sec_item; | |
| 79 sec_item.data = const_cast<unsigned char*>( | |
| 80 reinterpret_cast<const unsigned char*>(spki_der.data())); | |
| 81 sec_item.len = spki_der.size(); | |
| 82 CERTSubjectPublicKeyInfo* spki = | |
| 83 SECKEY_DecodeDERSubjectPublicKeyInfo(&sec_item); | |
| 84 if (!spki) | |
| 85 return false; | |
| 86 | |
| 87 // 1.2.840.10045.2 | |
| 88 // (iso.member-body.us.ansi-x9-62.id-ecPublicKey) | |
| 89 // (This omits the ASN.1 encoding of the type (OID) and length: the fact that | |
| 90 // this is an OID is already clear, and NSS omits it here.) | |
| 91 static const uint8 kIdEcPublicKey[] = { | |
| 92 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01 | |
| 93 }; | |
| 94 bool rv = false; | |
| 95 if (spki->algorithm.algorithm.len == sizeof(kIdEcPublicKey) && | |
| 96 !memcmp(spki->algorithm.algorithm.data, kIdEcPublicKey, | |
| 97 sizeof(kIdEcPublicKey))) { | |
| 98 rv = ConvertEcPublicKeyInfoToJwk(spki, public_key_jwk); | |
| 99 } | |
| 100 // TODO(juanlang): other algorithms | |
| 101 SECKEY_DestroySubjectPublicKeyInfo(spki); | |
|
Ryan Sleevi
2013/08/06 19:50:38
If you want added RAII safety, https://code.google
juanlang
2013/08/06 21:32:59
Nifty. Done.
| |
| 102 return rv; | |
| 103 } | |
| 104 | |
| 105 } // namespace JwkSerializer | |
| 106 | |
| 107 } // namespace net | |
| OLD | NEW |