Skip to content

Commit

Permalink
Fixed issues building with nocrypt. Improved logic on `ASN_BER_TO_DER…
Browse files Browse the repository at this point in the history
…`. Improved logic on unknown extension callback (new `WC_ASN_UNKNOWN_EXT_CB` gate).
  • Loading branch information
dgarske committed Jul 31, 2024
1 parent 877c1d7 commit 548a2c6
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 93 deletions.
3 changes: 1 addition & 2 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5359,8 +5359,7 @@ int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)

InitDecodedCert(cert, der->buffer, der->length, cm->heap);

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) && \
defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
if (cm->unknownExtCallback != NULL) {
wc_SetUnknownExtCallback(cert, cm->unknownExtCallback);
}
Expand Down
8 changes: 3 additions & 5 deletions src/ssl_certman.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,7 @@ void wolfSSL_CertManagerSetVerify(WOLFSSL_CERT_MANAGER* cm, VerifyCallback vc)
}
#endif /* NO_WOLFSSL_CM_VERIFY */

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
void wolfSSL_CertManagerSetUnknownExtCallback(WOLFSSL_CERT_MANAGER* cm,
wc_UnknownExtCallback cb)
{
Expand All @@ -620,7 +619,7 @@ void wolfSSL_CertManagerSetUnknownExtCallback(WOLFSSL_CERT_MANAGER* cm,
}

}
#endif /* WOLFSSL_CUSTOM_OID && WOLFSSL_ASN_TEMPLATE && HAVE_OID_DECODING */
#endif /* WC_ASN_UNKNOWN_EXT_CB */

#if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
/* Verify the certificate.
Expand Down Expand Up @@ -690,8 +689,7 @@ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff,
/* Create a decoded certificate with DER buffer. */
InitDecodedCert(cert, buff, (word32)sz, cm->heap);

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
if (cm->unknownExtCallback != NULL)
wc_SetUnknownExtCallback(cert, cm->unknownExtCallback);
#endif
Expand Down
19 changes: 10 additions & 9 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -3660,7 +3660,7 @@ int StreamOctetString(const byte* inBuf, word32 inBufSz, byte* out, word32* outS

/* Convert BER to DER */

/* Pull informtation from the ASN.1 BER encoded item header */
/* Pull information from the ASN.1 BER encoded item header */
static int GetBerHeader(const byte* data, word32* idx, word32 maxIdx,
byte* pTag, word32* pLen, int* indef)
{
Expand Down Expand Up @@ -6226,7 +6226,8 @@ static int RsaPssHashOidToMgf1(word32 oid, int* mgf)
return ret;
}

#ifndef NO_CERTS
#if !defined(NO_CERTS) && !defined(NO_ASN_CRYPT)

/* Convert a hash OID to a fake signature OID.
*
* @param [in] oid Hash OID.
Expand Down Expand Up @@ -21407,8 +21408,7 @@ enum {
#define certExtASN_Length (sizeof(certExtASN) / sizeof(ASNItem))
#endif

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
int wc_SetUnknownExtCallback(DecodedCert* cert,
wc_UnknownExtCallback cb) {
if (cert == NULL) {
Expand All @@ -21429,7 +21429,7 @@ int wc_SetUnknownExtCallbackEx(DecodedCert* cert,
cert->unknownExtCallbackExCtx = ctx;
return 0;
}
#endif
#endif /* WC_ASN_UNKNOWN_EXT_CB */

/*
* Processing the Certificate Extensions. This does not modify the current
Expand Down Expand Up @@ -21583,7 +21583,7 @@ static int DecodeCertExtensions(DecodedCert* cert)
/* Decode the extension by type. */
ret = DecodeExtensionType(input + idx, length, oid, critical, cert,
&isUnknownExt);
#if defined(WOLFSSL_CUSTOM_OID) && defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
if (isUnknownExt && (cert->unknownExtCallback != NULL ||
cert->unknownExtCallbackEx != NULL)) {
word16 decOid[MAX_OID_SZ];
Expand Down Expand Up @@ -21612,8 +21612,9 @@ static int DecodeCertExtensions(DecodedCert* cert)
cert->unknownExtCallbackExCtx);
}
}
#endif
#else
(void)isUnknownExt;
#endif

