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 #include "config.h" 32 #include "public/platform/WebCryptoAlgorithm.h" 33 34 #include "public/platform/WebCryptoAlgorithmParams.h" 35 #include "wtf/OwnPtr.h" 36 #include "wtf/ThreadSafeRefCounted.h" 37 38 namespace WebKit { 39 40 class WebCryptoAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoAlgorithmPrivate> { 41 public: 42 WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id, const char* name, PassOwnPtr<WebCryptoAlgorithmParams> params) 43 : id(id) 44 , name(name) 45 , params(params) 46 { 47 } 48 49 WebCryptoAlgorithmId id; 50 const char* const name; 51 OwnPtr<WebCryptoAlgorithmParams> params; 52 }; 53 54 WebCryptoAlgorithm::WebCryptoAlgorithm(WebCryptoAlgorithmId id, const char* name, PassOwnPtr<WebCryptoAlgorithmParams> params) 55 : m_private(adoptRef(new WebCryptoAlgorithmPrivate(id, name, params))) 56 { 57 } 58 59 WebCryptoAlgorithm WebCryptoAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId id, const char* name, WebCryptoAlgorithmParams* params) 60 { 61 return WebCryptoAlgorithm(id, name, adoptPtr(params)); 62 } 63 64 WebCryptoAlgorithmId WebCryptoAlgorithm::id() const 65 { 66 return m_private->id; 67 } 68 69 const char* WebCryptoAlgorithm::name() const 70 { 71 return m_private->name; 72 } 73 74 WebCryptoAlgorithmParamsType WebCryptoAlgorithm::paramsType() const 75 { 76 if (!m_private->params) 77 return WebCryptoAlgorithmParamsTypeNone; 78 return m_private->params->type(); 79 } 80 81 WebCryptoAesCbcParams* WebCryptoAlgorithm::aesCbcParams() const 82 { 83 if (paramsType() == WebCryptoAlgorithmParamsTypeAesCbcParams) 84 return static_cast<WebCryptoAesCbcParams*>(m_private->params.get()); 85 return 0; 86 } 87 88 WebCryptoAesKeyGenParams* WebCryptoAlgorithm::aesKeyGenParams() const 89 { 90 if (paramsType() == WebCryptoAlgorithmParamsTypeAesKeyGenParams) 91 return static_cast<WebCryptoAesKeyGenParams*>(m_private->params.get()); 92 return 0; 93 } 94 95 WebCryptoHmacParams* WebCryptoAlgorithm::hmacParams() const 96 { 97 if (paramsType() == WebCryptoAlgorithmParamsTypeHmacParams) 98 return static_cast<WebCryptoHmacParams*>(m_private->params.get()); 99 return 0; 100 } 101 102 WebCryptoRsaSsaParams* WebCryptoAlgorithm::rsaSsaParams() const 103 { 104 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaSsaParams) 105 return static_cast<WebCryptoRsaSsaParams*>(m_private->params.get()); 106 return 0; 107 } 108 109 WebCryptoRsaKeyGenParams* WebCryptoAlgorithm::rsaKeyGenParams() const 110 { 111 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaKeyGenParams) 112 return static_cast<WebCryptoRsaKeyGenParams*>(m_private->params.get()); 113 return 0; 114 } 115 116 void WebCryptoAlgorithm::assign(const WebCryptoAlgorithm& other) 117 { 118 m_private = other.m_private; 119 } 120 121 void WebCryptoAlgorithm::reset() 122 { 123 m_private.reset(); 124 } 125 126 } // namespace WebKit 127