|
21 | 21 | #include <botan/pkcs8.h> |
22 | 22 | #include <botan/pubkey.h> |
23 | 23 | #include <botan/x509_key.h> |
| 24 | + #include <botan/internal/pk_options.h> |
24 | 25 | #include <botan/internal/workfactor.h> |
25 | 26 |
|
26 | 27 | #include <fstream> |
27 | | - #include <sstream> |
28 | 28 |
|
29 | 29 | #if defined(BOTAN_HAS_DL_GROUP) |
30 | 30 | #include <botan/dl_group.h> |
@@ -97,26 +97,17 @@ BOTAN_REGISTER_COMMAND("keygen", PK_Keygen); |
97 | 97 |
|
98 | 98 | namespace { |
99 | 99 |
|
100 | | -std::string choose_sig_padding(const std::string& key, const std::string& padding, const std::string& hash) { |
101 | | - if(key == "RSA") { |
102 | | - std::ostringstream oss; |
103 | | - if(padding.empty()) { |
104 | | - oss << "PSS"; |
105 | | - } else { |
106 | | - oss << padding; |
107 | | - } |
108 | | - |
109 | | - oss << "(" << hash << ")"; |
110 | | - return oss.str(); |
111 | | - } else if(padding.empty()) { |
112 | | - return hash; |
113 | | - } else if(hash.empty()) { |
114 | | - return padding; |
115 | | - } else { |
116 | | - std::ostringstream oss; |
117 | | - oss << padding << "(" << hash << ")"; |
118 | | - return oss.str(); |
| 100 | +Botan::PK_Signature_Options sig_options( |
| 101 | + std::string_view key, std::string_view padding, std::string_view hash, bool use_der, std::string_view provider) { |
| 102 | + if(key == "RSA" && padding.empty()) { |
| 103 | + return sig_options(key, "PSS", hash, use_der, provider); |
119 | 104 | } |
| 105 | + |
| 106 | + return Botan::PK_Signature_Options() |
| 107 | + .with_hash(hash) |
| 108 | + .with_padding(padding) |
| 109 | + .with_der_encoded_signature(use_der) |
| 110 | + .with_provider(provider); |
120 | 111 | } |
121 | 112 |
|
122 | 113 | } // namespace |
@@ -196,21 +187,14 @@ class PK_Sign final : public Command { |
196 | 187 | throw CLI_Error_Unsupported("hashing", hash_fn); |
197 | 188 | } |
198 | 189 |
|
199 | | - const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn); |
200 | | - |
201 | | - auto format = Botan::Signature_Format::Standard; |
202 | | - |
203 | | - if(flag_set("der-format")) { |
204 | | - if(!key->_signature_element_size_for_DER_encoding()) { |
205 | | - throw CLI_Usage_Error("Key type " + key->algo_name() + |
206 | | - " does not support DER formatting for signatures"); |
207 | | - } |
208 | | - format = Botan::Signature_Format::DerSequence; |
| 190 | + if(flag_set("der-format") && !key->_signature_element_size_for_DER_encoding()) { |
| 191 | + throw CLI_Usage_Error("Key type " + key->algo_name() + " does not support DER formatting for signatures"); |
209 | 192 | } |
210 | 193 |
|
211 | | - const std::string provider = get_arg("provider"); |
| 194 | + const auto options = |
| 195 | + sig_options(key->algo_name(), get_arg("padding"), hash_fn, flag_set("der-format"), get_arg("provider")); |
212 | 196 |
|
213 | | - Botan::PK_Signer signer(*key, rng(), sig_padding, format, provider); |
| 197 | + Botan::PK_Signer signer(*key, rng(), options); |
214 | 198 |
|
215 | 199 | auto onData = [&signer](const uint8_t b[], size_t l) { signer.update(b, l); }; |
216 | 200 | Command::read_file(get_arg("file"), onData); |
@@ -254,18 +238,9 @@ class PK_Verify final : public Command { |
254 | 238 | throw CLI_Error_Unsupported("hashing", hash_fn); |
255 | 239 | } |
256 | 240 |
|
257 | | - const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn); |
258 | | - |
259 | | - auto format = Botan::Signature_Format::Standard; |
260 | | - if(flag_set("der-format")) { |
261 | | - if(key->message_parts() == 1) { |
262 | | - throw CLI_Usage_Error("Key type " + key->algo_name() + |
263 | | - " does not support DER formatting for signatures"); |
264 | | - } |
265 | | - format = Botan::Signature_Format::DerSequence; |
266 | | - } |
| 241 | + const auto options = sig_options(key->algo_name(), get_arg("padding"), hash_fn, flag_set("der-format"), ""); |
267 | 242 |
|
268 | | - Botan::PK_Verifier verifier(*key, sig_padding, format); |
| 243 | + Botan::PK_Verifier verifier(*key, options); |
269 | 244 | auto onData = [&verifier](const uint8_t b[], size_t l) { verifier.update(b, l); }; |
270 | 245 | Command::read_file(get_arg("file"), onData); |
271 | 246 |
|
|
0 commit comments