Skip to content

Commit 6edc38a

Browse files
committed
stir_shaken: Improve compatibility with older WolfSSL libraries
This patch avoids usage of newer API functions which are missing in both WolfSSL 4.3.0 (e.g. Ubuntu 20.04) and pre-1.1.0 OpenSSL, such as: - X509_STORE_set_verify_cb_func() - ECDSA_SIG_get0() - X509_get_ext_by_OBJ()
1 parent 09e7877 commit 6edc38a

1 file changed

Lines changed: 44 additions & 12 deletions

File tree

modules/stir_shaken/stir_shaken.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <openssl/x509.h>
4242
#else
4343
#include <wolfssl/options.h>
44+
#include <wolfssl/ssl.h>
4445
#include <wolfssl/openssl/x509.h>
4546
#endif
4647

@@ -51,10 +52,14 @@
5152
#include <openssl/x509v3.h>
5253
#include <openssl/pem.h>
5354
#include <openssl/err.h>
55+
#include <openssl/ssl.h>
56+
#include <openssl/ecdsa.h>
5457
#else
5558
#include <wolfssl/openssl/x509v3.h>
5659
#include <wolfssl/openssl/pem.h>
5760
#include <wolfssl/openssl/err.h>
61+
#include <wolfssl/openssl/ssl.h>
62+
#include <wolfssl/openssl/ecdsa.h>
5863
#endif
5964

6065
#include <stdlib.h>
@@ -276,6 +281,18 @@ static int ss_store_set_default_paths(X509_STORE *x509_store)
276281
#endif
277282
}
278283

284+
static void ss_store_set_verify_cb(X509_STORE *x509_store,
285+
int (*verify_cb)(int, X509_STORE_CTX *))
286+
{
287+
#ifdef WOLFSSL_VERSION
288+
/* Older wolfSSL builds may not provide store-level callback setter. */
289+
(void)x509_store;
290+
(void)verify_cb;
291+
#else
292+
X509_STORE_set_verify_cb_func(x509_store, verify_cb);
293+
#endif
294+
}
295+
279296
// called during mod_init
280297
static int init_cert_validation(void)
281298
{
@@ -284,7 +301,7 @@ static int init_cert_validation(void)
284301
LM_ERR("Failed to create X509_STORE_CTX object\n");
285302
return -1;
286303
}
287-
X509_STORE_set_verify_cb_func(store, verify_callback);
304+
ss_store_set_verify_cb(store, verify_callback);
288305

289306
if (ca_list || ca_dir) {
290307
if (X509_STORE_load_locations(store, ca_list, ca_dir) != 1) {
@@ -323,7 +340,7 @@ static int init_cert_ca_reload(void)
323340
LM_ERR("Failed to create X509_STORE_CTX object\n");
324341
return -1;
325342
}
326-
X509_STORE_set_verify_cb_func(store, verify_callback);
343+
ss_store_set_verify_cb(store, verify_callback);
327344

328345
/* check if ca_list param is set */
329346
if (!ca_list) {
@@ -360,7 +377,7 @@ static int init_cert_crl_reload(void)
360377
LM_ERR("Failed to create X509_STORE_CTX object\n");
361378
return -1;
362379
}
363-
X509_STORE_set_verify_cb_func(store, verify_callback);
380+
ss_store_set_verify_cb(store, verify_callback);
364381

365382
/* check if crl_list param is set */
366383
if (!crl_list) {
@@ -907,13 +924,16 @@ static int get_dest_tn_from_msg(struct sip_msg *msg, str *dest_tn)
907924
return 0;
908925
}
909926

910-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
911-
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
927+
static void ss_ecdsa_sig_get0(const ECDSA_SIG *sig, const BIGNUM **pr,
928+
const BIGNUM **ps)
912929
{
930+
#if !defined(WOLFSSL_VERSION) && OPENSSL_VERSION_NUMBER >= 0x10100000L
931+
ECDSA_SIG_get0(sig, pr, ps);
932+
#else
913933
*pr = sig->r;
914934
*ps = sig->s;
915-
}
916935
#endif
936+
}
917937

