|
21 | 21 |
|
22 | 22 | #include <tests/unit.h> |
23 | 23 |
|
| 24 | +#include <limits.h> |
| 25 | + |
24 | 26 | #ifdef NO_INLINE |
25 | 27 | #include <wolfssl/wolfcrypt/misc.h> |
26 | 28 | #else |
@@ -72,6 +74,67 @@ int test_wolfSSL_i2d_X509(void) |
72 | 74 | return EXPECT_RESULT(); |
73 | 75 | } |
74 | 76 |
|
| 77 | +int test_wolfSSL_X509_get_der_length_guards(void) |
| 78 | +{ |
| 79 | + EXPECT_DECLS; |
| 80 | +#if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) |
| 81 | + const unsigned char* cert_buf = server_cert_der_2048; |
| 82 | + X509* cert = NULL; |
| 83 | + int derSz = 0; |
| 84 | + word32 origLen = 0; |
| 85 | + |
| 86 | + ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048)); |
| 87 | + ExpectNotNull(cert); |
| 88 | + ExpectNotNull(cert->derCert); |
| 89 | + |
| 90 | + if (EXPECT_SUCCESS()) { |
| 91 | + origLen = cert->derCert->length; |
| 92 | + cert->derCert->length = ((word32)INT_MAX) + 1U; |
| 93 | + ExpectNull(wolfSSL_X509_get_der(cert, &derSz)); |
| 94 | + cert->derCert->length = origLen; |
| 95 | + } |
| 96 | + |
| 97 | + X509_free(cert); |
| 98 | +#endif |
| 99 | + return EXPECT_RESULT(); |
| 100 | +} |
| 101 | + |
| 102 | +int test_wolfSSL_i2d_X509_der_length_guards(void) |
| 103 | +{ |
| 104 | + EXPECT_DECLS; |
| 105 | +#if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) |
| 106 | + const unsigned char* cert_buf = server_cert_der_2048; |
| 107 | + unsigned char buf[4] = { 0x11, 0x22, 0x33, 0x44 }; |
| 108 | + const unsigned char origBuf[4] = { 0x11, 0x22, 0x33, 0x44 }; |
| 109 | + unsigned char* callerOut = buf; |
| 110 | + X509* cert = NULL; |
| 111 | + word32 origLen = 0; |
| 112 | + |
| 113 | + ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048)); |
| 114 | + ExpectNotNull(cert); |
| 115 | + ExpectNotNull(cert->derCert); |
| 116 | + |
| 117 | + if (EXPECT_SUCCESS()) { |
| 118 | + origLen = cert->derCert->length; |
| 119 | + |
| 120 | + cert->derCert->length = ((word32)INT_MAX) + 1U; |
| 121 | + ExpectIntEQ(i2d_X509(cert, &callerOut), MEMORY_E); |
| 122 | + ExpectPtrEq(callerOut, buf); |
| 123 | + ExpectIntEQ(XMEMCMP(buf, origBuf, sizeof(buf)), 0); |
| 124 | + |
| 125 | + cert->derCert->length = 0; |
| 126 | + ExpectIntEQ(i2d_X509(cert, &callerOut), MEMORY_E); |
| 127 | + ExpectPtrEq(callerOut, buf); |
| 128 | + ExpectIntEQ(XMEMCMP(buf, origBuf, sizeof(buf)), 0); |
| 129 | + |
| 130 | + cert->derCert->length = origLen; |
| 131 | + } |
| 132 | + |
| 133 | + X509_free(cert); |
| 134 | +#endif |
| 135 | + return EXPECT_RESULT(); |
| 136 | +} |
| 137 | + |
75 | 138 | int test_wolfSSL_PEM_read_X509(void) |
76 | 139 | { |
77 | 140 | EXPECT_DECLS; |
@@ -244,4 +307,3 @@ int test_wolfSSL_PEM_write_bio_X509(void) |
244 | 307 | #endif |
245 | 308 | return EXPECT_RESULT(); |
246 | 309 | } |
247 | | - |
|
0 commit comments