Skip to content

Commit 354691d

Browse files
committed
Copy pathlen in ASN1_OBJECT_dup() and set pathLengthSet in X509_add_ext() when adding basic constraints with a path length
1 parent 3540d89 commit 354691d

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/ssl_asn1.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,9 +2026,17 @@ WOLFSSL_ASN1_OBJECT* wolfSSL_ASN1_OBJECT_dup(WOLFSSL_ASN1_OBJECT* obj)
20262026
dupl->objSz = obj->objSz;
20272027
#ifdef OPENSSL_EXTRA
20282028
dupl->ca = obj->ca;
2029+
if (obj->pathlen != NULL) {
2030+
dupl->pathlen = wolfSSL_ASN1_INTEGER_dup(obj->pathlen);
2031+
if (dupl->pathlen == NULL) {
2032+
WOLFSSL_MSG("ASN1 pathlen alloc error");
2033+
wolfSSL_ASN1_OBJECT_free(dupl);
2034+
dupl = NULL;
2035+
}
2036+
}
20292037
#endif
20302038
/* Check for encoding. */
2031-
if (obj->obj) {
2039+
if (dupl != NULL && obj->obj) {
20322040
/* Allocate memory for ASN.1 OBJECT_ID DER encoding. */
20332041
dupl->obj = (const unsigned char*)XMALLOC(obj->objSz, NULL,
20342042
DYNAMIC_TYPE_ASN1);

src/x509.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ int wolfSSL_X509_add_ext(WOLFSSL_X509 *x509, WOLFSSL_X509_EXTENSION *ext,
13571357
if (ext->obj->pathlen) {
13581358
x509->pathLength = (word32)ext->obj->pathlen->length;
13591359
x509->basicConstPlSet = 1;
1360+
x509->pathLengthSet = 1;
13601361
}
13611362
x509->basicConstSet = 1;
13621363
}

tests/api/test_ossl_asn1.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,37 @@ int test_wolfSSL_ASN1_OBJECT(void)
870870
s.objSz = sizeof(der);
871871
ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
872872
ASN1_OBJECT_free(a);
873+
a = NULL;
874+
ASN1_OBJECT_free(&s);
875+
876+
/* Test dup copies pathlen when set */
877+
XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
878+
s.type = NID_basic_constraints;
879+
s.ca = 1;
880+
s.pathlen = wolfSSL_ASN1_INTEGER_new();
881+
ExpectNotNull(s.pathlen);
882+
if (s.pathlen != NULL) {
883+
s.pathlen->length = 5;
884+
}
885+
ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
886+
if (a != NULL) {
887+
ExpectIntEQ(a->ca, 1);
888+
ExpectNotNull(a->pathlen);
889+
if (a->pathlen != NULL) {
890+
ExpectIntEQ(a->pathlen->length, 5);
891+
}
892+
}
893+
ASN1_OBJECT_free(a);
894+
a = NULL;
895+
896+
/* Test dup with NULL pathlen leaves it NULL */
897+
wolfSSL_ASN1_INTEGER_free(s.pathlen);
898+
s.pathlen = NULL;
899+
ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
900+
if (a != NULL) {
901+
ExpectNull(a->pathlen);
902+
}
903+
ASN1_OBJECT_free(a);
873904
ASN1_OBJECT_free(&s);
874905
#endif /* OPENSSL_EXTRA */
875906
return EXPECT_RESULT();

tests/api/test_ossl_x509_ext.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ static int test_X509_add_basic_constraints(WOLFSSL_X509* x509)
282282
ExpectIntEQ(wolfSSL_X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
283283
ExpectIntEQ(x509->isCa, 0);
284284
ExpectIntEQ(x509->pathLength, 2);
285+
ExpectIntEQ(x509->pathLengthSet, 1);
285286
if (ext != NULL && ext->obj != NULL) {
286287
/* Add second time to without path length. */
287288
ext->obj->ca = 1;
@@ -290,6 +291,7 @@ static int test_X509_add_basic_constraints(WOLFSSL_X509* x509)
290291
ExpectIntEQ(wolfSSL_X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
291292
ExpectIntEQ(x509->isCa, 1);
292293
ExpectIntEQ(x509->pathLength, 2);
294+
ExpectIntEQ(x509->pathLengthSet, 1);
293295
ExpectIntEQ(wolfSSL_X509_get_isSet_pathLength(NULL), 0);
294296
ExpectIntEQ(wolfSSL_X509_get_isSet_pathLength(x509), 1);
295297
ExpectIntEQ(wolfSSL_X509_get_pathLength(NULL), 0);

0 commit comments

Comments
 (0)