918938
static str *build_identity_hf(EVP_PKEY *pkey,
919939
time_t date_ts, str *attest, str *cr_url, str *orig_tn,
@@ -980,7 +1000,7 @@ static str *build_identity_hf(EVP_PKEY *pkey,
9801000
pkg_free(der_sig_buf.s);
9811001
der_sig_buf.s = NULL;
9821002

983-
ECDSA_SIG_get0(sig, &r, &s);
1003+
ss_ecdsa_sig_get0(sig, &r, &s);
9841004
len = ss_bn2binpad(r, raw_sig_buf, R_S_INT_LEN);
9851005
if (len < 0 || len != R_S_INT_LEN) {
9861006
LM_ERR("Failed to convert R integer into binay represantation\n");
@@ -1052,6 +1072,15 @@ static str *build_identity_hf(EVP_PKEY *pkey,
10521072
return NULL;
10531073
}
10541074

1075+
static X509_INFO *ss_sk_X509_INFO_shift(STACK_OF(X509_INFO) *sk)
1076+
{
1077+
#ifdef WOLFSSL_VERSION
1078+
return sk_X509_INFO_pop(sk);
1079+
#else
1080+
return sk_X509_INFO_shift(sk);
1081+
#endif
1082+
}
1083+
10551084
static int load_cert(X509 **cert, STACK_OF(X509) **certchain, str *cert_buf)
10561085
{
10571086
BIO *cbio;
@@ -1099,7 +1128,7 @@ static int load_cert(X509 **cert, STACK_OF(X509) **certchain, str *cert_buf)
10991128
}
11001129

11011130
while (sk_X509_INFO_num(sk)) {
1102-
xi = sk_X509_INFO_shift(sk);
1131+
xi = ss_sk_X509_INFO_shift(sk);
11031132
if (xi->x509 != NULL) {
11041133
sk_X509_push(stack, xi->x509);
11051134
xi->x509 = NULL;
@@ -1719,11 +1748,13 @@ static int validate_certificate(X509 *cert, STACK_OF(X509) *certchain)
17191748
ASN1_OBJECT *ext_obj;
17201749
char ext_oid[128];
17211750
int ext_count, i, has_tn_authlist = 0;
1751+
int tn_authlist_nid;
17221752
int rc;
17231753

17241754
/* check the TN Authorization list extension */
1725-
if (tn_authlist_obj &&
1726-
X509_get_ext_by_OBJ(cert, tn_authlist_obj, -1) != -1) {
1755+
tn_authlist_nid = tn_authlist_obj ? OBJ_obj2nid(tn_authlist_obj) : NID_undef;
1756+
if (tn_authlist_nid != NID_undef &&
1757+
X509_get_ext_by_NID(cert, tn_authlist_nid, -1) != -1) {
17271758
has_tn_authlist = 1;
17281759
} else {
17291760
/*
@@ -1766,6 +1797,7 @@ static int validate_certificate(X509 *cert, STACK_OF(X509) *certchain)
17661797
LM_ERR("Error initializing verification context\n");
17671798
return -1;
17681799
}
1800+
X509_STORE_CTX_set_verify_cb(verify_ctx, verify_callback);
17691801

17701802
rc = X509_verify_cert(verify_ctx);
17711803

@@ -1838,7 +1870,7 @@ static int verify_signature(X509 *cert,
18381870

18391871
sig = ECDSA_SIG_new();
18401872

1841-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
1873+
#if defined(WOLFSSL_VERSION) || OPENSSL_VERSION_NUMBER < 0x10100000L
18421874
/* R and S components are already initialised by ECDSA_SIG_new() so
18431875
* they should be passed to BN_bin2bn() */
18441876
r_int = sig->r;
@@ -1858,7 +1890,7 @@ static int verify_signature(X509 *cert,
18581890
goto error;
18591891
}
18601892

1861-
#if OPENSSL_VERSION_NUMBER > 0x10100000L
1893+
#if !defined(WOLFSSL_VERSION) && OPENSSL_VERSION_NUMBER >= 0x10100000L
18621894
/* set the R and S components as they were not initialised by ECDSA_SIG_new() */
18631895
ECDSA_SIG_set0(sig, r_int, s_int);
18641896
#endif

0 commit comments

Comments
 (0)