Botan 3.14.1
Crypto and TLS for C&
p11_sig_stream.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 Signature Streaming
3* (C) 2026 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/p11_sig_stream.h>
9
10#include <botan/assert.h>
11#include <botan/p11_types.h>
12#include <utility>
13
14namespace Botan::PKCS11 {
15
16namespace {
17
18/*
19* Mechanisms which take a digest or an already encoded message and which the
20* PKCS #11 mechanism tables list as single part only for sign/verify
21*/
22bool is_single_part_only(MechanismType type) {
23 switch(type) {
30 return true;
31 default:
32 return false;
33 }
34}
35
36} // namespace
37
38Signature_Stream::Signature_Stream(Direction direction, const Object& key, const MechanismWrapper& mechanism) :
39 m_direction(direction),
40 m_key(key),
41 m_mechanism(mechanism),
42 m_single_part_only(is_single_part_only(mechanism.mechanism_type())) {}
43
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}
65
66void Signature_Stream::init() {
67 if(m_direction == Direction::Sign) {
68 m_key.module()->C_SignInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
69 } else {
70 m_key.module()->C_VerifyInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
71 }
72}
73
74void Signature_Stream::update_token(std::span<const uint8_t> input) {
75 try {
76 if(m_direction == Direction::Sign) {
77 m_key.module()->C_SignUpdate(m_key.session().handle(), input.data(), checked_ulong_cast(input.size()));
78 } else {
79 m_key.module()->C_VerifyUpdate(m_key.session().handle(), input.data(), checked_ulong_cast(input.size()));
80 }
81 } catch(...) {
82 // A failed update terminates the operation on the token
83 m_multipart_active = false;
84 throw;
85 }
86}
87
88void Signature_Stream::update(std::span<const uint8_t> input) {
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}
111
112std::vector<uint8_t> Signature_Stream::sign() {
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}
130
131bool Signature_Stream::verify(std::span<const uint8_t> signature) {
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}
161
162} // namespace Botan::PKCS11
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
bool C_VerifyInit(SessionHandle session, const Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:816
bool C_SignInit(SessionHandle session, const Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:698
Module & module() const
Definition p11_object.h:559
ObjectHandle handle() const
Definition p11_object.h:553
Session & session() const
Definition p11_object.h:556
SessionHandle handle() const
Definition p11_types.h:172
Signature_Stream(Direction direction, const Object &key, const MechanismWrapper &mechanism)
void update(std::span< const uint8_t > input)
bool verify(std::span< const uint8_t > signature)
Only valid for Direction::Verify.
std::vector< uint8_t > sign()
Only valid for Direction::Sign.
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
CK_ULONG Ulong
Definition p11.h:1204