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 WebCrypto_h
     32 #define WebCrypto_h
     33 
     34 #include "WebCommon.h"
     35 #include "WebCryptoKey.h"
     36 #include "WebPrivatePtr.h"
     37 
     38 namespace WebCore { class CryptoResult; }
     39 
     40 #if BLINK_IMPLEMENTATION
     41 namespace WTF { template <typename T> class PassRefPtr; }
     42 #endif
     43 
     44 namespace blink {
     45 
     46 class WebArrayBuffer;
     47 
     48 class WebCryptoResult {
     49 public:
     50     WebCryptoResult(const WebCryptoResult& o)
     51     {
     52         assign(o);
     53     }
     54 
     55     ~WebCryptoResult()
     56     {
     57         reset();
     58     }
     59 
     60     WebCryptoResult& operator=(const WebCryptoResult& o)
     61     {
     62         assign(o);
     63         return *this;
     64     }
     65 
     66     BLINK_EXPORT void completeWithError();
     67     BLINK_EXPORT void completeWithBuffer(const WebArrayBuffer&);
     68     BLINK_EXPORT void completeWithBuffer(const void*, unsigned);
     69     BLINK_EXPORT void completeWithBoolean(bool);
     70     BLINK_EXPORT void completeWithKey(const WebCryptoKey&);
     71     BLINK_EXPORT void completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey);
     72 
     73 #if BLINK_IMPLEMENTATION
     74     explicit WebCryptoResult(const WTF::PassRefPtr<WebCore::CryptoResult>&);
     75 #endif
     76 
     77 private:
     78     BLINK_EXPORT void reset();
     79     BLINK_EXPORT void assign(const WebCryptoResult&);
     80 
     81     WebPrivatePtr<WebCore::CryptoResult> m_impl;
     82 };
     83 
     84 class WebCrypto {
     85 public:
     86     // Starts a one-shot cryptographic operation which can complete either
     87     // synchronously, or asynchronously.
     88     //
     89     // Let the WebCryptoResult be called "result".
     90     //
     91     // The result should be set exactly once, from the same thread which
     92     // initiated the operation.
     93     //
     94     //   * WebCryptoAlgorithms parameters are guaranteed to be !isNull(),
     95     //     unless noted otherwise.
     96     //   * WebCryptoKey parameters are guaranteeed to be !isNull().
     97     //   * const unsigned char* data buffers are not valid after return.
     98     virtual void encrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsigned char* data, unsigned dataSize, WebCryptoResult result) { result.completeWithError(); }
     99     virtual void decrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsigned char* data, unsigned dataSize, WebCryptoResult result) { result.completeWithError(); }
    100     virtual void sign(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsigned char* data, unsigned dataSize, WebCryptoResult result) { result.completeWithError(); }
    101     virtual void verifySignature(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsigned char* signature, unsigned signatureSize, const unsigned char* data, unsigned dataSize, WebCryptoResult result) { result.completeWithError(); }
    102     virtual void digest(const WebCryptoAlgorithm&, const unsigned char* data, unsigned dataSize, WebCryptoResult result) { result.completeWithError(); }
    103     virtual void generateKey(const WebCryptoAlgorithm&, bool extractable, WebCryptoKeyUsageMask, WebCryptoResult result) { result.completeWithError(); }
    104     // The WebCryptoAlgorithm for importKey may be "isNull()" meaning that it
    105     // was unspecified by the caller.
    106     virtual void importKey(WebCryptoKeyFormat, const unsigned char* keyData, unsigned keyDataSize, const WebCryptoAlgorithm&, bool extractable, WebCryptoKeyUsageMask, WebCryptoResult result) { result.completeWithError(); }
    107     virtual void exportKey(WebCryptoKeyFormat, const WebCryptoKey&, WebCryptoResult result) { result.completeWithError(); }
    108 
    109     virtual void wrapKey(WebCryptoKeyFormat, const WebCryptoKey& key, const WebCryptoKey& wrappingKey, const WebCryptoAlgorithm&, WebCryptoResult result) { result.completeWithError(); }
    110 
    111     // It is possible for unwrappedKeyAlgorithm.isNull() meaning that it was
    112     // unspecified by the caller.
    113     virtual void unwrapKey(WebCryptoKeyFormat, const unsigned char* wrappedKey, unsigned wrappedKeySize, const WebCryptoKey&, const WebCryptoAlgorithm& unwrapAlgorithm, const WebCryptoAlgorithm& unwrappedKeyAlgorithm, bool extractable, WebCryptoKeyUsageMask, WebCryptoResult result) { result.completeWithError(); }
    114 
    115 protected:
    116     virtual ~WebCrypto() { }
    117 };
    118 
    119 } // namespace blink
    120 
    121 #endif
    122