Home | History | Annotate | Download | only in openssl
      1 // Copyright 2014 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 #ifndef CONTENT_CHILD_WEBCRYPTO_OPENSSL_KEY_OPENSSL_H_
      6 #define CONTENT_CHILD_WEBCRYPTO_OPENSSL_KEY_OPENSSL_H_
      7 
      8 #include <openssl/ossl_typ.h>
      9 #include <stdint.h>
     10 #include <vector>
     11 
     12 #include "base/macros.h"
     13 #include "crypto/scoped_openssl_types.h"
     14 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
     15 
     16 namespace content {
     17 
     18 namespace webcrypto {
     19 
     20 class CryptoData;
     21 class AsymKeyOpenSsl;
     22 class SymKeyOpenSsl;
     23 
     24 // Base key class for all OpenSSL keys, used to safely cast between types. Each
     25 // key maintains a copy of its serialized form in either 'raw', 'pkcs8', or
     26 // 'spki' format. This is to allow structured cloning of keys synchronously from
     27 // the target Blink thread without having to lock access to the key.
     28 class KeyOpenSsl : public blink::WebCryptoKeyHandle {
     29  public:
     30   explicit KeyOpenSsl(const CryptoData& serialized_key_data);
     31   virtual ~KeyOpenSsl();
     32 
     33   virtual SymKeyOpenSsl* AsSymKey();
     34   virtual AsymKeyOpenSsl* AsAsymKey();
     35 
     36   const std::vector<uint8_t>& serialized_key_data() const {
     37     return serialized_key_data_;
     38   }
     39 
     40  private:
     41   const std::vector<uint8_t> serialized_key_data_;
     42 };
     43 
     44 class SymKeyOpenSsl : public KeyOpenSsl {
     45  public:
     46   virtual ~SymKeyOpenSsl();
     47   explicit SymKeyOpenSsl(const CryptoData& raw_key_data);
     48 
     49   static SymKeyOpenSsl* Cast(const blink::WebCryptoKey& key);
     50 
     51   virtual SymKeyOpenSsl* AsSymKey() OVERRIDE;
     52 
     53   const std::vector<uint8_t>& raw_key_data() const {
     54     return serialized_key_data();
     55   }
     56 
     57  private:
     58   DISALLOW_COPY_AND_ASSIGN(SymKeyOpenSsl);
     59 };
     60 
     61 class AsymKeyOpenSsl : public KeyOpenSsl {
     62  public:
     63   virtual ~AsymKeyOpenSsl();
     64   AsymKeyOpenSsl(crypto::ScopedEVP_PKEY key,
     65                  const CryptoData& serialized_key_data);
     66 
     67   static AsymKeyOpenSsl* Cast(const blink::WebCryptoKey& key);
     68 
     69   virtual AsymKeyOpenSsl* AsAsymKey() OVERRIDE;
     70 
     71   EVP_PKEY* key() { return key_.get(); }
     72 
     73  private:
     74   crypto::ScopedEVP_PKEY key_;
     75 
     76   DISALLOW_COPY_AND_ASSIGN(AsymKeyOpenSsl);
     77 };
     78 
     79 }  // namespace webcrypto
     80 
     81 }  // namespace content
     82 
     83 #endif  // CONTENT_CHILD_WEBCRYPTO_OPENSSL_KEY_OPENSSL_H_
     84