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 blink { 39 40 class WebCryptoAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoAlgorithmPrivate> { 41 public: 42 WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgorithmParams> params) 43 : id(id) 44 , params(params) 45 { 46 } 47 48 WebCryptoAlgorithmId id; 49 OwnPtr<WebCryptoAlgorithmParams> params; 50 }; 51 52 WebCryptoAlgorithm::WebCryptoAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoAlgorithmParams> params) 53 : m_private(adoptRef(new WebCryptoAlgorithmPrivate(id, params))) 54 { 55 } 56 57 WebCryptoAlgorithm WebCryptoAlgorithm::createNull() 58 { 59 return WebCryptoAlgorithm(); 60 } 61 62 WebCryptoAlgorithm WebCryptoAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId id, WebCryptoAlgorithmParams* params) 63 { 64 return WebCryptoAlgorithm(id, adoptPtr(params)); 65 } 66 67 bool WebCryptoAlgorithm::isNull() const 68 { 69 return m_private.isNull(); 70 } 71 72 WebCryptoAlgorithmId WebCryptoAlgorithm::id() const 73 { 74 ASSERT(!isNull()); 75 return m_private->id; 76 } 77 78 WebCryptoAlgorithmParamsType WebCryptoAlgorithm::paramsType() const 79 { 80 ASSERT(!isNull()); 81 if (!m_private->params) 82 return WebCryptoAlgorithmParamsTypeNone; 83 return m_private->params->type(); 84 } 85 86 const WebCryptoAesCbcParams* WebCryptoAlgorithm::aesCbcParams() const 87 { 88 ASSERT(!isNull()); 89 if (paramsType() == WebCryptoAlgorithmParamsTypeAesCbcParams) 90 return static_cast<WebCryptoAesCbcParams*>(m_private->params.get()); 91 return 0; 92 } 93 94 const WebCryptoAesCtrParams* WebCryptoAlgorithm::aesCtrParams() const 95 { 96 ASSERT(!isNull()); 97 if (paramsType() == WebCryptoAlgorithmParamsTypeAesCtrParams) 98 return static_cast<WebCryptoAesCtrParams*>(m_private->params.get()); 99 return 0; 100 } 101 102 const WebCryptoAesKeyGenParams* WebCryptoAlgorithm::aesKeyGenParams() const 103 { 104 ASSERT(!isNull()); 105 if (paramsType() == WebCryptoAlgorithmParamsTypeAesKeyGenParams) 106 return static_cast<WebCryptoAesKeyGenParams*>(m_private->params.get()); 107 return 0; 108 } 109 110 const WebCryptoHmacParams* WebCryptoAlgorithm::hmacParams() const 111 { 112 ASSERT(!isNull()); 113 if (paramsType() == WebCryptoAlgorithmParamsTypeHmacParams) 114 return static_cast<WebCryptoHmacParams*>(m_private->params.get()); 115 return 0; 116 } 117 118 const WebCryptoHmacKeyParams* WebCryptoAlgorithm::hmacKeyParams() const 119 { 120 ASSERT(!isNull()); 121 if (paramsType() == WebCryptoAlgorithmParamsTypeHmacKeyParams) 122 return static_cast<WebCryptoHmacKeyParams*>(m_private->params.get()); 123 return 0; 124 } 125 126 const WebCryptoRsaSsaParams* WebCryptoAlgorithm::rsaSsaParams() const 127 { 128 ASSERT(!isNull()); 129 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaSsaParams) 130 return static_cast<WebCryptoRsaSsaParams*>(m_private->params.get()); 131 return 0; 132 } 133 134 const WebCryptoRsaKeyGenParams* WebCryptoAlgorithm::rsaKeyGenParams() const 135 { 136 ASSERT(!isNull()); 137 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaKeyGenParams) 138 return static_cast<WebCryptoRsaKeyGenParams*>(m_private->params.get()); 139 return 0; 140 } 141 142 const WebCryptoAesGcmParams* WebCryptoAlgorithm::aesGcmParams() const 143 { 144 ASSERT(!isNull()); 145 if (paramsType() == WebCryptoAlgorithmParamsTypeAesGcmParams) 146 return static_cast<WebCryptoAesGcmParams*>(m_private->params.get()); 147 return 0; 148 } 149 150 const WebCryptoRsaOaepParams* WebCryptoAlgorithm::rsaOaepParams() const 151 { 152 ASSERT(!isNull()); 153 if (paramsType() == WebCryptoAlgorithmParamsTypeRsaOaepParams) 154 return static_cast<WebCryptoRsaOaepParams*>(m_private->params.get()); 155 return 0; 156 } 157 158 void WebCryptoAlgorithm::assign(const WebCryptoAlgorithm& other) 159 { 160 m_private = other.m_private; 161 } 162 163 void WebCryptoAlgorithm::reset() 164 { 165 m_private.reset(); 166 } 167 168 } // namespace blink 169