| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/utility/importer/nss_decryptor.h" | 5 #include "chrome/utility/importer/nss_decryptor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 * the provisions above, a recipient may use your version of this file under | 59 * the provisions above, a recipient may use your version of this file under |
| 60 * the terms of any one of the MPL, the GPL or the LGPL. | 60 * the terms of any one of the MPL, the GPL or the LGPL. |
| 61 * | 61 * |
| 62 * ***** END LICENSE BLOCK ***** */ | 62 * ***** END LICENSE BLOCK ***** */ |
| 63 | 63 |
| 64 base::string16 NSSDecryptor::Decrypt(const std::string& crypt) const { | 64 base::string16 NSSDecryptor::Decrypt(const std::string& crypt) const { |
| 65 // Do nothing if NSS is not loaded. | 65 // Do nothing if NSS is not loaded. |
| 66 if (!is_nss_initialized_) | 66 if (!is_nss_initialized_) |
| 67 return base::string16(); | 67 return base::string16(); |
| 68 | 68 |
| 69 if (crypt.empty()) |
| 70 return base::string16(); |
| 71 |
| 69 // The old style password is encoded in base64. They are identified | 72 // The old style password is encoded in base64. They are identified |
| 70 // by a leading '~'. Otherwise, we should decrypt the text. | 73 // by a leading '~'. Otherwise, we should decrypt the text. |
| 71 std::string plain; | 74 std::string plain; |
| 72 if (crypt[0] != '~') { | 75 if (crypt[0] != '~') { |
| 73 std::string decoded_data; | 76 std::string decoded_data; |
| 74 base::Base64Decode(crypt, &decoded_data); | 77 if (!base::Base64Decode(crypt, &decoded_data)) |
| 78 return base::string16(); |
| 75 PK11SlotInfo* slot = GetKeySlotForDB(); | 79 PK11SlotInfo* slot = GetKeySlotForDB(); |
| 76 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL); | 80 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL); |
| 77 if (result != SECSuccess) { | 81 if (result != SECSuccess) { |
| 78 FreeSlot(slot); | 82 FreeSlot(slot); |
| 79 return base::string16(); | 83 return base::string16(); |
| 80 } | 84 } |
| 81 | 85 |
| 82 SECItem request; | 86 SECItem request; |
| 83 request.data = reinterpret_cast<unsigned char*>( | 87 request.data = reinterpret_cast<unsigned char*>( |
| 84 const_cast<char*>(decoded_data.data())); | 88 const_cast<char*>(decoded_data.data())); |
| 85 request.len = static_cast<unsigned int>(decoded_data.size()); | 89 request.len = static_cast<unsigned int>(decoded_data.size()); |
| 86 SECItem reply; | 90 SECItem reply; |
| 87 reply.data = NULL; | 91 reply.data = NULL; |
| 88 reply.len = 0; | 92 reply.len = 0; |
| 89 #if defined(USE_NSS) | 93 #if defined(USE_NSS) |
| 90 result = PK11SDR_DecryptWithSlot(slot, &request, &reply, NULL); | 94 result = PK11SDR_DecryptWithSlot(slot, &request, &reply, NULL); |
| 91 #else | 95 #else |
| 92 result = PK11SDR_Decrypt(&request, &reply, NULL); | 96 result = PK11SDR_Decrypt(&request, &reply, NULL); |
| 93 #endif // defined(USE_NSS) | 97 #endif // defined(USE_NSS) |
| 94 if (result == SECSuccess) | 98 if (result == SECSuccess) |
| 95 plain.assign(reinterpret_cast<char*>(reply.data), reply.len); | 99 plain.assign(reinterpret_cast<char*>(reply.data), reply.len); |
| 96 | 100 |
| 97 SECITEM_FreeItem(&reply, PR_FALSE); | 101 SECITEM_FreeItem(&reply, PR_FALSE); |
| 98 FreeSlot(slot); | 102 FreeSlot(slot); |
| 99 } else { | 103 } else { |
| 100 // Deletes the leading '~' before decoding. | 104 // Deletes the leading '~' before decoding. |
| 101 base::Base64Decode(crypt.substr(1), &plain); | 105 if (!base::Base64Decode(crypt.substr(1), &plain)) |
| 106 return base::string16(); |
| 102 } | 107 } |
| 103 | 108 |
| 104 return base::UTF8ToUTF16(plain); | 109 return base::UTF8ToUTF16(plain); |
| 105 } | 110 } |
| 106 | 111 |
| 107 // There are three versions of password files. They store saved user | 112 // There are three versions of password files. They store saved user |
| 108 // names and passwords. | 113 // names and passwords. |
| 109 // References: | 114 // References: |
| 110 // http://kb.mozillazine.org/Signons.txt | 115 // http://kb.mozillazine.org/Signons.txt |
| 111 // http://kb.mozillazine.org/Signons2.txt | 116 // http://kb.mozillazine.org/Signons2.txt |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 // The user name, password and action. | 300 // The user name, password and action. |
| 296 form.username_element = s2.ColumnString16(3); | 301 form.username_element = s2.ColumnString16(3); |
| 297 form.username_value = Decrypt(s2.ColumnString(5)); | 302 form.username_value = Decrypt(s2.ColumnString(5)); |
| 298 form.password_element = s2.ColumnString16(4); | 303 form.password_element = s2.ColumnString16(4); |
| 299 form.password_value = Decrypt(s2.ColumnString(6)); | 304 form.password_value = Decrypt(s2.ColumnString(6)); |
| 300 form.action = GURL(s2.ColumnString(2)).ReplaceComponents(rep); | 305 form.action = GURL(s2.ColumnString(2)).ReplaceComponents(rep); |
| 301 forms->push_back(form); | 306 forms->push_back(form); |
| 302 } | 307 } |
| 303 return true; | 308 return true; |
| 304 } | 309 } |
| OLD | NEW |