Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <openssl/bn.h> | |
| 8 #include <openssl/ec.h> | |
| 9 #include <openssl/ec_key.h> | |
| 10 #include <openssl/evp.h> | |
| 11 #include <openssl/x509.h> | |
| 12 | |
| 13 #include "base/base64.h" | |
| 7 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_util.h" | |
| 16 #include "base/values.h" | |
| 17 #include "crypto/openssl_util.h" | |
| 18 #include "crypto/scoped_openssl_types.h" | |
| 8 | 19 |
| 9 namespace net { | 20 namespace net { |
| 10 | 21 |
| 11 namespace JwkSerializer { | 22 namespace JwkSerializer { |
| 12 | 23 |
| 24 namespace { | |
| 25 | |
| 26 bool ConvertEcKeyToJwk(EVP_PKEY* pkey, | |
| 27 base::DictionaryValue* public_key_jwk, | |
| 28 const crypto::OpenSSLErrStackTracer& err_tracer) { | |
| 29 crypto::ScopedEC_KEY ec_key(EVP_PKEY_get1_EC_KEY(pkey)); | |
| 30 if (!ec_key) | |
| 31 return false; | |
| 32 const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key.get()); | |
| 33 if (!ec_group) | |
| 34 return false; | |
| 35 | |
| 36 std::string curve_name; | |
| 37 int nid = EC_GROUP_get_curve_name(ec_group); | |
| 38 if (nid == NID_X9_62_prime256v1) { | |
| 39 curve_name = "P-256"; | |
| 40 } else { | |
| 41 // TODO(juanlang): other curves | |
|
agl
2014/07/30 00:05:53
It seems dumb to leave this.
NID_secp224r1 -> "P-
davidben
2014/07/30 00:13:33
draft-ietf-jose-json-web-algorithms-28 doesn't spe
| |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 int degree_bytes = (EC_GROUP_get_degree(ec_group) + 7) / 8; | |
| 46 | |
| 47 const EC_POINT* ec_point = EC_KEY_get0_public_key(ec_key.get()); | |
| 48 if (!ec_point) | |
| 49 return false; | |
| 50 | |
| 51 crypto::ScopedBIGNUM x(BN_new()); | |
| 52 crypto::ScopedBIGNUM y(BN_new()); | |
| 53 if (!x || !y || | |
|
agl
2014/07/30 00:05:54
allocation can't fail in Chromium.
davidben
2014/07/30 00:13:33
Done.
| |
| 54 !EC_POINT_get_affine_coordinates_GFp(ec_group, ec_point, | |
| 55 x.get(), y.get(), NULL)) { | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 // The coordinates are encoded with leading zeros included. | |
| 60 std::string x_bytes; | |
| 61 std::string y_bytes; | |
| 62 if (!BN_bn2bin_padded(reinterpret_cast<uint8_t*>( | |
| 63 WriteInto(&x_bytes, degree_bytes + 1)), degree_bytes, x.get()) || | |
| 64 !BN_bn2bin_padded(reinterpret_cast<uint8_t*>( | |
| 65 WriteInto(&y_bytes, degree_bytes + 1)), degree_bytes, y.get())) { | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 public_key_jwk->SetString("kty", "EC"); | |
| 70 public_key_jwk->SetString("crv", curve_name); | |
| 71 | |
| 72 std::string x_b64; | |
| 73 base::Base64Encode(x_bytes, &x_b64); | |
| 74 public_key_jwk->SetString("x", x_b64); | |
| 75 | |
| 76 std::string y_b64; | |
| 77 base::Base64Encode(y_bytes, &y_b64); | |
| 78 public_key_jwk->SetString("y", y_b64); | |
| 79 | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 13 bool ConvertSpkiFromDerToJwk( | 85 bool ConvertSpkiFromDerToJwk( |
| 14 const base::StringPiece& spki_der, | 86 const base::StringPiece& spki_der, |
| 15 base::DictionaryValue* public_key_jwk) { | 87 base::DictionaryValue* public_key_jwk) { |
| 16 // TODO(juanlang): implement | 88 public_key_jwk->Clear(); |
| 17 NOTIMPLEMENTED(); | 89 |
| 18 return false; | 90 crypto::EnsureOpenSSLInit(); |
| 91 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 92 | |
| 93 const uint8_t *data = reinterpret_cast<const uint8_t*>(spki_der.data()); | |
| 94 const uint8_t *ptr = data; | |
| 95 crypto::ScopedEVP_PKEY pubkey(d2i_PUBKEY(NULL, &ptr, spki_der.size())); | |
| 96 if (!pubkey || ptr != data + spki_der.size()) | |
| 97 return false; | |
| 98 | |
| 99 if (pubkey->type == EVP_PKEY_EC) { | |
| 100 return ConvertEcKeyToJwk(pubkey.get(), public_key_jwk, err_tracer); | |
| 101 } else { | |
| 102 // TODO(juanlang): other algorithms | |
| 103 return false; | |
| 104 } | |
| 19 } | 105 } |
| 20 | 106 |
| 21 } // namespace JwkSerializer | 107 } // namespace JwkSerializer |
| 22 | 108 |
| 23 } // namespace net | 109 } // namespace net |
| OLD | NEW |