| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/rsa_private_key.h" | 5 #include "crypto/rsa_private_key.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 // RSA algorithm OID | 75 // RSA algorithm OID |
| 76 for (size_t i = sizeof(kRsaAlgorithmIdentifier); i > 0; --i) | 76 for (size_t i = sizeof(kRsaAlgorithmIdentifier); i > 0; --i) |
| 77 content.push_front(kRsaAlgorithmIdentifier[i - 1]); | 77 content.push_front(kRsaAlgorithmIdentifier[i - 1]); |
| 78 | 78 |
| 79 PrependInteger(&version, 1, &content); | 79 PrependInteger(&version, 1, &content); |
| 80 PrependTypeHeaderAndLength(kSequenceTag, content.size(), &content); | 80 PrependTypeHeaderAndLength(kSequenceTag, content.size(), &content); |
| 81 | 81 |
| 82 // Copy everying into the output. | 82 // Copy everying into the output. |
| 83 output->reserve(content.size()); | 83 output->reserve(content.size()); |
| 84 for (std::list<uint8>::iterator i = content.begin(); i != content.end(); ++i) | 84 output->assign(content.begin(), content.end()); |
| 85 output->push_back(*i); | |
| 86 | 85 |
| 87 return true; | 86 return true; |
| 88 } | 87 } |
| 89 | 88 |
| 90 bool PrivateKeyInfoCodec::ExportPublicKeyInfo(std::vector<uint8>* output) { | 89 bool PrivateKeyInfoCodec::ExportPublicKeyInfo(std::vector<uint8>* output) { |
| 91 // Create a sequence with the modulus (n) and public exponent (e). | 90 // Create a sequence with the modulus (n) and public exponent (e). |
| 92 std::vector<uint8> bit_string; | 91 std::vector<uint8> bit_string; |
| 93 if (!ExportPublicKey(&bit_string)) | 92 if (!ExportPublicKey(&bit_string)) |
| 94 return false; | 93 return false; |
| 95 | 94 |
| 96 // Add the sequence as the contents of a bit string. | 95 // Add the sequence as the contents of a bit string. |
| 97 std::list<uint8> content; | 96 std::list<uint8> content; |
| 98 PrependBitString(&bit_string[0], static_cast<int>(bit_string.size()), | 97 PrependBitString(&bit_string[0], static_cast<int>(bit_string.size()), |
| 99 &content); | 98 &content); |
| 100 | 99 |
| 101 // Add the RSA algorithm OID. | 100 // Add the RSA algorithm OID. |
| 102 for (size_t i = sizeof(kRsaAlgorithmIdentifier); i > 0; --i) | 101 for (size_t i = sizeof(kRsaAlgorithmIdentifier); i > 0; --i) |
| 103 content.push_front(kRsaAlgorithmIdentifier[i - 1]); | 102 content.push_front(kRsaAlgorithmIdentifier[i - 1]); |
| 104 | 103 |
| 105 // Finally, wrap everything in a sequence. | 104 // Finally, wrap everything in a sequence. |
| 106 PrependTypeHeaderAndLength(kSequenceTag, content.size(), &content); | 105 PrependTypeHeaderAndLength(kSequenceTag, content.size(), &content); |
| 107 | 106 |
| 108 // Copy everything into the output. | 107 // Copy everything into the output. |
| 109 output->reserve(content.size()); | 108 output->reserve(content.size()); |
| 110 for (std::list<uint8>::iterator i = content.begin(); i != content.end(); ++i) | 109 output->assign(content.begin(), content.end()); |
| 111 output->push_back(*i); | |
| 112 | 110 |
| 113 return true; | 111 return true; |
| 114 } | 112 } |
| 115 | 113 |
| 116 bool PrivateKeyInfoCodec::ExportPublicKey(std::vector<uint8>* output) { | 114 bool PrivateKeyInfoCodec::ExportPublicKey(std::vector<uint8>* output) { |
| 117 // Create a sequence with the modulus (n) and public exponent (e). | 115 // Create a sequence with the modulus (n) and public exponent (e). |
| 118 std::list<uint8> content; | 116 std::list<uint8> content; |
| 119 PrependInteger(&public_exponent_[0], | 117 PrependInteger(&public_exponent_[0], |
| 120 static_cast<int>(public_exponent_.size()), | 118 static_cast<int>(public_exponent_.size()), |
| 121 &content); | 119 &content); |
| 122 PrependInteger(&modulus_[0], static_cast<int>(modulus_.size()), &content); | 120 PrependInteger(&modulus_[0], static_cast<int>(modulus_.size()), &content); |
| 123 PrependTypeHeaderAndLength(kSequenceTag, content.size(), &content); | 121 PrependTypeHeaderAndLength(kSequenceTag, content.size(), &content); |
| 124 | 122 |
| 125 // Copy everything into the output. | 123 // Copy everything into the output. |
| 126 output->reserve(content.size()); | 124 output->reserve(content.size()); |
| 127 for (std::list<uint8>::iterator i = content.begin(); i != content.end(); ++i) | 125 output->assign(content.begin(), content.end()); |
| 128 output->push_back(*i); | |
| 129 | 126 |
| 130 return true; | 127 return true; |
| 131 } | 128 } |
| 132 | 129 |
| 133 bool PrivateKeyInfoCodec::Import(const std::vector<uint8>& input) { | 130 bool PrivateKeyInfoCodec::Import(const std::vector<uint8>& input) { |
| 134 if (input.empty()) { | 131 if (input.empty()) { |
| 135 return false; | 132 return false; |
| 136 } | 133 } |
| 137 | 134 |
| 138 // Parse the private key info up to the public key values, ignoring | 135 // Parse the private key info up to the public key values, ignoring |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 int pad = expected_size - temp.size(); | 228 int pad = expected_size - temp.size(); |
| 232 int index = 0; | 229 int index = 0; |
| 233 if (out->size() == expected_size + 1) { | 230 if (out->size() == expected_size + 1) { |
| 234 READ_ASSERT(out->front() == 0x00); | 231 READ_ASSERT(out->front() == 0x00); |
| 235 pad++; | 232 pad++; |
| 236 index++; | 233 index++; |
| 237 } else { | 234 } else { |
| 238 READ_ASSERT(out->size() <= expected_size); | 235 READ_ASSERT(out->size() <= expected_size); |
| 239 } | 236 } |
| 240 | 237 |
| 241 while (pad) { | 238 out->insert(out->end(), pad, 0x00); |
| 242 out->push_back(0x00); | |
| 243 pad--; | |
| 244 } | |
| 245 out->insert(out->end(), temp.begin(), temp.end()); | 239 out->insert(out->end(), temp.begin(), temp.end()); |
| 246 | 240 |
| 247 // Reverse output if little-endian. | 241 // Reverse output if little-endian. |
| 248 if (!big_endian_) | 242 if (!big_endian_) |
| 249 reverse(out->begin(), out->end()); | 243 reverse(out->begin(), out->end()); |
| 250 return true; | 244 return true; |
| 251 } | 245 } |
| 252 | 246 |
| 253 bool PrivateKeyInfoCodec::ReadIntegerImpl(uint8** pos, | 247 bool PrivateKeyInfoCodec::ReadIntegerImpl(uint8** pos, |
| 254 uint8* end, | 248 uint8* end, |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 // The version should be zero. | 375 // The version should be zero. |
| 382 for (uint32 i = 0; i < length; ++i) { | 376 for (uint32 i = 0; i < length; ++i) { |
| 383 READ_ASSERT(**pos == 0x00); | 377 READ_ASSERT(**pos == 0x00); |
| 384 (*pos)++; | 378 (*pos)++; |
| 385 } | 379 } |
| 386 | 380 |
| 387 return true; | 381 return true; |
| 388 } | 382 } |
| 389 | 383 |
| 390 } // namespace crypto | 384 } // namespace crypto |
| OLD | NEW |