8#include <botan/x509_obj.h>
9#include <botan/pubkey.h>
10#include <botan/der_enc.h>
11#include <botan/ber_dec.h>
13#include <botan/internal/emsa.h>
14#include <botan/internal/fmt.h>
33 std::string got_label;
38 bool is_alternate =
false;
41 if(got_label == alt_label)
81 .raw_bytes(m_tbs_bits)
115std::pair<Certificate_Status_Code, std::string>
153 std::vector<uint8_t> output;
166std::string x509_signature_padding_for(
167 const std::string& algo_name,
168 std::string_view hash_fn,
169 std::string_view user_specified_padding)
171 if(algo_name ==
"DSA" ||
172 algo_name ==
"ECDSA" ||
173 algo_name ==
"ECGDSA" ||
174 algo_name ==
"ECKCDSA" ||
175 algo_name ==
"GOST-34.10" ||
176 algo_name ==
"GOST-34.10-2012-256" ||
177 algo_name ==
"GOST-34.10-2012-512")
179 BOTAN_ARG_CHECK(user_specified_padding.empty() || user_specified_padding ==
"EMSA1",
180 "Invalid padding scheme for DSA-like scheme");
182 return hash_fn.empty() ?
"SHA-256" : std::string(hash_fn);
184 else if(algo_name ==
"RSA")
188 if(user_specified_padding.empty())
191 return "EMSA3(SHA-256)";
193 return fmt(
"EMSA3({})", hash_fn);
198 return fmt(
"{}(SHA-256)", user_specified_padding);
200 return fmt(
"{}({})", user_specified_padding, hash_fn);
203 else if(algo_name ==
"Ed25519")
205 return user_specified_padding.empty() ?
"Pure" : std::string(user_specified_padding);
207 else if(algo_name.starts_with(
"Dilithium-"))
209 return user_specified_padding.empty() ?
"Randomized" : std::string(user_specified_padding);
213 throw Invalid_Argument(
"Unknown X.509 signing key type: " + algo_name);
217std::string format_padding_error_message(std::string_view key_name,
218 std::string_view signer_hash_fn,
219 std::string_view user_hash_fn,
220 std::string_view chosen_padding,
221 std::string_view user_specified_padding)
223 std::ostringstream oss;
225 oss <<
"Specified hash function " << user_hash_fn
226 <<
" is incompatible with " << key_name;
228 if(!signer_hash_fn.empty())
229 oss <<
" chose hash function " << signer_hash_fn;
231 if(!chosen_padding.empty())
232 oss <<
" chose padding " << chosen_padding;
233 if(!user_specified_padding.empty())
234 oss <<
" with user specified padding " << user_specified_padding;
247 std::string_view hash_fn,
248 std::string_view user_specified_padding)
252 if(!user_specified_padding.empty())
256 auto pk_signer = std::make_unique<PK_Signer>(key, rng, user_specified_padding, format);
257 if(!hash_fn.empty() && pk_signer->hash_function() != hash_fn)
260 format_padding_error_message(key.
algo_name(), pk_signer->hash_function(),
261 hash_fn,
"", user_specified_padding)
269 const std::string padding = x509_signature_padding_for(key.
algo_name(), hash_fn, user_specified_padding);
273 auto pk_signer = std::make_unique<PK_Signer>(key, rng, padding, format);
274 if(!hash_fn.empty() && pk_signer->hash_function() != hash_fn)
277 format_padding_error_message(key.
algo_name(), pk_signer->hash_function(),
278 hash_fn, padding, user_specified_padding));
#define BOTAN_ARG_CHECK(expr, msg)
std::vector< uint8_t > BER_encode() const
virtual std::string algo_name() const =0
BER_Decoder & decode(bool &out)
BER_Decoder start_sequence()
DER_Encoder & start_sequence()
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
DER_Encoder & encode(bool b)
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
std::string hash_function() const
virtual Signature_Format default_x509_signature_format() const
const std::vector< uint8_t > & signed_body() const
void decode_from(BER_Decoder &from) override
std::string PEM_encode() const
const AlgorithmIdentifier & signature_algorithm() const
virtual std::vector< std::string > alternate_PEM_labels() const
static std::unique_ptr< PK_Signer > choose_sig_format(const Private_Key &key, RandomNumberGenerator &rng, std::string_view hash_fn, std::string_view padding_algo)
std::vector< uint8_t > tbs_data() const
const std::vector< uint8_t > & signature() const
static std::vector< uint8_t > make_signed(PK_Signer &signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &tbs)
void encode_into(DER_Encoder &to) const override
std::pair< Certificate_Status_Code, std::string > verify_signature(const Public_Key &key) const
void load_data(DataSource &src)
virtual std::string PEM_label() const =0
bool check_signature(const Public_Key &key) const
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
bool maybe_BER(DataSource &source)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
bool matches(DataSource &source, std::string_view extra, size_t search_range)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
std::string fmt(std::string_view format, const T &... args)
@ SIGNATURE_ALGO_BAD_PARAMS
std::vector< T, secure_allocator< T > > secure_vector