OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/devtools/adb/android_rsa.h" | 5 #include "chrome/browser/devtools/adb/android_rsa.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/browser/prefs/pref_service_syncable.h" | 9 #include "chrome/browser/prefs/pref_service_syncable.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 if (pq) | 189 if (pq) |
190 *pq = q; | 190 *pq = q; |
191 else | 191 else |
192 BnFree(q); | 192 BnFree(q); |
193 if (pr) | 193 if (pr) |
194 *pr = ca; | 194 *pr = ca; |
195 else | 195 else |
196 BnFree(ca); | 196 BnFree(ca); |
197 } | 197 } |
198 | 198 |
199 crypto::RSAPrivateKey* GetOrCreatePrivateKey(Profile* profile) { | 199 } // namespace |
| 200 |
| 201 crypto::RSAPrivateKey* AndroidRSAPrivateKey(Profile* profile) { |
200 std::string encoded_key = | 202 std::string encoded_key = |
201 profile->GetPrefs()->GetString(prefs::kDevToolsAdbKey); | 203 profile->GetPrefs()->GetString(prefs::kDevToolsAdbKey); |
202 std::string decoded_key; | 204 std::string decoded_key; |
203 scoped_ptr<crypto::RSAPrivateKey> key; | 205 scoped_ptr<crypto::RSAPrivateKey> key; |
204 if (!encoded_key.empty() && base::Base64Decode(encoded_key, &decoded_key)) { | 206 if (!encoded_key.empty() && base::Base64Decode(encoded_key, &decoded_key)) { |
205 std::vector<uint8> key_info(decoded_key.begin(), decoded_key.end()); | 207 std::vector<uint8> key_info(decoded_key.begin(), decoded_key.end()); |
206 key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); | 208 key.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
207 } | 209 } |
208 if (!key) { | 210 if (!key) { |
209 key.reset(crypto::RSAPrivateKey::Create(2048)); | 211 key.reset(crypto::RSAPrivateKey::Create(2048)); |
210 std::vector<uint8> key_info; | 212 std::vector<uint8> key_info; |
211 if (!key || !key->ExportPrivateKey(&key_info)) | 213 if (!key || !key->ExportPrivateKey(&key_info)) |
212 return NULL; | 214 return NULL; |
213 | 215 |
214 std::string key_string(key_info.begin(), key_info.end()); | 216 std::string key_string(key_info.begin(), key_info.end()); |
215 if (base::Base64Encode(key_string, &encoded_key)) { | 217 if (base::Base64Encode(key_string, &encoded_key)) { |
216 profile->GetPrefs()->SetString(prefs::kDevToolsAdbKey, | 218 profile->GetPrefs()->SetString(prefs::kDevToolsAdbKey, |
217 encoded_key); | 219 encoded_key); |
218 } | 220 } |
219 } | 221 } |
220 return key.release(); | 222 return key.release(); |
221 } | 223 } |
222 | 224 |
223 } // namespace | 225 std::string AndroidRSAPublicKey(crypto::RSAPrivateKey* key) { |
224 | 226 std::vector<uint8> public_key; |
225 std::string AndroidRSAPublicKey(Profile* profile) { | |
226 scoped_ptr<crypto::RSAPrivateKey> key(GetOrCreatePrivateKey(profile)); | |
227 if (!key) | 227 if (!key) |
228 return kDummyRSAPublicKey; | 228 return kDummyRSAPublicKey; |
229 | 229 |
230 std::vector<uint8> public_key; | |
231 key->ExportPublicKey(&public_key); | 230 key->ExportPublicKey(&public_key); |
232 std::string asn1(public_key.begin(), public_key.end()); | 231 std::string asn1(public_key.begin(), public_key.end()); |
233 | 232 |
234 base::StringPiece pk; | 233 base::StringPiece pk; |
235 if (!net::asn1::ExtractSubjectPublicKeyFromSPKI(asn1, &pk)) | 234 if (!net::asn1::ExtractSubjectPublicKeyFromSPKI(asn1, &pk)) |
236 return kDummyRSAPublicKey; | 235 return kDummyRSAPublicKey; |
237 | 236 |
238 // Skip 10 byte asn1 prefix to the modulus. | 237 // Skip 10 byte asn1 prefix to the modulus. |
239 std::vector<uint8> pk_data(pk.data() + 10, pk.data() + pk.length()); | 238 std::vector<uint8> pk_data(pk.data() + 10, pk.data() + pk.length()); |
240 uint32* n = BnNew(); | 239 uint32* n = BnNew(); |
(...skipping 30 matching lines...) Expand all Loading... |
271 BnFree(n); | 270 BnFree(n); |
272 BnFree(r); | 271 BnFree(r); |
273 BnFree(rr); | 272 BnFree(rr); |
274 | 273 |
275 std::string output; | 274 std::string output; |
276 std::string input(reinterpret_cast<char*>(&pkey), sizeof(pkey)); | 275 std::string input(reinterpret_cast<char*>(&pkey), sizeof(pkey)); |
277 base::Base64Encode(input, &output); | 276 base::Base64Encode(input, &output); |
278 return output; | 277 return output; |
279 } | 278 } |
280 | 279 |
281 std::string AndroidRSASign(Profile* profile, const std::string& body) { | 280 std::string AndroidRSASign(crypto::RSAPrivateKey* key, |
282 scoped_ptr<crypto::RSAPrivateKey> key(GetOrCreatePrivateKey(profile)); | 281 const std::string& body) { |
283 if (!key) | |
284 return std::string(); | |
285 | |
286 std::vector<uint8> digest(body.begin(), body.end()); | 282 std::vector<uint8> digest(body.begin(), body.end()); |
287 std::vector<uint8> result; | 283 std::vector<uint8> result; |
288 if (!crypto::SignatureCreator::Sign(key.get(), vector_as_array(&digest), | 284 if (!crypto::SignatureCreator::Sign(key, vector_as_array(&digest), |
289 digest.size(), &result)) { | 285 digest.size(), &result)) { |
290 return std::string(); | 286 return std::string(); |
291 } | 287 } |
292 return std::string(result.begin(), result.end()); | 288 return std::string(result.begin(), result.end()); |
293 } | 289 } |
OLD | NEW |