OLD | NEW |
(Empty) | |
| 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 "modules/crypto/Key.h" |
| 33 |
| 34 #include "modules/crypto/Algorithm.h" |
| 35 |
| 36 namespace WebCore { |
| 37 |
| 38 namespace { |
| 39 |
| 40 const char* keyTypeToString(WebKit::WebCryptoKeyType type) |
| 41 { |
| 42 switch (type) { |
| 43 case WebKit::WebCryptoKeyTypeSecret: |
| 44 return "secret"; |
| 45 case WebKit::WebCryptoKeyTypePublic: |
| 46 return "public"; |
| 47 case WebKit::WebCryptoKeyTypePrivate: |
| 48 return "private"; |
| 49 } |
| 50 ASSERT_NOT_REACHED(); |
| 51 return 0; |
| 52 } |
| 53 |
| 54 const char* keyUsageToString(WebKit::WebCryptoKeyUsage usage) |
| 55 { |
| 56 switch (usage) { |
| 57 case WebKit::WebCryptoKeyUsageEncrypt: |
| 58 return "encrypt"; |
| 59 case WebKit::WebCryptoKeyUsageDecrypt: |
| 60 return "decrypt"; |
| 61 case WebKit::WebCryptoKeyUsageSign: |
| 62 return "sign"; |
| 63 case WebKit::WebCryptoKeyUsageVerify: |
| 64 return "verify"; |
| 65 case WebKit::WebCryptoKeyUsageDerive: |
| 66 return "derive"; |
| 67 case WebKit::WebCryptoKeyUsageWrap: |
| 68 return "wrap"; |
| 69 case WebKit::WebCryptoKeyUsageUnwrap: |
| 70 return "unwrap"; |
| 71 case WebKit::EndOfWebCryptoKeyUsage: |
| 72 break; |
| 73 } |
| 74 ASSERT_NOT_REACHED(); |
| 75 return 0; |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 Key::Key(const WebKit::WebCryptoKey& key) |
| 81 : m_key(key) |
| 82 { |
| 83 ScriptWrappable::init(this); |
| 84 } |
| 85 |
| 86 String Key::type() const |
| 87 { |
| 88 return ASCIILiteral(keyTypeToString(m_key.type())); |
| 89 } |
| 90 |
| 91 bool Key::extractable() const |
| 92 { |
| 93 return m_key.extractable(); |
| 94 } |
| 95 |
| 96 Algorithm* Key::algorithm() |
| 97 { |
| 98 if (!m_algorithm) |
| 99 m_algorithm = Algorithm::create(m_key.algorithm()); |
| 100 return m_algorithm.get(); |
| 101 } |
| 102 |
| 103 // FIXME: This creates a new javascript array each time. What should happen |
| 104 // instead is return the same (immutable) array. (Javascript callers can |
| 105 // distinguish this by doing an == test on the arrays and seeing they are |
| 106 // different). |
| 107 Vector<String> Key::keyUsage() const |
| 108 { |
| 109 Vector<String> result; |
| 110 |
| 111 // The WebCryptoKeyUsage values are consecutive powers of 2. Test each one i
n order. |
| 112 for (int i = 0; (1 << i) < WebKit::EndOfWebCryptoKeyUsage; ++i) { |
| 113 WebKit::WebCryptoKeyUsage usage = static_cast<WebKit::WebCryptoKeyUsage>
(1 << i); |
| 114 if (m_key.keyUsage() & usage) |
| 115 result.append(ASCIILiteral(keyUsageToString(usage))); |
| 116 } |
| 117 return result; |
| 118 } |
| 119 |
| 120 } // namespace WebCore |
OLD | NEW |