@@ -815,7 +815,7 @@ bool PrintGeneralName(const BIOPointer& out, const GENERAL_NAME* gen) {
815
815
816
816
bool SafeX509SubjectAltNamePrint (const BIOPointer& out, X509_EXTENSION* ext) {
817
817
auto ret = OBJ_obj2nid (X509_EXTENSION_get_object (ext));
818
- NCRYPTO_ASSERT_EQUAL (ret, NID_subject_alt_name, " unexpected extension type " ) ;
818
+ if (ret != NID_subject_alt_name) return false ;
819
819
820
820
GENERAL_NAMES* names = static_cast <GENERAL_NAMES*>(X509V3_EXT_d2i (ext));
821
821
if (names == nullptr ) return false ;
@@ -838,7 +838,7 @@ bool SafeX509SubjectAltNamePrint(const BIOPointer& out, X509_EXTENSION* ext) {
838
838
839
839
bool SafeX509InfoAccessPrint (const BIOPointer& out, X509_EXTENSION* ext) {
840
840
auto ret = OBJ_obj2nid (X509_EXTENSION_get_object (ext));
841
- NCRYPTO_ASSERT_EQUAL (ret, NID_info_access, " unexpected extension type " ) ;
841
+ if (ret != NID_info_access) return false ;
842
842
843
843
AUTHORITY_INFO_ACCESS* descs =
844
844
static_cast <AUTHORITY_INFO_ACCESS*>(X509V3_EXT_d2i (ext));
@@ -1173,7 +1173,6 @@ bool X509View::enumUsages(UsageCallback callback) const {
1173
1173
const int count = sk_ASN1_OBJECT_num (eku.get ());
1174
1174
char buf[256 ]{};
1175
1175
1176
- int j = 0 ;
1177
1176
for (int i = 0 ; i < count; i++) {
1178
1177
if (OBJ_obj2txt (buf, sizeof (buf), sk_ASN1_OBJECT_value (eku.get (), i), 1 ) >=
1179
1178
0 ) {
@@ -1186,7 +1185,6 @@ bool X509View::enumUsages(UsageCallback callback) const {
1186
1185
bool X509View::ifRsa (KeyCallback<Rsa> callback) const {
1187
1186
if (cert_ == nullptr ) return true ;
1188
1187
OSSL3_CONST EVP_PKEY* pkey = X509_get0_pubkey (cert_);
1189
- OSSL3_CONST RSA* rsa = nullptr ;
1190
1188
auto id = EVP_PKEY_id (pkey);
1191
1189
if (id == EVP_PKEY_RSA || id == EVP_PKEY_RSA2 || id == EVP_PKEY_RSA_PSS) {
1192
1190
Rsa rsa (EVP_PKEY_get0_RSA (pkey));
@@ -1200,7 +1198,6 @@ bool X509View::ifRsa(KeyCallback<Rsa> callback) const {
1200
1198
bool X509View::ifEc (KeyCallback<Ec> callback) const {
1201
1199
if (cert_ == nullptr ) return true ;
1202
1200
OSSL3_CONST EVP_PKEY* pkey = X509_get0_pubkey (cert_);
1203
- OSSL3_CONST EC_KEY* ec = nullptr ;
1204
1201
auto id = EVP_PKEY_id (pkey);
1205
1202
if (id == EVP_PKEY_EC) {
1206
1203
Ec ec (EVP_PKEY_get0_EC_KEY (pkey));
@@ -2383,7 +2380,7 @@ EVPKeyPointer::operator Rsa() const {
2383
2380
2384
2381
// TODO(tniessen): Remove the "else" branch once we drop support for OpenSSL
2385
2382
// versions older than 1.1.1e via FIPS / dynamic linking.
2386
- const RSA* rsa;
2383
+ OSSL3_CONST RSA* rsa;
2387
2384
if (OPENSSL_VERSION_NUMBER >= 0x1010105fL ) {
2388
2385
rsa = EVP_PKEY_get0_RSA (get ());
2389
2386
} else {
@@ -3417,7 +3414,7 @@ DataPointer CipherImpl(const EVPKeyPointer& key,
3417
3414
3418
3415
Rsa::Rsa () : rsa_ (nullptr ) {}
3419
3416
3420
- Rsa::Rsa (const RSA* ptr) : rsa_ (ptr) {}
3417
+ Rsa::Rsa (OSSL3_CONST RSA* ptr) : rsa_ (ptr) {}
3421
3418
3422
3419
const Rsa::PublicKey Rsa::getPublicKey () const {
3423
3420
if (rsa_ == nullptr ) return {};
@@ -3553,7 +3550,7 @@ DataPointer Cipher::recover(const EVPKeyPointer& key,
3553
3550
3554
3551
Ec::Ec () : ec_ (nullptr ) {}
3555
3552
3556
- Ec::Ec (const EC_KEY* key) : ec_ (key) {}
3553
+ Ec::Ec (OSSL3_CONST EC_KEY* key) : ec_ (key) {}
3557
3554
3558
3555
const EC_GROUP* Ec::getGroup () const {
3559
3556
return ECKeyPointer::GetGroup (ec_);
@@ -3615,7 +3612,7 @@ DataPointer EVPMDCtxPointer::digestFinal(size_t length) {
3615
3612
}
3616
3613
3617
3614
bool EVPMDCtxPointer::digestFinalInto (Buffer<void >* buf) {
3618
- if (!ctx_) false ;
3615
+ if (!ctx_) return false ;
3619
3616
3620
3617
auto ptr = static_cast <unsigned char *>(buf->data );
3621
3618
@@ -3735,4 +3732,85 @@ bool extractP1363(const Buffer<const unsigned char>& buf,
3735
3732
BignumPointer::EncodePaddedInto (asn1_sig.s (), dest + n, n) > 0 ;
3736
3733
}
3737
3734
3735
+ // ============================================================================
3736
+
3737
+ HMACCtxPointer::HMACCtxPointer () : ctx_ (nullptr ) {}
3738
+
3739
+ HMACCtxPointer::HMACCtxPointer (HMAC_CTX* ctx) : ctx_ (ctx) {}
3740
+
3741
+ HMACCtxPointer::HMACCtxPointer (HMACCtxPointer&& other) noexcept
3742
+ : ctx_ (other.release ()) {}
3743
+
3744
+ HMACCtxPointer& HMACCtxPointer::operator =(HMACCtxPointer&& other) noexcept {
3745
+ ctx_.reset (other.release ());
3746
+ return *this ;
3747
+ }
3748
+
3749
+ HMACCtxPointer::~HMACCtxPointer () {
3750
+ reset ();
3751
+ }
3752
+
3753
+ void HMACCtxPointer::reset (HMAC_CTX* ctx) {
3754
+ ctx_.reset (ctx);
3755
+ }
3756
+
3757
+ HMAC_CTX* HMACCtxPointer::release () {
3758
+ return ctx_.release ();
3759
+ }
3760
+
3761
+ bool HMACCtxPointer::init (const Buffer<const void >& buf, const EVP_MD* md) {
3762
+ if (!ctx_) return false ;
3763
+ return HMAC_Init_ex (ctx_.get (), buf.data , buf.len , md, nullptr ) == 1 ;
3764
+ }
3765
+
3766
+ bool HMACCtxPointer::update (const Buffer<const void >& buf) {
3767
+ if (!ctx_) return false ;
3768
+ return HMAC_Update (ctx_.get (),
3769
+ static_cast <const unsigned char *>(buf.data ),
3770
+ buf.len ) == 1 ;
3771
+ }
3772
+
3773
+ DataPointer HMACCtxPointer::digest () {
3774
+ auto data = DataPointer::Alloc (EVP_MAX_MD_SIZE);
3775
+ if (!data) return {};
3776
+ Buffer<void > buf = data;
3777
+ if (!digestInto (&buf)) return {};
3778
+ return data.resize (buf.len );
3779
+ }
3780
+
3781
+ bool HMACCtxPointer::digestInto (Buffer<void >* buf) {
3782
+ if (!ctx_) return false ;
3783
+
3784
+ unsigned int len = buf->len ;
3785
+ if (!HMAC_Final (ctx_.get (), static_cast <unsigned char *>(buf->data ), &len)) {
3786
+ return false ;
3787
+ }
3788
+ buf->len = len;
3789
+ return true ;
3790
+ }
3791
+
3792
+ HMACCtxPointer HMACCtxPointer::New () {
3793
+ return HMACCtxPointer (HMAC_CTX_new ());
3794
+ }
3795
+
3796
+ DataPointer hashDigest (const Buffer<const unsigned char >& buf,
3797
+ const EVP_MD* md) {
3798
+ if (md == nullptr ) return {};
3799
+ size_t md_len = EVP_MD_size (md);
3800
+ unsigned int result_size;
3801
+ auto data = DataPointer::Alloc (md_len);
3802
+ if (!data) return {};
3803
+
3804
+ if (!EVP_Digest (buf.data ,
3805
+ buf.len ,
3806
+ reinterpret_cast <unsigned char *>(data.get ()),
3807
+ &result_size,
3808
+ md,
3809
+ nullptr )) {
3810
+ return {};
3811
+ }
3812
+
3813
+ return data.resize (result_size);
3814
+ }
3815
+
3738
3816
} // namespace ncrypto
0 commit comments