Home | History | Annotate | Download | only in platform
      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     WebCryptoKeyUsageDeriveBits = 1 << 7,
     54 #if INSIDE_BLINK
     55     EndOfWebCryptoKeyUsage,
     56 #endif
     57 };
     58 
     59 // A bitfield of WebCryptoKeyUsage
     60 typedef int WebCryptoKeyUsageMask;
     61 
     62 enum WebCryptoKeyFormat {
     63     WebCryptoKeyFormatRaw,
     64     WebCryptoKeyFormatPkcs8,
     65     WebCryptoKeyFormatSpki,
     66     WebCryptoKeyFormatJwk,
     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     ~WebCryptoKey() { reset(); }
     93 
     94     WebCryptoKey(const WebCryptoKey& other) { assign(other); }
     95     WebCryptoKey& operator=(const WebCryptoKey& other)
     96     {
     97         assign(other);
     98         return *this;
     99     }
    100 
    101     // For an explanation of these parameters see:
    102     // https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#key-interface-members
    103     //
    104     // Note that the caller is passing ownership of the WebCryptoKeyHandle*.
    105     BLINK_PLATFORM_EXPORT static WebCryptoKey create(WebCryptoKeyHandle*, WebCryptoKeyType, bool extractable, const WebCryptoKeyAlgorithm&, WebCryptoKeyUsageMask);
    106 
    107     BLINK_PLATFORM_EXPORT static WebCryptoKey createNull();
    108 
    109     // Returns the opaque key handle that was set by the embedder.
    110     //   * Safe to downcast to known type (since embedder creates all the keys)
    111     //   * Returned pointer's lifetime is bound to |this|
    112     BLINK_PLATFORM_EXPORT WebCryptoKeyHandle* handle() const;
    113 
    114     BLINK_PLATFORM_EXPORT WebCryptoKeyType type() const;
    115     BLINK_PLATFORM_EXPORT bool extractable() const;
    116     BLINK_PLATFORM_EXPORT const WebCryptoKeyAlgorithm& algorithm() const;
    117     BLINK_PLATFORM_EXPORT WebCryptoKeyUsageMask usages() const;
    118 
    119     BLINK_PLATFORM_EXPORT bool isNull() const;
    120 
    121 private:
    122     WebCryptoKey() { }
    123     BLINK_PLATFORM_EXPORT void assign(const WebCryptoKey& other);
    124     BLINK_PLATFORM_EXPORT void reset();
    125 
    126     WebPrivatePtr<WebCryptoKeyPrivate> m_private;
    127 };
    128 
    129 // Base class for the embedder to define its own opaque key handle. The lifetime
    130 // of this object is controlled by WebCryptoKey using reference counting.
    131 class WebCryptoKeyHandle {
    132 public:
    133     virtual ~WebCryptoKeyHandle() { }
    134 };
    135 
    136 } // namespace blink
    137 
    138 #endif
    139