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 <openssl/crypto.h> | |
| 6 #include <openssl/evp.h> | |
| 7 #include <openssl/x509.h> | |
| 8 | |
| 9 #include "net/base/openssl_util.h" | |
| 10 #include "net/base/x509_certificate.h" | |
| 11 | |
| 12 namespace net { | |
| 13 | |
| 14 EVP_PKEY* EVP_PKEY_dup(EVP_PKEY* key) { | |
| 15 if (key != NULL) | |
| 16 CRYPTO_add(&key->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
| 17 return key; | |
| 18 } | |
| 19 | |
| 20 OpenSSLKeyPairList::KeyPair::KeyPair(EVP_PKEY* pub_key, | |
| 21 EVP_PKEY* priv_key) { | |
| 22 public_key_ = EVP_PKEY_dup(pub_key); | |
| 23 private_key_ = EVP_PKEY_dup(priv_key); | |
| 24 } | |
| 25 | |
| 26 OpenSSLKeyPairList::KeyPair::~KeyPair() { | |
| 27 EVP_PKEY_free(public_key_); | |
| 28 EVP_PKEY_free(private_key_); | |
| 29 } | |
| 30 | |
| 31 OpenSSLKeyPairList::KeyPair::KeyPair(const KeyPair& other) { | |
| 32 public_key_ = EVP_PKEY_dup(other.public_key_); | |
| 33 private_key_ = EVP_PKEY_dup(other.private_key_); | |
| 34 } | |
| 35 | |
| 36 // Find the private key corresponding to a given public one. | |
| 37 // |public_key| is an EVP_PKEY holding a public key. | |
| 38 // Returns the matching EVP_PKEY holding the private key, or NULL if | |
| 39 // not found. This increases the returned key's reference count, so | |
| 40 // the caller shall call EVP_PKEY_free() on it. | |
|
Ryan Sleevi
2013/02/12 00:25:17
Why not just take a hash of the public key, to avo
digit1
2013/02/12 15:05:25
I really don't think this is a performance-critica
| |
| 41 EVP_PKEY* OpenSSLKeyPairList::FindPrivateKey(EVP_PKEY* public_key) { | |
| 42 if (!public_key) | |
| 43 return NULL; | |
| 44 for (size_t n = 0; n < pairs_.size(); ++n) { | |
| 45 if (EVP_PKEY_cmp(pairs_[n].public_key_, public_key) == 1) | |
| 46 return EVP_PKEY_dup(pairs_[n].private_key_); | |
| 47 } | |
| 48 return NULL; | |
| 49 } | |
| 50 | |
| 51 // Add a (public/private) key pair | |
| 52 // |public_key| is the public key EVP_PKEY. Cannot be NULL. | |
| 53 // |private_key| is the private key EVP_PKEY, this can be the same | |
| 54 // object than |public_key| is it holds both a public and a private key. | |
| 55 // Cannot be NULL. | |
| 56 // Returns true on success, false otherwise. On success, this increases | |
| 57 // the reference counts of both objects. | |
| 58 bool OpenSSLKeyPairList::AddKeyPair(EVP_PKEY* public_key, | |
| 59 EVP_PKEY* private_key) { | |
| 60 if (public_key == NULL || private_key == NULL) | |
| 61 return false; | |
| 62 for (size_t n = 0; n < pairs_.size(); ++n) { | |
| 63 if (EVP_PKEY_cmp(pairs_[n].public_key_, public_key) == 1) { | |
| 64 // Already listed, return immediately because the private key | |
| 65 // cannot change. | |
| 66 return true; | |
| 67 } | |
| 68 } | |
|
Ryan Sleevi
2013/02/12 00:25:17
Why are you duplicating the logic of FindPrivateKe
digit1
2013/02/12 15:05:25
Hmmm... because it's only 3 lines of code. I'll fi
| |
| 69 // Append new pair. | |
| 70 pairs_.push_back(KeyPair(public_key, private_key)); | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 EVP_PKEY* GetCertificatePublicKeyOpenSSL(const X509Certificate& cert) { | |
| 75 // X509_PUBKEY_get() increments the reference count of its result. | |
| 76 // Unlike X509_get_X509_PUBKEY() which simply returns a direct pointer. | |
| 77 return X509_PUBKEY_get( | |
| 78 X509_get_X509_PUBKEY(cert.os_cert_handle())); | |
| 79 } | |
| 80 | |
| 81 } // namespace net | |
| OLD | NEW |