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_NSS_KEY_NSS_H_ 6#define CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_ 7 8#include <stdint.h> 9#include <vector> 10 11#include "crypto/scoped_nss_types.h" 12#include "third_party/WebKit/public/platform/WebCryptoKey.h" 13 14namespace content { 15 16namespace webcrypto { 17 18class CryptoData; 19class PrivateKeyNss; 20class PublicKeyNss; 21class SymKeyNss; 22 23// Base key class for all NSS keys, used to safely cast between types. Each key 24// maintains a copy of its serialized form in either 'raw', 'pkcs8', or 'spki' 25// format. This is to allow structured cloning of keys synchronously from the 26// target Blink thread without having to lock access to the key. 27class KeyNss : public blink::WebCryptoKeyHandle { 28 public: 29 explicit KeyNss(const CryptoData& serialized_key_data); 30 virtual ~KeyNss(); 31 32 virtual SymKeyNss* AsSymKey(); 33 virtual PublicKeyNss* AsPublicKey(); 34 virtual PrivateKeyNss* AsPrivateKey(); 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 44class SymKeyNss : public KeyNss { 45 public: 46 virtual ~SymKeyNss(); 47 SymKeyNss(crypto::ScopedPK11SymKey key, const CryptoData& raw_key_data); 48 49 static SymKeyNss* Cast(const blink::WebCryptoKey& key); 50 51 PK11SymKey* key() { return key_.get(); } 52 virtual SymKeyNss* AsSymKey() OVERRIDE; 53 54 const std::vector<uint8_t>& raw_key_data() const { 55 return serialized_key_data(); 56 } 57 58 private: 59 crypto::ScopedPK11SymKey key_; 60 61 DISALLOW_COPY_AND_ASSIGN(SymKeyNss); 62}; 63 64class PublicKeyNss : public KeyNss { 65 public: 66 virtual ~PublicKeyNss(); 67 PublicKeyNss(crypto::ScopedSECKEYPublicKey key, const CryptoData& spki_data); 68 69 static PublicKeyNss* Cast(const blink::WebCryptoKey& key); 70 71 SECKEYPublicKey* key() { return key_.get(); } 72 virtual PublicKeyNss* AsPublicKey() OVERRIDE; 73 74 const std::vector<uint8_t>& spki_data() const { 75 return serialized_key_data(); 76 } 77 78 private: 79 crypto::ScopedSECKEYPublicKey key_; 80 81 DISALLOW_COPY_AND_ASSIGN(PublicKeyNss); 82}; 83 84class PrivateKeyNss : public KeyNss { 85 public: 86 virtual ~PrivateKeyNss(); 87 PrivateKeyNss(crypto::ScopedSECKEYPrivateKey key, 88 const CryptoData& pkcs8_data); 89 90 static PrivateKeyNss* Cast(const blink::WebCryptoKey& key); 91 92 SECKEYPrivateKey* key() { return key_.get(); } 93 virtual PrivateKeyNss* AsPrivateKey() OVERRIDE; 94 95 const std::vector<uint8_t>& pkcs8_data() const { 96 return serialized_key_data(); 97 } 98 99 private: 100 crypto::ScopedSECKEYPrivateKey key_; 101 102 DISALLOW_COPY_AND_ASSIGN(PrivateKeyNss); 103}; 104 105} // namespace webcrypto 106 107} // namespace content 108 109#endif // CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_ 110