| 611 | } |
| 612 | |
| 613 | static bool decrypt_repo_key (Key_file& key_file, const char* key_name, uint32_t key_version, const std::vector<std::string>& secret_keys, const std::string& keys_path) |
| 614 | { |
| 615 | std::exception_ptr gpg_error; |
| 616 | |
| 617 | for (std::vector<std::string>::const_iterator seckey(secret_keys.begin()); seckey != secret_keys.end(); ++seckey) { |
| 618 | std::ostringstream path_builder; |
| 619 | path_builder << keys_path << '/' << (key_name ? key_name : "default") << '/' << key_version << '/' << *seckey << ".gpg"; |
| 620 | std::string path(path_builder.str()); |
| 621 | if (access(path.c_str(), F_OK) == 0) { |
| 622 | std::stringstream decrypted_contents; |
| 623 | try { |
| 624 | gpg_decrypt_from_file(path, decrypted_contents); |
| 625 | } catch (const Gpg_error&) { |
| 626 | gpg_error = std::current_exception(); |
| 627 | continue; |
| 628 | } |
| 629 | Key_file this_version_key_file; |
| 630 | this_version_key_file.load(decrypted_contents); |
| 631 | const Key_file::Entry* this_version_entry = this_version_key_file.get(key_version); |
| 632 | if (!this_version_entry) { |
| 633 | throw Error("GPG-encrypted keyfile is malformed because it does not contain expected key version"); |
| 634 | } |
| 635 | if (!same_key_name(key_name, this_version_key_file.get_key_name())) { |
| 636 | throw Error("GPG-encrypted keyfile is malformed because it does not contain expected key name"); |
| 637 | } |
| 638 | key_file.set_key_name(key_name); |
| 639 | key_file.add(*this_version_entry); |
| 640 | return true; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (gpg_error) { |
| 645 | std::rethrow_exception(gpg_error); |
| 646 | } |
| 647 | |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | static bool decrypt_repo_keys (std::vector<Key_file>& key_files, uint32_t key_version, const std::vector<std::string>& secret_keys, const std::string& keys_path) |
| 652 | { |
no test coverage detected