/* Move index on to next extension. */
idx += length;
Expand Down Expand Up @@ -34482,7 +34483,7 @@ int wc_EccPublicKeyDecode(const byte* input, word32* inOutIdx,
#endif /* WOLFSSL_ASN_TEMPLATE */
}

#if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
#ifdef HAVE_ECC_KEY_EXPORT
/* build DER formatted ECC key, include optional public key if requested,
* return length on success, negative on error */
int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 *inLen,
Expand Down Expand Up @@ -34913,7 +34914,7 @@ int wc_EccKeyToPKCS8(ecc_key* key, byte* output,
return eccToPKCS8(key, output, outLen, 1);
}
#endif /* HAVE_PKCS8 */
#endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
#endif /* HAVE_ECC_KEY_EXPORT */
#endif /* HAVE_ECC */

#ifdef WC_ENABLE_ASYM_KEY_IMPORT
Expand Down
17 changes: 6 additions & 11 deletions wolfcrypt/src/pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,7 @@ int wc_PKCS7_Init(PKCS7* pkcs7, void* heap, int devId)
return 0;
}

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
void wc_PKCS7_SetUnknownExtCallback(PKCS7* pkcs7, wc_UnknownExtCallback cb)
{
if (pkcs7 != NULL) {
Expand Down Expand Up @@ -1083,8 +1082,7 @@ int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* derCert, word32 derCertSz)
int devId;
Pkcs7Cert* cert;
Pkcs7Cert* lastCert;
#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
wc_UnknownExtCallback cb;
#endif

Expand All @@ -1095,16 +1093,14 @@ int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* derCert, word32 derCertSz)
heap = pkcs7->heap;
devId = pkcs7->devId;
cert = pkcs7->certList;
#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
cb = pkcs7->unknownExtCallback;
#ifdef WC_ASN_UNKNOWN_EXT_CB
cb = pkcs7->unknownExtCallback; /* save / restore callback */
#endif
ret = wc_PKCS7_Init(pkcs7, heap, devId);
if (ret != 0)
return ret;

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
pkcs7->unknownExtCallback = cb;
#endif
pkcs7->certList = cert;
Expand Down Expand Up @@ -1155,8 +1151,7 @@ int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* derCert, word32 derCertSz)
}

InitDecodedCert(dCert, derCert, derCertSz, pkcs7->heap);
#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
if (pkcs7->unknownExtCallback != NULL)
wc_SetUnknownExtCallback(dCert, pkcs7->unknownExtCallback);
#endif
Expand Down
10 changes: 3 additions & 7 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void);
#if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void);
#endif
#if defined(ASN_BER_TO_DER) && \
(defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
defined(OPENSSL_EXTRA_X509_SMALL))
#ifdef ASN_BER_TO_DER
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void);
#endif
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void);
Expand Down Expand Up @@ -53957,9 +53955,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
#endif /* WOLFSSL_PUBLIC_MP */


#if defined(ASN_BER_TO_DER) && \
(defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
defined(OPENSSL_EXTRA_X509_SMALL))
#ifdef ASN_BER_TO_DER
/* wc_BerToDer is only public facing in the case of test cert or opensslextra */
typedef struct berDerTestData {
const byte *in;
Expand Down Expand Up @@ -54075,7 +54071,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)

return 0;
}
#endif
#endif /* ASN_BER_TO_DER */

