1
1
#pragma once
2
2
3
+ #include < cstddef>
3
4
#include < list>
4
5
#include < memory>
5
6
#include < optional>
6
7
#include < string>
7
8
#include < string_view>
8
- #include < vector>
9
9
#include " openssl/bn.h"
10
10
#include < openssl/x509.h>
11
11
#include < openssl/dh.h>
@@ -82,6 +82,15 @@ namespace ncrypto {
82
82
void * operator new (size_t ) = delete ; \
83
83
void operator delete (void *) = delete ;
84
84
85
+ [[noreturn]] inline void unreachable () {
86
+ #ifdef __GNUC__
87
+ __builtin_unreachable ();
88
+ #elif defined(_MSC_VER)
89
+ __assume (false );
90
+ #else
91
+ #endif
92
+ }
93
+
85
94
// ============================================================================
86
95
// Error handling utilities
87
96
@@ -190,30 +199,91 @@ using SSLPointer = DeleteFnPtr<SSL, SSL_free>;
190
199
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>;
191
200
using X509Pointer = DeleteFnPtr<X509, X509_free>;
192
201
202
+ // An unowned, unmanaged pointer to a buffer of data.
203
+ struct Buffer {
204
+ void * data;
205
+ size_t len;
206
+ };
207
+
208
+ // A managed pointer to a buffer of data. When destroyed the underlying
209
+ // buffer will be freed.
210
+ class DataPointer final {
211
+ public:
212
+ static DataPointer Alloc (size_t len);
213
+
214
+ DataPointer () = default ;
215
+ explicit DataPointer (void * data, size_t len);
216
+ explicit DataPointer (const Buffer& buffer);
217
+ DataPointer (DataPointer&& other) noexcept ;
218
+ DataPointer& operator =(DataPointer&& other) noexcept ;
219
+ NCRYPTO_DISALLOW_COPY (DataPointer)
220
+ ~DataPointer ();
221
+
222
+ inline bool operator ==(std::nullptr_t ) noexcept { return data_ == nullptr ; }
223
+ inline operator bool () const { return data_ != nullptr ; }
224
+ inline void * get () const noexcept { return data_; }
225
+ inline size_t size () const noexcept { return len_; }
226
+ void reset (void * data = nullptr , size_t len = 0 );
227
+ void reset (const Buffer& buffer);
228
+
229
+ // Releases ownership of the underlying data buffer. It is the caller's
230
+ // responsibility to ensure the buffer is appropriately freed.
231
+ Buffer release ();
232
+
233
+ // Returns a Buffer struct that is a view of the underlying data.
234
+ inline operator const Buffer () const {
235
+ return {
236
+ .data = data_,
237
+ .len = len_,
238
+ };
239
+ }
240
+
241
+ private:
242
+ void * data_ = nullptr ;
243
+ size_t len_ = 0 ;
244
+ };
245
+
193
246
class BignumPointer final {
194
247
public:
195
248
BignumPointer () = default ;
196
249
explicit BignumPointer (BIGNUM* bignum);
250
+ explicit BignumPointer (const unsigned char * data, size_t len);
197
251
BignumPointer (BignumPointer&& other) noexcept ;
198
252
BignumPointer& operator =(BignumPointer&& other) noexcept ;
199
253
NCRYPTO_DISALLOW_COPY (BignumPointer)
200
254
~BignumPointer ();
201
255
202
- bool operator ==(const BignumPointer& other) noexcept ;
203
- bool operator ==(const BIGNUM* other) noexcept ;
204
- inline bool operator ==(std::nullptr_t ) noexcept { return bn_ == nullptr ; }
256
+ int operator <=>(const BignumPointer& other) const noexcept ;
257
+ int operator <=>(const BIGNUM* other) const noexcept ;
205
258
inline operator bool () const { return bn_ != nullptr ; }
206
259
inline BIGNUM* get () const noexcept { return bn_.get (); }
207
260
void reset (BIGNUM* bn = nullptr );
261
+ void reset (const unsigned char * data, size_t len);
208
262
BIGNUM* release ();
209
263
210
- size_t byteLength ();
264
+ bool isZero () const ;
265
+ bool isOne () const ;
266
+
267
+ bool setWord (unsigned long w);
268
+ unsigned long getWord () const ;
269
+
270
+ size_t byteLength () const ;
211
271
212
- std::vector<uint8_t > encode ();
213
- std::vector<uint8_t > encodePadded (size_t size);
272
+ DataPointer toHex () const ;
273
+ DataPointer encode () const ;
274
+ DataPointer encodePadded (size_t size) const ;
275
+ size_t encodeInto (unsigned char * out) const ;
276
+ size_t encodePaddedInto (unsigned char * out, size_t size) const ;
214
277
215
- static std::vector<uint8_t > encode (const BIGNUM* bn);
216
- static std::vector<uint8_t > encodePadded (const BIGNUM* bn, size_t size);
278
+ static BignumPointer New ();
279
+ static BignumPointer NewSecure ();
280
+ static DataPointer Encode (const BIGNUM* bn);
281
+ static DataPointer EncodePadded (const BIGNUM* bn, size_t size);
282
+ static size_t EncodePaddedInto (const BIGNUM* bn, unsigned char * out, size_t size);
283
+ static int GetBitCount (const BIGNUM* bn);
284
+ static int GetByteCount (const BIGNUM* bn);
285
+ static unsigned long GetWord (const BIGNUM* bn);
286
+ static const BIGNUM* One ();
217
287
218
288
private:
219
289
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
@@ -269,11 +339,6 @@ bool testFipsEnabled();
269
339
// ============================================================================
270
340
// Various utilities
271
341
272
- struct Buffer {
273
- const void * data;
274
- size_t len;
275
- };
276
-
277
342
bool CSPRNG (void * buffer, size_t length) NCRYPTO_MUST_USE_RESULT;
278
343
279
344
// This callback is used to avoid the default passphrase callback in OpenSSL
@@ -285,6 +350,9 @@ int NoPasswordCallback(char* buf, int size, int rwflag, void* u);
285
350
286
351
int PasswordCallback (char * buf, int size, int rwflag, void * u);
287
352
353
+ bool SafeX509SubjectAltNamePrint (const BIOPointer& out, X509_EXTENSION* ext);
354
+ bool SafeX509InfoAccessPrint (const BIOPointer& out, X509_EXTENSION* ext);
355
+
288
356
// ============================================================================
289
357
// Version metadata
290
358
#define NCRYPTO_VERSION " 0.0.1"
0 commit comments