From d8249fb2cb0688968870d215a49a00cfe91ba6ee Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 11:09:24 +0000 Subject: [PATCH 01/28] openldap 2.6.13: enable CRL with --enable-openldap, update workflow OpenLDAP's TLSCRLCheck maps to X509_STORE_set_flags(X509_V_FLAG_CRL_CHECK), which needs HAVE_CRL. Force CRL on for --enable-openldap like the other application recipes do. --- .github/workflows/openldap.yml | 2 ++ configure.ac | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/openldap.yml b/.github/workflows/openldap.yml index aa4a2b36950..83ca3952bff 100644 --- a/.github/workflows/openldap.yml +++ b/.github/workflows/openldap.yml @@ -55,6 +55,8 @@ jobs: git_ref: OPENLDAP_REL_ENG_2_6_7 - osp_ref: 2.6.9 git_ref: OPENLDAP_REL_ENG_2_6_9 + - osp_ref: 2.6.13 + git_ref: OPENLDAP_REL_ENG_2_6_13 name: ${{ matrix.osp_ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 diff --git a/configure.ac b/configure.ac index f825af4ff6e..ec0bd60d4b8 100644 --- a/configure.ac +++ b/configure.ac @@ -9904,7 +9904,7 @@ if test "x$ENABLED_NGINX" = "xyes" || test "x$ENABLED_HAPROXY" = "xyes" || \ test "x$ENABLED_OPENVPN" = "xyes" || test "x$ENABLED_WPAS" != "xno" || \ test "x$ENABLED_LIGHTY" = "xyes" || test "x$ENABLED_NETSNMP" = "xyes" || \ test "x$ENABLED_KRB" = "xyes" || test "x$ENABLED_STRONGSWAN" = "xyes" || \ - test "x$ENABLED_MOSQUITTO" = "xyes" + test "x$ENABLED_MOSQUITTO" = "xyes" || test "x$ENABLED_OPENLDAP" = "xyes" then ENABLED_CRL=yes fi From 35a982d9469691dc1a953e3915325b8c936b7b69 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 11:09:11 +0000 Subject: [PATCH 02/28] msmtp 1.8.32: add to workflow matrix --- .github/workflows/msmtp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/msmtp.yml b/.github/workflows/msmtp.yml index bce9abf8815..1713503b902 100644 --- a/.github/workflows/msmtp.yml +++ b/.github/workflows/msmtp.yml @@ -46,7 +46,7 @@ jobs: strategy: fail-fast: false matrix: - ref: [ 1.8.28 ] + ref: [ 1.8.28, 1.8.32 ] name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 From 73a87a67197121a0cbc04dc5e674f5304e31c3b0 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 12:46:18 +0000 Subject: [PATCH 03/28] libvncserver 0.9.15: allow DH_compute_key without generator, update workflow OpenSSL's DH_compute_key only needs p and priv_key. libvncserver's ARD authentication sets just those and failed in SetDhInternal. Make g optional there but leave the key marked unsynced so a generator set later is picked up, and require g in the paths that need it. --- .github/workflows/libvncserver.yml | 2 +- src/pk.c | 29 ++++++++++++++++++----------- tests/api/test_ossl_dh.c | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/.github/workflows/libvncserver.yml b/.github/workflows/libvncserver.yml index 609822ee857..1f0547143b3 100644 --- a/.github/workflows/libvncserver.yml +++ b/.github/workflows/libvncserver.yml @@ -48,7 +48,7 @@ jobs: strategy: fail-fast: false matrix: - ref: [ 0.9.13, 0.9.14 ] + ref: [ 0.9.13, 0.9.14, 0.9.15 ] name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 diff --git a/src/pk.c b/src/pk.c index 5a852aff4c9..5cab5967a4f 100644 --- a/src/pk.c +++ b/src/pk.c @@ -2937,8 +2937,8 @@ WOLFSSL_DH* wolfSSL_DH_dup(WOLFSSL_DH* dh) WOLFSSL_ENTER("wolfSSL_DH_dup"); - /* Validate parameters. */ - if (dh == NULL) { + /* Validate parameters. Duplicating needs the full parameter set. */ + if ((dh == NULL) || (dh->p == NULL) || (dh->g == NULL)) { WOLFSSL_ERROR_MSG("Bad parameter"); err = 1; } @@ -3708,7 +3708,7 @@ int wolfSSL_i2d_DHparams(const WOLFSSL_DH *dh, unsigned char **out) int err = 0; /* Validate parameters. */ - if (dh == NULL) { + if ((dh == NULL) || (dh->p == NULL) || (dh->g == NULL)) { WOLFSSL_ERROR_MSG("Bad parameters"); err = 1; } @@ -3752,7 +3752,7 @@ int wolfSSL_i2d_DHparams(const WOLFSSL_DH *dh, unsigned char **out) WOLFSSL_ENTER("wolfSSL_i2d_DHparams"); /* Validate parameters. */ - if (dh == NULL) { + if ((dh == NULL) || (dh->p == NULL) || (dh->g == NULL)) { WOLFSSL_ERROR_MSG("Bad parameters"); len = 0; } @@ -4029,8 +4029,13 @@ static int wolfssl_dhparams_to_der(WOLFSSL_DH* dh, unsigned char** out, (void)heap; + /* Validate parameters. */ + if ((dh->p == NULL) || (dh->g == NULL)) { + WOLFSSL_ERROR_MSG("Bad parameters"); + err = 1; + } /* Set internal parameters based on external parameters. */ - if ((dh->inSet == 0) && (SetDhInternal(dh) != 1)) { + if ((!err) && (dh->inSet == 0) && (SetDhInternal(dh) != 1)) { WOLFSSL_ERROR_MSG("Unable to set internal DH structure"); err = 1; } @@ -4233,8 +4238,8 @@ int SetDhInternal(WOLFSSL_DH* dh) WOLFSSL_ENTER("SetDhInternal"); - /* Validate parameters. */ - if ((dh == NULL) || (dh->p == NULL) || (dh->g == NULL)) { + /* Validate parameters. g is optional: key agreement only needs p. */ + if ((dh == NULL) || (dh->p == NULL)) { WOLFSSL_ERROR_MSG("Bad function arguments"); ret = WOLFSSL_FATAL_ERROR; } @@ -4254,8 +4259,8 @@ int SetDhInternal(WOLFSSL_DH* dh) ret = WOLFSSL_FATAL_ERROR; } } - if (ret == 1) { - /* Transfer generator. */ + /* Transfer generator if available. */ + if ((ret == 1) && (dh->g != NULL)) { if (wolfssl_bn_get_value(dh->g, &key->g) != 1) { ret = WOLFSSL_FATAL_ERROR; } @@ -4286,8 +4291,10 @@ int SetDhInternal(WOLFSSL_DH* dh) #endif /* WOLFSSL_DH_EXTRA */ if (ret == 1) { - /* On success record that the internal values have been set. */ - dh->inSet = 1; + /* Record that the internal values have been set. Without a generator + * the internal key is incomplete, so keep it unset to force a + * re-sync when the generator is added later. */ + dh->inSet = (dh->g != NULL); } return ret; diff --git a/tests/api/test_ossl_dh.c b/tests/api/test_ossl_dh.c index 5574d9d3ceb..c6d47da62d2 100644 --- a/tests/api/test_ossl_dh.c +++ b/tests/api/test_ossl_dh.c @@ -89,6 +89,32 @@ int test_wolfSSL_DH(void) ExpectIntEQ(DH_compute_key_padded(buf2, dh->pub_key, dh2), sz1); ExpectIntEQ(XMEMCMP(buf, buf2, (size_t)sz1), 0); + /* Key agreement with only p and priv_key set (no g). */ + if (EXPECT_SUCCESS()) { + DH *dh3 = NULL; + + ExpectNotNull(dh3 = DH_new()); + if (dh3 != NULL) { + ExpectNotNull(dh3->p = BN_dup(dh->p)); + ExpectNotNull(dh3->priv_key = BN_dup(dh->priv_key)); + } + ExpectIntEQ(DH_compute_key_padded(buf2, dh2->pub_key, dh3), sz1); + ExpectIntEQ(XMEMCMP(buf, buf2, (size_t)sz1), 0); + /* Encoding and duplicating parameters still need g. */ + ExpectIntEQ(i2d_DHparams(dh3, NULL), 0); + ExpectNull(DHparams_dup(dh3)); + /* Adding g afterwards must give a usable key pair. */ + if (dh3 != NULL) { + ExpectNotNull(dh3->g = BN_dup(dh->g)); + } + ExpectIntEQ(DH_generate_key(dh3), 1); + if (dh3 != NULL) { + ExpectIntNE(BN_is_zero(dh3->pub_key), 1); + ExpectIntGT(DH_compute_key(buf2, dh3->pub_key, dh2), 0); + } + DH_free(dh3); + } + if (dh2 != NULL) DH_free(dh2); } From 6d4814d344007a6f7c1e99dedde3d498b0c747ec Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 11:05:34 +0000 Subject: [PATCH 04/28] jwt-cpp 0.7.2: add to workflow matrix --- .github/workflows/jwt-cpp.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/jwt-cpp.yml b/.github/workflows/jwt-cpp.yml index 857eb7371fc..fb4050a9abb 100644 --- a/.github/workflows/jwt-cpp.yml +++ b/.github/workflows/jwt-cpp.yml @@ -50,6 +50,8 @@ jobs: fail-fast: false matrix: config: + - ref: 0.7.2 + runner: ubuntu-24.04 - ref: 0.7.0 runner: ubuntu-24.04 - ref: 0.6.0 From dca58e7108deb49bd9cee1b9f91d8b08b6d596d9 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 10:54:30 +0000 Subject: [PATCH 05/28] net-snmp 5.9.5.2: add to workflow matrix net-snmp 5.9.5 and newer has wolfSSL support upstream, so the new entry builds without an OSP patch. The patch file is now a matrix variable. --- .github/workflows/net-snmp.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/net-snmp.yml b/.github/workflows/net-snmp.yml index 9364dbe8095..58e817cf774 100644 --- a/.github/workflows/net-snmp.yml +++ b/.github/workflows/net-snmp.yml @@ -49,6 +49,11 @@ jobs: # List of releases to test include: - ref: 5.9.3 + patch_file: $GITHUB_WORKSPACE/osp/net-snmp/5.9.3.patch + test_opts: -e 'agentxperl' + # 5.9.5+ has wolfSSL support upstream, no patch needed + - ref: 5.9.5.2 + patch_file: '' test_opts: -e 'agentxperl' name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} @@ -78,7 +83,7 @@ jobs: repository: net-snmp/net-snmp ref: v${{ matrix.ref }} path: net-snmp - patch-file: $GITHUB_WORKSPACE/osp/net-snmp/${{ matrix.ref }}.patch + patch-file: ${{ matrix.patch_file }} configure: --disable-shared --with-wolfssl=$GITHUB_WORKSPACE/build-dir check: false From 531388d88b44dc7766901ae3b13a0c285b6ac5c8 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 12:22:14 +0000 Subject: [PATCH 06/28] bind9 9.20.23: accept EVP_PKEY_HMAC in EVP_PKEY_new_raw_private_key OpenSSL accepts EVP_PKEY_HMAC there. wolfSSL only accepted Ed25519, Ed448, X25519 and X448. Forward HMAC keys to wolfSSL_EVP_PKEY_new_mac_key(). Add bind9 9.20.23 to the bind workflow matrix. --- .github/workflows/bind.yml | 2 +- tests/api/test_evp_pkey.c | 18 ++++++++++++++++++ wolfcrypt/src/evp_pk.c | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.github/workflows/bind.yml b/.github/workflows/bind.yml index fc9c9752ee2..ad70d7f6594 100644 --- a/.github/workflows/bind.yml +++ b/.github/workflows/bind.yml @@ -48,7 +48,7 @@ jobs: fail-fast: false matrix: # List of releases to test - ref: [ 9.18.0, 9.18.28, 9.18.33, 9.20.11 ] + ref: [ 9.18.0, 9.18.28, 9.18.33, 9.20.11, 9.20.23 ] name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index aefd4908b54..d5302088547 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -508,6 +508,24 @@ int test_wolfSSL_EVP_PKEY_new_mac_key(void) ExpectIntEQ((int)checkPwSz, 0); wolfSSL_EVP_PKEY_free(key); key = NULL; + + /* EVP_PKEY_new_raw_private_key accepts HMAC keys too. */ + ExpectNotNull(key = wolfSSL_EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, + NULL, pw, (size_t)pwSz)); + ExpectIntEQ(EVP_PKEY_id(key), EVP_PKEY_HMAC); + checkPw = NULL; + checkPwSz = 0; + ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz)); + ExpectIntEQ((int)checkPwSz, pwSz); + ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0); + wolfSSL_EVP_PKEY_free(key); + key = NULL; + + ExpectNotNull(key = wolfSSL_EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, + NULL, NULL, 0)); + ExpectIntEQ(key->pkey_sz, 0); + wolfSSL_EVP_PKEY_free(key); + key = NULL; #endif /* OPENSSL_EXTRA */ return EXPECT_RESULT(); } diff --git a/wolfcrypt/src/evp_pk.c b/wolfcrypt/src/evp_pk.c index 32290ae00cf..b18635da34e 100644 --- a/wolfcrypt/src/evp_pk.c +++ b/wolfcrypt/src/evp_pk.c @@ -597,6 +597,16 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_private_key(int type, (void)e; WOLFSSL_ENTER("wolfSSL_EVP_PKEY_new_raw_private_key"); + #ifdef OPENSSL_EXTRA + /* HMAC keys are raw octets, same as EVP_PKEY_new_mac_key. */ + if (type == WC_EVP_PKEY_HMAC) { + if (len > (size_t)INT_MAX) { + return NULL; + } + return wolfSSL_EVP_PKEY_new_mac_key(type, e, priv, (int)len); + } + #endif /* OPENSSL_EXTRA */ + if (priv == NULL || len == 0) { return NULL; } From d13aa353acd80f98408c5f0d8b3f5b73db8eb671 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 12:11:12 +0000 Subject: [PATCH 07/28] libspdm 3.8.2: add EVP_PKEY_dup, parse basicConstraints strings, update workflow libspdm 3.8.2 calls EVP_PKEY_dup() unconditionally when generating a CSR and builds the CSR's basicConstraints extension from an OpenSSL style string with X509V3_EXT_nconf_nid(). - Record whether an EVP_PKEY holds private key material in a new isPriv flag, set wherever a key is built. The struct is zeroed on creation, so a key that holds nothing reads as public. - Add wolfSSL_EVP_PKEY_dup(): a deep copy that re-decodes the cached DER the same way it was made, so no failed decode is left on the error queue. - Parse "CA:TRUE|FALSE[,pathlen:N]" in createExtFromStr(). The format is OpenSSL's, so the parser lives in src/x509.c next to its only caller. Spaces and tabs around a token are ignored, CA is required, each token may appear once and the path length is capped at WOLFSSL_MAX_PATH_LEN. - Accept the "critical," prefix for every NID in X509V3_EXT_nconf_nid() and X509V3_EXT_nconf(), and only as a prefix, as OpenSSL does. - Add unit tests and libspdm 3.8.2 to the workflow matrix. --- .github/workflows/libspdm.yml | 2 +- src/x509.c | 126 ++++++++++++++++ tests/api/test_evp_pkey.c | 260 +++++++++++++++++++++++++++++++++ tests/api/test_evp_pkey.h | 2 + tests/api/test_ossl_x509_ext.c | 55 +++++++ wolfcrypt/src/evp.c | 16 ++ wolfcrypt/src/evp_pk.c | 47 +++++- wolfssl/openssl/evp.h | 2 + wolfssl/ssl.h | 1 + 9 files changed, 508 insertions(+), 3 deletions(-) diff --git a/.github/workflows/libspdm.yml b/.github/workflows/libspdm.yml index 66318ba68d7..936656b61f7 100644 --- a/.github/workflows/libspdm.yml +++ b/.github/workflows/libspdm.yml @@ -48,7 +48,7 @@ jobs: fail-fast: false matrix: # List of releases to test - ref: [ 3.7.0 ] + ref: [ 3.7.0, 3.8.2 ] name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 diff --git a/src/x509.c b/src/x509.c index 37b1e263da7..b9ac3e829ba 100644 --- a/src/x509.c +++ b/src/x509.c @@ -3443,6 +3443,124 @@ void wolfSSL_X509V3_set_ctx_nodb(WOLFSSL_X509V3_CTX* ctx) #endif /* !NO_WOLFSSL_STUB */ #ifdef OPENSSL_EXTRA +static const char* wolfssl_x509v3_skip_ws(const char* s) +{ + while (*s == ' ' || *s == '\t') + s++; + return s; +} + +/* Check for a leading "critical," in an extension value. Whitespace is + * allowed around the keyword and the comma. Advances value past the prefix + * and any leading whitespace. + * + * @return 1 when the value is critical. + * @return 0 otherwise. + */ +static int wolfssl_x509v3_check_critical(const char** value) +{ + const char* s = wolfssl_x509v3_skip_ws(*value); + int crit = 0; + + if (XSTRNCMP(s, "critical", 8) == 0) { + const char* p = wolfssl_x509v3_skip_ws(s + 8); + if (*p == ',') { + s = wolfssl_x509v3_skip_ws(p + 1); + crit = 1; + } + } + + *value = s; + return crit; +} + +/* Set the basicConstraints extension value from an OpenSSL style string. + * Format: "CA:TRUE|FALSE[,pathlen:N]". Spaces and tabs around a token are + * ignored. CA is required. Each token may appear only once. + * + * @return WOLFSSL_SUCCESS on success. + * @return WOLFSSL_FAILURE on bad value or allocation error. + */ +static int wolfssl_ext_bc_from_str(WOLFSSL_X509_EXTENSION* ext, + const char* value) +{ + const char* s = value; + int isCa = 0; + int caSet = 0; + int pathLen = 0; + int pathLenSet = 0; + + ext->obj = wolfSSL_OBJ_nid2obj(WC_NID_basic_constraints); + if (ext->obj == NULL) { + WOLFSSL_MSG("wolfSSL_OBJ_nid2obj failed"); + return WOLFSSL_FAILURE; + } + + for (;;) { + const char* tok; + const char* end; + const char* next; + size_t len; + + tok = wolfssl_x509v3_skip_ws(s); + end = tok; + while ((*end != '\0') && (*end != ',')) + end++; + next = end; + while ((end > tok) && ((end[-1] == ' ') || (end[-1] == '\t'))) + end--; + len = (size_t)(end - tok); + + if ((len == 7) && (XSTRNCASECMP(tok, "CA:TRUE", 7) == 0)) { + if (caSet) + return WOLFSSL_FAILURE; + isCa = 1; + caSet = 1; + } + else if ((len == 8) && (XSTRNCASECMP(tok, "CA:FALSE", 8) == 0)) { + if (caSet) + return WOLFSSL_FAILURE; + caSet = 1; + } + else if ((len > 8) && (XSTRNCASECMP(tok, "pathlen:", 8) == 0)) { + const char* num; + + if (pathLenSet) + return WOLFSSL_FAILURE; + for (num = tok + 8; num < end; num++) { + if ((*num < '0') || (*num > '9')) + return WOLFSSL_FAILURE; + pathLen = (pathLen * 10) + (*num - '0'); + if (pathLen > WOLFSSL_MAX_PATH_LEN) + return WOLFSSL_FAILURE; + } + pathLenSet = 1; + } + else { + return WOLFSSL_FAILURE; + } + + if (*next != ',') + break; + s = next + 1; + } + + if (!caSet) { + WOLFSSL_MSG("basicConstraints value missing CA"); + return WOLFSSL_FAILURE; + } + + ext->obj->ca = isCa; + if (pathLenSet) { + ext->obj->pathlen = wolfSSL_ASN1_INTEGER_new(); + if (ext->obj->pathlen == NULL) + return WOLFSSL_FAILURE; + ext->obj->pathlen->length = pathLen; + } + + return WOLFSSL_SUCCESS; +} + static WOLFSSL_X509_EXTENSION* createExtFromStr(int nid, const char *value) { WOLFSSL_X509_EXTENSION* ext; @@ -3453,6 +3571,7 @@ static WOLFSSL_X509_EXTENSION* createExtFromStr(int nid, const char *value) return NULL; } ext->value.nid = nid; + ext->crit = wolfssl_x509v3_check_critical(&value); switch (nid) { case WC_NID_subject_key_identifier: @@ -3519,6 +3638,12 @@ static WOLFSSL_X509_EXTENSION* createExtFromStr(int nid, const char *value) } ext->value.type = EXT_KEY_USAGE_OID; break; + case WC_NID_basic_constraints: + if (wolfssl_ext_bc_from_str(ext, value) != WOLFSSL_SUCCESS) { + WOLFSSL_MSG("wolfssl_ext_bc_from_str error"); + goto err_cleanup; + } + break; default: WOLFSSL_MSG("invalid or unsupported NID"); goto err_cleanup; @@ -6980,6 +7105,7 @@ static WOLFSSL_EVP_PKEY* X509DecodePubKey(WOLFSSL_X509* x509) } XMEMCPY(key->pkey.ptr, x509->pubKey.buffer, x509->pubKey.length); key->pkey_sz = (int)x509->pubKey.length; + key->isPriv = 0; #ifdef HAVE_ECC key->pkey_curve = (int)x509->pkCurveOID; diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index d5302088547..ef637eca6d6 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -832,6 +832,266 @@ int test_EVP_PKEY_cmp(void) return EXPECT_RESULT(); } +#if defined(OPENSSL_EXTRA) && \ + ((!defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)) || \ + (defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256))) +/* Check dup is a distinct key with identical encoding, usable after src is + * freed. */ +static int test_EVP_PKEY_dup_check(EVP_PKEY** src, int id, int priv) +{ + EXPECT_DECLS; + EVP_PKEY* dup = NULL; + unsigned char* srcDer = NULL; + unsigned char* dupDer = NULL; + int srcSz = 0; + int dupSz = 0; + + ERR_clear_error(); + ExpectNotNull(dup = EVP_PKEY_dup(*src)); + /* A successful dup must not leave anything on the error queue. */ + ExpectIntEQ(ERR_peek_error(), 0); + ExpectPtrNE(dup, *src); + ExpectIntEQ(EVP_PKEY_id(dup), id); + if (priv) { + ExpectIntGT(srcSz = i2d_PrivateKey(*src, &srcDer), 0); + } + else { + ExpectIntGT(srcSz = i2d_PUBKEY(*src, &srcDer), 0); + } + EVP_PKEY_free(*src); + *src = NULL; + if (priv) { + ExpectIntGT(dupSz = i2d_PrivateKey(dup, &dupDer), 0); + } + else { + ExpectIntGT(dupSz = i2d_PUBKEY(dup, &dupDer), 0); + } + ExpectIntEQ(srcSz, dupSz); + ExpectIntEQ(XMEMCMP(srcDer, dupDer, (size_t)srcSz), 0); +#ifndef NO_RSA + if (id == EVP_PKEY_RSA) { + ExpectNotNull(EVP_PKEY_get0_RSA(dup)); + } +#endif +#ifdef HAVE_ECC + if (id == EVP_PKEY_EC) { + ExpectNotNull(EVP_PKEY_get0_EC_KEY(dup)); + } +#endif + XFREE(srcDer, NULL, DYNAMIC_TYPE_OPENSSL); + XFREE(dupDer, NULL, DYNAMIC_TYPE_OPENSSL); + EVP_PKEY_free(dup); + + return EXPECT_RESULT(); +} +#endif /* OPENSSL_EXTRA && (RSA 2048 or ECC 256 cert buffers) */ + +/* d2i_evp_pkey() only knows DSA and DH in these builds. */ +#if defined(OPENSSL_EXTRA) && !defined(NO_DSA) && \ + defined(USE_CERT_BUFFERS_2048) && (defined(WOLFSSL_QT) || \ + defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)) + #define TEST_EVP_PKEY_DUP_DSA +#endif +#if defined(OPENSSL_EXTRA) && !defined(NO_DH) && \ + defined(USE_CERT_BUFFERS_2048) && (defined(WOLFSSL_QT) || \ + defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)) && \ + (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) + #define TEST_EVP_PKEY_DUP_DH +#endif + +#if defined(TEST_EVP_PKEY_DUP_DSA) || defined(TEST_EVP_PKEY_DUP_DH) +/* Check dup carries the same cached DER and the same private/public state, + * and that it left the error queue clean. */ +static int test_EVP_PKEY_dup_der_check(EVP_PKEY* src, int id, int priv) +{ + EXPECT_DECLS; + EVP_PKEY* dup = NULL; + + ERR_clear_error(); + ExpectNotNull(dup = EVP_PKEY_dup(src)); + /* A successful dup must not leave anything on the error queue. */ + ExpectIntEQ(ERR_peek_error(), 0); + ExpectPtrNE(dup, src); + ExpectIntEQ(EVP_PKEY_id(dup), id); + ExpectIntEQ(dup->isPriv, priv); + ExpectIntEQ(dup->pkey_sz, src->pkey_sz); + ExpectIntEQ(XMEMCMP(dup->pkey.ptr, src->pkey.ptr, (size_t)src->pkey_sz), + 0); + + EVP_PKEY_free(dup); + + return EXPECT_RESULT(); +} +#endif /* TEST_EVP_PKEY_DUP_DSA || TEST_EVP_PKEY_DUP_DH */ + +int test_wolfSSL_EVP_PKEY_dup(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) + EVP_PKEY* key = NULL; + EVP_PKEY* dup = NULL; + const unsigned char* in; +#if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \ + defined(WOLFSSL_KEY_TO_DER) + EVP_PKEY* set1 = NULL; + RSA* rsa = NULL; +#endif + +#if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) + in = client_key_der_2048; + ExpectNotNull(key = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &in, + (long)sizeof_client_key_der_2048)); + ExpectIntEQ(test_EVP_PKEY_dup_check(&key, EVP_PKEY_RSA, 1), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + + in = client_keypub_der_2048; + ExpectNotNull(key = d2i_PUBKEY(NULL, &in, + (long)sizeof_client_keypub_der_2048)); + ExpectIntEQ(test_EVP_PKEY_dup_check(&key, EVP_PKEY_RSA, 0), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; +#endif + +#if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256) + in = ecc_clikey_der_256; + ExpectNotNull(key = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &in, + (long)sizeof_ecc_clikey_der_256)); + ExpectIntEQ(test_EVP_PKEY_dup_check(&key, EVP_PKEY_EC, 1), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + + in = ecc_clikeypub_der_256; + ExpectNotNull(key = d2i_PUBKEY(NULL, &in, + (long)sizeof_ecc_clikeypub_der_256)); + ExpectIntEQ(test_EVP_PKEY_dup_check(&key, EVP_PKEY_EC, 0), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; +#endif + +#ifdef TEST_EVP_PKEY_DUP_DSA + in = dsa_key_der_2048; + ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_DSA, NULL, &in, + (long)sizeof_dsa_key_der_2048)); + ExpectIntEQ(test_EVP_PKEY_dup_der_check(key, EVP_PKEY_DSA, 1), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + + in = dsa_pub_key_der_2048; + ExpectNotNull(key = d2i_PublicKey(EVP_PKEY_DSA, NULL, &in, + (long)sizeof_dsa_pub_key_der_2048)); + ExpectIntEQ(test_EVP_PKEY_dup_der_check(key, EVP_PKEY_DSA, 0), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; +#endif /* TEST_EVP_PKEY_DUP_DSA */ + +#ifdef TEST_EVP_PKEY_DUP_DH + in = dh_key_der_2048; + ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_DH, NULL, &in, + (long)sizeof_dh_key_der_2048)); + ExpectIntEQ(test_EVP_PKEY_dup_der_check(key, EVP_PKEY_DH, 1), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; +#endif /* TEST_EVP_PKEY_DUP_DH */ + + /* HMAC key */ + ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, + (const unsigned char*)"password", 8)); + ExpectNotNull(dup = EVP_PKEY_dup(key)); + ExpectPtrNE(dup, key); + ExpectIntEQ(EVP_PKEY_id(dup), EVP_PKEY_HMAC); + ExpectIntEQ(dup->pkey_sz, 8); + ExpectIntEQ(XMEMCMP(dup->pkey.ptr, "password", 8), 0); + EVP_PKEY_free(dup); + dup = NULL; + EVP_PKEY_free(key); + key = NULL; + +#if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \ + defined(WOLFSSL_KEY_TO_DER) + /* EVP_PKEY_set1_RSA() re-encodes the RSA key, so the flag has to follow + * the encoder it picks. */ + in = client_keypub_der_2048; + ExpectNotNull(key = d2i_PUBKEY(NULL, &in, + (long)sizeof_client_keypub_der_2048)); + ExpectNotNull(rsa = EVP_PKEY_get1_RSA(key)); + ExpectNotNull(set1 = EVP_PKEY_new()); + ExpectIntEQ(EVP_PKEY_set1_RSA(set1, rsa), WOLFSSL_SUCCESS); + ExpectIntEQ(set1->isPriv, 0); + ExpectNotNull(dup = EVP_PKEY_dup(set1)); + ExpectIntEQ(dup->isPriv, 0); + EVP_PKEY_free(dup); + dup = NULL; + EVP_PKEY_free(set1); + set1 = NULL; + RSA_free(rsa); + rsa = NULL; + EVP_PKEY_free(key); + key = NULL; + + in = client_key_der_2048; + ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &in, + (long)sizeof_client_key_der_2048)); + ExpectNotNull(rsa = EVP_PKEY_get1_RSA(key)); + ExpectNotNull(set1 = EVP_PKEY_new()); + ExpectIntEQ(EVP_PKEY_set1_RSA(set1, rsa), WOLFSSL_SUCCESS); + ExpectIntEQ(set1->isPriv, 1); + ExpectNotNull(dup = EVP_PKEY_dup(set1)); + ExpectIntEQ(dup->isPriv, 1); + EVP_PKEY_free(dup); + dup = NULL; + EVP_PKEY_free(set1); + set1 = NULL; + RSA_free(rsa); + rsa = NULL; + EVP_PKEY_free(key); + key = NULL; +#endif /* !NO_RSA && USE_CERT_BUFFERS_2048 && WOLFSSL_KEY_TO_DER */ + + /* The constructors record whether the key holds private material. */ +#if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) + in = client_key_der_2048; + ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &in, + (long)sizeof_client_key_der_2048)); + ExpectIntEQ(key->isPriv, 1); + EVP_PKEY_free(key); + key = NULL; + + in = client_keypub_der_2048; + ExpectNotNull(key = d2i_PublicKey(EVP_PKEY_RSA, NULL, &in, + (long)sizeof_client_keypub_der_2048)); + ExpectIntEQ(key->isPriv, 0); + EVP_PKEY_free(key); + key = NULL; +#endif + ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, + (const unsigned char*)"password", 8)); + ExpectIntEQ(key->isPriv, 1); + EVP_PKEY_free(key); + key = NULL; + + /* A new key holds nothing, so it is public by default. */ + ExpectNotNull(key = EVP_PKEY_new()); + ExpectIntEQ(key->isPriv, 0); + EVP_PKEY_free(key); + key = NULL; + + /* Bad cases */ + ExpectNull(EVP_PKEY_dup(NULL)); + ExpectNotNull(key = EVP_PKEY_new()); + ExpectNull(EVP_PKEY_dup(key)); + EVP_PKEY_free(key); +#endif /* OPENSSL_EXTRA */ + return EXPECT_RESULT(); +} + int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void) { EXPECT_DECLS; diff --git a/tests/api/test_evp_pkey.h b/tests/api/test_evp_pkey.h index 6cf395cd426..5f2ea234893 100644 --- a/tests/api/test_evp_pkey.h +++ b/tests/api/test_evp_pkey.h @@ -37,6 +37,7 @@ int test_wolfSSL_EVP_PKEY_new_mac_key(void); int test_wolfSSL_EVP_PKEY_hkdf(void); int test_wolfSSL_EVP_PBE_scrypt(void); int test_EVP_PKEY_cmp(void); +int test_wolfSSL_EVP_PKEY_dup(void); int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void); int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void); int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void); @@ -100,6 +101,7 @@ int test_wolfSSL_CTX_use_PrivateKey_pkcs8_repopulate(void); TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_hkdf), \ TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PBE_scrypt), \ TEST_DECL_GROUP("evp_pkey", test_EVP_PKEY_cmp), \ + TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_dup), \ TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_set1_get1_DSA), \ TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY), \ TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_get0_EC_KEY), \ diff --git a/tests/api/test_ossl_x509_ext.c b/tests/api/test_ossl_x509_ext.c index 5391271c3b3..9cf1c10c987 100644 --- a/tests/api/test_ossl_x509_ext.c +++ b/tests/api/test_ossl_x509_ext.c @@ -1223,6 +1223,7 @@ int test_wolfSSL_X509V3_EXT_nconf(void) size_t i; X509_EXTENSION* ext = NULL; X509* x509 = NULL; + X509* x509Crit = NULL; unsigned int keyUsageFlags; unsigned int extKeyUsageFlags; WOLFSSL_CONF conf; @@ -1230,6 +1231,7 @@ int test_wolfSSL_X509V3_EXT_nconf(void) #ifndef NO_WOLFSSL_STUB WOLFSSL_LHASH lhash; #endif + char bcTooLong[32]; ExpectNotNull(x509 = X509_new()); ExpectNull(X509V3_EXT_nconf(NULL, NULL, ext_names[0], NULL)); @@ -1274,6 +1276,59 @@ int test_wolfSSL_X509V3_EXT_nconf(void) ext = NULL; } + /* basicConstraints from string */ + ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + "CA:FALSE")); + if (ext != NULL) { + ExpectNotNull(ext->obj); + ExpectIntEQ(ext->obj->type, NID_basic_constraints); + ExpectIntEQ(ext->obj->ca, 0); + ExpectNull(ext->obj->pathlen); + ExpectIntEQ(ext->crit, 0); + } + X509_EXTENSION_free(ext); + ext = NULL; + ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, + NID_basic_constraints, "critical, CA:TRUE, pathlen:2")); + if (ext != NULL) { + ExpectNotNull(ext->obj); + ExpectIntEQ(ext->obj->ca, 1); + ExpectNotNull(ext->obj->pathlen); + ExpectIntEQ(ext->obj->pathlen->length, 2); + ExpectIntEQ(ext->crit, 1); + } + ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_X509_get_pathLength(x509), 2); + X509_EXTENSION_free(ext); + ext = NULL; + ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + "CA:MAYBE")); + ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + "pathlen:1")); + ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + "CA:TRUE,pathlen:x")); + XSNPRINTF(bcTooLong, sizeof(bcTooLong), "CA:TRUE,pathlen:%d", + WOLFSSL_MAX_PATH_LEN + 1); + ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + bcTooLong)); + /* critical is only accepted as a prefix, and only once. */ + ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + "CA:TRUE,critical")); + ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, + "critical,critical,CA:TRUE")); + + /* The critical prefix is handled for every extension type. */ + ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, NID_key_usage, + "critical,digitalSignature")); + ExpectIntEQ(ext->crit, 1); + ExpectNotNull(x509Crit = X509_new()); + ExpectIntEQ(X509_add_ext(x509Crit, ext, -1), WOLFSSL_SUCCESS); + ExpectIntEQ(X509_get_key_usage(x509Crit), KU_DIGITAL_SIGNATURE); + X509_free(x509Crit); + x509Crit = NULL; + X509_EXTENSION_free(ext); + ext = NULL; + /* Test adding extension to X509 */ for (i = 0; i < ext_nids_count; i++) { ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index 684f31e35a7..c2bd93c0776 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -4177,6 +4177,7 @@ int wolfSSL_EVP_PKEY_keygen(WOLFSSL_EVP_PKEY_CTX *ctx, pkey->pkey.ptr = (char*)edDer; pkey->pkey_sz = edDerSz; pkey->pkcs8HeaderSz = (word16)hdrIdx; + pkey->isPriv = 1; ret = WOLFSSL_SUCCESS; } else { @@ -4956,6 +4957,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_mac_key(int type, WOLFSSL_ENGINE* e, } pkey->pkey_sz = keylen; pkey->type = pkey->save_type = type; + pkey->isPriv = 1; } } @@ -5007,6 +5009,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_CMAC_key(WOLFSSL_ENGINE* e, pkey->pkey_sz = (int)len; pkey->type = pkey->save_type = WC_EVP_PKEY_CMAC; pkey->cmacCtx = ctx; + pkey->isPriv = 1; } } else { @@ -9737,6 +9740,7 @@ static void clearEVPPkeyKeys(WOLFSSL_EVP_PKEY *pkey) if(pkey == NULL) return; WOLFSSL_ENTER("clearEVPPkeyKeys"); + pkey->isPriv = 0; #ifndef NO_RSA if (pkey->rsa != NULL && pkey->ownRsa == 1) { wolfSSL_RSA_free(pkey->rsa); @@ -9962,6 +9966,7 @@ static int PopulateRSAEvpPkeyDer(WOLFSSL_EVP_PKEY *pkey) } else { pkey->pkey_sz = derSz; + pkey->isPriv = (rsa->type == RSA_PRIVATE); return WOLFSSL_SUCCESS; } } @@ -10101,6 +10106,7 @@ int wolfSSL_EVP_PKEY_set1_DSA(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DSA *key) return WOLFSSL_FAILURE; } pkey->pkey_sz = derSz; + pkey->isPriv = (dsa->type == DSA_PRIVATE); XMEMCPY(pkey->pkey.ptr, derBuf, (size_t)derSz); XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER); @@ -10300,6 +10306,7 @@ int wolfSSL_EVP_PKEY_set1_DH(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DH *key) /* Store DH key into pkey (DER format) */ pkey->pkey.ptr = (char*)derBuf; pkey->pkey_sz = (int)derSz; + pkey->isPriv = (havePrivate != 0); return WOLFSSL_SUCCESS; } @@ -10436,6 +10443,7 @@ static int ECC_populate_EVP_PKEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY *key) pkey->pkey_sz = (int)derSz; pkey->pkey.ptr = (char*)derBuf; pkey->pkcs8HeaderSz = key->pkcs8HeaderSz; + pkey->isPriv = 1; return WOLFSSL_SUCCESS; } else { @@ -10485,6 +10493,7 @@ static int ECC_populate_EVP_PKEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY *key) * it, or the export paths start inside the new * encoding. */ pkey->pkcs8HeaderSz = 0; + pkey->isPriv = 1; return WOLFSSL_SUCCESS; } else { @@ -10552,6 +10561,7 @@ static int ECC_populate_EVP_PKEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY *key) } if (derBuf != NULL) { pkey->pkey_sz = (int)derSz; + pkey->isPriv = 0; return WOLFSSL_SUCCESS; } else { @@ -10602,6 +10612,7 @@ void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx) int wolfSSL_EVP_PKEY_assign_EC_KEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY* key) { int ret; + int isPriv; if (pkey == NULL || key == NULL) return WOLFSSL_FAILURE; @@ -10609,7 +10620,11 @@ int wolfSSL_EVP_PKEY_assign_EC_KEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY* key) /* try and populate public pkey_sz and pkey.ptr */ ret = ECC_populate_EVP_PKEY(pkey, key); if (ret == WOLFSSL_SUCCESS) { /* take ownership of key if can be used */ + /* The encoding just cached describes the new key, so carry its + * private/public state over the clear below. */ + isPriv = pkey->isPriv; clearEVPPkeyKeys(pkey); /* clear out any previous keys */ + pkey->isPriv = isPriv; pkey->type = WC_EVP_PKEY_EC; pkey->ecc = key; @@ -11408,6 +11423,7 @@ int wolfSSL_EVP_PKEY_assign_RSA(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_RSA* key) if (ret >= 0) { pkey->pkey_sz = ret; pkey->pkey.ptr = (char*)derBuf; + pkey->isPriv = 1; } else { /* failure - okay to ignore */ XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER); diff --git a/wolfcrypt/src/evp_pk.c b/wolfcrypt/src/evp_pk.c index b18635da34e..519f134af53 100644 --- a/wolfcrypt/src/evp_pk.c +++ b/wolfcrypt/src/evp_pk.c @@ -55,8 +55,6 @@ static int d2i_make_pkey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem, int prevSz = 0; int ret = 1; - (void)priv; - /* Get or create the EVP PKEY object. */ if (*out != NULL) { pkey = *out; @@ -120,6 +118,7 @@ static int d2i_make_pkey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem, if (ret == 1) { /* Set key type passed in and return object. */ pkey->type = type; + pkey->isPriv = (priv != 0); *out = pkey; } if ((ret == 0) && (*out == NULL)) { @@ -581,6 +580,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_public_key(int type, } XMEMCPY(pkey->pkey.ptr, pub, len); pkey->pkey_sz = (int)len; + pkey->isPriv = 0; return pkey; } @@ -751,6 +751,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_private_key(int type, } XMEMCPY(pkey->pkey.ptr, priv, len); pkey->pkey_sz = (int)len; + pkey->isPriv = 1; return pkey; } @@ -1749,6 +1750,7 @@ static WOLFSSL_EVP_PKEY* d2i_evp_pkey(int type, WOLFSSL_EVP_PKEY** out, local->type = type; local->pkey_sz = (int)inSz; local->pkcs8HeaderSz = pkcs8HeaderSz; + local->isPriv = (priv != 0); local->pkey.ptr = (char*)XMALLOC((size_t)inSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY); if (local->pkey.ptr == NULL) { @@ -1913,6 +1915,45 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out, return d2i_evp_pkey(type, out, in, inSz, 1); } + +/* Deep copy of a key by re-decoding its cached DER. + * + * @param [in] pkey Key to copy. + * @return New WOLFSSL_EVP_PKEY on success. + * @return NULL when pkey is NULL, holds no DER or decoding fails. + */ +WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey) +{ + WOLFSSL_EVP_PKEY* dup = NULL; + const unsigned char* der; + + WOLFSSL_ENTER("wolfSSL_EVP_PKEY_dup"); + + if (pkey == NULL || pkey->pkey.ptr == NULL || pkey->pkey_sz <= 0) { + WOLFSSL_MSG("No key data to duplicate"); + return NULL; + } + + der = (const unsigned char*)pkey->pkey.ptr; + if (pkey->type == WC_EVP_PKEY_HMAC) { + dup = wolfSSL_EVP_PKEY_new_mac_key(WC_EVP_PKEY_HMAC, NULL, der, + pkey->pkey_sz); + } + else { + /* Every path that caches DER records whether it is private, so the + * encoding is decoded the same way it was made. */ + dup = d2i_evp_pkey(pkey->type, NULL, &der, pkey->pkey_sz, + pkey->isPriv); + if (dup != NULL) { + #ifdef HAVE_ECC + dup->pkey_curve = pkey->pkey_curve; + #endif + dup->save_type = pkey->save_type; + } + } + + return dup; +} #endif /* OPENSSL_EXTRA */ #ifdef OPENSSL_ALL @@ -2317,6 +2358,7 @@ WOLFSSL_PKCS8_PRIV_KEY_INFO* wolfSSL_d2i_PKCS8_PKEY( /* Copy in DER data and size. */ XMEMCPY(pkcs8->pkey.ptr, rawDer.buffer, rawDer.length); pkcs8->pkey_sz = (int)rawDer.length; + pkcs8->isPriv = 1; } /* Dispose of PKCS#8 DER data - raw DER reference data in pkcs8Der. */ @@ -2414,6 +2456,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_id(int type, WOLFSSL_EVP_PKEY** out, local->type = type; local->pkey_sz = 0; local->pkcs8HeaderSz = 0; + local->isPriv = 1; switch (type) { #ifndef NO_RSA diff --git a/wolfssl/openssl/evp.h b/wolfssl/openssl/evp.h index c55ba2cf551..c9ef9d800bf 100644 --- a/wolfssl/openssl/evp.h +++ b/wolfssl/openssl/evp.h @@ -1030,6 +1030,7 @@ WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_private_key(int type, WOLFSSL_API void wolfSSL_EVP_PKEY_free(WOLFSSL_EVP_PKEY* key); WOLFSSL_API int wolfSSL_EVP_PKEY_size(WOLFSSL_EVP_PKEY *pkey); WOLFSSL_API int wolfSSL_EVP_PKEY_copy_parameters(WOLFSSL_EVP_PKEY *to, const WOLFSSL_EVP_PKEY *from); +WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey); WOLFSSL_API int wolfSSL_EVP_PKEY_missing_parameters(WOLFSSL_EVP_PKEY *pkey); WOLFSSL_API int wolfSSL_EVP_PKEY_cmp(const WOLFSSL_EVP_PKEY *a, const WOLFSSL_EVP_PKEY *b); WOLFSSL_API int wolfSSL_EVP_PKEY_type(int type); @@ -1454,6 +1455,7 @@ WOLFSSL_API int wolfSSL_EVP_SignInit_ex(WOLFSSL_EVP_MD_CTX* ctx, #define EVP_PKEY_new_raw_private_key wolfSSL_EVP_PKEY_new_raw_private_key #define EVP_PKEY_free wolfSSL_EVP_PKEY_free #define EVP_PKEY_up_ref wolfSSL_EVP_PKEY_up_ref +#define EVP_PKEY_dup wolfSSL_EVP_PKEY_dup #define EVP_PKEY_size wolfSSL_EVP_PKEY_size #define EVP_PKEY_copy_parameters wolfSSL_EVP_PKEY_copy_parameters #define EVP_PKEY_missing_parameters wolfSSL_EVP_PKEY_missing_parameters diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 85182d2062b..5317d429f19 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -650,6 +650,7 @@ struct WOLFSSL_EVP_PKEY { word16 pkcs8HeaderSz; /* option bits */ + WC_BITFIELD isPriv:1; /* key holds private material, 0 means public only */ WC_BITFIELD ownDh:1; /* if struct owns DH and should free it */ WC_BITFIELD ownEcc:1; /* if struct owns ECC and should free it */ WC_BITFIELD ownDsa:1; /* if struct owns DSA and should free it */ From 5d69c05f8edf17ddecf057d36c62f9a29a013ee4 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 13:17:45 +0000 Subject: [PATCH 08/28] socat 1.8.1.1: fix "aNULL" cipher list keyword, update workflow The OpenSSL-style "aNULL" keyword only set the anon signature flag, so InitSuites never generated the DH_anon suites (they also need haveDH), and nothing set useAnon from a cipher list, so a server without a certificate that listed anonymous suites failed in wolfSSL_accept with NO_PRIVATE_KEY. Set haveDH for "aNULL" and, in the OpenSSL compat parser, allow anon on the CTX/SSL when the resulting list contains an anonymous suite. socat OPENSSL-LISTEN with ciphers=aNULL then completes the handshake instead of erroring out (its test 318 records ok either way, so the suite does not show the difference). Add the socat 1.8.1.1 matrix entry. Its test numbering matches 1.8.0.3 (the new tests are appended), so the expect_fail list is the 1.8.0.3 one minus 402, 459 and 460: 402 passes now, and the osp patch keeps the derived config.h.in defines through autoheader, so the ispeed and tcpwrap deny-table options exist again. --- .github/workflows/socat.yml | 2 + src/internal.c | 24 +++++++- src/ssl.c | 21 ++++++- tests/api.c | 117 ++++++++++++++++++++++++++++++++++++ wolfssl/internal.h | 3 + 5 files changed, 165 insertions(+), 2 deletions(-) diff --git a/.github/workflows/socat.yml b/.github/workflows/socat.yml index dda73b5a529..8a8ef687a67 100644 --- a/.github/workflows/socat.yml +++ b/.github/workflows/socat.yml @@ -56,6 +56,8 @@ jobs: expect_fail: "23,36,64,146,155,156,205,207,216,227,307,309,310,321,386,399,402,403,459,460,467,468,475,478,491,492,494,528,529" - socat_version: "1.8.0.3" expect_fail: "23,146,155,156,205,207,307,321,386,399,402,459,460,467,468,475,478,491,492,494,495,528,529" + - socat_version: "1.8.1.1" + expect_fail: "23,146,155,156,205,207,307,321,386,399,467,468,475,478,491,492,494,495,528,529" steps: - name: Checkout wolfSSL CI actions uses: actions/checkout@v5 diff --git a/src/internal.c b/src/internal.c index d8af1b9446c..a51f9f0e0ac 100644 --- a/src/internal.c +++ b/src/internal.c @@ -34923,6 +34923,25 @@ static void RemoveExcludedSuites(byte* suites, int* idx, int anon, int enull) } *idx = out; } + +#ifdef HAVE_ANON +/* Return 1 when suites contains an anonymous (no peer authentication) + * cipher suite. */ +int SuitesHaveAnon(const Suites* suites) +{ + int i; + + if (suites == NULL) + return 0; + for (i = 0; (i + 1) < suites->suiteSz; i += 2) { + if (CipherSuiteExcluded(suites->suites[i], suites->suites[i + 1], + 1, 0)) { + return 1; + } + } + return 0; +} +#endif /* HAVE_ANON */ #endif /* OPENSSL_EXTRA || OPENSSL_ALL */ /** @@ -35119,8 +35138,11 @@ static int ParseCipherList(Suites* suites, } if (XSTRCMP(name, "aNULL") == 0) { - if (allowing) + if (allowing) { haveSig |= SIG_ANON; + /* Anonymous suites are DH_anon; InitSuites needs DH too. */ + haveDH = 1; + } else haveSig &= ~SIG_ANON; /* Track exclusion (sticky) so an explicit ADH suite is dropped at diff --git a/src/ssl.c b/src/ssl.c index 08ac7e4b8af..875a525850f 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -3353,7 +3353,7 @@ static int CheckcipherList(const char* list) * * returns WOLFSSL_SUCCESS on success and sets the cipher suite list */ -static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl, +static int wolfSSL_parse_cipher_list_ex(WOLFSSL_CTX* ctx, WOLFSSL* ssl, Suites* suites, const char* list) { int ret = 0; @@ -3486,6 +3486,25 @@ static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl, return ret; } +static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl, + Suites* suites, const char* list) +{ + int ret = wolfSSL_parse_cipher_list_ex(ctx, ssl, suites, list); + +#ifdef HAVE_ANON + /* Like OpenSSL, a list with anonymous suites lets a server run without + * a certificate. */ + if (ret == WOLFSSL_SUCCESS && SuitesHaveAnon(suites)) { + if (ctx != NULL) + ctx->useAnon = 1; + else if (ssl != NULL) + ssl->options.useAnon = 1; + } +#endif + + return ret; +} + #endif diff --git a/tests/api.c b/tests/api.c index 3dfbeac31f6..6d80ce67a09 100644 --- a/tests/api.c +++ b/tests/api.c @@ -3387,6 +3387,122 @@ static int test_wolfSSL_set_cipher_list_exclusions(void) #endif /* TEST_CIPHER_EXCLUDE_ANON || TEST_CIPHER_EXCLUDE_NULL */ return EXPECT_RESULT(); } + +/* OpenSSL "aNULL" selects the anonymous suites; a server that only lists + * them has no certificate. Check the keyword generates the ADH suites and + * that both ends handshake without any certificate over TLS 1.2. */ +#if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT) && \ + !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_NO_TLS12) && \ + defined(BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA) && \ + !defined(WOLFSSL_SESSION_EXPORT) + #define TEST_CIPHER_LIST_ANON +#endif + +#ifdef TEST_CIPHER_LIST_ANON +static int test_cipher_list_anon_ctx_ready(WOLFSSL_CTX* ctx) +{ + EXPECT_DECLS; + wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL); + ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "aNULL"), WOLFSSL_SUCCESS); + return EXPECT_RESULT(); +} + +static int test_cipher_list_anon_on_result(WOLFSSL* ssl) +{ + EXPECT_DECLS; + ExpectStrEQ(wolfSSL_get_cipher_name(ssl), "ADH-AES128-SHA"); + return EXPECT_RESULT(); +} +#endif + +static int test_wolfSSL_set_cipher_list_anon(void) +{ + EXPECT_DECLS; +#ifdef TEST_CIPHER_LIST_ANON + WOLFSSL_CTX* ctx = NULL; + WOLFSSL* ssl = NULL; + test_ssl_cbf client_cbf; + test_ssl_cbf server_cbf; + + /* OPENSSL_COMPATIBLE_DEFAULTS already allows anon on every CTX, so clear + * the flag first: the checks below must see the cipher list set it. */ + + /* "aNULL" on the CTX allows anon there and on SSLs made from it. A + * certificate-less server CTX can only make an SSL once anon is on. */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())); + if (ctx != NULL) + ctx->useAnon = 0; + ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "aNULL"), WOLFSSL_SUCCESS); + ExpectIntEQ(ctx->useAnon, 1); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(ssl->options.useAnon, 1); + wolfSSL_free(ssl); + ssl = NULL; + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* "aNULL" alone must generate the anonymous suites. */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())); + if (ctx != NULL) + ctx->useAnon = 0; + ExpectNotNull(ssl = wolfSSL_new(ctx)); + if (ssl != NULL) + ssl->options.useAnon = 0; + ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "aNULL"), WOLFSSL_SUCCESS); + ExpectIntEQ(test_suites_contains(ssl, CIPHER_BYTE, + TLS_DH_anon_WITH_AES_128_CBC_SHA), 1); + ExpectIntEQ(ssl->options.useAnon, 1); + wolfSSL_free(ssl); + ssl = NULL; + + /* An explicit anonymous suite also allows anon on the SSL. */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + if (ssl != NULL) + ssl->options.useAnon = 0; + ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ADH-AES128-SHA"), + WOLFSSL_SUCCESS); + ExpectIntEQ(ssl->options.useAnon, 1); + wolfSSL_free(ssl); + ssl = NULL; + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* A list without anonymous suites must not turn anon on. */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())); + if (ctx != NULL) + ctx->useAnon = 0; + ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL"), + WOLFSSL_SUCCESS); + ExpectIntEQ(ctx->useAnon, 0); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(ssl->options.useAnon, 0); + ExpectIntEQ(test_suites_contains(ssl, CIPHER_BYTE, + TLS_DH_anon_WITH_AES_128_CBC_SHA), 0); + wolfSSL_free(ssl); + ssl = NULL; + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* Handshake with no certificates on either side. */ + XMEMSET(&client_cbf, 0, sizeof(client_cbf)); + XMEMSET(&server_cbf, 0, sizeof(server_cbf)); + client_cbf.method = wolfTLSv1_2_client_method; + server_cbf.method = wolfTLSv1_2_server_method; + client_cbf.caPemFile = ""; + client_cbf.certPemFile = ""; + client_cbf.keyPemFile = ""; + server_cbf.caPemFile = ""; + server_cbf.certPemFile = ""; + server_cbf.keyPemFile = ""; + client_cbf.ctx_ready = test_cipher_list_anon_ctx_ready; + server_cbf.ctx_ready = test_cipher_list_anon_ctx_ready; + client_cbf.on_result = test_cipher_list_anon_on_result; + server_cbf.on_result = test_cipher_list_anon_on_result; + ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf, + &server_cbf, NULL), TEST_SUCCESS); +#endif + return EXPECT_RESULT(); +} #ifdef TEST_CIPHER_EXCLUDE_ANON #undef TEST_CIPHER_EXCLUDE_ANON #endif @@ -49876,6 +49992,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_tls13_null_cipher_default_list), TEST_DECL(test_tls13_null_cipher_explicit_keep), TEST_DECL(test_wolfSSL_SSL_set_ciphersuites), + TEST_DECL(test_wolfSSL_set_cipher_list_anon), TEST_DECL(test_wolfSSL_set_alpn_protos_default_fails), TEST_DECL(test_wolfSSL_set_alpn_protos_binary_safe), TEST_DECL(test_wolfSSL_CTX_use_certificate), diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 459adfb515c..290994b0b5a 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -2730,6 +2730,9 @@ WOLFSSL_LOCAL int SetCipherList(const WOLFSSL_CTX* ctx, Suites* suites, const char* list); WOLFSSL_LOCAL int SetCipherListFromBytes(WOLFSSL_CTX* ctx, Suites* suites, const byte* list, const int listSz); +#if defined(HAVE_ANON) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) +WOLFSSL_LOCAL int SuitesHaveAnon(const Suites* suites); +#endif WOLFSSL_LOCAL int SetSuitesHashSigAlgo(Suites* suites, const char* list); #ifndef PSK_TYPES_DEFINED From ee1808bbea0eced24d1c20aab6bbfe56c38e7f90 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 10:08:45 +0000 Subject: [PATCH 09/28] krb5 1.22.2: update workflow --- .github/workflows/krb5.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/krb5.yml b/.github/workflows/krb5.yml index 623a254e289..4f893191739 100644 --- a/.github/workflows/krb5.yml +++ b/.github/workflows/krb5.yml @@ -51,7 +51,7 @@ jobs: fail-fast: false matrix: # List of releases to test - ref: [ 1.21.1 ] + ref: [ 1.21.1, 1.22.2 ] name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-22.04 From 3e238e82f246700b2c4768429599fb8126355e59 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 11:53:10 +0000 Subject: [PATCH 10/28] libimobiledevice 1.4.0: add ASN1_TIME_set, fix X509_pubkey_digest, add workflow ASN1_TIME_set() was a stub, so certificates generated through the compatibility layer only had the default validity period. X509V3_set_ctx() now stores the issuer and subject in the context instead of allocating an unused X509. libimobiledevice builds the subjectKeyIdentifier "hash" from X509_pubkey_digest(). OpenSSL digests the subjectPublicKey, but X509_set_pubkey() stores a SubjectPublicKeyInfo where a decoded certificate stores the key alone, so certificates built in memory got a different digest. Skip the SubjectPublicKeyInfo wrapper when present. Add the libimobiledevice workflow. libimobiledevice has no test suite and a runner has no iOS device, so .github/scripts/idevice-emulator.py plays one: it speaks the usbmuxd protocol on a Unix socket (ListDevices, Listen, Connect, ReadBUID and the pair record calls) and the lockdownd protocol on the connection tunnelled behind it (QueryType, GetValue, Pair, ValidatePair, Unpair, StartSession with the TLS upgrade, StopSession, StartService, Goodbye). The workflow builds the dependency chain (libplist, libimobiledevice-glue, libusbmuxd, libtatsu) from source, checks that the library and tools link against wolfSSL, and runs idevice_id, idevicepair pair/validate/unpair and ideviceinfo against the emulator. It then asserts on the emulator log: the pair record certificates and private keys check out, the session negotiated TLS with the host presenting the RootCertificate as its client certificate, requests were served inside that session, and the unpair reached the pair record store. A second run reports iOS 9, for which libimobiledevice pins the session to TLS 1.0, so wolfSSL is built with --enable-tlsv10. --- .../ci-deps/packages-ubuntu-24.04-full.txt | 2 + .github/scripts/idevice-emulator.py | 559 ++++++++++++++++++ .github/workflows/libimobiledevice.yml | 224 +++++++ src/ssl_asn1.c | 17 +- src/x509.c | 68 +-- tests/api.c | 19 + tests/api/test_ossl_asn1.c | 54 ++ tests/api/test_ossl_asn1.h | 2 + tests/api/test_ossl_x509_ext.c | 31 +- wolfssl/ssl.h | 3 +- 10 files changed, 916 insertions(+), 63 deletions(-) create mode 100755 .github/scripts/idevice-emulator.py create mode 100644 .github/workflows/libimobiledevice.yml diff --git a/.github/ci-deps/packages-ubuntu-24.04-full.txt b/.github/ci-deps/packages-ubuntu-24.04-full.txt index 27c7dab0e1f..e2f5415eda0 100644 --- a/.github/ci-deps/packages-ubuntu-24.04-full.txt +++ b/.github/ci-deps/packages-ubuntu-24.04-full.txt @@ -83,6 +83,7 @@ libsqlite3-dev libssl-dev libtool liburcu-dev +libusb-1.0-0-dev libuv1-dev linux-libc-dev make @@ -96,6 +97,7 @@ pkg-config pkgconf psmisc python3 +python3-cryptography python3-docutils python3-impacket python3-ldb diff --git a/.github/scripts/idevice-emulator.py b/.github/scripts/idevice-emulator.py new file mode 100755 index 00000000000..3a8c2a35112 --- /dev/null +++ b/.github/scripts/idevice-emulator.py @@ -0,0 +1,559 @@ +#!/usr/bin/env python3 +# Emulated iOS device for the libimobiledevice CI job. +# +# libimobiledevice never speaks USB: it talks to usbmuxd over a Unix socket, +# and libusbmuxd 2.x uses the one named by USBMUXD_SOCKET_ADDRESS=UNIX:. +# This serves that socket plus the lockdownd service usbmuxd tunnels to, so +# the unmodified tools run a real pair / session exchange with no hardware. +# +# Two protocols, both plists on a socket: +# +# usbmuxd 16-byte little-endian header (length including the header, +# version, message type, tag) followed by an XML plist. +# See usbmuxd/src/usbmuxd-proto.h and src/client.c. +# lockdownd 4-byte big-endian length followed by an XML plist, on the +# connection usbmuxd tunnels to port 62078. +# See libimobiledevice/src/lockdown.c. +# +# The device RSA key is generated at startup. The host reads its public half +# through GetValue DevicePublicKey, issues the pair record certificates from +# it, and sends them in the Pair request; this checks those certificates and +# then serves the session TLS with the DeviceCertificate it was handed, +# requiring the host to present the RootCertificate as its client +# certificate - which is what libimobiledevice sends (src/idevice.c). + +import argparse +import asyncio +import contextlib +import datetime +import errno +import os +import plistlib +import shutil +import signal +import socket +import ssl +import struct +import sys +import tempfile +import uuid +import warnings + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import padding, rsa + +# usbmuxd framing and constants (usbmuxd/src/usbmuxd-proto.h). +USBMUX_HEADER = struct.Struct('I') +LOCKDOWN_PORT = 62078 +LOCKDOWN_TYPE = 'com.apple.mobile.lockdown' + +# pair_record_generate_keys_and_certs() dates the certificates ten years out. +MIN_DEVICE_CERT_LIFETIME = datetime.timedelta(days=9 * 365) + + +class Log: + """One line per event, for the CI job to assert on.""" + + def __init__(self, path): + self._file = open(path, 'w', buffering=1, encoding='utf-8') + + def __call__(self, message): + stamp = datetime.datetime.now().strftime('%H:%M:%S.%f')[:-3] + self._file.write('%s %s\n' % (stamp, message)) + + def close(self): + self._file.close() + + +def not_valid_after(cert): + """cryptography 42 deprecated the naive not_valid_after.""" + value = getattr(cert, 'not_valid_after_utc', None) + if value is None: + value = cert.not_valid_after.replace(tzinfo=datetime.timezone.utc) + return value + + +def subject_text(cert): + """The tools' certificates carry an empty subject DN.""" + return cert.subject.rfc4514_string() or '' + + +class Device: + """Everything the emulated device knows about itself.""" + + def __init__(self, args): + self.udid = args.udid + self.device_id = 1 + self.buid = str(uuid.uuid4()).upper() + self.key = rsa.generate_private_key(public_exponent=65537, + key_size=2048) + # lockdownd hands out the device public key in PKCS#1 form; that is + # what pair_record_generate_keys_and_certs() reads back with + # PEM_read_bio_RSAPublicKey(). + self.public_key_pem = self.key.public_key().public_bytes( + serialization.Encoding.PEM, + serialization.PublicFormat.PKCS1) + self.values = { + None: { + 'BuildVersion': '22G86', + 'CPUArchitecture': 'arm64e', + 'DeviceClass': 'iPhone', + 'DeviceColor': '1', + 'DeviceName': args.device_name, + 'DevicePublicKey': self.public_key_pem, + 'HardwareModel': 'D73AP', + 'HardwarePlatform': 't8110', + 'ProductName': 'iPhone OS', + 'ProductType': args.product_type, + 'ProductVersion': args.product_version, + 'ProtocolVersion': '2', + 'SerialNumber': 'F2LX90ABCDEF', + 'TimeIntervalSince1970': 0, + 'TimeZone': 'Etc/UTC', + 'UniqueDeviceID': self.udid, + 'WiFiAddress': args.wifi_address, + }, + } + # The pair record the device itself keeps, as set by Pair and dropped + # by Unpair. Separate from the host-side records usbmuxd stores. + self.pair_record = None + + +class Emulator: + def __init__(self, args, log): + self.log = log + self.device = Device(args) + # The host-side pair record store usbmuxd keeps, keyed by UDID. + self.records = {} + self._tmpdir = tempfile.mkdtemp(prefix='idevice-emulator-') + + def close(self): + shutil.rmtree(self._tmpdir, ignore_errors=True) + + # ---- usbmuxd ----------------------------------------------------------- + + def _attached(self): + return { + 'MessageType': 'Attached', + 'DeviceID': self.device.device_id, + 'Properties': { + 'ConnectionSpeed': 480000000, + 'ConnectionType': 'USB', + 'DeviceID': self.device.device_id, + 'LocationID': 0, + 'ProductID': 0x12a8, + 'SerialNumber': self.device.udid, + }, + } + + @staticmethod + def _send_usbmux(writer, tag, payload): + body = plistlib.dumps(payload) + writer.write(USBMUX_HEADER.pack(USBMUX_HEADER.size + len(body), + USBMUX_VERSION_PLIST, + USBMUX_MESSAGE_PLIST, tag) + body) + + def _send_result(self, writer, tag, number): + self._send_usbmux(writer, tag, {'MessageType': 'Result', + 'Number': number}) + + async def handle_usbmux(self, reader, writer): + try: + while True: + header = await reader.readexactly(USBMUX_HEADER.size) + length, _, message, tag = USBMUX_HEADER.unpack(header) + payload = await reader.readexactly( + max(0, length - USBMUX_HEADER.size)) + if message != USBMUX_MESSAGE_PLIST or not payload: + self.log('usbmux: message type %d -> BadCommand' % message) + self._send_result(writer, tag, RESULT_BADCOMMAND) + continue + if await self._usbmux_request(reader, writer, tag, + plistlib.loads(payload)): + return + await writer.drain() + except (asyncio.IncompleteReadError, ConnectionResetError, + BrokenPipeError): + pass + finally: + with contextlib.suppress(OSError): + writer.close() + + async def _usbmux_request(self, reader, writer, tag, request): + """Answer one usbmuxd request; True means the connection was taken + over by the tunnelled lockdownd session.""" + message = request.get('MessageType') + record_id = request.get('PairRecordID') + + if message == 'ListDevices': + self._send_usbmux(writer, tag, {'DeviceList': [self._attached()]}) + self.log('usbmux: ListDevices -> 1 device %s' % self.device.udid) + elif message == 'Listen': + self._send_result(writer, tag, RESULT_OK) + self._send_usbmux(writer, 0, self._attached()) + self.log('usbmux: Listen -> Attached DeviceID=%d' + % self.device.device_id) + elif message == 'ReadBUID': + self._send_usbmux(writer, tag, {'BUID': self.device.buid}) + self.log('usbmux: ReadBUID -> %s' % self.device.buid) + elif message == 'ReadPairRecord': + data = self.records.get(record_id) + if data is None: + self._send_result(writer, tag, RESULT_NOENT) + self.log('usbmux: ReadPairRecord %s -> not found (ENOENT)' + % record_id) + else: + self._send_usbmux(writer, tag, {'PairRecordData': data}) + self.log('usbmux: ReadPairRecord %s -> %d bytes' + % (record_id, len(data))) + elif message == 'SavePairRecord': + data = request['PairRecordData'] + try: + detail = self._check_saved_record(data) + except (ValueError, KeyError, TypeError) as exc: + self._send_result(writer, tag, RESULT_BADCOMMAND) + self.log('pair: stored pair record REJECTED: %s' % exc) + return False + self.records[record_id] = data + self._send_result(writer, tag, RESULT_OK) + self.log('usbmux: SavePairRecord %s -> stored %d bytes, %s' + % (record_id, len(data), detail)) + elif message == 'DeletePairRecord': + existed = self.records.pop(record_id, None) is not None + self._send_result(writer, tag, RESULT_OK) + self.log('usbmux: DeletePairRecord %s -> %s' % ( + record_id, 'deleted' if existed else 'no such record')) + elif message == 'Connect': + return await self._usbmux_connect(reader, writer, tag, request) + else: + self._send_result(writer, tag, RESULT_BADCOMMAND) + self.log('usbmux: %s -> BadCommand' % message) + return False + + async def _usbmux_connect(self, reader, writer, tag, request): + # PortNumber travels in network byte order. + port = socket.ntohs(request.get('PortNumber', 0) & 0xffff) + device_id = request.get('DeviceID') + if device_id != self.device.device_id: + self._send_result(writer, tag, RESULT_BADDEV) + self.log('usbmux: Connect DeviceID=%s -> BadDevice' % device_id) + return False + if port != LOCKDOWN_PORT: + self._send_result(writer, tag, RESULT_CONNREFUSED) + self.log('usbmux: Connect port=%d -> ConnectionRefused' % port) + return False + self._send_result(writer, tag, RESULT_OK) + await writer.drain() + self.log('usbmux: Connect DeviceID=%d port=%d -> lockdownd' + % (device_id, port)) + # From here the socket carries the device stream verbatim, so the + # lockdownd handler takes it over for the rest of the connection. + await self.serve_lockdown(reader, writer) + return True + + # ---- lockdownd --------------------------------------------------------- + + @staticmethod + async def _send_lockdown(writer, reply): + body = plistlib.dumps(reply) + writer.write(LOCKDOWN_HEADER.pack(len(body)) + body) + await writer.drain() + + async def serve_lockdown(self, reader, writer): + session_id = None + try: + while True: + header = await reader.readexactly(LOCKDOWN_HEADER.size) + (length,) = LOCKDOWN_HEADER.unpack(header) + request = plistlib.loads(await reader.readexactly(length)) + name = request.get('Request') + tail = ' [session %s]' % session_id if session_id else '' + + if name == 'QueryType': + reply = {'Request': name, 'Type': LOCKDOWN_TYPE} + self.log('lockdown: QueryType -> %s%s' + % (LOCKDOWN_TYPE, tail)) + elif name == 'GetValue': + reply = self._get_value(request, tail) + elif name in ('Pair', 'ValidatePair', 'Unpair'): + reply = self._pairing(name, request) + elif name == 'StartSession': + reply = self._start_session(request) + elif name == 'StopSession': + reply = {'Request': name, 'Result': 'Success'} + self.log('lockdown: StopSession%s -> Success' % tail) + session_id = None + elif name == 'StartService': + # No services are emulated. + reply = {'Request': name, 'Error': 'InvalidService'} + self.log('lockdown: StartService %s -> InvalidService%s' + % (request.get('Service'), tail)) + elif name == 'Goodbye': + reply = {'Request': name, 'Result': 'Success'} + self.log('lockdown: Goodbye -> Success%s' % tail) + await self._send_lockdown(writer, reply) + return + else: + reply = {'Request': name, 'Error': 'InvalidRequest'} + self.log('lockdown: %s -> InvalidRequest%s' % (name, tail)) + + await self._send_lockdown(writer, reply) + + if reply.get('EnableSessionSSL'): + session_id = reply['SessionID'] + reader, writer = await self._enable_ssl(reader, writer) + except (asyncio.IncompleteReadError, ConnectionResetError, + BrokenPipeError, ssl.SSLError): + pass + + def _get_value(self, request, tail): + domain = request.get('Domain') + key = request.get('Key') + values = self.device.values.get(domain, {}) + reply = {'Request': 'GetValue'} + if domain is not None: + reply['Domain'] = domain + if key is not None: + reply['Key'] = key + if key is None: + reply['Value'] = values + shown = '%d values' % len(values) + elif key in values: + reply['Value'] = values[key] + shown = repr(values[key])[:60] + else: + reply['Error'] = 'MissingValue' + shown = 'MissingValue' + self.log('lockdown: GetValue domain=%s key=%s -> %s%s' + % (domain or '-', key or '-', shown, tail)) + return reply + + def _pairing(self, name, request): + record = request.get('PairRecord') or {} + host_id = record.get('HostID') + if name == 'Unpair': + self.device.pair_record = None + self.log('lockdown: Unpair HostID=%s -> Success, device pair ' + 'record dropped' % host_id) + return {'Request': name, 'Result': 'Success'} + if name == 'ValidatePair': + known = self.device.pair_record + if not known or known.get('HostID') != host_id: + self.log('lockdown: ValidatePair HostID=%s -> InvalidHostID' + % host_id) + return {'Request': name, 'Error': 'InvalidHostID'} + self.log('lockdown: ValidatePair HostID=%s -> Success' % host_id) + return {'Request': name, 'Result': 'Success'} + + self.log('lockdown: Pair HostID=%s SystemBUID=%s' + % (host_id, record.get('SystemBUID'))) + try: + self.log('pair: pair record verified: %s' % self._check_record( + record)) + except (ValueError, KeyError, TypeError, + x509.ExtensionNotFound) as exc: + self.log('pair: pair record REJECTED: %s' % exc) + return {'Request': name, 'Error': 'InvalidPairRecord'} + self.device.pair_record = record + # A real device answers a successful pairing with an escrow bag. + return {'Request': name, 'Result': 'Success', + 'EscrowBag': os.urandom(64)} + + def _check_record(self, record): + """Check the certificates the host generated for this device.""" + root = x509.load_pem_x509_certificate(record['RootCertificate']) + host = x509.load_pem_x509_certificate(record['HostCertificate']) + device = x509.load_pem_x509_certificate(record['DeviceCertificate']) + if not record.get('HostID') or not record.get('SystemBUID'): + raise ValueError('HostID or SystemBUID missing') + + for name, cert in (('root', root), ('host', host), + ('device', device)): + # Every certificate is signed by the root private key, and the + # root signs itself. + try: + root.public_key().verify(cert.signature, + cert.tbs_certificate_bytes, + padding.PKCS1v15(), + cert.signature_hash_algorithm) + except Exception: + raise ValueError('%s certificate does not chain to the root' + % name) + + if not root.extensions.get_extension_for_class( + x509.BasicConstraints).value.ca: + raise ValueError('root certificate is not a CA') + for name, cert in (('host', host), ('device', device)): + if cert.extensions.get_extension_for_class( + x509.BasicConstraints).value.ca: + raise ValueError('%s certificate is a CA' % name) + usage = cert.extensions.get_extension_for_class( + x509.KeyUsage).value + if not (usage.digital_signature and usage.key_encipherment + and not usage.content_commitment + and not usage.data_encipherment + and not usage.key_agreement + and not usage.key_cert_sign and not usage.crl_sign): + raise ValueError('%s certificate key usage is %s' + % (name, usage)) + + public_key = self.device.key.public_key() + if device.public_key().public_numbers() != public_key.public_numbers(): + raise ValueError('device certificate carries a foreign key') + # RFC 5280 method 1: the SHA-1 of the public key bit string. + expected = x509.SubjectKeyIdentifier.from_public_key(public_key) + found = device.extensions.get_extension_for_class( + x509.SubjectKeyIdentifier).value + if found.digest != expected.digest: + raise ValueError('device certificate subject key identifier %s ' + 'does not match the device key' + % found.digest.hex()) + + lifetime = not_valid_after(device) - datetime.datetime.now( + datetime.timezone.utc) + if lifetime < MIN_DEVICE_CERT_LIFETIME: + raise ValueError('device certificate expires in %s' % lifetime) + + return ('root is a CA and self-signed (%s), host and device ' + 'certificates chain to it, neither is a CA, both carry ' + 'digitalSignature+keyEncipherment, device SKI %s matches the ' + 'device key, device certificate valid for %d days' + % (root.signature_hash_algorithm.name, + found.digest.hex(), lifetime.days)) + + def _check_saved_record(self, data): + """Check the record the host stores for itself. + + The Pair request strips the private keys out, so they are only + visible here; libimobiledevice reads the root key back out of this + record to authenticate the session TLS (src/idevice.c). + """ + record = plistlib.loads(data) + for name in ('Root', 'Host'): + cert = x509.load_pem_x509_certificate(record[name + + 'Certificate']) + key = serialization.load_pem_private_key( + record[name + 'PrivateKey'], None) + if (key.public_key().public_numbers() + != cert.public_key().public_numbers()): + raise ValueError('%sPrivateKey does not match %sCertificate' + % (name, name)) + return 'root and host private keys match their certificates' + + def _start_session(self, request): + host_id = request.get('HostID') + known = self.device.pair_record + if not known or known.get('HostID') != host_id: + self.log('lockdown: StartSession HostID=%s -> InvalidHostID' + % host_id) + return {'Request': 'StartSession', 'Error': 'InvalidHostID'} + session_id = str(uuid.uuid4()).upper() + self.log('lockdown: StartSession HostID=%s -> SessionID=%s ' + 'EnableSessionSSL=true' % (host_id, session_id)) + return {'Request': 'StartSession', 'Result': 'Success', + 'SessionID': session_id, 'EnableSessionSSL': True} + + # ---- session TLS ------------------------------------------------------- + + def _ssl_context(self): + record = self.device.pair_record + cert = os.path.join(self._tmpdir, 'device.pem') + with open(cert, 'wb') as handle: + handle.write(record['DeviceCertificate']) + handle.write(self.device.key.private_bytes( + serialization.Encoding.PEM, + serialization.PrivateFormat.TraditionalOpenSSL, + serialization.NoEncryption())) + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + with warnings.catch_warnings(): + # The enum member is deprecated, TLS 1.0 itself still works and + # libimobiledevice pins it for devices below iOS 10 + # (src/idevice.c). + warnings.simplefilter('ignore', DeprecationWarning) + context.minimum_version = ssl.TLSVersion.TLSv1 + context.set_ciphers('DEFAULT:@SECLEVEL=0') + # The host presents the RootCertificate as its client certificate. + context.verify_mode = ssl.CERT_REQUIRED + context.load_verify_locations( + cadata=record['RootCertificate'].decode('ascii')) + context.load_cert_chain(cert) + return context + + async def _enable_ssl(self, reader, writer): + # Stop reading before the StartSession reply reaches the host, so its + # ClientHello cannot land in the stream buffer that start_tls() is + # about to replace. + writer.transport.pause_reading() + await writer.start_tls(self._ssl_context()) + session = writer.get_extra_info('ssl_object') + peer = x509.load_der_x509_certificate(session.getpeercert(True)) + root = x509.load_pem_x509_certificate( + self.device.pair_record['RootCertificate']) + self.log('tls: session established version=%s cipher=%s client ' + 'certificate subject=%s (%s)' + % (session.version(), session.cipher()[0], subject_text(peer), + 'matches RootCertificate from the pair record' + if peer == root else 'NOT the RootCertificate')) + return reader, writer + + +async def main_async(args): + log = Log(args.log) + emulator = Emulator(args, log) + if os.path.exists(args.socket): + os.unlink(args.socket) + server = await asyncio.start_unix_server(emulator.handle_usbmux, + path=args.socket) + log('ready: udid=%s ProductVersion=%s socket=%s' + % (emulator.device.udid, args.product_version, args.socket)) + + stop = asyncio.Event() + loop = asyncio.get_running_loop() + for signame in (signal.SIGTERM, signal.SIGINT): + loop.add_signal_handler(signame, stop.set) + await stop.wait() + + log('stopping on signal') + server.close() + await server.wait_closed() + emulator.close() + with contextlib.suppress(OSError): + os.unlink(args.socket) + log.close() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--socket', default='/tmp/idevice-emulator.sock', + help='Unix socket to serve the usbmuxd protocol on') + parser.add_argument('--log', default='/tmp/idevice-emulator.log', + help='file to write one line per event to') + parser.add_argument('--udid', + default='1e2f3a4b5c6d7e8f90a1b2c3d4e5f60718293a4b', + help='UDID the emulated device reports') + parser.add_argument('--product-version', default='18.6', + help='iOS version the emulated device reports') + parser.add_argument('--product-type', default='iPhone15,2') + parser.add_argument('--device-name', default='CI Emulated Device') + parser.add_argument('--wifi-address', default='00:1a:2b:3c:4d:5e') + args = parser.parse_args() + asyncio.run(main_async(args)) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/.github/workflows/libimobiledevice.yml b/.github/workflows/libimobiledevice.yml new file mode 100644 index 00000000000..6846e96dff0 --- /dev/null +++ b/.github/workflows/libimobiledevice.yml @@ -0,0 +1,224 @@ +name: libimobiledevice Tests + +# START OF COMMON SECTION +on: + push: + branches: [ 'release/**' ] + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + branches: [ '*' ] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read +# END OF COMMON SECTION + +jobs: + build_wolfssl: + name: Build wolfSSL + if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} + # Just to keep it the same as the testing target + runs-on: ubuntu-24.04 + # This should be a safe limit for the tests to run. + timeout-minutes: 4 + steps: + - name: Build wolfSSL + uses: wolfSSL/actions-build-autotools-project@v1 + with: + path: wolfssl + configure: --enable-all --enable-tlsv10 + install: true + check: false + + - name: tar build-dir + run: tar -zcf build-dir.tgz build-dir + + - name: Upload built lib + uses: actions/upload-artifact@v6 + with: + name: wolf-install-libimobiledevice + path: build-dir.tgz + retention-days: 5 + + libimobiledevice_check: + strategy: + fail-fast: false + matrix: + # List of releases to test + include: + - ref: 1.4.0 + libplist: 2.7.0 + glue: 1.3.2 + libusbmuxd: 2.1.1 + libtatsu: 1.0.5 + name: ${{ matrix.ref }} + if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} + runs-on: ubuntu-24.04 + # This should be a safe limit for the tests to run. + timeout-minutes: 15 + needs: build_wolfssl + steps: + - name: Checkout wolfSSL CI actions + uses: actions/checkout@v5 + with: + sparse-checkout: | + .github/actions + .github/scripts + fetch-depth: 1 + + - name: Download lib + uses: actions/download-artifact@v7 + with: + name: wolf-install-libimobiledevice + + - name: untar build-dir + run: tar -xf build-dir.tgz + + - name: Install dependencies + uses: ./.github/actions/install-apt-deps + with: + packages: libusb-1.0-0-dev libcurl4-openssl-dev libreadline-dev python3-cryptography + ghcr-debs-tag: ubuntu-24.04-full + + - name: Checkout OSP + uses: actions/checkout@v5 + with: + repository: wolfssl/osp + path: osp + fetch-depth: 1 + + # The Ubuntu packages are too old (libtatsu needs libplist >= 2.6.0) + # and libimobiledevice-glue/libtatsu are not packaged, so build the + # dependency chain from source into the wolfSSL prefix. None of these + # use OpenSSL or wolfSSL. + - name: Checkout libplist + uses: actions/checkout@v5 + with: + repository: libimobiledevice/libplist + path: libplist + ref: ${{ matrix.libplist }} + fetch-depth: 1 + + - name: Checkout libimobiledevice-glue + uses: actions/checkout@v5 + with: + repository: libimobiledevice/libimobiledevice-glue + path: libimobiledevice-glue + ref: ${{ matrix.glue }} + fetch-depth: 1 + + - name: Checkout libusbmuxd + uses: actions/checkout@v5 + with: + repository: libimobiledevice/libusbmuxd + path: libusbmuxd + ref: ${{ matrix.libusbmuxd }} + fetch-depth: 1 + + - name: Checkout libtatsu + uses: actions/checkout@v5 + with: + repository: libimobiledevice/libtatsu + path: libtatsu + ref: ${{ matrix.libtatsu }} + fetch-depth: 1 + + - name: Checkout libimobiledevice + uses: actions/checkout@v5 + with: + repository: libimobiledevice/libimobiledevice + path: libimobiledevice + ref: ${{ matrix.ref }} + fetch-depth: 1 + + - name: Build dependencies + run: | + export PKG_CONFIG_PATH=$GITHUB_WORKSPACE/build-dir/lib/pkgconfig + for dep in libplist libimobiledevice-glue libusbmuxd libtatsu; do + pushd $dep + ./autogen.sh --prefix=$GITHUB_WORKSPACE/build-dir --without-cython + make -j + make install + popd + done + + - name: Build libimobiledevice + working-directory: libimobiledevice + run: | + export PKG_CONFIG_PATH=$GITHUB_WORKSPACE/build-dir/lib/pkgconfig + patch -p1 < $GITHUB_WORKSPACE/osp/libimobiledevice/${{ matrix.ref }}.patch + ./autogen.sh --prefix=$GITHUB_WORKSPACE/build-dir --enable-wolfssl --without-cython + make -j + make install + + # libimobiledevice has no test suite and a runner has no iOS device. + # .github/scripts/idevice-emulator.py plays one: it serves the usbmuxd + # socket named by USBMUXD_SOCKET_ADDRESS and the lockdownd service + # behind it, checks the pair record the tools generate through wolfSSL + # and requires the session TLS. The second run reports iOS 9, for which + # libimobiledevice pins the session to TLS 1.0 (needs --enable-tlsv10). + - name: Test libimobiledevice + env: + UDID: 1e2f3a4b5c6d7e8f90a1b2c3d4e5f60718293a4b + USBMUXD_SOCKET_ADDRESS: UNIX:/tmp/idevice-emulator.sock + run: | + export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/build-dir/lib + bin=$GITHUB_WORKSPACE/build-dir/bin + ldd $GITHUB_WORKSPACE/build-dir/lib/libimobiledevice-1.0.so | grep wolfssl + ldd $bin/idevicepair | grep wolfssl + + run_against_emulator() { + local version=$1 want_tls=$2 emulator i devices + local log=/tmp/idevice-emulator-$version.log + rm -f /tmp/idevice-emulator.sock + python3 .github/scripts/idevice-emulator.py \ + --socket /tmp/idevice-emulator.sock --log $log \ + --udid $UDID --product-version $version & + emulator=$! + for i in $(seq 1 100); do + [ -S /tmp/idevice-emulator.sock ] && break + sleep 0.1 + done + + # Assign on its own line: a command substitution inside test, + # or inside a `local` declaration, would discard idevice_id's + # exit status. + devices=$($bin/idevice_id -l) + test "$devices" = "$UDID" + $bin/idevicepair pair + $bin/idevicepair validate + $bin/ideviceinfo | grep "^ProductVersion: $version\$" + $bin/idevicepair unpair + + # Also checks that the emulator exits cleanly on SIGTERM. + kill $emulator + wait $emulator + + # What the emulator saw: pair record OK, session over the expected + # TLS version with the RootCertificate as client certificate, + # requests served inside it, unpair reached the record store. + grep -q 'pair: pair record verified: ' $log + grep -q 'private keys match their certificates' $log + grep -qE "tls: session established version=$want_tls" $log + grep -q 'matches RootCertificate from the pair record' $log + grep -q 'lockdown: GetValue .* \[session ' $log + grep -q 'usbmux: DeletePairRecord .* -> deleted' $log + if grep -qE 'REJECTED|NOT the RootCertificate' $log; then + exit 1 + fi + } + + run_against_emulator 18.6 'TLSv1\.[23] ' + run_against_emulator 9.3 'TLSv1 ' + + - name: Emulator logs + if: always() + run: | + for log in /tmp/idevice-emulator-*.log; do + [ -f "$log" ] || continue + echo "=== $log ===" + cat "$log" + done diff --git a/src/ssl_asn1.c b/src/ssl_asn1.c index 809a5f24b4b..83cce858a42 100644 --- a/src/ssl_asn1.c +++ b/src/ssl_asn1.c @@ -3948,23 +3948,28 @@ void wolfSSL_ASN1_TIME_free(WOLFSSL_ASN1_TIME* t) XFREE(t, NULL, DYNAMIC_TYPE_OPENSSL); } -#ifndef NO_WOLFSSL_STUB +#if !defined(NO_ASN_TIME) && !defined(USER_TIME) && !defined(TIME_OVERRIDES) /* Set the Unix time GMT into ASN.1 TIME object. * - * Not implemented. - * - * @param [in, out] a ASN.1 TIME object. + * @param [in, out] a ASN.1 TIME object. Allocated when NULL. * @param [in] t Unix time GMT. - * @return An ASN.1 TIME object. + * @return An ASN.1 TIME object on success. + * @return NULL on failure. */ WOLFSSL_ASN1_TIME *wolfSSL_ASN1_TIME_set(WOLFSSL_ASN1_TIME *a, time_t t) +{ + WOLFSSL_ENTER("wolfSSL_ASN1_TIME_set"); + return wolfSSL_ASN1_TIME_adj(a, t, 0, 0); +} +#elif !defined(NO_WOLFSSL_STUB) +WOLFSSL_ASN1_TIME *wolfSSL_ASN1_TIME_set(WOLFSSL_ASN1_TIME *a, time_t t) { WOLFSSL_STUB("wolfSSL_ASN1_TIME_set"); (void)a; (void)t; return a; } -#endif /* !NO_WOLFSSL_STUB */ +#endif /* !NO_ASN_TIME && !USER_TIME && !TIME_OVERRIDES */ #ifndef NO_ASN_TIME /* Convert time to Unix time (GMT). diff --git a/src/x509.c b/src/x509.c index b9ac3e829ba..dcafcf3abd4 100644 --- a/src/x509.c +++ b/src/x509.c @@ -4380,6 +4380,11 @@ int wolfSSL_X509_pubkey_digest(const WOLFSSL_X509 *x509, const WOLFSSL_EVP_MD *digest, unsigned char* buf, unsigned int* len) { int ret; + const byte* key; + word32 sz; + word32 idx = 0; + int keySz; + int len2 = 0; WOLFSSL_ENTER("wolfSSL_X509_pubkey_digest"); @@ -4393,8 +4398,23 @@ int wolfSSL_X509_pubkey_digest(const WOLFSSL_X509 *x509, return WOLFSSL_FAILURE; } - ret = wolfSSL_EVP_Digest(x509->pubKey.buffer, x509->pubKey.length, buf, - len, digest, NULL); + key = x509->pubKey.buffer; + sz = x509->pubKey.length; + keySz = (int)sz; + + /* OpenSSL digests the subjectPublicKey. Decoded certificates keep the + * key alone but wolfSSL_X509_set_pubkey stores a SubjectPublicKeyInfo, + * so step over that wrapper when it is present. */ + if ((GetSequence(key, &idx, &len2, sz) >= 0) && + (GetSequence(key, &idx, &len2, sz) >= 0)) { + idx += (word32)len2; + if (CheckBitString(key, &idx, &len2, sz, 1, NULL) >= 0) { + key += idx; + keySz = len2; + } + } + + ret = wolfSSL_EVP_Digest(key, keySz, buf, len, digest, NULL); WOLFSSL_LEAVE("wolfSSL_X509_pubkey_digest", ret); return ret; } @@ -18708,54 +18728,24 @@ void wolfSSL_X509V3_set_ctx(WOLFSSL_X509V3_CTX* ctx, WOLFSSL_X509* issuer, WOLFSSL_X509* subject, WOLFSSL_X509* req, WOLFSSL_X509_CRL* crl, int flag) { - int ret = WOLFSSL_SUCCESS; WOLFSSL_ENTER("wolfSSL_X509V3_set_ctx"); - if (!ctx) { - ret = WOLFSSL_FAILURE; + if (ctx == NULL) { WOLFSSL_MSG("wolfSSL_X509V3_set_ctx() called with null ctx."); + return; } - if (ret == WOLFSSL_SUCCESS && (ctx->x509 != NULL)) { - ret = WOLFSSL_FAILURE; - WOLFSSL_MSG("wolfSSL_X509V3_set_ctx() called " - "with ctx->x509 already allocated."); - } - - if (ret == WOLFSSL_SUCCESS) { - ctx->x509 = wolfSSL_X509_new_ex( - (issuer && issuer->heap) ? issuer->heap : - (subject && subject->heap) ? subject->heap : - (req && req->heap) ? req->heap : - NULL); - if (!ctx->x509) { - ret = WOLFSSL_FAILURE; - WOLFSSL_MSG("wolfSSL_X509_new_ex() failed " - "in wolfSSL_X509V3_set_ctx()."); - } - } - - /* Set parameters in ctx as long as ret == WOLFSSL_SUCCESS */ - if (ret == WOLFSSL_SUCCESS && issuer) - ret = wolfSSL_X509_set_issuer_name(ctx->x509, &issuer->issuer); - - if (ret == WOLFSSL_SUCCESS && subject) - ret = wolfSSL_X509_set_subject_name(ctx->x509, &subject->subject); + ctx->issuer = issuer; + ctx->subject = subject; - if (ret == WOLFSSL_SUCCESS && req) { + if (req != NULL) { WOLFSSL_MSG("req not implemented."); } - - if (ret == WOLFSSL_SUCCESS && crl) { + if (crl != NULL) { WOLFSSL_MSG("crl not implemented."); } - - if (ret == WOLFSSL_SUCCESS && flag) { + if (flag != 0) { WOLFSSL_MSG("flag not implemented."); } - - if (ret != WOLFSSL_SUCCESS) { - WOLFSSL_MSG("Error setting WOLFSSL_X509V3_CTX parameters."); - } } #ifndef NO_BIO diff --git a/tests/api.c b/tests/api.c index 6d80ce67a09..c4e2a400a22 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15949,7 +15949,12 @@ static int test_wolfSSL_certs(void) /************* Get Digest of Certificate ******************/ { byte digest[64]; /* max digest size */ + byte digest2[64]; word32 digestSz; + byte keyId[WC_SHA_DIGEST_SIZE]; + int keyIdSz = (int)sizeof(keyId); + EVP_PKEY* pubKey = NULL; + X509* x509Pub = NULL; X509* x509Empty = NULL; XMEMSET(digest, 0, sizeof(digest)); @@ -15963,6 +15968,20 @@ static int test_wolfSSL_certs(void) NULL), WOLFSSL_SUCCESS); ExpectIntEQ(X509_pubkey_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz), WOLFSSL_SUCCESS); + /* SHA-1 of the subjectPublicKey is the subject key identifier. */ + ExpectIntEQ((int)digestSz, WC_SHA_DIGEST_SIZE); + ExpectNotNull(wolfSSL_X509_get_subjectKeyID(x509ext, keyId, &keyIdSz)); + ExpectIntEQ(keyIdSz, WC_SHA_DIGEST_SIZE); + ExpectIntEQ(XMEMCMP(digest, keyId, WC_SHA_DIGEST_SIZE), 0); + /* A key stored with X509_set_pubkey digests to the same value. */ + ExpectNotNull(pubKey = X509_get_pubkey(x509ext)); + ExpectNotNull(x509Pub = wolfSSL_X509_new()); + ExpectIntEQ(X509_set_pubkey(x509Pub, pubKey), WOLFSSL_SUCCESS); + ExpectIntEQ(X509_pubkey_digest(x509Pub, wolfSSL_EVP_sha1(), digest2, + &digestSz), WOLFSSL_SUCCESS); + ExpectIntEQ(XMEMCMP(digest2, keyId, WC_SHA_DIGEST_SIZE), 0); + wolfSSL_X509_free(x509Pub); + EVP_PKEY_free(pubKey); ExpectIntEQ(X509_pubkey_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz), WOLFSSL_SUCCESS); diff --git a/tests/api/test_ossl_asn1.c b/tests/api/test_ossl_asn1.c index a81bbb5a106..bc4a3107e11 100644 --- a/tests/api/test_ossl_asn1.c +++ b/tests/api/test_ossl_asn1.c @@ -2365,6 +2365,60 @@ int test_wolfSSL_ASN1_TIME_adj(void) return EXPECT_RESULT(); } +int test_wolfSSL_ASN1_TIME_set(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \ + !defined(USER_TIME) && !defined(TIME_OVERRIDES) + const int year = 365*24*60*60; + const int day = 24*60*60; + const int hour = 60*60; + const int mini = 60; + WOLFSSL_ASN1_TIME* s = NULL; + WOLFSSL_ASN1_TIME* asn_time = NULL; + char date_str[CTC_DATE_SIZE + 1]; + time_t t; + + /* 2000/2/15 20:30:00 */ + t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day; + + ExpectNotNull(s = wolfSSL_ASN1_TIME_new()); + ExpectPtrEq(wolfSSL_ASN1_TIME_set(s, t), s); + if (s != NULL) { + ExpectIntEQ(s->type, ASN_UTC_TIME); + ExpectIntEQ(s->length, ASN_UTC_TIME_SIZE - 1); + XMEMCPY(date_str, s->data, CTC_DATE_SIZE); + date_str[CTC_DATE_SIZE] = '\0'; + ExpectIntEQ(XMEMCMP(date_str, "000215203000Z", 13), 0); + } + wolfSSL_ASN1_TIME_free(s); + s = NULL; + + /* Allocated when NULL passed in. */ + ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_set(NULL, t)); + if (asn_time != NULL) { + ExpectIntEQ(asn_time->type, ASN_UTC_TIME); + ExpectIntEQ(XMEMCMP(asn_time->data, "000215203000Z", 13), 0); + } + wolfSSL_ASN1_TIME_free(asn_time); + asn_time = NULL; + +#if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) + /* 2055/03/01 09:00:00 uses GeneralizedTime. */ + t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day; + ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_set(NULL, t)); + if (asn_time != NULL) { + ExpectIntEQ(asn_time->type, ASN_GENERALIZED_TIME); + ExpectIntEQ(asn_time->length, ASN_GENERALIZED_TIME_SIZE - 1); + ExpectIntEQ(XMEMCMP(asn_time->data, "20550301090000Z", 15), 0); + } + wolfSSL_ASN1_TIME_free(asn_time); + asn_time = NULL; +#endif +#endif + return EXPECT_RESULT(); +} + int test_wolfSSL_ASN1_TIME_to_tm(void) { EXPECT_DECLS; diff --git a/tests/api/test_ossl_asn1.h b/tests/api/test_ossl_asn1.h index 61a905ea7ac..690ca6d5ccc 100644 --- a/tests/api/test_ossl_asn1.h +++ b/tests/api/test_ossl_asn1.h @@ -51,6 +51,7 @@ int test_wolfSSL_ASN1_TIME(void); int test_wolfSSL_ASN1_TIME_to_string(void); int test_wolfSSL_ASN1_TIME_diff_compare(void); int test_wolfSSL_ASN1_TIME_adj(void); +int test_wolfSSL_ASN1_TIME_set(void); int test_wolfSSL_ASN1_TIME_to_tm(void); int test_wolfSSL_ASN1_TIME_to_generalizedtime(void); int test_wolfSSL_ASN1_TIME_print(void); @@ -99,6 +100,7 @@ int test_ASN1_strings(void); TEST_DECL_GROUP("ossl_asn1_tm", test_wolfSSL_ASN1_TIME_to_string), \ TEST_DECL_GROUP("ossl_asn1_tm", test_wolfSSL_ASN1_TIME_diff_compare), \ TEST_DECL_GROUP("ossl_asn1_tm", test_wolfSSL_ASN1_TIME_adj), \ + TEST_DECL_GROUP("ossl_asn1_tm", test_wolfSSL_ASN1_TIME_set), \ TEST_DECL_GROUP("ossl_asn1_tm", test_wolfSSL_ASN1_TIME_to_tm), \ TEST_DECL_GROUP("ossl_asn1_tm", \ test_wolfSSL_ASN1_TIME_to_generalizedtime), \ diff --git a/tests/api/test_ossl_x509_ext.c b/tests/api/test_ossl_x509_ext.c index 9cf1c10c987..3bd733afd77 100644 --- a/tests/api/test_ossl_x509_ext.c +++ b/tests/api/test_ossl_x509_ext.c @@ -1095,25 +1095,22 @@ int test_wolfSSL_X509V3_set_ctx(void) wolfSSL_X509V3_set_ctx(NULL, NULL, NULL, NULL, NULL, 0); wolfSSL_X509V3_set_ctx(&ctx, NULL, NULL, NULL, NULL, 0); - wolfSSL_X509_free(ctx.x509); - ctx.x509 = NULL; + ExpectNull(ctx.issuer); + ExpectNull(ctx.subject); wolfSSL_X509V3_set_ctx(&ctx, issuer, NULL, NULL, NULL, 0); - wolfSSL_X509_free(ctx.x509); - ctx.x509 = NULL; + ExpectPtrEq(ctx.issuer, issuer); + ExpectNull(ctx.subject); wolfSSL_X509V3_set_ctx(&ctx, NULL, subject, NULL, NULL, 0); - wolfSSL_X509_free(ctx.x509); - ctx.x509 = NULL; - wolfSSL_X509V3_set_ctx(&ctx, NULL, NULL, &req, NULL, 0); - wolfSSL_X509_free(ctx.x509); - ctx.x509 = NULL; - wolfSSL_X509V3_set_ctx(&ctx, NULL, NULL, NULL, &crl, 0); - wolfSSL_X509_free(ctx.x509); - ctx.x509 = NULL; - wolfSSL_X509V3_set_ctx(&ctx, NULL, NULL, NULL, NULL, 1); - /* X509 allocated in context results in 'failure' (but not return). */ - wolfSSL_X509V3_set_ctx(&ctx, NULL, NULL, NULL, NULL, 0); - wolfSSL_X509_free(ctx.x509); - ctx.x509 = NULL; + ExpectNull(ctx.issuer); + ExpectPtrEq(ctx.subject, subject); + wolfSSL_X509V3_set_ctx(&ctx, issuer, subject, &req, &crl, 1); + ExpectPtrEq(ctx.issuer, issuer); + ExpectPtrEq(ctx.subject, subject); + /* No zero init required. */ + XMEMSET(&ctx, 0xff, sizeof(ctx)); + wolfSSL_X509V3_set_ctx(&ctx, issuer, subject, NULL, NULL, 0); + ExpectPtrEq(ctx.issuer, issuer); + ExpectPtrEq(ctx.subject, subject); wolfSSL_X509_free(subject); wolfSSL_X509_free(issuer); diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 5317d429f19..eb873c96b7d 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -527,7 +527,8 @@ struct WOLFSSL_ACCESS_DESCRIPTION { }; struct WOLFSSL_X509V3_CTX { - WOLFSSL_X509* x509; + WOLFSSL_X509* issuer; + WOLFSSL_X509* subject; }; /* The fields before d are given in order by X509_NAME_OBJ() in src/x509.c, From 1fe50d18ca249579cc5288fc36a5d6e3ab276acd Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Sep 2026 09:43:14 +0000 Subject: [PATCH 11/28] tcpdump 4.99.6: enable MD5 with --enable-tcpdump, add workflow tcpdump uses MD5 for the TCP MD5 signature option. The "options that require MD5" check runs before the tcpdump block sets OPENSSL_EXTRA and DES3, so MD5 stayed disabled. Turn it on in the tcpdump block. The workflow builds libpcap 1.10.5 next to tcpdump because the libpcap 1.10.4 shipped by Ubuntu 24.04 reads pcap timestamps as signed 32-bit values and fails the time_2038_overflow, time_2039, time_2106 and time_2106_max tests. --- .github/workflows/tcpdump.yml | 122 ++++++++++++++++++++++++++++++++++ configure.ac | 5 ++ 2 files changed, 127 insertions(+) create mode 100644 .github/workflows/tcpdump.yml diff --git a/.github/workflows/tcpdump.yml b/.github/workflows/tcpdump.yml new file mode 100644 index 00000000000..accbbfcb09c --- /dev/null +++ b/.github/workflows/tcpdump.yml @@ -0,0 +1,122 @@ +name: tcpdump Tests + +# START OF COMMON SECTION +on: + push: + branches: [ 'release/**' ] + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + branches: [ '*' ] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read +# END OF COMMON SECTION + +jobs: + build_wolfssl: + name: Build wolfSSL + if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} + # Just to keep it the same as the testing target + runs-on: ubuntu-24.04 + # This should be a safe limit for the tests to run. + timeout-minutes: 4 + steps: + - name: Build wolfSSL + uses: wolfSSL/actions-build-autotools-project@v1 + with: + path: wolfssl + configure: --enable-tcpdump + install: true + check: false + + - name: tar build-dir + run: tar -zcf build-dir.tgz build-dir + + - name: Upload built lib + uses: actions/upload-artifact@v6 + with: + name: wolf-install-tcpdump + path: build-dir.tgz + retention-days: 5 + + tcpdump_check: + strategy: + fail-fast: false + matrix: + # List of releases to test + ref: [ 4.99.6 ] + name: ${{ matrix.ref }} + if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} + runs-on: ubuntu-24.04 + # This should be a safe limit for the tests to run. + timeout-minutes: 10 + needs: build_wolfssl + steps: + - name: Checkout wolfSSL CI actions + uses: actions/checkout@v5 + with: + sparse-checkout: .github/actions + fetch-depth: 1 + + - name: Download lib + uses: actions/download-artifact@v7 + with: + name: wolf-install-tcpdump + + - name: untar build-dir + run: tar -xf build-dir.tgz + + - name: Install dependencies + uses: ./.github/actions/install-apt-deps + with: + packages: bison flex + ghcr-debs-tag: ubuntu-24.04-full + + - name: Checkout OSP + uses: actions/checkout@v5 + with: + repository: wolfssl/osp + path: osp + fetch-depth: 1 + + # Ubuntu 24.04 ships libpcap 1.10.4, which reads pcap timestamps as + # signed 32-bit values. That breaks the time_2038_overflow, time_2039, + # time_2106 and time_2106_max tests. Build libpcap 1.10.5 next to + # tcpdump instead; tcpdump's configure picks up ../libpcap on its own. + - name: Checkout libpcap + uses: actions/checkout@v5 + with: + repository: the-tcpdump-group/libpcap + path: libpcap + ref: libpcap-1.10.5 + fetch-depth: 1 + + - name: Build libpcap + working-directory: libpcap + run: | + ./autogen.sh + ./configure + make -j + + - name: Checkout tcpdump + uses: actions/checkout@v5 + with: + repository: the-tcpdump-group/tcpdump + path: tcpdump + ref: tcpdump-${{ matrix.ref }} + fetch-depth: 1 + + - name: Build and test tcpdump + working-directory: tcpdump + run: | + export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/build-dir/lib:$LD_LIBRARY_PATH + patch -p1 < $GITHUB_WORKSPACE/osp/tcpdump/${{ matrix.ref }}/tcpdump-${{ matrix.ref }}.patch + autoreconf -ivf + ./configure --with-wolfssl=$GITHUB_WORKSPACE/build-dir + make -j + ldd ./tcpdump | grep wolfssl + make check diff --git a/configure.ac b/configure.ac index ec0bd60d4b8..b69a24a5f8c 100644 --- a/configure.ac +++ b/configure.ac @@ -11381,6 +11381,11 @@ then then ENABLED_DES3="yes" fi + + if test "x$ENABLED_MD5" = "xno" + then + ENABLED_MD5="yes" + fi fi # sblim-sfcb support From bdc774884dfc407d7cad30590e10552706c79d85 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 3 Dec 2026 05:14:29 +0000 Subject: [PATCH 12/28] python 3.14.5: free AIA entries, add workflow OpenSSL's AUTHORITY_INFO_ACCESS_free is an ASN.1 item free and releases the ACCESS_DESCRIPTION entries. wolfSSL's freed only the stack nodes, so CPython's _get_aia_uri leaked the entries. No in-tree caller relied on the shallow free. Add the 3.14.5 entry to the Python workflow matrix. --- .github/workflows/python.yml | 16 ++++++++++++++++ src/x509.c | 7 +++---- tests/api/test_ossl_x509_ext.c | 5 +++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 95d33be9965..7255df7bf9d 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -96,6 +96,22 @@ jobs: test_urllib2_localnet test_xmlrpc test_docxmlrpc + - python_ver: 3.14.5 + tests: >- + test_ssl + test.test_asyncio.test_ssl + test.test_asyncio.test_sslproto + test_hashlib + test_hmac + test_secrets + test_ftplib + test_imaplib + test_poplib + test_smtplib + test_httplib + test_urllib2_localnet + test_xmlrpc + test_docxmlrpc name: Python ${{ matrix.python_ver }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 diff --git a/src/x509.c b/src/x509.c index dcafcf3abd4..e4474abdd00 100644 --- a/src/x509.c +++ b/src/x509.c @@ -5521,14 +5521,13 @@ void wolfSSL_sk_ACCESS_DESCRIPTION_free(WOLFSSL_STACK* sk) } -/* AUTHORITY_INFO_ACCESS object is a stack of ACCESS_DESCRIPTION objects, - * to free the stack the WOLFSSL_ACCESS_DESCRIPTION stack free function is - * used */ +/* AUTHORITY_INFO_ACCESS object is a stack of ACCESS_DESCRIPTION objects. + * Free the entries and the stack, as OpenSSL does. */ void wolfSSL_AUTHORITY_INFO_ACCESS_free( WOLF_STACK_OF(WOLFSSL_ACCESS_DESCRIPTION)* sk) { WOLFSSL_ENTER("wolfSSL_AUTHORITY_INFO_ACCESS_free"); - wolfSSL_sk_ACCESS_DESCRIPTION_free(sk); + wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(sk, wolfSSL_ACCESS_DESCRIPTION_free); } void wolfSSL_AUTHORITY_INFO_ACCESS_pop_free( diff --git a/tests/api/test_ossl_x509_ext.c b/tests/api/test_ossl_x509_ext.c index 3bd733afd77..a0494e4a4a3 100644 --- a/tests/api/test_ossl_x509_ext.c +++ b/tests/api/test_ossl_x509_ext.c @@ -1692,6 +1692,11 @@ int test_wolfSSL_X509V3_EXT_aia(void) wolfSSL_AUTHORITY_INFO_ACCESS_pop_free(aia, wolfSSL_ACCESS_DESCRIPTION_free); + aia = NULL; + /* Plain free releases the entries too. */ + ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS *) + wolfSSL_X509V3_EXT_d2i(ext)); + wolfSSL_AUTHORITY_INFO_ACCESS_free(aia); wolfSSL_ASN1_OBJECT_free(entry); wolfSSL_sk_free(node); wolfSSL_ASN1_OBJECT_free(obj); From b6f1ec945ee5321390c4adffce8c15624ff4783e Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 26 Nov 2026 05:12:33 +0000 Subject: [PATCH 13/28] softhsm 2.7.0: add BN_mask_bits, EVP_PKEY_RSA_PSS, update workflow - Add EVP_PKEY_RSA_PSS. - Implement BN_mask_bits (was a stub) and guard its declaration like the definition: wolfSSL_mask_bits is not built with WOLFSSL_SP_MATH, and an unconditional declaration turned a missing API into a link error. - Test that partial EVP_DecryptUpdate calls with padding stay within inl + block_size. - Add softhsm 2.7.0 to the workflow matrix. ERR_set_mark/ERR_pop_to_mark are now provided by master. --- .github/workflows/softhsm.yml | 2 +- src/ssl_bn.c | 30 +++++++++++---- tests/api/test_evp_cipher.c | 69 +++++++++++++++++++++++++++++++++++ tests/api/test_evp_cipher.h | 2 + tests/api/test_evp_pkey.c | 3 ++ tests/api/test_ossl_bn.c | 22 +++++++++-- wolfssl/openssl/bn.h | 4 ++ wolfssl/openssl/evp.h | 5 +++ 8 files changed, 125 insertions(+), 12 deletions(-) diff --git a/.github/workflows/softhsm.yml b/.github/workflows/softhsm.yml index aeb60ce2271..6b0610638bf 100644 --- a/.github/workflows/softhsm.yml +++ b/.github/workflows/softhsm.yml @@ -48,7 +48,7 @@ jobs: fail-fast: false matrix: # List of releases to test - ref: [ 2.6.1 ] + ref: [ 2.6.1, 2.7.0 ] name: ${{ matrix.ref }} if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 diff --git a/src/ssl_bn.c b/src/ssl_bn.c index 0681324d66b..1d300104b89 100644 --- a/src/ssl_bn.c +++ b/src/ssl_bn.c @@ -880,24 +880,38 @@ int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn) return ret; } -#ifndef NO_WOLFSSL_STUB -/* Mask the lowest n bits. - * - * TODO: mp_mod_2d() +#ifndef WOLFSSL_SP_MATH +/* Keep only the lowest n bits. bn = bn mod 2^n * * Return compliant with OpenSSL. * * @param [in, out] bn Big number to operation on. * @param [in] n Number of bits. + * @return 1 on success. + * @return 0 when bn or internal representation of bn is NULL. + * @return 0 when n is negative. * @return 0 on failure. */ int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n) { - (void)bn; - (void)n; + int ret = 1; + WOLFSSL_ENTER("wolfSSL_BN_mask_bits"); - WOLFSSL_STUB("BN_mask_bits"); - return 0; + + /* Validate parameters. */ + if (BN_IS_NULL(bn) || (n < 0)) { + WOLFSSL_MSG("bn NULL error"); + ret = 0; + } + + /* Use wolfCrypt perform operation. */ + if ((ret == 1) && (mp_mod_2d((mp_int*)bn->internal, n, + (mp_int*)bn->internal) != MP_OKAY)) { + WOLFSSL_MSG("mp_mod_2d error"); + ret = 0; + } + + return ret; } #endif diff --git a/tests/api/test_evp_cipher.c b/tests/api/test_evp_cipher.c index 0b26a39fc87..337f59f5a25 100644 --- a/tests/api/test_evp_cipher.c +++ b/tests/api/test_evp_cipher.c @@ -644,6 +644,75 @@ int test_wolfSSL_EVP_CipherUpdate_Null(void) return EXPECT_RESULT(); } +/* Decrypting with padding in parts must never write more than + * inl + block_size bytes into out. */ +int test_wolfSSL_EVP_DecryptUpdate_partial(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \ + defined(WOLFSSL_AES_128) + WOLFSSL_EVP_CIPHER_CTX* ctx = NULL; + const byte key[16] = { 0 }; + const byte iv[16] = { 0 }; + byte plain[70]; + byte cipher[80]; + byte dec[96]; + byte out[96]; + int cipherLen = 0; + int decLen = 0; + int outl = 0; + int i; + /* Part sizes that leave a held back block next to buffered bytes. */ + const int parts[] = { 3, 16, 3, 29, 16, 8, 5 }; + int off = 0; + + for (i = 0; i < (int)sizeof(plain); i++) + plain[i] = (byte)i; + + ExpectNotNull(ctx = wolfSSL_EVP_CIPHER_CTX_new()); + ExpectIntEQ(wolfSSL_EVP_CipherInit_ex(ctx, wolfSSL_EVP_aes_128_cbc(), + NULL, key, iv, 1), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, cipher, &outl, plain, + (int)sizeof(plain)), WOLFSSL_SUCCESS); + cipherLen = outl; + ExpectIntEQ(wolfSSL_EVP_CipherFinal(ctx, cipher + cipherLen, &outl), + WOLFSSL_SUCCESS); + cipherLen += outl; + ExpectIntEQ(cipherLen, 80); + + ExpectIntEQ(wolfSSL_EVP_CipherInit_ex(ctx, wolfSSL_EVP_aes_128_cbc(), + NULL, key, iv, 0), WOLFSSL_SUCCESS); + for (i = 0; i < (int)(sizeof(parts) / sizeof(parts[0])); i++) { + int inl = parts[i]; + int j; + + XMEMSET(out, 0xAA, sizeof(out)); + ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, out, &outl, cipher + off, + inl), WOLFSSL_SUCCESS); + ExpectIntLE(outl, inl + 16); + /* Nothing written past inl + block_size. */ + for (j = inl + 16; j < (int)sizeof(out); j++) + ExpectIntEQ(out[j], 0xAA); + ExpectIntLE(decLen + outl, (int)sizeof(dec)); + if (EXPECT_SUCCESS()) + XMEMCPY(dec + decLen, out, (size_t)outl); + decLen += outl; + off += inl; + } + ExpectIntEQ(off, cipherLen); + ExpectIntEQ(wolfSSL_EVP_CipherFinal(ctx, out, &outl), WOLFSSL_SUCCESS); + ExpectIntLE(decLen + outl, (int)sizeof(dec)); + if (EXPECT_SUCCESS()) + XMEMCPY(dec + decLen, out, (size_t)outl); + decLen += outl; + ExpectIntEQ(decLen, (int)sizeof(plain)); + ExpectBufEQ(dec, plain, sizeof(plain)); + + wolfSSL_EVP_CIPHER_CTX_free(ctx); +#endif + return EXPECT_RESULT(); +} + /* Test for wolfSSL_EVP_CIPHER_type_string() */ int test_wolfSSL_EVP_CIPHER_type_string(void) { diff --git a/tests/api/test_evp_cipher.h b/tests/api/test_evp_cipher.h index 634fd7e17a7..47f08e5d1b6 100644 --- a/tests/api/test_evp_cipher.h +++ b/tests/api/test_evp_cipher.h @@ -32,6 +32,7 @@ int test_wolfSSL_EVP_get_cipherbynid(void); int test_wolfSSL_EVP_CIPHER_block_size(void); int test_wolfSSL_EVP_CIPHER_iv_length(void); int test_wolfSSL_EVP_CipherUpdate_Null(void); +int test_wolfSSL_EVP_DecryptUpdate_partial(void); int test_wolfSSL_EVP_CIPHER_type_string(void); int test_wolfSSL_EVP_BytesToKey(void); int test_wolfSSL_EVP_Cipher_extra(void); @@ -97,6 +98,7 @@ int test_evp_cipher_cfb1_length_overflow(void); TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_block_size), \ TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_iv_length), \ TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CipherUpdate_Null), \ + TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_DecryptUpdate_partial), \ TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_type_string), \ TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_BytesToKey), \ TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_Cipher_extra), \ diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index ef637eca6d6..f040dbe662f 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -144,6 +144,9 @@ int test_wolfSSL_EVP_PKEY_id(void) ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA); + ExpectIntEQ(EVP_PKEY_RSA_PSS, NID_rsassaPss); + ExpectIntNE(EVP_PKEY_RSA_PSS, EVP_PKEY_RSA); + EVP_PKEY_free(pkey); #endif return EXPECT_RESULT(); diff --git a/tests/api/test_ossl_bn.c b/tests/api/test_ossl_bn.c index b4b4056a676..e168a04e34d 100644 --- a/tests/api/test_ossl_bn.c +++ b/tests/api/test_ossl_bn.c @@ -532,9 +532,25 @@ int test_wolfSSL_BN_bits(void) ExpectIntEQ(BN_set_bit(a, 129), 1); ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL); -#ifndef NO_WOLFSSL_STUB - ExpectIntEQ(BN_mask_bits(a, 1), 0); -#endif + /* Invalid parameters. */ + ExpectIntEQ(BN_mask_bits(NULL, 1), 0); + ExpectIntEQ(BN_mask_bits(&emptyBN, 1), 0); + ExpectIntEQ(BN_mask_bits(a, -1), 0); + + /* a = 2^129 + 2 */ + ExpectIntEQ(BN_mask_bits(a, 128), 1); + ExpectIntEQ(BN_num_bits(a), 2); + ExpectIntEQ(BN_get_word(a), 2); + /* No change when a already fits. */ + ExpectIntEQ(BN_mask_bits(a, 200), 1); + ExpectIntEQ(BN_get_word(a), 2); + ExpectIntEQ(BN_mask_bits(a, 1), 1); + ExpectIntEQ(BN_is_zero(a), 1); + ExpectIntEQ(BN_set_word(a, 0xff), 1); + ExpectIntEQ(BN_mask_bits(a, 4), 1); + ExpectIntEQ(BN_get_word(a), 0xf); + ExpectIntEQ(BN_mask_bits(a, 0), 1); + ExpectIntEQ(BN_is_zero(a), 1); BN_free(a); #endif diff --git a/wolfssl/openssl/bn.h b/wolfssl/openssl/bn.h index 7371f8a03ac..badb69d6341 100644 --- a/wolfssl/openssl/bn.h +++ b/wolfssl/openssl/bn.h @@ -140,7 +140,9 @@ WOLFSSL_API int wolfSSL_BN_bn2binpad(const WOLFSSL_BIGNUM* bn, unsigned char* r, WOLFSSL_API WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* str, int len, WOLFSSL_BIGNUM* ret); +#ifndef WOLFSSL_SP_MATH WOLFSSL_API int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n); +#endif WOLFSSL_API int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom); @@ -263,7 +265,9 @@ typedef WOLFSSL_BN_GENCB BN_GENCB; #define BN_gcd wolfSSL_BN_gcd #define BN_value_one wolfSSL_BN_value_one +#ifndef WOLFSSL_SP_MATH #define BN_mask_bits wolfSSL_mask_bits +#endif #define BN_pseudo_rand wolfSSL_BN_pseudo_rand #define BN_rand wolfSSL_BN_rand diff --git a/wolfssl/openssl/evp.h b/wolfssl/openssl/evp.h index c9ef9d800bf..7032b6a169f 100644 --- a/wolfssl/openssl/evp.h +++ b/wolfssl/openssl/evp.h @@ -495,6 +495,10 @@ enum { #define WOLFSSL_EVP_PKEY_PRINT_INDENT_MAX 128 +/* wolfSSL decodes RSA-PSS keys as WC_EVP_PKEY_RSA. This id is only for + * applications that switch on it alongside WC_EVP_PKEY_RSA. */ +#define WC_EVP_PKEY_RSA_PSS WC_NID_rsassaPss + #define WC_EVP_PKEY_OP_SIGN (1 << 3) #define WC_EVP_PKEY_OP_VERIFY (1 << 5) #define WC_EVP_PKEY_OP_ENCRYPT (1 << 6) @@ -520,6 +524,7 @@ enum { #define ARC4_TYPE WC_ARC4_TYPE #define NULL_CIPHER_TYPE WC_NULL_CIPHER_TYPE #define EVP_PKEY_RSA WC_EVP_PKEY_RSA +#define EVP_PKEY_RSA_PSS WC_EVP_PKEY_RSA_PSS #define EVP_PKEY_DSA WC_EVP_PKEY_DSA #define EVP_PKEY_EC WC_EVP_PKEY_EC #define AES_128_GCM_TYPE WC_AES_128_GCM_TYPE From 796acdd2dc58638c615150bfb34318c11f8ba07f Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 21 Sep 2026 05:41:01 +0000 Subject: [PATCH 14/28] Address review: guard BN_mask_bits by mp_mod_2d availability sp_mod_2d() is only compiled when (WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY) || HAVE_ECC, so guarding on WOLFSSL_SP_MATH alone left a verify-only SP build declaring BN_mask_bits without a backend. Track the backend condition with WOLFSSL_HAVE_MP_MOD_2D and use it for the implementation, the declaration, the alias and the test. --- src/ssl_bn.c | 2 +- tests/api/test_ossl_bn.c | 2 ++ wolfssl/openssl/bn.h | 12 ++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ssl_bn.c b/src/ssl_bn.c index 1d300104b89..9ae687d011f 100644 --- a/src/ssl_bn.c +++ b/src/ssl_bn.c @@ -880,7 +880,7 @@ int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn) return ret; } -#ifndef WOLFSSL_SP_MATH +#ifdef WOLFSSL_HAVE_MP_MOD_2D /* Keep only the lowest n bits. bn = bn mod 2^n * * Return compliant with OpenSSL. diff --git a/tests/api/test_ossl_bn.c b/tests/api/test_ossl_bn.c index e168a04e34d..f46ec787530 100644 --- a/tests/api/test_ossl_bn.c +++ b/tests/api/test_ossl_bn.c @@ -532,6 +532,7 @@ int test_wolfSSL_BN_bits(void) ExpectIntEQ(BN_set_bit(a, 129), 1); ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL); +#ifdef WOLFSSL_HAVE_MP_MOD_2D /* Invalid parameters. */ ExpectIntEQ(BN_mask_bits(NULL, 1), 0); ExpectIntEQ(BN_mask_bits(&emptyBN, 1), 0); @@ -551,6 +552,7 @@ int test_wolfSSL_BN_bits(void) ExpectIntEQ(BN_get_word(a), 0xf); ExpectIntEQ(BN_mask_bits(a, 0), 1); ExpectIntEQ(BN_is_zero(a), 1); +#endif BN_free(a); #endif diff --git a/wolfssl/openssl/bn.h b/wolfssl/openssl/bn.h index badb69d6341..f6f516151f8 100644 --- a/wolfssl/openssl/bn.h +++ b/wolfssl/openssl/bn.h @@ -140,7 +140,15 @@ WOLFSSL_API int wolfSSL_BN_bn2binpad(const WOLFSSL_BIGNUM* bn, unsigned char* r, WOLFSSL_API WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* str, int len, WOLFSSL_BIGNUM* ret); -#ifndef WOLFSSL_SP_MATH +/* mp_mod_2d() is not built in every SP math configuration - see sp_int.c. */ +#if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL) + #define WOLFSSL_HAVE_MP_MOD_2D +#elif (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \ + defined(HAVE_ECC) + #define WOLFSSL_HAVE_MP_MOD_2D +#endif + +#ifdef WOLFSSL_HAVE_MP_MOD_2D WOLFSSL_API int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n); #endif @@ -265,7 +273,7 @@ typedef WOLFSSL_BN_GENCB BN_GENCB; #define BN_gcd wolfSSL_BN_gcd #define BN_value_one wolfSSL_BN_value_one -#ifndef WOLFSSL_SP_MATH +#ifdef WOLFSSL_HAVE_MP_MOD_2D #define BN_mask_bits wolfSSL_mask_bits #endif From 299467c1c3a459b904c03df2b7818e761beaf40d Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 21 Sep 2026 05:41:01 +0000 Subject: [PATCH 15/28] Address review: let EVP_PKEY_dup copy an empty HMAC key EVP_PKEY_new_mac_key() and EVP_PKEY_new_raw_private_key() accept a zero-length HMAC key, but the pkey_sz <= 0 check rejected it. An HMAC key is raw octets, so only a negative size is invalid. --- tests/api/test_evp_pkey.c | 11 +++++++++++ wolfcrypt/src/evp_pk.c | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index f040dbe662f..38692083928 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -1017,6 +1017,17 @@ int test_wolfSSL_EVP_PKEY_dup(void) EVP_PKEY_free(key); key = NULL; + /* An empty HMAC key is still a key. */ + ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0)); + ExpectNotNull(dup = EVP_PKEY_dup(key)); + ExpectPtrNE(dup, key); + ExpectIntEQ(EVP_PKEY_id(dup), EVP_PKEY_HMAC); + ExpectIntEQ(dup->pkey_sz, 0); + EVP_PKEY_free(dup); + dup = NULL; + EVP_PKEY_free(key); + key = NULL; + #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \ defined(WOLFSSL_KEY_TO_DER) /* EVP_PKEY_set1_RSA() re-encodes the RSA key, so the flag has to follow diff --git a/wolfcrypt/src/evp_pk.c b/wolfcrypt/src/evp_pk.c index 519f134af53..7b31d7f84ad 100644 --- a/wolfcrypt/src/evp_pk.c +++ b/wolfcrypt/src/evp_pk.c @@ -1929,7 +1929,9 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey) WOLFSSL_ENTER("wolfSSL_EVP_PKEY_dup"); - if (pkey == NULL || pkey->pkey.ptr == NULL || pkey->pkey_sz <= 0) { + /* An HMAC key is raw octets, so an empty one is still a key. */ + if (pkey == NULL || pkey->pkey.ptr == NULL || pkey->pkey_sz < 0 || + ((pkey->pkey_sz == 0) && (pkey->type != WC_EVP_PKEY_HMAC))) { WOLFSSL_MSG("No key data to duplicate"); return NULL; } From 4a3acb9399a95e5be71dcba0cb9f7443758c544d Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 22 Sep 2026 05:20:50 +0000 Subject: [PATCH 16/28] Fix CI: dup an HMAC key whose empty buffer allocation returned NULL XMALLOC(0) may return NULL, so EVP_PKEY_new_mac_key() with a zero-length key leaves pkey.ptr NULL. EVP_PKEY_dup() rejected that as "no key data". Key the NULL-buffer check off pkey_sz instead. --- wolfcrypt/src/evp_pk.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/evp_pk.c b/wolfcrypt/src/evp_pk.c index 7b31d7f84ad..01726383d67 100644 --- a/wolfcrypt/src/evp_pk.c +++ b/wolfcrypt/src/evp_pk.c @@ -1929,9 +1929,11 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey) WOLFSSL_ENTER("wolfSSL_EVP_PKEY_dup"); - /* An HMAC key is raw octets, so an empty one is still a key. */ - if (pkey == NULL || pkey->pkey.ptr == NULL || pkey->pkey_sz < 0 || - ((pkey->pkey_sz == 0) && (pkey->type != WC_EVP_PKEY_HMAC))) { + /* An HMAC key is raw octets, so an empty one is still a key. XMALLOC(0) + * is allowed to return NULL, so an empty key carries no buffer either. */ + if ((pkey == NULL) || (pkey->pkey_sz < 0) || + ((pkey->pkey_sz == 0) ? (pkey->type != WC_EVP_PKEY_HMAC) + : (pkey->pkey.ptr == NULL))) { WOLFSSL_MSG("No key data to duplicate"); return NULL; } From a5de2d74329f4eb6c3e672c98a53adbbf496939b Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 22 Sep 2026 05:20:50 +0000 Subject: [PATCH 17/28] Fix CI: build EVP_PKEY_dup DER comparison only where the encoders exist i2d_PrivateKey()/i2d_PUBKEY() are compiled out when NO_ASN or NO_PWDBASED is set, so leantls + opensslextra failed to link tests/unit.test. --- tests/api/test_evp_pkey.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index 38692083928..a800eaae3c6 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -835,9 +835,14 @@ int test_EVP_PKEY_cmp(void) return EXPECT_RESULT(); } -#if defined(OPENSSL_EXTRA) && \ +/* i2d_PrivateKey()/i2d_PUBKEY() need the DER encoders. */ +#if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED) && \ ((!defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)) || \ (defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256))) + #define TEST_EVP_PKEY_DUP_I2D +#endif + +#ifdef TEST_EVP_PKEY_DUP_I2D /* Check dup is a distinct key with identical encoding, usable after src is * freed. */ static int test_EVP_PKEY_dup_check(EVP_PKEY** src, int id, int priv) @@ -887,7 +892,7 @@ static int test_EVP_PKEY_dup_check(EVP_PKEY** src, int id, int priv) return EXPECT_RESULT(); } -#endif /* OPENSSL_EXTRA && (RSA 2048 or ECC 256 cert buffers) */ +#endif /* TEST_EVP_PKEY_DUP_I2D */ /* d2i_evp_pkey() only knows DSA and DH in these builds. */ #if defined(OPENSSL_EXTRA) && !defined(NO_DSA) && \ @@ -940,7 +945,8 @@ int test_wolfSSL_EVP_PKEY_dup(void) RSA* rsa = NULL; #endif -#if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) +#if defined(TEST_EVP_PKEY_DUP_I2D) && !defined(NO_RSA) && \ + defined(USE_CERT_BUFFERS_2048) in = client_key_der_2048; ExpectNotNull(key = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &in, (long)sizeof_client_key_der_2048)); @@ -958,7 +964,8 @@ int test_wolfSSL_EVP_PKEY_dup(void) key = NULL; #endif -#if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256) +#if defined(TEST_EVP_PKEY_DUP_I2D) && defined(HAVE_ECC) && \ + defined(USE_CERT_BUFFERS_256) in = ecc_clikey_der_256; ExpectNotNull(key = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &in, (long)sizeof_ecc_clikey_der_256)); @@ -1102,6 +1109,8 @@ int test_wolfSSL_EVP_PKEY_dup(void) ExpectNotNull(key = EVP_PKEY_new()); ExpectNull(EVP_PKEY_dup(key)); EVP_PKEY_free(key); + + (void)in; #endif /* OPENSSL_EXTRA */ return EXPECT_RESULT(); } From a42f04caa0c9d093a06ab30a448ea803913fc8b8 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 22 Sep 2026 05:20:50 +0000 Subject: [PATCH 18/28] Fix CI: add usbmux BUID to the codespell ignore list buid is the usbmuxd ReadBUID protocol field, not a typo. --- .github/workflows/codespell.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codespell.yml b/.github/workflows/codespell.yml index 69e7911374e..afb893381f9 100644 --- a/.github/workflows/codespell.yml +++ b/.github/workflows/codespell.yml @@ -28,7 +28,7 @@ jobs: check_filenames: true check_hidden: true # Add comma separated list of words that occur multiple times that should be ignored (sorted alphabetically, case sensitive) - ignore_words_list: addIn,adin,ameba,aNULL,brunch,carryIn,chainG,ciph,cLen,cliKs,cna,dout,FPR,fpr,haveA,inCreated,inOut,inout,larg,LEAPYEAR,Merget,optionA,parm,parms,repid,rIn,userA,ser,siz,te,Te,HSI,failT,toLen,vor,checkin + ignore_words_list: addIn,adin,ameba,aNULL,brunch,buid,carryIn,chainG,ciph,cLen,cliKs,cna,dout,FPR,fpr,haveA,inCreated,inOut,inout,larg,LEAPYEAR,Merget,optionA,parm,parms,repid,rIn,userA,ser,siz,te,Te,HSI,failT,toLen,vor,checkin # The exclude_file contains lines of code that should be ignored. This is useful for individual lines which have non-words that can safely be ignored. exclude_file: '.codespellexcludelines' # To skip files entirely from being processed, add it to the following list: From 0f488c77cfa55845653154b4131d14acc0b76c07 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 22 Sep 2026 15:01:46 +0000 Subject: [PATCH 19/28] Fix CI: cast away the unused XSNPRINTF return in the pathlen test clang-tidy's bugprone-unused-return-value lists snprintf as a checked function, and XSNPRINTF resolves to it. --- tests/api/test_ossl_x509_ext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api/test_ossl_x509_ext.c b/tests/api/test_ossl_x509_ext.c index a0494e4a4a3..472c687622a 100644 --- a/tests/api/test_ossl_x509_ext.c +++ b/tests/api/test_ossl_x509_ext.c @@ -1304,7 +1304,7 @@ int test_wolfSSL_X509V3_EXT_nconf(void) "pathlen:1")); ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, "CA:TRUE,pathlen:x")); - XSNPRINTF(bcTooLong, sizeof(bcTooLong), "CA:TRUE,pathlen:%d", + (void)XSNPRINTF(bcTooLong, sizeof(bcTooLong), "CA:TRUE,pathlen:%d", WOLFSSL_MAX_PATH_LEN + 1); ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, NID_basic_constraints, bcTooLong)); From 4c5d188aef9634b68689db8184576c037ee5eb97 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 22 Sep 2026 15:01:46 +0000 Subject: [PATCH 20/28] Fix CI: assign a 0/1 value to the isPriv bitfield MSVC reported C4244 for the int-to-byte narrowing into the one-bit field, matching how the rest of evp.c sets it. --- wolfcrypt/src/evp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index c2bd93c0776..e67e945d9a1 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -10624,7 +10624,7 @@ int wolfSSL_EVP_PKEY_assign_EC_KEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY* key) * private/public state over the clear below. */ isPriv = pkey->isPriv; clearEVPPkeyKeys(pkey); /* clear out any previous keys */ - pkey->isPriv = isPriv; + pkey->isPriv = (isPriv != 0); pkey->type = WC_EVP_PKEY_EC; pkey->ecc = key; From 79fa39dd000e56e0c666ff83ec68894392f2905f Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 5 Oct 2026 13:12:39 +0000 Subject: [PATCH 21/28] Address review: record whether EVP_PKEY caches raw or DER key data EVP_PKEY_new_raw_*_key() and X509_get_pubkey() for Ed25519 and ML-DSA keys cache raw key bytes in pkey.ptr, while the d2i, keygen and set1 paths cache DER. Add an isRaw bit, set it on every path that fills pkey.ptr, and let EVP_PKEY_dup() rebuild raw keys with the raw constructors based on it instead of the key length. --- src/x509.c | 4 + tests/api/test_evp_pkey.c | 228 ++++++++++++++++++++++++++++++++++++-- wolfcrypt/src/evp.c | 10 ++ wolfcrypt/src/evp_pk.c | 27 ++++- wolfssl/openssl/evp.h | 3 +- wolfssl/ssl.h | 3 +- 6 files changed, 259 insertions(+), 16 deletions(-) diff --git a/src/x509.c b/src/x509.c index e4474abdd00..e5ec35f4092 100644 --- a/src/x509.c +++ b/src/x509.c @@ -7088,11 +7088,15 @@ static WOLFSSL_EVP_PKEY* X509DecodePubKey(WOLFSSL_X509* x509) ) { key->type = WC_EVP_PKEY_DILITHIUM; WOLFSSL_ATOMIC_STORE(key->mldsaOID, x509->pubKeyOID); + /* Certificate holds the raw key, not an SPKI. */ + key->isRaw = 1; } #endif #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT) else if (x509->pubKeyOID == ED25519k) { key->type = WC_EVP_PKEY_ED25519; + /* Certificate holds the raw key, not an SPKI. */ + key->isRaw = 1; } #endif #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT) && \ diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index a800eaae3c6..de2a81e2638 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -907,10 +907,19 @@ static int test_EVP_PKEY_dup_check(EVP_PKEY** src, int id, int priv) #define TEST_EVP_PKEY_DUP_DH #endif -#if defined(TEST_EVP_PKEY_DUP_DSA) || defined(TEST_EVP_PKEY_DUP_DH) -/* Check dup carries the same cached DER and the same private/public state, - * and that it left the error queue clean. */ -static int test_EVP_PKEY_dup_der_check(EVP_PKEY* src, int id, int priv) +/* Key types EVP_PKEY_new_raw_*_key() supports in these builds. */ +#if defined(OPENSSL_EXTRA) && ((defined(HAVE_ED25519) && \ + defined(HAVE_ED25519_KEY_IMPORT)) || (defined(HAVE_ED448) && \ + defined(HAVE_ED448_KEY_IMPORT)) || defined(HAVE_CURVE25519) || \ + defined(HAVE_CURVE448)) + #define TEST_EVP_PKEY_DUP_RAW +#endif + +#if defined(TEST_EVP_PKEY_DUP_DSA) || defined(TEST_EVP_PKEY_DUP_DH) || \ + defined(TEST_EVP_PKEY_DUP_RAW) +/* Check dup carries the same cached key data in the same format and the same + * private/public state, and that it left the error queue clean. */ +static int test_EVP_PKEY_dup_data_check(EVP_PKEY* src, int id, int priv) { EXPECT_DECLS; EVP_PKEY* dup = NULL; @@ -922,6 +931,7 @@ static int test_EVP_PKEY_dup_der_check(EVP_PKEY* src, int id, int priv) ExpectPtrNE(dup, src); ExpectIntEQ(EVP_PKEY_id(dup), id); ExpectIntEQ(dup->isPriv, priv); + ExpectIntEQ(dup->isRaw, src->isRaw); ExpectIntEQ(dup->pkey_sz, src->pkey_sz); ExpectIntEQ(XMEMCMP(dup->pkey.ptr, src->pkey.ptr, (size_t)src->pkey_sz), 0); @@ -930,7 +940,59 @@ static int test_EVP_PKEY_dup_der_check(EVP_PKEY* src, int id, int priv) return EXPECT_RESULT(); } -#endif /* TEST_EVP_PKEY_DUP_DSA || TEST_EVP_PKEY_DUP_DH */ +#endif /* TEST_EVP_PKEY_DUP_DSA || TEST_EVP_PKEY_DUP_DH || + * TEST_EVP_PKEY_DUP_RAW */ + +#ifdef TEST_EVP_PKEY_DUP_RAW +/* Dup a raw key and, when hdr is given, the same key wrapped in DER. Both + * are close in length, so dup has to go by the stored format. */ +static int test_EVP_PKEY_dup_raw_check(int id, int priv, + const unsigned char* raw, size_t rawSz, const unsigned char* hdr, + size_t hdrSz) +{ + EXPECT_DECLS; + EVP_PKEY* key = NULL; + unsigned char der[80]; + const unsigned char* p = der; + + if (priv) { + ExpectNotNull(key = EVP_PKEY_new_raw_private_key(id, NULL, raw, + rawSz)); + } + else { + ExpectNotNull(key = EVP_PKEY_new_raw_public_key(id, NULL, raw, + rawSz)); + } + ExpectIntEQ(key->isRaw, 1); + ExpectIntEQ(key->pkey_sz, (int)rawSz); + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, id, priv), TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + + if (hdr != NULL) { + ExpectIntLE(hdrSz + rawSz, sizeof(der)); + if (EXPECT_SUCCESS()) { + XMEMCPY(der, hdr, hdrSz); + XMEMCPY(der + hdrSz, raw, rawSz); + } + if (priv) { + ExpectNotNull(key = d2i_PrivateKey(id, NULL, &p, + (long)(hdrSz + rawSz))); + } + else { + ExpectNotNull(key = d2i_PublicKey(id, NULL, &p, + (long)(hdrSz + rawSz))); + } + ExpectIntEQ(key->isRaw, 0); + ExpectIntEQ(key->pkey_sz, (int)(hdrSz + rawSz)); + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, id, priv), + TEST_SUCCESS); + EVP_PKEY_free(key); + } + + return EXPECT_RESULT(); +} +#endif /* TEST_EVP_PKEY_DUP_RAW */ int test_wolfSSL_EVP_PKEY_dup(void) { @@ -987,7 +1049,7 @@ int test_wolfSSL_EVP_PKEY_dup(void) in = dsa_key_der_2048; ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_DSA, NULL, &in, (long)sizeof_dsa_key_der_2048)); - ExpectIntEQ(test_EVP_PKEY_dup_der_check(key, EVP_PKEY_DSA, 1), + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, EVP_PKEY_DSA, 1), TEST_SUCCESS); EVP_PKEY_free(key); key = NULL; @@ -995,7 +1057,7 @@ int test_wolfSSL_EVP_PKEY_dup(void) in = dsa_pub_key_der_2048; ExpectNotNull(key = d2i_PublicKey(EVP_PKEY_DSA, NULL, &in, (long)sizeof_dsa_pub_key_der_2048)); - ExpectIntEQ(test_EVP_PKEY_dup_der_check(key, EVP_PKEY_DSA, 0), + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, EVP_PKEY_DSA, 0), TEST_SUCCESS); EVP_PKEY_free(key); key = NULL; @@ -1005,7 +1067,7 @@ int test_wolfSSL_EVP_PKEY_dup(void) in = dh_key_der_2048; ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_DH, NULL, &in, (long)sizeof_dh_key_der_2048)); - ExpectIntEQ(test_EVP_PKEY_dup_der_check(key, EVP_PKEY_DH, 1), + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, EVP_PKEY_DH, 1), TEST_SUCCESS); EVP_PKEY_free(key); key = NULL; @@ -1035,6 +1097,156 @@ int test_wolfSSL_EVP_PKEY_dup(void) EVP_PKEY_free(key); key = NULL; + /* Raw keys cache the raw bytes, DER keys cache DER. Keys from RFC 8032 + * and RFC 7748. */ +#if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT) + { + static const unsigned char priv[] = { + 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, + 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, + 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, + 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60 + }; + static const unsigned char pub[] = { + 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, + 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, + 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, + 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a + }; + /* PKCS#8 and SPKI headers for Ed25519. */ + static const unsigned char privHdr[] = { + 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, + 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20 + }; + static const unsigned char pubHdr[] = { + 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, + 0x70, 0x03, 0x21, 0x00 + }; + + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_ED25519, 1, priv, + sizeof(priv), privHdr, sizeof(privHdr)), TEST_SUCCESS); + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_ED25519, 0, pub, + sizeof(pub), pubHdr, sizeof(pubHdr)), TEST_SUCCESS); + + /* dup goes by the stored format, not the length. */ + ExpectNotNull(key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, + NULL, pub, sizeof(pub))); + if (key != NULL) { + key->isRaw = 0; + } + ExpectNull(dup = EVP_PKEY_dup(key)); + EVP_PKEY_free(dup); + dup = NULL; + EVP_PKEY_free(key); + key = NULL; + } + + /* X509_get_pubkey() caches the raw key from the certificate. */ + { + X509* x509 = NULL; + + in = server_ed25519_cert; + ExpectNotNull(x509 = d2i_X509(NULL, &in, + (long)sizeof_server_ed25519_cert)); + ExpectNotNull(key = X509_get_pubkey(x509)); + ExpectIntEQ(key->isRaw, 1); + ExpectIntEQ(key->pkey_sz, ED25519_PUB_KEY_SIZE); + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, EVP_PKEY_ED25519, 0), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + X509_free(x509); + } +#endif +#if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT) + { + static const unsigned char priv[] = { + 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, + 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, + 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f, + 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3, + 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e, + 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, + 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, + 0x5b + }; + static const unsigned char pub[] = { + 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, + 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, + 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, + 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, + 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, + 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, + 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, + 0x80 + }; + /* PKCS#8 and SPKI headers for Ed448. */ + static const unsigned char privHdr[] = { + 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, + 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39 + }; + static const unsigned char pubHdr[] = { + 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, + 0x71, 0x03, 0x3a, 0x00 + }; + + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_ED448, 1, priv, + sizeof(priv), privHdr, sizeof(privHdr)), TEST_SUCCESS); + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_ED448, 0, pub, + sizeof(pub), pubHdr, sizeof(pubHdr)), TEST_SUCCESS); + } +#endif +#ifdef HAVE_CURVE25519 + { + static const unsigned char priv[] = { + 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, + 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, + 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, + 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a + }; + static const unsigned char pub[] = { + 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, + 0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, + 0x0d, 0xbf, 0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4, + 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a + }; + + /* X25519 has no DER decoder for EVP_PKEY. */ + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_X25519, 1, priv, + sizeof(priv), NULL, 0), TEST_SUCCESS); + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_X25519, 0, pub, + sizeof(pub), NULL, 0), TEST_SUCCESS); + } +#endif +#ifdef HAVE_CURVE448 + { + static const unsigned char priv[] = { + 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, + 0x75, 0xcf, 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, + 0xee, 0x9e, 0xe8, 0xba, 0xe8, 0xbc, 0x55, 0x65, + 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9, 0xba, 0xf5, + 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91, + 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, + 0x9a, 0xc2, 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b + }; + static const unsigned char pub[] = { + 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, + 0x7d, 0x22, 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, + 0x27, 0x3b, 0xd2, 0xb8, 0x3d, 0xe0, 0x9c, 0x63, + 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5, 0xd9, 0xbb, + 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4, + 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, + 0x17, 0x7f, 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0 + }; + + /* X448 has no DER decoder for EVP_PKEY. */ + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_X448, 1, priv, + sizeof(priv), NULL, 0), TEST_SUCCESS); + ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_X448, 0, pub, + sizeof(pub), NULL, 0), TEST_SUCCESS); + } +#endif + #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \ defined(WOLFSSL_KEY_TO_DER) /* EVP_PKEY_set1_RSA() re-encodes the RSA key, so the flag has to follow diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index e67e945d9a1..fdcb9bc9f0c 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -4178,6 +4178,7 @@ int wolfSSL_EVP_PKEY_keygen(WOLFSSL_EVP_PKEY_CTX *ctx, pkey->pkey_sz = edDerSz; pkey->pkcs8HeaderSz = (word16)hdrIdx; pkey->isPriv = 1; + pkey->isRaw = 0; ret = WOLFSSL_SUCCESS; } else { @@ -4958,6 +4959,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_mac_key(int type, WOLFSSL_ENGINE* e, pkey->pkey_sz = keylen; pkey->type = pkey->save_type = type; pkey->isPriv = 1; + pkey->isRaw = 1; } } @@ -5010,6 +5012,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_CMAC_key(WOLFSSL_ENGINE* e, pkey->type = pkey->save_type = WC_EVP_PKEY_CMAC; pkey->cmacCtx = ctx; pkey->isPriv = 1; + pkey->isRaw = 1; } } else { @@ -9967,6 +9970,7 @@ static int PopulateRSAEvpPkeyDer(WOLFSSL_EVP_PKEY *pkey) else { pkey->pkey_sz = derSz; pkey->isPriv = (rsa->type == RSA_PRIVATE); + pkey->isRaw = 0; return WOLFSSL_SUCCESS; } } @@ -10107,6 +10111,7 @@ int wolfSSL_EVP_PKEY_set1_DSA(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DSA *key) } pkey->pkey_sz = derSz; pkey->isPriv = (dsa->type == DSA_PRIVATE); + pkey->isRaw = 0; XMEMCPY(pkey->pkey.ptr, derBuf, (size_t)derSz); XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER); @@ -10307,6 +10312,7 @@ int wolfSSL_EVP_PKEY_set1_DH(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DH *key) pkey->pkey.ptr = (char*)derBuf; pkey->pkey_sz = (int)derSz; pkey->isPriv = (havePrivate != 0); + pkey->isRaw = 0; return WOLFSSL_SUCCESS; } @@ -10444,6 +10450,7 @@ static int ECC_populate_EVP_PKEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY *key) pkey->pkey.ptr = (char*)derBuf; pkey->pkcs8HeaderSz = key->pkcs8HeaderSz; pkey->isPriv = 1; + pkey->isRaw = 0; return WOLFSSL_SUCCESS; } else { @@ -10494,6 +10501,7 @@ static int ECC_populate_EVP_PKEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY *key) * encoding. */ pkey->pkcs8HeaderSz = 0; pkey->isPriv = 1; + pkey->isRaw = 0; return WOLFSSL_SUCCESS; } else { @@ -10562,6 +10570,7 @@ static int ECC_populate_EVP_PKEY(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_EC_KEY *key) if (derBuf != NULL) { pkey->pkey_sz = (int)derSz; pkey->isPriv = 0; + pkey->isRaw = 0; return WOLFSSL_SUCCESS; } else { @@ -11424,6 +11433,7 @@ int wolfSSL_EVP_PKEY_assign_RSA(WOLFSSL_EVP_PKEY* pkey, WOLFSSL_RSA* key) pkey->pkey_sz = ret; pkey->pkey.ptr = (char*)derBuf; pkey->isPriv = 1; + pkey->isRaw = 0; } else { /* failure - okay to ignore */ XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER); diff --git a/wolfcrypt/src/evp_pk.c b/wolfcrypt/src/evp_pk.c index 01726383d67..85b9262dfff 100644 --- a/wolfcrypt/src/evp_pk.c +++ b/wolfcrypt/src/evp_pk.c @@ -119,6 +119,7 @@ static int d2i_make_pkey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem, /* Set key type passed in and return object. */ pkey->type = type; pkey->isPriv = (priv != 0); + pkey->isRaw = 0; *out = pkey; } if ((ret == 0) && (*out == NULL)) { @@ -581,6 +582,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_public_key(int type, XMEMCPY(pkey->pkey.ptr, pub, len); pkey->pkey_sz = (int)len; pkey->isPriv = 0; + pkey->isRaw = 1; return pkey; } @@ -752,6 +754,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_private_key(int type, XMEMCPY(pkey->pkey.ptr, priv, len); pkey->pkey_sz = (int)len; pkey->isPriv = 1; + pkey->isRaw = 1; return pkey; } @@ -1751,6 +1754,7 @@ static WOLFSSL_EVP_PKEY* d2i_evp_pkey(int type, WOLFSSL_EVP_PKEY** out, local->pkey_sz = (int)inSz; local->pkcs8HeaderSz = pkcs8HeaderSz; local->isPriv = (priv != 0); + local->isRaw = 0; local->pkey.ptr = (char*)XMALLOC((size_t)inSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY); if (local->pkey.ptr == NULL) { @@ -1916,16 +1920,16 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out, return d2i_evp_pkey(type, out, in, inSz, 1); } -/* Deep copy of a key by re-decoding its cached DER. +/* Deep copy of a key by re-decoding its cached DER or raw key. * * @param [in] pkey Key to copy. * @return New WOLFSSL_EVP_PKEY on success. - * @return NULL when pkey is NULL, holds no DER or decoding fails. + * @return NULL when pkey is NULL, holds no key data or decoding fails. */ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey) { WOLFSSL_EVP_PKEY* dup = NULL; - const unsigned char* der; + const unsigned char* data; WOLFSSL_ENTER("wolfSSL_EVP_PKEY_dup"); @@ -1938,15 +1942,25 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey) return NULL; } - der = (const unsigned char*)pkey->pkey.ptr; + data = (const unsigned char*)pkey->pkey.ptr; if (pkey->type == WC_EVP_PKEY_HMAC) { - dup = wolfSSL_EVP_PKEY_new_mac_key(WC_EVP_PKEY_HMAC, NULL, der, + dup = wolfSSL_EVP_PKEY_new_mac_key(WC_EVP_PKEY_HMAC, NULL, data, pkey->pkey_sz); } + else if (pkey->isRaw) { + if (pkey->isPriv) { + dup = wolfSSL_EVP_PKEY_new_raw_private_key(pkey->type, NULL, data, + (size_t)pkey->pkey_sz); + } + else { + dup = wolfSSL_EVP_PKEY_new_raw_public_key(pkey->type, NULL, data, + (size_t)pkey->pkey_sz); + } + } else { /* Every path that caches DER records whether it is private, so the * encoding is decoded the same way it was made. */ - dup = d2i_evp_pkey(pkey->type, NULL, &der, pkey->pkey_sz, + dup = d2i_evp_pkey(pkey->type, NULL, &data, pkey->pkey_sz, pkey->isPriv); if (dup != NULL) { #ifdef HAVE_ECC @@ -2363,6 +2377,7 @@ WOLFSSL_PKCS8_PRIV_KEY_INFO* wolfSSL_d2i_PKCS8_PKEY( XMEMCPY(pkcs8->pkey.ptr, rawDer.buffer, rawDer.length); pkcs8->pkey_sz = (int)rawDer.length; pkcs8->isPriv = 1; + pkcs8->isRaw = 0; } /* Dispose of PKCS#8 DER data - raw DER reference data in pkcs8Der. */ diff --git a/wolfssl/openssl/evp.h b/wolfssl/openssl/evp.h index 7032b6a169f..78bbd5bb901 100644 --- a/wolfssl/openssl/evp.h +++ b/wolfssl/openssl/evp.h @@ -1035,7 +1035,8 @@ WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_raw_private_key(int type, WOLFSSL_API void wolfSSL_EVP_PKEY_free(WOLFSSL_EVP_PKEY* key); WOLFSSL_API int wolfSSL_EVP_PKEY_size(WOLFSSL_EVP_PKEY *pkey); WOLFSSL_API int wolfSSL_EVP_PKEY_copy_parameters(WOLFSSL_EVP_PKEY *to, const WOLFSSL_EVP_PKEY *from); -WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup(const WOLFSSL_EVP_PKEY* pkey); +WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_dup( + const WOLFSSL_EVP_PKEY* pkey); WOLFSSL_API int wolfSSL_EVP_PKEY_missing_parameters(WOLFSSL_EVP_PKEY *pkey); WOLFSSL_API int wolfSSL_EVP_PKEY_cmp(const WOLFSSL_EVP_PKEY *a, const WOLFSSL_EVP_PKEY *b); WOLFSSL_API int wolfSSL_EVP_PKEY_type(int type); diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index eb873c96b7d..90d3fd84759 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -600,7 +600,7 @@ struct WOLFSSL_EVP_PKEY { wolfSSL_Ref ref; union { - char* ptr; /* der format of key */ + char* ptr; /* key data, format given by isRaw and isPriv */ } pkey; #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) #ifndef NO_RSA @@ -652,6 +652,7 @@ struct WOLFSSL_EVP_PKEY { /* option bits */ WC_BITFIELD isPriv:1; /* key holds private material, 0 means public only */ + WC_BITFIELD isRaw:1; /* pkey.ptr holds raw key bytes, 0 means DER */ WC_BITFIELD ownDh:1; /* if struct owns DH and should free it */ WC_BITFIELD ownEcc:1; /* if struct owns ECC and should free it */ WC_BITFIELD ownDsa:1; /* if struct owns DSA and should free it */ From bf4c97d2b78c3c49ae61e8f0f8cf46f3bfd58026 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 5 Nov 2026 17:29:21 +0000 Subject: [PATCH 22/28] Mark Ed448 certificate keys as raw in X509_get_pubkey X509DecodePubKey() now decodes Ed448 keys, and like Ed25519 it caches the raw key bytes from the certificate. Set isRaw so EVP_PKEY_dup() rebuilds the key with the raw constructor instead of decoding it as DER. --- src/x509.c | 2 ++ tests/api/test_evp_pkey.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/x509.c b/src/x509.c index e5ec35f4092..e27ae4ae6fa 100644 --- a/src/x509.c +++ b/src/x509.c @@ -7103,6 +7103,8 @@ static WOLFSSL_EVP_PKEY* X509DecodePubKey(WOLFSSL_X509* x509) (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) else if (x509->pubKeyOID == ED448k) { key->type = WC_EVP_PKEY_ED448; + /* Certificate holds the raw key, not an SPKI. */ + key->isRaw = 1; } #endif #ifdef OPENSSL_EXTRA diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index de2a81e2638..a9a683c895b 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -1195,6 +1195,24 @@ int test_wolfSSL_EVP_PKEY_dup(void) ExpectIntEQ(test_EVP_PKEY_dup_raw_check(EVP_PKEY_ED448, 0, pub, sizeof(pub), pubHdr, sizeof(pubHdr)), TEST_SUCCESS); } + +#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) + /* X509_get_pubkey() caches the raw key from the certificate. */ + { + X509* x509 = NULL; + + ExpectNotNull(x509 = X509_load_certificate_file( + "./certs/ed448/server-ed448.pem", SSL_FILETYPE_PEM)); + ExpectNotNull(key = X509_get_pubkey(x509)); + ExpectIntEQ(key->isRaw, 1); + ExpectIntEQ(key->pkey_sz, ED448_PUB_KEY_SIZE); + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, EVP_PKEY_ED448, 0), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + X509_free(x509); + } +#endif #endif #ifdef HAVE_CURVE25519 { From 1b51d42556efa1596406cdee1d6546ab6b266068 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 5 Nov 2026 17:29:36 +0000 Subject: [PATCH 23/28] Clear isRaw when X509_get_pubkey re-encodes an ML-DSA key X509DecodePubKey() marks ML-DSA certificate keys raw, then replaces the raw key with an SPKI where the encoder is built in. The stale flag made EVP_PKEY_dup() call the raw constructor, which has no ML-DSA support, so it returned NULL. Clear the flag so the SPKI is decoded as DER. --- src/x509.c | 1 + tests/api/test_evp_pkey.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/x509.c b/src/x509.c index e27ae4ae6fa..aecad60f403 100644 --- a/src/x509.c +++ b/src/x509.c @@ -7276,6 +7276,7 @@ static WOLFSSL_EVP_PKEY* X509DecodePubKey(WOLFSSL_X509* x509) XFREE(key->pkey.ptr, x509->heap, DYNAMIC_TYPE_PUBLIC_KEY); key->pkey.ptr = (char*)spki; key->pkey_sz = (int)spkiSz; + key->isRaw = 0; } #endif /* WOLFSSL_HAVE_MLDSA */ } diff --git a/tests/api/test_evp_pkey.c b/tests/api/test_evp_pkey.c index a9a683c895b..b9347e3be1a 100644 --- a/tests/api/test_evp_pkey.c +++ b/tests/api/test_evp_pkey.c @@ -915,8 +915,16 @@ static int test_EVP_PKEY_dup_check(EVP_PKEY** src, int id, int priv) #define TEST_EVP_PKEY_DUP_RAW #endif +/* X509_get_pubkey() re-encodes ML-DSA keys as an SPKI in these builds. */ +#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_HAVE_MLDSA) && \ + defined(WOLFSSL_MLDSA_PUBLIC_KEY) && !defined(WOLFSSL_MLDSA_NO_ASN1) && \ + defined(WC_ENABLE_ASYM_KEY_EXPORT) && !defined(NO_FILESYSTEM) && \ + !defined(NO_CERTS) + #define TEST_EVP_PKEY_DUP_MLDSA_CERT +#endif + #if defined(TEST_EVP_PKEY_DUP_DSA) || defined(TEST_EVP_PKEY_DUP_DH) || \ - defined(TEST_EVP_PKEY_DUP_RAW) + defined(TEST_EVP_PKEY_DUP_RAW) || defined(TEST_EVP_PKEY_DUP_MLDSA_CERT) /* Check dup carries the same cached key data in the same format and the same * private/public state, and that it left the error queue clean. */ static int test_EVP_PKEY_dup_data_check(EVP_PKEY* src, int id, int priv) @@ -941,7 +949,7 @@ static int test_EVP_PKEY_dup_data_check(EVP_PKEY* src, int id, int priv) return EXPECT_RESULT(); } #endif /* TEST_EVP_PKEY_DUP_DSA || TEST_EVP_PKEY_DUP_DH || - * TEST_EVP_PKEY_DUP_RAW */ + * TEST_EVP_PKEY_DUP_RAW || TEST_EVP_PKEY_DUP_MLDSA_CERT */ #ifdef TEST_EVP_PKEY_DUP_RAW /* Dup a raw key and, when hdr is given, the same key wrapped in DER. Both @@ -1264,6 +1272,22 @@ int test_wolfSSL_EVP_PKEY_dup(void) sizeof(pub), NULL, 0), TEST_SUCCESS); } #endif +#ifdef TEST_EVP_PKEY_DUP_MLDSA_CERT + /* X509_get_pubkey() replaces the raw ML-DSA key with an SPKI. */ + { + X509* x509 = NULL; + + ExpectNotNull(x509 = X509_load_certificate_file( + "./certs/mldsa/mldsa44-cert.pem", SSL_FILETYPE_PEM)); + ExpectNotNull(key = X509_get_pubkey(x509)); + ExpectIntEQ(key->isRaw, 0); + ExpectIntEQ(test_EVP_PKEY_dup_data_check(key, EVP_PKEY_DILITHIUM, 0), + TEST_SUCCESS); + EVP_PKEY_free(key); + key = NULL; + X509_free(x509); + } +#endif #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \ defined(WOLFSSL_KEY_TO_DER) From 91f289aa9ee865b0e7220231ceb7d3c8299440d0 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 16 Nov 2026 05:22:13 +0000 Subject: [PATCH 24/28] Address review: free X509_get_ext_d2i AIA stacks by stack type X509_get_ext_d2i(NID_info_access) returns a stack of ASN1_OBJECTs that borrow x509->authInfo. AUTHORITY_INFO_ACCESS_free freed them as ACCESS_DESCRIPTIONs, double freeing authInfo. Free the entries by stack type instead. Map ACCESS_DESCRIPTION stacks to their free function in OPENSSL_EXTRA builds too, as they are created there. --- src/ssl_sk.c | 2 +- src/x509.c | 5 ++--- tests/api/test_ossl_x509_ext.c | 5 +++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ssl_sk.c b/src/ssl_sk.c index df177a9b160..01eed03c001 100644 --- a/src/ssl_sk.c +++ b/src/ssl_sk.c @@ -851,7 +851,7 @@ static wolfSSL_sk_freefunc wolfssl_sk_get_free_func(WOLF_STACK_TYPE type) switch(type) { case STACK_TYPE_ACCESS_DESCRIPTION: - #if defined(OPENSSL_ALL) + #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) func = (wolfSSL_sk_freefunc)wolfSSL_ACCESS_DESCRIPTION_free; #endif break; diff --git a/src/x509.c b/src/x509.c index aecad60f403..3b1f4c2c069 100644 --- a/src/x509.c +++ b/src/x509.c @@ -5521,13 +5521,12 @@ void wolfSSL_sk_ACCESS_DESCRIPTION_free(WOLFSSL_STACK* sk) } -/* AUTHORITY_INFO_ACCESS object is a stack of ACCESS_DESCRIPTION objects. - * Free the entries and the stack, as OpenSSL does. */ +/* Free the entries by stack type and the stack. */ void wolfSSL_AUTHORITY_INFO_ACCESS_free( WOLF_STACK_OF(WOLFSSL_ACCESS_DESCRIPTION)* sk) { WOLFSSL_ENTER("wolfSSL_AUTHORITY_INFO_ACCESS_free"); - wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(sk, wolfSSL_ACCESS_DESCRIPTION_free); + wolfSSL_sk_pop_free(sk, NULL); } void wolfSSL_AUTHORITY_INFO_ACCESS_pop_free( diff --git a/tests/api/test_ossl_x509_ext.c b/tests/api/test_ossl_x509_ext.c index 472c687622a..9f53b2ae097 100644 --- a/tests/api/test_ossl_x509_ext.c +++ b/tests/api/test_ossl_x509_ext.c @@ -1998,6 +1998,11 @@ int test_wolfSSL_X509V3_EXT(void) wolfSSL_sk_ACCESS_DESCRIPTION_pop_free( (WOLFSSL_AUTHORITY_INFO_ACCESS*)ext2, NULL); ext2 = NULL; + /* Stack of ASN1_OBJECTs borrowing x509 data: must not free that data. */ + ExpectNotNull(ext2 = (WOLFSSL_X509_EXTENSION*)X509_get_ext_d2i(x509, + NID_info_access, NULL, NULL)); + wolfSSL_AUTHORITY_INFO_ACCESS_free((WOLFSSL_AUTHORITY_INFO_ACCESS*)ext2); + ext2 = NULL; #if defined(WOLFSSL_QT) ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */ #else From b9b1bf580120b8dcc27c8de678b41dc0f5d2a8d9 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 16 Nov 2026 05:22:13 +0000 Subject: [PATCH 25/28] Address review: reject a DH without generator in EVP_PKEY_set1_DH SetDhInternal now accepts a missing g, so set1_DH would encode g=0. --- tests/api/test_ossl_dh.c | 11 +++++++++++ wolfcrypt/src/evp.c | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/api/test_ossl_dh.c b/tests/api/test_ossl_dh.c index c6d47da62d2..ce9ef61c8f3 100644 --- a/tests/api/test_ossl_dh.c +++ b/tests/api/test_ossl_dh.c @@ -103,6 +103,17 @@ int test_wolfSSL_DH(void) /* Encoding and duplicating parameters still need g. */ ExpectIntEQ(i2d_DHparams(dh3, NULL), 0); ExpectNull(DHparams_dup(dh3)); + #if (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \ + defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \ + !defined(NO_FILESYSTEM) + { + EVP_PKEY* pkey = NULL; + + ExpectNotNull(pkey = EVP_PKEY_new()); + ExpectIntEQ(EVP_PKEY_set1_DH(pkey, dh3), WOLFSSL_FAILURE); + EVP_PKEY_free(pkey); + } + #endif /* Adding g afterwards must give a usable key pair. */ if (dh3 != NULL) { ExpectNotNull(dh3->g = BN_dup(dh->g)); diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index fdcb9bc9f0c..9c85046504d 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -10248,7 +10248,8 @@ int wolfSSL_EVP_PKEY_set1_DH(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DH *key) WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_DH"); - if (pkey == NULL || key == NULL) + /* DER encoding needs a generator. */ + if (pkey == NULL || key == NULL || key->g == NULL) return WOLFSSL_FAILURE; clearEVPPkeyKeys(pkey); From c137d978cc039ab09199488548279bdaec1fb447 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 16 Nov 2026 05:22:13 +0000 Subject: [PATCH 26/28] Address review: keep the sign in BN_mask_bits Mask the magnitude as OpenSSL does. mp_mod_2d made negatives positive. --- src/ssl_bn.c | 25 +++++++++++++++++++------ tests/api/test_ossl_bn.c | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/ssl_bn.c b/src/ssl_bn.c index 9ae687d011f..5c8ad4cb497 100644 --- a/src/ssl_bn.c +++ b/src/ssl_bn.c @@ -881,7 +881,7 @@ int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn) } #ifdef WOLFSSL_HAVE_MP_MOD_2D -/* Keep only the lowest n bits. bn = bn mod 2^n +/* Keep only the lowest n bits of the magnitude. Sign is kept, as in OpenSSL. * * Return compliant with OpenSSL. * @@ -895,6 +895,7 @@ int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn) int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n) { int ret = 1; + int neg = 0; WOLFSSL_ENTER("wolfSSL_BN_mask_bits"); @@ -904,11 +905,23 @@ int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n) ret = 0; } - /* Use wolfCrypt perform operation. */ - if ((ret == 1) && (mp_mod_2d((mp_int*)bn->internal, n, - (mp_int*)bn->internal) != MP_OKAY)) { - WOLFSSL_MSG("mp_mod_2d error"); - ret = 0; + if (ret == 1) { + /* mp_mod_2d() of a negative gives a positive residue. */ + neg = mp_isneg((mp_int*)bn->internal); + if (neg && (mp_abs((mp_int*)bn->internal, (mp_int*)bn->internal) != + MP_OKAY)) { + WOLFSSL_MSG("mp_abs error"); + ret = 0; + } + /* Use wolfCrypt perform operation. */ + if ((ret == 1) && (mp_mod_2d((mp_int*)bn->internal, n, + (mp_int*)bn->internal) != MP_OKAY)) { + WOLFSSL_MSG("mp_mod_2d error"); + ret = 0; + } + if (neg && !mp_iszero((mp_int*)bn->internal)) { + mp_setneg((mp_int*)bn->internal); + } } return ret; diff --git a/tests/api/test_ossl_bn.c b/tests/api/test_ossl_bn.c index f46ec787530..4e2d345ab6b 100644 --- a/tests/api/test_ossl_bn.c +++ b/tests/api/test_ossl_bn.c @@ -552,6 +552,30 @@ int test_wolfSSL_BN_bits(void) ExpectIntEQ(BN_get_word(a), 0xf); ExpectIntEQ(BN_mask_bits(a, 0), 1); ExpectIntEQ(BN_is_zero(a), 1); +#if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \ + defined(WOLFSSL_SP_INT_NEGATIVE)) + /* Negative: magnitude is masked and sign kept. -31 -> -15. */ + { + BIGNUM* b = NULL; + + ExpectNotNull(b = BN_new()); + ExpectIntEQ(BN_set_word(b, 31), 1); + ExpectIntEQ(BN_set_word(a, 0), 1); + ExpectIntEQ(BN_sub(a, a, b), 1); + ExpectIntEQ(BN_is_negative(a), 1); + ExpectIntEQ(BN_mask_bits(a, 4), 1); + ExpectIntEQ(BN_is_negative(a), 1); + ExpectIntEQ(BN_get_word(a), 15); + /* Zero result is not negative. */ + ExpectIntEQ(BN_set_word(b, 16), 1); + ExpectIntEQ(BN_set_word(a, 0), 1); + ExpectIntEQ(BN_sub(a, a, b), 1); + ExpectIntEQ(BN_mask_bits(a, 4), 1); + ExpectIntEQ(BN_is_zero(a), 1); + ExpectIntEQ(BN_is_negative(a), 0); + BN_free(b); + } +#endif #endif BN_free(a); From dc77e4ac1523837bbe54fc4236cc97b385704c5b Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 26 Nov 2026 05:20:38 +0000 Subject: [PATCH 27/28] Fix CI: store basicConstraints string pathlen as value bytes Master now keeps ASN1_INTEGER data as the value magnitude, so the pathlen parsed from a basicConstraints string must be set as a value rather than in the length field. --- src/x509.c | 3 +-- tests/api/test_ossl_x509_ext.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/x509.c b/src/x509.c index 3b1f4c2c069..32452f6f84f 100644 --- a/src/x509.c +++ b/src/x509.c @@ -3552,10 +3552,9 @@ static int wolfssl_ext_bc_from_str(WOLFSSL_X509_EXTENSION* ext, ext->obj->ca = isCa; if (pathLenSet) { - ext->obj->pathlen = wolfSSL_ASN1_INTEGER_new(); + ext->obj->pathlen = x509_asn1_integer_new_word32((word32)pathLen); if (ext->obj->pathlen == NULL) return WOLFSSL_FAILURE; - ext->obj->pathlen->length = pathLen; } return WOLFSSL_SUCCESS; diff --git a/tests/api/test_ossl_x509_ext.c b/tests/api/test_ossl_x509_ext.c index 9f53b2ae097..a6e56c6cec5 100644 --- a/tests/api/test_ossl_x509_ext.c +++ b/tests/api/test_ossl_x509_ext.c @@ -1291,7 +1291,7 @@ int test_wolfSSL_X509V3_EXT_nconf(void) ExpectNotNull(ext->obj); ExpectIntEQ(ext->obj->ca, 1); ExpectNotNull(ext->obj->pathlen); - ExpectIntEQ(ext->obj->pathlen->length, 2); + ExpectIntEQ(wolfSSL_ASN1_INTEGER_get(ext->obj->pathlen), 2); ExpectIntEQ(ext->crit, 1); } ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS); From 9a3e7573d23ec21b36b5189c47f437d03adc846c Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 4 Dec 2026 05:11:05 +0000 Subject: [PATCH 28/28] Fix CI: raise tcpdump_check timeout to fit apt setup check-workflows.py requires room for the 750s apt budget. --- .github/workflows/tcpdump.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tcpdump.yml b/.github/workflows/tcpdump.yml index accbbfcb09c..e5086951c82 100644 --- a/.github/workflows/tcpdump.yml +++ b/.github/workflows/tcpdump.yml @@ -53,7 +53,7 @@ jobs: if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }} runs-on: ubuntu-24.04 # This should be a safe limit for the tests to run. - timeout-minutes: 10 + timeout-minutes: 15 needs: build_wolfssl steps: - name: Checkout wolfSSL CI actions