| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS 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 "update_engine/payload_signer.h" | 5 #include "update_engine/payload_signer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include <base/logging.h> |
| 8 #include "base/string_util.h" | 8 #include <base/string_util.h> |
| 9 #include <openssl/pem.h> |
| 10 |
| 9 #include "update_engine/omaha_hash_calculator.h" | 11 #include "update_engine/omaha_hash_calculator.h" |
| 10 #include "update_engine/subprocess.h" | 12 #include "update_engine/subprocess.h" |
| 11 #include "update_engine/update_metadata.pb.h" | 13 #include "update_engine/update_metadata.pb.h" |
| 12 #include "update_engine/utils.h" | 14 #include "update_engine/utils.h" |
| 13 | 15 |
| 14 using std::string; | 16 using std::string; |
| 15 using std::vector; | 17 using std::vector; |
| 16 | 18 |
| 17 namespace chromeos_update_engine { | 19 namespace chromeos_update_engine { |
| 18 | 20 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 for (; sig_index < signatures.signatures_size(); sig_index++) { | 110 for (; sig_index < signatures.signatures_size(); sig_index++) { |
| 109 const Signatures_Signature& signature = signatures.signatures(sig_index); | 111 const Signatures_Signature& signature = signatures.signatures(sig_index); |
| 110 if (signature.has_version() && | 112 if (signature.has_version() && |
| 111 signature.version() == kSignatureMessageVersion) { | 113 signature.version() == kSignatureMessageVersion) { |
| 112 break; | 114 break; |
| 113 } | 115 } |
| 114 } | 116 } |
| 115 TEST_AND_RETURN_FALSE(sig_index < signatures.signatures_size()); | 117 TEST_AND_RETURN_FALSE(sig_index < signatures.signatures_size()); |
| 116 | 118 |
| 117 const Signatures_Signature& signature = signatures.signatures(sig_index); | 119 const Signatures_Signature& signature = signatures.signatures(sig_index); |
| 118 const string sig_data = signature.data(); | 120 const string& sig_data = signature.data(); |
| 119 string sig_path; | |
| 120 TEST_AND_RETURN_FALSE( | |
| 121 utils::MakeTempFile("/var/run/signature.XXXXXX", &sig_path, NULL)); | |
| 122 ScopedPathUnlinker sig_path_unlinker(sig_path); | |
| 123 TEST_AND_RETURN_FALSE(utils::WriteFile(sig_path.c_str(), | |
| 124 &sig_data[0], | |
| 125 sig_data.size())); | |
| 126 string hash_path; | |
| 127 TEST_AND_RETURN_FALSE( | |
| 128 utils::MakeTempFile("/var/run/hash.XXXXXX", &hash_path, NULL)); | |
| 129 ScopedPathUnlinker hash_path_unlinker(hash_path); | |
| 130 | 121 |
| 131 // TODO(petkov): This runs on the client so it will be cleaner if it uses | 122 // The code below executes the equivalent of: |
| 132 // direct openssl library calls. | 123 // |
| 133 vector<string> cmd; | 124 // openssl rsautl -verify -pubin -inkey |public_key_path| |
| 134 SplitString("/usr/bin/openssl rsautl -verify -pubin -inkey x -in x -out x", | 125 // -in |sig_data| -out |out_hash_data| |
| 135 ' ', | |
| 136 &cmd); | |
| 137 cmd[cmd.size() - 5] = public_key_path; | |
| 138 cmd[cmd.size() - 3] = sig_path; | |
| 139 cmd[cmd.size() - 1] = hash_path; | |
| 140 | 126 |
| 141 int return_code = 0; | 127 // Loads the public key. |
| 142 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code)); | 128 FILE* fpubkey = fopen(public_key_path.c_str(), "rb"); |
| 143 TEST_AND_RETURN_FALSE(return_code == 0); | 129 TEST_AND_RETURN_FALSE(fpubkey != NULL); |
| 130 char dummy_password[] = { ' ', 0 }; // Ensure no password is read from stdin. |
| 131 RSA* rsa = PEM_read_RSA_PUBKEY(fpubkey, NULL, NULL, dummy_password); |
| 132 fclose(fpubkey); |
| 133 TEST_AND_RETURN_FALSE(rsa != NULL); |
| 134 unsigned int keysize = RSA_size(rsa); |
| 135 if (sig_data.size() > 2 * keysize) { |
| 136 LOG(ERROR) << "Signature size is too big for public key size."; |
| 137 RSA_free(rsa); |
| 138 return false; |
| 139 } |
| 144 | 140 |
| 145 TEST_AND_RETURN_FALSE(utils::ReadFile(hash_path, out_hash_data)); | 141 // Decrypts the signature. |
| 142 vector<char> hash_data(keysize); |
| 143 int decrypt_size = RSA_public_decrypt( |
| 144 sig_data.size(), |
| 145 reinterpret_cast<const unsigned char*>(sig_data.data()), |
| 146 reinterpret_cast<unsigned char*>(hash_data.data()), |
| 147 rsa, |
| 148 RSA_PKCS1_PADDING); |
| 149 RSA_free(rsa); |
| 150 TEST_AND_RETURN_FALSE(decrypt_size > 0 && |
| 151 decrypt_size <= static_cast<int>(hash_data.size())); |
| 152 hash_data.resize(decrypt_size); |
| 153 out_hash_data->swap(hash_data); |
| 146 return true; | 154 return true; |
| 147 } | 155 } |
| 148 | 156 |
| 149 } // namespace chromeos_update_engine | 157 } // namespace chromeos_update_engine |
| OLD | NEW |