Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: Source/modules/crypto/Key.cpp

Issue 18033004: WebCrypto: Add WebKit API structure for keys. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: TEMPORARY: explore alternate implementations for building keyUsage() Vector Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/crypto/Key.h ('k') | Source/modules/crypto/Key.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 ASSERT_NOT_REACHED();
73 return 0;
74 }
75 ASSERT_NOT_REACHED();
76 return 0;
77 }
78
79 } // namespace
80
81 Key::Key(const WebKit::WebCryptoKey& key)
82 : m_key(key)
83 {
84 ScriptWrappable::init(this);
85 }
86
87 String Key::type() const
88 {
89 return ASCIILiteral(keyTypeToString(m_key.type()));
90 }
91
92 bool Key::extractable() const
93 {
94 return m_key.extractable();
95 }
96
97 Algorithm* Key::algorithm()
98 {
99 if (!m_algorithm)
100 m_algorithm = Algorithm::create(m_key.algorithm());
101 return m_algorithm.get();
102 }
103
104 Vector<String> Key::keyUsage() const
105 {
106 Vector<String> result;
107
108 // The WebCryptoKeyUsage values are consecutive powers of 2. Test each one i n order.
109 for (int i = 0; (1 << i) <= WebKit::EndOfWebCryptoKeyUsage - 1; ++i) {
abarth-chromium 2013/07/03 21:22:54 Why <= and -1 ? The normal idiom is < and no -1..
110 WebKit::WebCryptoKeyUsage usage = static_cast<WebKit::WebCryptoKeyUsage> (1 << i);
111 if (m_key.keyUsage() & usage)
112 result.append(ASCIILiteral(keyUsageToString(usage)));
113 }
114 return result;
115 }
116
117 // Here is an alternate implementation. It doesn't require any casting, however
118 // the notable downside is that anyone adding a new value to WebCryptoKeyUsage
119 // must also update this function.
abarth-chromium 2013/07/03 21:22:54 I think the first version is better because it's l
eroman 2013/07/03 21:35:19 I have uploaded a new patch which switches over to
120 Vector<String> Key::keyUsage() const
121 {
122 // Make sure we are testing against all the values. If this compile assertio n
123 // breaks, you must update the ifs below.
124 const int NumEnumValues = 7;
125 COMPILE_ASSERT(WebKit::EndOfWebCryptoKeyUsage == (1<<(NumEnumValues-1)) + 1, missing_enums);
126
127 Vector<String> result;
128
129 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageEncrypt)
130 result.append(ASCIILiteral("encrypt"));
131 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageDecrypt)
132 result.append(ASCIILiteral("decrypt"));
133 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageSign)
134 result.append(ASCIILiteral("sign"));
135 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageVerify)
136 result.append(ASCIILiteral("verify"));
137 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageDerive)
138 result.append(ASCIILiteral("derive"));
139 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageWrap)
140 result.append(ASCIILiteral("wrap"));
141 if (m_key_.keyUsage() & WebKit::WebCryptoKeyUsageUnwrap)
142 result.append(ASCIILiteral("unwrap"));
143
144 return result;
145 }
146
147 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/Key.h ('k') | Source/modules/crypto/Key.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698