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 WebCryptoKey_h 32 #define WebCryptoKey_h 33 34 #include "WebCommon.h" 35 #include "WebPrivatePtr.h" 36 37 namespace blink { 38 39 enum WebCryptoKeyType { 40 WebCryptoKeyTypeSecret, 41 WebCryptoKeyTypePublic, 42 WebCryptoKeyTypePrivate, 43 }; 44 45 enum WebCryptoKeyUsage { 46 WebCryptoKeyUsageEncrypt = 1 << 0, 47 WebCryptoKeyUsageDecrypt = 1 << 1, 48 WebCryptoKeyUsageSign = 1 << 2, 49 WebCryptoKeyUsageVerify = 1 << 3, 50 WebCryptoKeyUsageDeriveKey = 1 << 4, 51 WebCryptoKeyUsageWrapKey = 1 << 5, 52 WebCryptoKeyUsageUnwrapKey = 1 << 6, 53 #if INSIDE_BLINK 54 EndOfWebCryptoKeyUsage, 55 #endif 56 }; 57 58 // A bitfield of WebCryptoKeyUsage 59 typedef int WebCryptoKeyUsageMask; 60 61 enum WebCryptoKeyFormat { 62 WebCryptoKeyFormatRaw, 63 WebCryptoKeyFormatPkcs8, 64 WebCryptoKeyFormatSpki, 65 WebCryptoKeyFormatJwk, 66 }; 67 68 class WebCryptoAlgorithm; 69 class WebCryptoKeyPrivate; 70 class WebCryptoKeyHandle; 71 72 // The WebCryptoKey represents a key from the Web Crypto API: 73 // 74 // https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#key-interface 75 // 76 // WebCryptoKey is just a reference-counted wrapper that manages the lifetime of 77 // a "WebCryptoKeyHandle*". 78 // 79 // WebCryptoKey is: 80 // * Copiable (cheaply) 81 // * Threadsafe if the embedder's WebCryptoKeyHandle is also threadsafe. 82 // 83 // The embedder is responsible for creating all WebCryptoKeys, and therefore can 84 // safely assume any details regarding the type of the wrapped 85 // WebCryptoKeyHandle*. 86 // 87 // If WebCryptoKey "isNull()" then it is invalid to call any of the other 88 // methods on it (other than destruction, assignment, or isNull()). 89 // 90 // FIXME: Define the interface to use for structured clone. 91 // Cloning across a process boundary will need serialization, 92 // however cloning for in-process workers could just share the same 93 // (threadsafe) handle. 94 class WebCryptoKey { 95 public: 96 ~WebCryptoKey() { reset(); } 97 98 WebCryptoKey(const WebCryptoKey& other) { assign(other); } 99 WebCryptoKey& operator=(const WebCryptoKey& other) 100 { 101 assign(other); 102 return *this; 103 } 104 105 // For an explanation of these parameters see: 106 // https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#key-interface-members 107 // 108 // Note that the caller is passing ownership of the WebCryptoKeyHandle*. 109 BLINK_PLATFORM_EXPORT static WebCryptoKey create(WebCryptoKeyHandle*, WebCryptoKeyType, bool extractable, const WebCryptoAlgorithm&, WebCryptoKeyUsageMask); 110 111 BLINK_PLATFORM_EXPORT static WebCryptoKey createNull(); 112 113 // Returns the opaque key handle that was set by the embedder. 114 // * Safe to downcast to known type (since embedder creates all the keys) 115 // * Returned pointer's lifetime is bound to |this| 116 BLINK_PLATFORM_EXPORT WebCryptoKeyHandle* handle() const; 117 118 BLINK_PLATFORM_EXPORT WebCryptoKeyType type() const; 119 BLINK_PLATFORM_EXPORT bool extractable() const; 120 BLINK_PLATFORM_EXPORT const WebCryptoAlgorithm& algorithm() const; 121 BLINK_PLATFORM_EXPORT WebCryptoKeyUsageMask usages() const; 122 123 BLINK_PLATFORM_EXPORT bool isNull() const; 124 125 private: 126 WebCryptoKey() { } 127 BLINK_PLATFORM_EXPORT void assign(const WebCryptoKey& other); 128 BLINK_PLATFORM_EXPORT void reset(); 129 130 WebPrivatePtr<WebCryptoKeyPrivate> m_private; 131 }; 132 133 // Base class for the embedder to define its own opaque key handle. The lifetime 134 // of this object is controlled by WebCryptoKey using reference counting. 135 class WebCryptoKeyHandle { 136 public: 137 virtual ~WebCryptoKeyHandle() { } 138 }; 139 140 } // namespace blink 141 142 #endif 143