#ifdef DEBUG_WOLFSSL
static THREAD_LS_T int log_cnt = 0;
Expand Down
3 changes: 1 addition & 2 deletions wolfssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2650,8 +2650,7 @@ struct WOLFSSL_CERT_MANAGER {
#ifdef HAVE_DILITHIUM
short minDilithiumKeySz; /* minimum allowed Dilithium key size */
#endif
#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
wc_UnknownExtCallback unknownExtCallback;
#endif
};
Expand Down
3 changes: 1 addition & 2 deletions wolfssl/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3690,8 +3690,7 @@ WOLFSSL_API void wolfSSL_CTX_SetPerformTlsRecordProcessingCb(WOLFSSL_CTX* ctx,
WOLFSSL_API void wolfSSL_CertManagerFree(WOLFSSL_CERT_MANAGER* cm);
WOLFSSL_API int wolfSSL_CertManager_up_ref(WOLFSSL_CERT_MANAGER* cm);

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
WOLFSSL_API void wolfSSL_CertManagerSetUnknownExtCallback(
WOLFSSL_CERT_MANAGER* cm,
wc_UnknownExtCallback cb);
Expand Down
20 changes: 9 additions & 11 deletions wolfssl/wolfcrypt/asn.h
Original file line number Diff line number Diff line change
Expand Up @@ -1660,8 +1660,7 @@ typedef struct TrustedPeerCert TrustedPeerCert;
typedef struct SignatureCtx SignatureCtx;
typedef struct CertSignCtx CertSignCtx;

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
typedef int (*wc_UnknownExtCallback)(const word16* oid, word32 oidSz, int crit,
const unsigned char* der, word32 derSz);
typedef int (*wc_UnknownExtCallbackEx)(const word16* oid, word32 oidSz,
Expand Down Expand Up @@ -1887,7 +1886,7 @@ struct DecodedCert {
/* WOLFSSL_X509_NAME structures (used void* to avoid including ssl.h) */
void* issuerName;
void* subjectName;
#endif /* OPENSSL_EXTRA */
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
#ifdef WOLFSSL_SEP
int deviceTypeSz;
byte* deviceType;
Expand Down Expand Up @@ -1997,8 +1996,7 @@ struct DecodedCert {
#ifdef HAVE_RPK
byte isRPK : 1; /* indicate the cert is Raw-Public-Key cert in RFC7250 */
#endif
#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
wc_UnknownExtCallback unknownExtCallback;
wc_UnknownExtCallbackEx unknownExtCallbackEx;
void *unknownExtCallbackExCtx;
Expand Down Expand Up @@ -2141,15 +2139,16 @@ typedef enum MimeStatus
} MimeStatus;
#endif /* HAVE_SMIME */


WOLFSSL_LOCAL int HashIdAlg(word32 oidSum);
WOLFSSL_LOCAL int CalcHashId(const byte* data, word32 len, byte* hash);
WOLFSSL_LOCAL int CalcHashId_ex(const byte* data, word32 len, byte* hash,
int hashAlg);
WOLFSSL_LOCAL int GetName(DecodedCert* cert, int nameType, int maxIdx);

WOLFSSL_ASN_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
#ifdef ASN_BER_TO_DER
WOLFSSL_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
word32* derSz);
#endif
WOLFSSL_LOCAL int StreamOctetString(const byte* inBuf, word32 inBufSz,
byte* out, word32* outSz, word32* idx);

Expand All @@ -2167,11 +2166,10 @@ WOLFSSL_ASN_API void FreeDecodedCert(DecodedCert* cert);
WOLFSSL_ASN_API int ParseCert(DecodedCert* cert, int type, int verify,
void* cm);

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
WOLFSSL_ASN_API int wc_SetUnknownExtCallback(DecodedCert* cert,
#ifdef WC_ASN_UNKNOWN_EXT_CB
WOLFSSL_API int wc_SetUnknownExtCallback(DecodedCert* cert,
wc_UnknownExtCallback cb);
WOLFSSL_ASN_API int wc_SetUnknownExtCallbackEx(DecodedCert* cert,
WOLFSSL_API int wc_SetUnknownExtCallbackEx(DecodedCert* cert,
wc_UnknownExtCallbackEx cb,
void *ctx);
#endif
Expand Down
6 changes: 2 additions & 4 deletions wolfssl/wolfcrypt/pkcs7.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ struct PKCS7 {
word32 plainDigestSz;
word32 pkcs7DigestSz;

#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
wc_UnknownExtCallback unknownExtCallback;
#endif

Expand All @@ -363,8 +362,7 @@ struct PKCS7 {
};

WOLFSSL_API PKCS7* wc_PKCS7_New(void* heap, int devId);
#if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
&& defined(HAVE_OID_DECODING)
#ifdef WC_ASN_UNKNOWN_EXT_CB
WOLFSSL_API void wc_PKCS7_SetUnknownExtCallback(PKCS7* pkcs7,
wc_UnknownExtCallback cb);
#endif
Expand Down
Loading

0 comments on commit 548a2c6

Please sign in to comment.