1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_CRYPTO_KEY_H_ 32 #define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_CRYPTO_KEY_H_ 33 34 #include "third_party/blink/public/platform/web_common.h" 35 #include "third_party/blink/public/platform/web_private_ptr.h" 36 37 namespace blink { 38 39 enum WebCryptoKeyType { 40 kWebCryptoKeyTypeSecret, 41 kWebCryptoKeyTypePublic, 42 kWebCryptoKeyTypePrivate, 43 }; 44 45 enum WebCryptoKeyUsage { 46 kWebCryptoKeyUsageEncrypt = 1 << 0, 47 kWebCryptoKeyUsageDecrypt = 1 << 1, 48 kWebCryptoKeyUsageSign = 1 << 2, 49 kWebCryptoKeyUsageVerify = 1 << 3, 50 kWebCryptoKeyUsageDeriveKey = 1 << 4, 51 kWebCryptoKeyUsageWrapKey = 1 << 5, 52 kWebCryptoKeyUsageUnwrapKey = 1 << 6, 53 kWebCryptoKeyUsageDeriveBits = 1 << 7, 54 #if INSIDE_BLINK 55 kEndOfWebCryptoKeyUsage, 56 #endif 57 }; 58 59 // A bitfield of WebCryptoKeyUsage 60 typedef int WebCryptoKeyUsageMask; 61 62 enum WebCryptoKeyFormat { 63 kWebCryptoKeyFormatRaw, 64 kWebCryptoKeyFormatPkcs8, 65 kWebCryptoKeyFormatSpki, 66 kWebCryptoKeyFormatJwk, 67 }; 68 69 class WebCryptoKeyAlgorithm; 70 class WebCryptoKeyPrivate; 71 class WebCryptoKeyHandle; 72 73 // The WebCryptoKey represents a key from the Web Crypto API: 74 // 75 // https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#key-interface 76 // 77 // WebCryptoKey is just a reference-counted wrapper that manages the lifetime of 78 // a "WebCryptoKeyHandle*". 79 // 80 // WebCryptoKey is: 81 // * Copiable (cheaply) 82 // * Threadsafe if the embedder's WebCryptoKeyHandle is also threadsafe. 83 // 84 // The embedder is responsible for creating all WebCryptoKeys, and therefore can 85 // safely assume any details regarding the type of the wrapped 86 // WebCryptoKeyHandle*. 87 // 88 // If WebCryptoKey "IsNull()" then it is invalid to call any of the other 89 // methods on it (other than destruction, assignment, or IsNull()). 90 class WebCryptoKey { 91 public: 92 // Constructs a "null" key (One for which isNull() returns true). 93 WebCryptoKey() = default; ~WebCryptoKey()94 ~WebCryptoKey() { Reset(); } 95 WebCryptoKey(const WebCryptoKey & other)96 WebCryptoKey(const WebCryptoKey& other) { Assign(other); } 97 WebCryptoKey& operator=(const WebCryptoKey& other) { 98 Assign(other); 99 return *this; 100 } 101 102 // For an explanation of these parameters see: 103 // https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#key-interface-members 104 // 105 // Note that the caller is passing ownership of the WebCryptoKeyHandle*. 106 BLINK_PLATFORM_EXPORT static WebCryptoKey Create(WebCryptoKeyHandle*, 107 WebCryptoKeyType, 108 bool extractable, 109 const WebCryptoKeyAlgorithm&, 110 WebCryptoKeyUsageMask); 111 112 BLINK_PLATFORM_EXPORT static WebCryptoKey CreateNull(); 113 114 // Returns the opaque key handle that was set by the embedder. 115 // * Safe to downcast to known type (since embedder creates all the keys) 116 // * Returned pointer's lifetime is bound to |this| 117 BLINK_PLATFORM_EXPORT WebCryptoKeyHandle* Handle() const; 118 119 BLINK_PLATFORM_EXPORT WebCryptoKeyType GetType() const; 120 BLINK_PLATFORM_EXPORT bool Extractable() const; 121 BLINK_PLATFORM_EXPORT const WebCryptoKeyAlgorithm& Algorithm() const; 122 BLINK_PLATFORM_EXPORT WebCryptoKeyUsageMask Usages() const; 123 124 BLINK_PLATFORM_EXPORT bool IsNull() const; 125 126 BLINK_PLATFORM_EXPORT bool KeyUsageAllows( 127 const blink::WebCryptoKeyUsage) const; 128 129 private: 130 BLINK_PLATFORM_EXPORT void Assign(const WebCryptoKey& other); 131 BLINK_PLATFORM_EXPORT void Reset(); 132 133 WebPrivatePtr<WebCryptoKeyPrivate> private_; 134 }; 135 136 // Base class for the embedder to define its own opaque key handle. The lifetime 137 // of this object is controlled by WebCryptoKey using reference counting. 138 class WebCryptoKeyHandle { 139 public: 140 virtual ~WebCryptoKeyHandle() = default; 141 }; 142 143 } // namespace blink 144 145 #endif 146