Botan 3.14.1
Crypto and TLS for C&
Botan::PKCS11::Signature_Stream Class Referencefinal

#include <p11_sig_stream.h>

Public Types

enum class  Direction : uint8_t { Sign , Verify }

Public Member Functions

Signature_Stream & operator= (const Signature_Stream &)=delete
Signature_Stream & operator= (Signature_Stream &&)=delete
std::vector< uint8_t > sign ()
 Only valid for Direction::Sign.
 Signature_Stream (const Signature_Stream &)=delete
 Signature_Stream (Direction direction, const Object &key, const MechanismWrapper &mechanism)
 Signature_Stream (Signature_Stream &&)=delete
void update (std::span< const uint8_t > input)
bool verify (std::span< const uint8_t > signature)
 Only valid for Direction::Verify.
 ~Signature_Stream () noexcept

Detailed Description

Maps the streaming update() interface of a signature or verification operation onto the PKCS #11 single and multiple part functions.

Input is buffered until a second nonempty update() call so that the common case of a single update() uses C_Sign/C_Verify. Mechanisms which only support single part operations (such as CKM_ECDSA) always buffer the entire input.

The token operation is only initialized once it is actually needed, and an abandoned multiple part operation is terminated on destruction so the session remains usable.

Definition at line 30 of file p11_sig_stream.h.

Member Enumeration Documentation

◆ Direction

enum class Botan::PKCS11::Signature_Stream::Direction : uint8_t
strong
Enumerator
Sign 
Verify 

Definition at line 32 of file p11_sig_stream.h.

Constructor & Destructor Documentation

◆ Signature_Stream() [1/3]

Botan::PKCS11::Signature_Stream::Signature_Stream ( Direction direction,
const Object & key,
const MechanismWrapper & mechanism )
Parameters
directionwhether signatures are created or verified
keythe key object; must outlive this object
mechanismthe mechanism; must outlive this object

Definition at line 38 of file p11_sig_stream.cpp.

38 :
39 m_direction(direction),
40 m_key(key),
41 m_mechanism(mechanism),
42 m_single_part_only(is_single_part_only(mechanism.mechanism_type())) {}

Referenced by operator=(), operator=(), Signature_Stream(), and Signature_Stream().

◆ ~Signature_Stream()

Botan::PKCS11::Signature_Stream::~Signature_Stream ( )
noexcept

Definition at line 44 of file p11_sig_stream.cpp.

44 {
45 if(!m_multipart_active) {
46 return;
47 }
48
49 /*
50 * Terminate the abandoned operation; otherwise every later operation of the
51 * same kind on this session fails with CKR_OPERATION_ACTIVE. Only a final
52 * call which is not a length query is guaranteed to end the operation.
53 */
54 try {
55 if(m_direction == Direction::Sign) {
56 std::vector<uint8_t> discarded;
57 m_key.module()->C_SignFinal(m_key.session().handle(), discarded, nullptr);
58 } else {
59 const uint8_t dummy_signature = 0;
60 m_key.module()->C_VerifyFinal(m_key.session().handle(), &dummy_signature, 1, nullptr);
61 }
62 } catch(...) { // NOLINT(*-empty-catch)
63 }
64}

References Sign.

◆ Signature_Stream() [2/3]

Botan::PKCS11::Signature_Stream::Signature_Stream ( const Signature_Stream & )
delete

References Signature_Stream().

◆ Signature_Stream() [3/3]

Botan::PKCS11::Signature_Stream::Signature_Stream ( Signature_Stream && )
delete

References Signature_Stream().

Member Function Documentation

◆ operator=() [1/2]

Signature_Stream & Botan::PKCS11::Signature_Stream::operator= ( const Signature_Stream & )
delete

References Signature_Stream().

◆ operator=() [2/2]

Signature_Stream & Botan::PKCS11::Signature_Stream::operator= ( Signature_Stream && )
delete

◆ sign()

std::vector< uint8_t > Botan::PKCS11::Signature_Stream::sign ( )

Only valid for Direction::Sign.

Definition at line 112 of file p11_sig_stream.cpp.

112 {
113 BOTAN_STATE_CHECK(m_direction == Direction::Sign);
114
115 std::vector<uint8_t> signature;
116
117 if(m_multipart_active) {
118 // Any outcome of C_SignFinal other than a length query ends the operation
119 m_multipart_active = false;
120 m_key.module()->C_SignFinal(m_key.session().handle(), signature);
121 } else {
122 const auto message = std::exchange(m_buffer, {});
123
124 init();
125 m_key.module()->C_Sign(m_key.session().handle(), message, signature);
126 }
127
128 return signature;
129}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49

References BOTAN_STATE_CHECK, and Sign.

Referenced by operator=().

◆ update()

void Botan::PKCS11::Signature_Stream::update ( std::span< const uint8_t > input)

Definition at line 88 of file p11_sig_stream.cpp.

88 {
89 if(input.empty()) {
90 return;
91 }
92
93 if(m_multipart_active) {
94 update_token(input);
95 return;
96 }
97
98 if(m_single_part_only || m_buffer.empty()) {
99 m_buffer.insert(m_buffer.end(), input.begin(), input.end());
100 return;
101 }
102
103 // Second nonempty update: switch to a multiple part operation
104 const auto buffered = std::exchange(m_buffer, {});
105
106 init();
107 m_multipart_active = true;
108 update_token(buffered);
109 update_token(input);
110}

Referenced by operator=().

◆ verify()

bool Botan::PKCS11::Signature_Stream::verify ( std::span< const uint8_t > signature)

Only valid for Direction::Verify.

Definition at line 131 of file p11_sig_stream.cpp.

131 {
132 BOTAN_STATE_CHECK(m_direction == Direction::Verify);
133
134 // Avoid passing a null pointer, which some modules reject
135 const uint8_t empty_signature = 0;
136 const uint8_t* sig_ptr = signature.empty() ? &empty_signature : signature.data();
137 const Ulong sig_len = checked_ulong_cast(signature.size());
138
140
141 if(m_multipart_active) {
142 // C_VerifyFinal always ends the operation
143 m_multipart_active = false;
144 m_key.module()->C_VerifyFinal(m_key.session().handle(), sig_ptr, sig_len, &return_value);
145 } else {
146 const auto message = std::exchange(m_buffer, {});
147
148 init();
149 m_key.module()->C_Verify(
150 m_key.session().handle(), message.data(), checked_ulong_cast(message.size()), sig_ptr, sig_len, &return_value);
151 }
152
153 if(return_value == ReturnValue::SignatureInvalid || return_value == ReturnValue::SignatureLenRange) {
154 return false;
155 } else if(return_value == ReturnValue::OK) {
156 return true;
157 } else {
158 throw PKCS11_ReturnError(return_value);
159 }
160}
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
CK_ULONG Ulong
Definition p11.h:1204

References BOTAN_STATE_CHECK, Botan::PKCS11::checked_ulong_cast(), Botan::PKCS11::OK, Botan::PKCS11::SignatureInvalid, Botan::PKCS11::SignatureLenRange, and Verify.

Referenced by operator=().


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