| 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> | 7 #include <openssl/bn.h> |
| 8 #include <openssl/bytestring.h> | 8 #include <openssl/bytestring.h> |
| 9 #include <openssl/ec.h> | 9 #include <openssl/ec.h> |
| 10 #include <openssl/ec_key.h> | 10 #include <openssl/ec_key.h> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 int degree_bytes = (EC_GROUP_get_degree(ec_group) + 7) / 8; | 48 int degree_bytes = (EC_GROUP_get_degree(ec_group) + 7) / 8; |
| 49 | 49 |
| 50 const EC_POINT* ec_point = EC_KEY_get0_public_key(ec_key.get()); | 50 const EC_POINT* ec_point = EC_KEY_get0_public_key(ec_key.get()); |
| 51 if (!ec_point) | 51 if (!ec_point) |
| 52 return false; | 52 return false; |
| 53 | 53 |
| 54 crypto::ScopedBIGNUM x(BN_new()); | 54 crypto::ScopedBIGNUM x(BN_new()); |
| 55 crypto::ScopedBIGNUM y(BN_new()); | 55 crypto::ScopedBIGNUM y(BN_new()); |
| 56 if (!EC_POINT_get_affine_coordinates_GFp(ec_group, ec_point, | 56 if (!EC_POINT_get_affine_coordinates_GFp(ec_group, ec_point, x.get(), y.get(), |
| 57 x.get(), y.get(), NULL)) { | 57 NULL)) { |
| 58 return false; | 58 return false; |
| 59 } | 59 } |
| 60 | 60 |
| 61 // The coordinates are encoded with leading zeros included. | 61 // The coordinates are encoded with leading zeros included. |
| 62 std::string x_bytes; | 62 std::string x_bytes; |
| 63 std::string y_bytes; | 63 std::string y_bytes; |
| 64 if (!BN_bn2bin_padded(reinterpret_cast<uint8_t*>( | 64 if (!BN_bn2bin_padded(reinterpret_cast<uint8_t*>( |
| 65 base::WriteInto(&x_bytes, degree_bytes + 1)), | 65 base::WriteInto(&x_bytes, degree_bytes + 1)), |
| 66 degree_bytes, x.get()) || | 66 degree_bytes, x.get()) || |
| 67 !BN_bn2bin_padded(reinterpret_cast<uint8_t*>( | 67 !BN_bn2bin_padded(reinterpret_cast<uint8_t*>( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 81 std::string y_b64; | 81 std::string y_b64; |
| 82 base::Base64UrlEncode(y_bytes, base::Base64UrlEncodePolicy::OMIT_PADDING, | 82 base::Base64UrlEncode(y_bytes, base::Base64UrlEncodePolicy::OMIT_PADDING, |
| 83 &y_b64); | 83 &y_b64); |
| 84 public_key_jwk->SetString("y", y_b64); | 84 public_key_jwk->SetString("y", y_b64); |
| 85 | 85 |
| 86 return true; | 86 return true; |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 bool ConvertSpkiFromDerToJwk( | 91 bool ConvertSpkiFromDerToJwk(const base::StringPiece& spki_der, |
| 92 const base::StringPiece& spki_der, | 92 base::DictionaryValue* public_key_jwk) { |
| 93 base::DictionaryValue* public_key_jwk) { | |
| 94 public_key_jwk->Clear(); | 93 public_key_jwk->Clear(); |
| 95 | 94 |
| 96 crypto::EnsureOpenSSLInit(); | 95 crypto::EnsureOpenSSLInit(); |
| 97 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 96 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 98 | 97 |
| 99 CBS cbs; | 98 CBS cbs; |
| 100 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(spki_der.data()), | 99 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(spki_der.data()), |
| 101 spki_der.size()); | 100 spki_der.size()); |
| 102 crypto::ScopedEVP_PKEY pubkey(EVP_parse_public_key(&cbs)); | 101 crypto::ScopedEVP_PKEY pubkey(EVP_parse_public_key(&cbs)); |
| 103 if (!pubkey || CBS_len(&cbs) != 0) | 102 if (!pubkey || CBS_len(&cbs) != 0) |
| 104 return false; | 103 return false; |
| 105 | 104 |
| 106 if (pubkey->type == EVP_PKEY_EC) { | 105 if (pubkey->type == EVP_PKEY_EC) { |
| 107 return ConvertEcKeyToJwk(pubkey.get(), public_key_jwk, err_tracer); | 106 return ConvertEcKeyToJwk(pubkey.get(), public_key_jwk, err_tracer); |
| 108 } else { | 107 } else { |
| 109 // TODO(juanlang): other algorithms | 108 // TODO(juanlang): other algorithms |
| 110 return false; | 109 return false; |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 114 } // namespace JwkSerializer | 113 } // namespace JwkSerializer |
| 115 | 114 |
| 116 } // namespace net | 115 } // namespace net |
| OLD | NEW |