Botan 3.14.1
Crypto and TLS for C&
Botan::PK_Verifier Class Referencefinal

#include <pubkey.h>

Public Member Functions

bool check_signature (const uint8_t sig[], size_t length)
bool check_signature (std::span< const uint8_t > sig)
std::string hash_function () const
PK_Verifier & operator= (const PK_Verifier &)=delete
PK_Verifier & operator= (PK_Verifier &&) noexcept
 PK_Verifier (const PK_Verifier &)=delete
 PK_Verifier (const Public_Key &pub_key, const AlgorithmIdentifier &signature_algorithm, std::string_view provider="")
 PK_Verifier (const Public_Key &pub_key, const PK_Signature_Options &options=PK_Signature_Options())
 PK_Verifier (const Public_Key &pub_key, std::string_view padding, Signature_Format format=Signature_Format::Standard, std::string_view provider="")
 PK_Verifier (PK_Verifier &&) noexcept
void set_input_format (Signature_Format format)
void update (const uint8_t msg_part[], size_t length)
void update (std::span< const uint8_t > in)
void update (std::string_view in)
void update (uint8_t in)
bool verify_message (const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
bool verify_message (std::span< const uint8_t > msg, std::span< const uint8_t > sig)
 ~PK_Verifier ()

Detailed Description

Public Key Verifier. Use the verify_message() functions for small messages. Use multiple calls update() to process large messages and verify the signature by finally calling check_signature().

Definition at line 308 of file pubkey.h.

Constructor & Destructor Documentation

◆ PK_Verifier() [1/5]

Botan::PK_Verifier::PK_Verifier ( const Public_Key & pub_key,
const PK_Signature_Options & options = PK_Signature_Options() )
explicit

Construct a PK Verifier.

Parameters
pub_keythe public key to verify against
optionsrelating to the signature; schemes without any parameters (eg Ed25519, ML-DSA, XMSS) can be used with the default.

Definition at line 454 of file pubkey.cpp.

454 {
455 const PK_Signature_Options_Reader reader(options, PK_Signature_Options_Reader::Usage::Verification);
456
457 m_op = key._create_verification_op(reader);
458
459 if(!m_op) {
460 throw Invalid_Argument(fmt("Key type {} does not support signature verification", key.algo_name()));
461 }
462
463 m_sig_element_size = key._signature_element_size_for_DER_encoding();
464 m_sig_format = reader.using_der_encoded_signature() ? Signature_Format::DerSequence : Signature_Format::Standard;
465
466 if(m_sig_format == Signature_Format::DerSequence && !m_sig_element_size.has_value()) {
467 throw Invalid_Argument(fmt("Key type {} does not support DER encoded signatures", key.algo_name()));
468 }
469
470 reader.throw_if_unexamined(key.algo_name());
471}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::Public_Key::_create_verification_op(), Botan::Asymmetric_Key::_signature_element_size_for_DER_encoding(), Botan::Asymmetric_Key::algo_name(), Botan::DerSequence, Botan::fmt(), Botan::Standard, and Botan::PK_Signature_Options_Reader::using_der_encoded_signature().

Referenced by hash_function(), operator=(), operator=(), PK_Verifier(), PK_Verifier(), and PK_Verifier().

◆ PK_Verifier() [2/5]

Botan::PK_Verifier::PK_Verifier ( const Public_Key & pub_key,
std::string_view padding,
Signature_Format format = Signature_Format::Standard,
std::string_view provider = "" )

Construct a PK Verifier.

Parameters
pub_keythe public key to verify against
paddingthe padding/hash to use (eg "EMSA_PKCS1(SHA-256)")
formatthe signature format to use
providerthe provider to use

Definition at line 445 of file pubkey.cpp.

448 :
449 PK_Verifier(pub_key,
450 parse_legacy_sig_options(pub_key, padding)
451 .with_der_encoded_signature(format == Signature_Format::DerSequence)
452 .with_provider(provider)) {}
PK_Verifier(const Public_Key &pub_key, const PK_Signature_Options &options=PK_Signature_Options())
Definition pubkey.cpp:454
PK_Signature_Options parse_legacy_sig_options(const Public_Key &key, std::string_view params)

References Botan::parse_legacy_sig_options(), and PK_Verifier().

◆ PK_Verifier() [3/5]

Botan::PK_Verifier::PK_Verifier ( const Public_Key & pub_key,
const AlgorithmIdentifier & signature_algorithm,
std::string_view provider = "" )

Construct a PK Verifier (X.509 specific)

This constructor will attempt to decode signature_format relative to the public key provided. If they seem to be inconsistent or otherwise unsupported, a Decoding_Error is thrown.

Parameters
pub_keythe public key to verify against
signature_algorithmthe supposed signature algorithm
providerthe provider to use

Definition at line 473 of file pubkey.cpp.

475 {
476 m_op = key.create_x509_verification_op(signature_algorithm, provider);
477 if(!m_op) {
478 throw Invalid_Argument(fmt("Key type {} does not support X.509 signature verification", key.algo_name()));
479 }
480
481 m_sig_format = key._default_x509_signature_format();
482 m_sig_element_size = key._signature_element_size_for_DER_encoding();
483}

References Botan::Asymmetric_Key::_default_x509_signature_format(), Botan::Asymmetric_Key::_signature_element_size_for_DER_encoding(), Botan::Asymmetric_Key::algo_name(), Botan::Public_Key::create_x509_verification_op(), and Botan::fmt().

◆ ~PK_Verifier()

Botan::PK_Verifier::~PK_Verifier ( )
default

◆ PK_Verifier() [4/5]

Botan::PK_Verifier::PK_Verifier ( const PK_Verifier & )
delete

References PK_Verifier().

◆ PK_Verifier() [5/5]

Botan::PK_Verifier::PK_Verifier ( PK_Verifier && )
defaultnoexcept

References PK_Verifier().

Member Function Documentation

◆ check_signature() [1/2]

bool Botan::PK_Verifier::check_signature ( const uint8_t sig[],
size_t length )

Check the signature of the buffered message, i.e. the one build by successive calls to update.

Parameters
sigthe signature to be verified as a byte array
lengththe length of the above byte array
Returns
true if the signature is valid, false otherwise

Definition at line 541 of file pubkey.cpp.

541 {
542 try {
543 if(m_sig_format == Signature_Format::Standard) {
544 return m_op->is_valid_signature({sig, length});
545 } else if(m_sig_format == Signature_Format::DerSequence) {
546 bool decoding_success = false;
547 std::vector<uint8_t> real_sig;
548
549 BOTAN_ASSERT_NOMSG(m_sig_element_size.has_value());
550
551 try {
552 real_sig = decode_der_signature_pair({sig, length}, m_sig_element_size.value());
553 decoding_success = true;
554 } catch(...) {}
555
556 // It is critical that is_valid_signature is called even if DER decoding failed, since
557 // that is what resets the internal state (message hashes, etc)
558 const bool accept = m_op->is_valid_signature(real_sig);
559
560 return accept && decoding_success;
561 } else {
562 throw Internal_Error("PK_Verifier: Invalid signature format enum");
563 }
564 } catch(Invalid_Argument&) {
565 return false;
566 } catch(Decoding_Error&) {
567 return false;
568 } catch(Encoding_Error&) {
569 return false;
570 }
571}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82

References BOTAN_ASSERT_NOMSG, Botan::DerSequence, and Botan::Standard.

Referenced by Botan::ed25519_verify(), Botan::Roughtime::Response::validate(), verify_message(), Botan::OCSP::Response::verify_signature(), and Botan::X509_Object::verify_signature().

◆ check_signature() [2/2]

bool Botan::PK_Verifier::check_signature ( std::span< const uint8_t > sig)
inline

Check the signature of the buffered message, i.e. the one build by successive calls to update.

Parameters
sigthe signature to be verified
Returns
true if the signature is valid, false otherwise

Definition at line 416 of file pubkey.h.

416{ return check_signature(sig.data(), sig.size()); }
bool check_signature(const uint8_t sig[], size_t length)
Definition pubkey.cpp:541

References check_signature().

Referenced by check_signature().

◆ hash_function()

std::string Botan::PK_Verifier::hash_function ( ) const

Return the hash function which is being used to verify signatures. This should never return an empty string however it may return a string which does not map directly to a hash function, in particular if "Raw" (unhashed) encoding is being used.

Definition at line 490 of file pubkey.cpp.

490 {
491 return m_op->hash_function();
492}

References hash_function(), and PK_Verifier().

Referenced by hash_function(), Botan::OCSP::Response::verify_signature(), and Botan::X509_Object::verify_signature().

◆ operator=() [1/2]

PK_Verifier & Botan::PK_Verifier::operator= ( const PK_Verifier & )
delete

References PK_Verifier().

◆ operator=() [2/2]

PK_Verifier & Botan::PK_Verifier::operator= ( PK_Verifier && )
defaultnoexcept

References PK_Verifier(), and verify_message().

◆ set_input_format()

void Botan::PK_Verifier::set_input_format ( Signature_Format format)

Set the format of the signatures fed to this verifier.

Parameters
formatthe signature format to use

Definition at line 494 of file pubkey.cpp.

494 {
495 if(format == Signature_Format::DerSequence) {
496 BOTAN_ARG_CHECK(m_sig_element_size.has_value(), "This key does not support DER signatures");
497 }
498 m_sig_format = format;
499}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, and Botan::DerSequence.

◆ update() [1/4]

void Botan::PK_Verifier::update ( const uint8_t msg_part[],
size_t length )

Add a message part of the message corresponding to the signature to be verified.

Parameters
msg_partthe new message part as a byte array
lengththe length of the above byte array

Definition at line 510 of file pubkey.cpp.

510 {
511 m_op->update({in, length});
512}

◆ update() [2/4]

void Botan::PK_Verifier::update ( std::span< const uint8_t > in)
inline

Add a message part of the message corresponding to the signature to be verified.

Parameters
inthe new message part

Definition at line 393 of file pubkey.h.

393{ update(in.data(), in.size()); }
void update(uint8_t in)
Definition pubkey.h:378

References update().

Referenced by update().

◆ update() [3/4]

void Botan::PK_Verifier::update ( std::string_view in)

Add a message part of the message corresponding to the signature to be verified.

Definition at line 506 of file pubkey.cpp.

506 {
507 this->update(as_span_of_bytes(in));
508}
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59

References Botan::as_span_of_bytes(), and update().

◆ update() [4/4]

void Botan::PK_Verifier::update ( uint8_t in)
inline

Add a message part (single byte) of the message corresponding to the signature to be verified.

Parameters
inthe byte to add

Definition at line 378 of file pubkey.h.

378{ update(&in, 1); }

References update().

Referenced by Botan::ed25519_verify(), update(), update(), Botan::Roughtime::Response::validate(), verify_message(), Botan::OCSP::Response::verify_signature(), and Botan::X509_Object::verify_signature().

◆ verify_message() [1/2]

bool Botan::PK_Verifier::verify_message ( const uint8_t msg[],
size_t msg_length,
const uint8_t sig[],
size_t sig_length )

Verify a signature.

Parameters
msgthe message that the signature belongs to, as a byte array
msg_lengththe length of the above byte array msg
sigthe signature as a byte array
sig_lengththe length of the above byte array sig
Returns
true if the signature is valid

Definition at line 501 of file pubkey.cpp.

501 {
502 update(msg, msg_length);
503 return check_signature(sig, sig_length);
504}

References check_signature(), and update().

Referenced by operator=(), Botan::KeyPair::signature_consistency_check(), Botan::TLS::Callbacks::tls_verify_message(), and verify_message().

◆ verify_message() [2/2]

bool Botan::PK_Verifier::verify_message ( std::span< const uint8_t > msg,
std::span< const uint8_t > sig )
inline

Verify a signature.

Parameters
msgthe message that the signature belongs to
sigthe signature
Returns
true if the signature is valid

Definition at line 369 of file pubkey.h.

369 {
370 return verify_message(msg.data(), msg.size(), sig.data(), sig.size());
371 }
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition pubkey.cpp:501

References verify_message().


The documentation for this class was generated from the following files: