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

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

Issue 19885002: WebCrypto: Add interfaces for importKey(). (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase and rename Interface --> Private 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
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 return "secret"; 44 return "secret";
45 case WebKit::WebCryptoKeyTypePublic: 45 case WebKit::WebCryptoKeyTypePublic:
46 return "public"; 46 return "public";
47 case WebKit::WebCryptoKeyTypePrivate: 47 case WebKit::WebCryptoKeyTypePrivate:
48 return "private"; 48 return "private";
49 } 49 }
50 ASSERT_NOT_REACHED(); 50 ASSERT_NOT_REACHED();
51 return 0; 51 return 0;
52 } 52 }
53 53
54 struct KeyUsageMapping {
55 WebKit::WebCryptoKeyUsage value;
56 const char* const name;
57 };
58
59 const KeyUsageMapping keyUsageMappings[] = {
60 { WebKit::WebCryptoKeyUsageEncrypt, "encrypt" },
61 { WebKit::WebCryptoKeyUsageDecrypt, "decrypt" },
62 { WebKit::WebCryptoKeyUsageSign, "sign" },
63 { WebKit::WebCryptoKeyUsageVerify, "verify" },
64 { WebKit::WebCryptoKeyUsageDeriveKey, "deriveKey" },
65 { WebKit::WebCryptoKeyUsageWrapKey, "wrapKey" },
66 { WebKit::WebCryptoKeyUsageUnwrapKey, "unwrapKey" },
67 };
68
69 COMPILE_ASSERT(WebKit::EndOfWebCryptoKeyUsage == (1 << 6) + 1, update_keyUsageMa ppings);
70
54 const char* keyUsageToString(WebKit::WebCryptoKeyUsage usage) 71 const char* keyUsageToString(WebKit::WebCryptoKeyUsage usage)
55 { 72 {
56 switch (usage) { 73 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
57 case WebKit::WebCryptoKeyUsageEncrypt: 74 if (keyUsageMappings[i].value == usage)
58 return "encrypt"; 75 return keyUsageMappings[i].name;
59 case WebKit::WebCryptoKeyUsageDecrypt:
60 return "decrypt";
61 case WebKit::WebCryptoKeyUsageSign:
62 return "sign";
63 case WebKit::WebCryptoKeyUsageVerify:
64 return "verify";
65 case WebKit::WebCryptoKeyUsageDeriveKey:
66 return "deriveKey";
67 case WebKit::WebCryptoKeyUsageWrapKey:
68 return "wrapKey";
69 case WebKit::WebCryptoKeyUsageUnwrapKey:
70 return "unwrapKey";
71 case WebKit::EndOfWebCryptoKeyUsage:
72 break;
73 } 76 }
74 ASSERT_NOT_REACHED(); 77 ASSERT_NOT_REACHED();
75 return 0; 78 return 0;
76 } 79 }
77 80
81 WebKit::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
82 {
83 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
84 if (keyUsageMappings[i].name == usageString)
85 return keyUsageMappings[i].value;
86 }
87 return 0;
88 }
89
78 } // namespace 90 } // namespace
79 91
92 Key::~Key()
93 {
94 }
95
80 Key::Key(const WebKit::WebCryptoKey& key) 96 Key::Key(const WebKit::WebCryptoKey& key)
81 : m_key(key) 97 : m_key(key)
82 { 98 {
83 ScriptWrappable::init(this); 99 ScriptWrappable::init(this);
84 } 100 }
85 101
86 String Key::type() const 102 String Key::type() const
87 { 103 {
88 return ASCIILiteral(keyTypeToString(m_key.type())); 104 return ASCIILiteral(keyTypeToString(m_key.type()));
89 } 105 }
(...skipping 10 matching lines...) Expand all
100 return m_algorithm.get(); 116 return m_algorithm.get();
101 } 117 }
102 118
103 // FIXME: This creates a new javascript array each time. What should happen 119 // FIXME: This creates a new javascript array each time. What should happen
104 // instead is return the same (immutable) array. (Javascript callers can 120 // instead is return the same (immutable) array. (Javascript callers can
105 // distinguish this by doing an == test on the arrays and seeing they are 121 // distinguish this by doing an == test on the arrays and seeing they are
106 // different). 122 // different).
107 Vector<String> Key::usages() const 123 Vector<String> Key::usages() const
108 { 124 {
109 Vector<String> result; 125 Vector<String> result;
110 126 for (int i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
111 // The WebCryptoKeyUsage values are consecutive powers of 2. Test each one i n order. 127 WebKit::WebCryptoKeyUsage usage = keyUsageMappings[i].value;
112 for (int i = 0; (1 << i) < WebKit::EndOfWebCryptoKeyUsage; ++i) {
113 WebKit::WebCryptoKeyUsage usage = static_cast<WebKit::WebCryptoKeyUsage> (1 << i);
114 if (m_key.usages() & usage) 128 if (m_key.usages() & usage)
115 result.append(ASCIILiteral(keyUsageToString(usage))); 129 result.append(ASCIILiteral(keyUsageToString(usage)));
116 } 130 }
117 return result; 131 return result;
118 } 132 }
119 133
134 bool Key::parseFormat(const String& formatString, WebKit::WebCryptoKeyFormat& fo rmat)
135 {
136 // There are few enough values that testing serially is fast enough.
137 if (formatString == "raw") {
138 format = WebKit::WebCryptoKeyFormatRaw;
139 return true;
140 }
141 if (formatString == "pkcs8") {
142 format = WebKit::WebCryptoKeyFormatPkcs8;
143 return true;
144 }
145 if (formatString == "spki") {
146 format = WebKit::WebCryptoKeyFormatSpki;
147 return true;
148 }
149 if (formatString == "jwk") {
150 format = WebKit::WebCryptoKeyFormatJwk;
151 return true;
152 }
153
154 return false;
155 }
156
157 bool Key::parseUsageMask(const Vector<String>& usages, WebKit::WebCryptoKeyUsage Mask& mask)
158 {
159 mask = 0;
160 for (size_t i = 0; i < usages.size(); ++i) {
161 WebKit::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
162 if (!usage)
163 return false;
164 mask |= usage;
165 }
166 return true;
167 }
168
120 } // namespace WebCore 169 } // 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