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

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: Use a wrapper for the result rather than raw pointer 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
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 const char* keyUsageToString(WebKit::WebCryptoKeyUsage usage) 54 const char* keyUsageToString(WebKit::WebCryptoKeyUsage usage)
55 { 55 {
56 // NOTE: Keep this in sync with keyUsageStringToMask() below.
abarth-chromium 2013/07/23 21:45:49 IMHO, a table would be more maintainable here, but
eroman 2013/07/23 23:29:02 Done.
56 switch (usage) { 57 switch (usage) {
57 case WebKit::WebCryptoKeyUsageEncrypt: 58 case WebKit::WebCryptoKeyUsageEncrypt:
58 return "encrypt"; 59 return "encrypt";
59 case WebKit::WebCryptoKeyUsageDecrypt: 60 case WebKit::WebCryptoKeyUsageDecrypt:
60 return "decrypt"; 61 return "decrypt";
61 case WebKit::WebCryptoKeyUsageSign: 62 case WebKit::WebCryptoKeyUsageSign:
62 return "sign"; 63 return "sign";
63 case WebKit::WebCryptoKeyUsageVerify: 64 case WebKit::WebCryptoKeyUsageVerify:
64 return "verify"; 65 return "verify";
65 case WebKit::WebCryptoKeyUsageDeriveKey: 66 case WebKit::WebCryptoKeyUsageDeriveKey:
66 return "deriveKey"; 67 return "deriveKey";
67 case WebKit::WebCryptoKeyUsageWrapKey: 68 case WebKit::WebCryptoKeyUsageWrapKey:
68 return "wrapKey"; 69 return "wrapKey";
69 case WebKit::WebCryptoKeyUsageUnwrapKey: 70 case WebKit::WebCryptoKeyUsageUnwrapKey:
70 return "unwrapKey"; 71 return "unwrapKey";
71 case WebKit::EndOfWebCryptoKeyUsage: 72 case WebKit::EndOfWebCryptoKeyUsage:
72 break; 73 break;
73 } 74 }
74 ASSERT_NOT_REACHED(); 75 ASSERT_NOT_REACHED();
75 return 0; 76 return 0;
76 } 77 }
77 78
79 WebKit::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
80 {
81 // There are few enough values that testing serially is fast enough.
82 if (usageString == "encrypt")
83 return WebKit::WebCryptoKeyUsageEncrypt;
84 if (usageString == "decrypt")
85 return WebKit::WebCryptoKeyUsageDecrypt;
86 if (usageString == "sign")
87 return WebKit::WebCryptoKeyUsageSign;
88 if (usageString == "verify")
89 return WebKit::WebCryptoKeyUsageVerify;
90 if (usageString == "deriveKey")
91 return WebKit::WebCryptoKeyUsageDeriveKey;
92 if (usageString == "wrapKey")
93 return WebKit::WebCryptoKeyUsageWrapKey;
94 if (usageString == "unwrapKey")
95 return WebKit::WebCryptoKeyUsageUnwrapKey;
96
97 return 0;
98 }
99
78 } // namespace 100 } // namespace
79 101
102 Key::~Key()
103 {
104 }
105
80 Key::Key(const WebKit::WebCryptoKey& key) 106 Key::Key(const WebKit::WebCryptoKey& key)
81 : m_key(key) 107 : m_key(key)
82 { 108 {
83 ScriptWrappable::init(this); 109 ScriptWrappable::init(this);
84 } 110 }
85 111
86 String Key::type() const 112 String Key::type() const
87 { 113 {
88 return ASCIILiteral(keyTypeToString(m_key.type())); 114 return ASCIILiteral(keyTypeToString(m_key.type()));
89 } 115 }
(...skipping 20 matching lines...) Expand all
110 136
111 // The WebCryptoKeyUsage values are consecutive powers of 2. Test each one i n order. 137 // The WebCryptoKeyUsage values are consecutive powers of 2. Test each one i n order.
112 for (int i = 0; (1 << i) < WebKit::EndOfWebCryptoKeyUsage; ++i) { 138 for (int i = 0; (1 << i) < WebKit::EndOfWebCryptoKeyUsage; ++i) {
113 WebKit::WebCryptoKeyUsage usage = static_cast<WebKit::WebCryptoKeyUsage> (1 << i); 139 WebKit::WebCryptoKeyUsage usage = static_cast<WebKit::WebCryptoKeyUsage> (1 << i);
114 if (m_key.usages() & usage) 140 if (m_key.usages() & usage)
115 result.append(ASCIILiteral(keyUsageToString(usage))); 141 result.append(ASCIILiteral(keyUsageToString(usage)));
116 } 142 }
117 return result; 143 return result;
118 } 144 }
119 145
146 bool Key::parseFormat(const String& formatString, WebKit::WebCryptoKeyFormat& fo rmat)
147 {
148 // There are few enough values that testing serially is fast enough.
149 if (formatString == "raw") {
150 format = WebKit::WebCryptoKeyFormatRaw;
151 return true;
152 }
153 if (formatString == "pkcs8") {
154 format = WebKit::WebCryptoKeyFormatPkcs8;
155 return true;
156 }
157 if (formatString == "spki") {
158 format = WebKit::WebCryptoKeyFormatSpki;
159 return true;
160 }
161 if (formatString == "jwk") {
162 format = WebKit::WebCryptoKeyFormatJwk;
163 return true;
164 }
165
166 return false;
167 }
168
169 bool Key::parseUsageMask(const Vector<String>& usages, WebKit::WebCryptoKeyUsage Mask& mask)
170 {
171 mask = 0;
172 for (size_t i = 0; i < usages.size(); ++i) {
173 WebKit::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
174 if (!usage)
175 return false;
176 mask |= usage;
177 }
178 return true;
179 }
180
120 } // namespace WebCore 181 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698