|
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,116 @@ 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 | + const byte* der = NULL; |
| 83 | + X509* cert = NULL; |
| 84 | + const byte* origBuf = NULL; |
| 85 | + word32 origLen = 0; |
| 86 | + int derSz = 0; |
| 87 | + |
| 88 | + ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048)); |
| 89 | + ExpectNotNull(cert); |
| 90 | + ExpectNotNull(cert->derCert); |
| 91 | + ExpectNotNull(cert->derCert->buffer); |
| 92 | + |
| 93 | + if (EXPECT_SUCCESS()) { |
| 94 | + origLen = cert->derCert->length; |
| 95 | + origBuf = cert->derCert->buffer; |
| 96 | + cert->derCert->length = (word32)INT_MAX; |
| 97 | + der = wolfSSL_X509_get_der(cert, &derSz); |
| 98 | + cert->derCert->length = origLen; |
| 99 | + ExpectPtrEq(der, origBuf); |
| 100 | + ExpectIntEQ(derSz, INT_MAX); |
| 101 | + |
| 102 | + cert->derCert->length = ((word32)INT_MAX) + 1U; |
| 103 | + der = wolfSSL_X509_get_der(cert, &derSz); |
| 104 | + cert->derCert->length = origLen; |
| 105 | + ExpectNull(der); |
| 106 | + |
| 107 | + cert->derCert->length = 0; |
| 108 | + der = wolfSSL_X509_get_der(cert, &derSz); |
| 109 | + cert->derCert->length = origLen; |
| 110 | + ExpectPtrEq(der, origBuf); |
| 111 | + ExpectIntEQ(derSz, 0); |
| 112 | + |
| 113 | + ExpectPtrEq(wolfSSL_X509_get_der(cert, &derSz), origBuf); |
| 114 | + ExpectIntGT(derSz, 0); |
| 115 | + } |
| 116 | + |
| 117 | + X509_free(cert); |
| 118 | +#endif |
| 119 | + return EXPECT_RESULT(); |
| 120 | +} |
| 121 | + |
| 122 | +int test_wolfSSL_i2d_X509_der_length_guards(void) |
| 123 | +{ |
| 124 | + EXPECT_DECLS; |
| 125 | +#if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) |
| 126 | + const unsigned char* cert_buf = server_cert_der_2048; |
| 127 | + unsigned char* overflowOut = NULL; |
| 128 | + unsigned char overflowBuf[4] = { 0x11, 0x22, 0x33, 0x44 }; |
| 129 | + const unsigned char overflowExpected[4] = { 0x11, 0x22, 0x33, 0x44 }; |
| 130 | + unsigned char* overflowCallerOut = overflowBuf; |
| 131 | + unsigned char* zeroLenOut = NULL; |
| 132 | + unsigned char zeroLenBuf[4] = { 0x55, 0x66, 0x77, 0x88 }; |
| 133 | + const unsigned char zeroLenExpected[4] = { 0x55, 0x66, 0x77, 0x88 }; |
| 134 | + unsigned char* zeroLenCallerOut = zeroLenBuf; |
| 135 | + unsigned char* successOut = NULL; |
| 136 | + X509* cert = NULL; |
| 137 | + word32 origLen = 0; |
| 138 | + int ret = 0; |
| 139 | + |
| 140 | + ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048)); |
| 141 | + ExpectNotNull(cert); |
| 142 | + ExpectNotNull(cert->derCert); |
| 143 | + ExpectNotNull(cert->derCert->buffer); |
| 144 | + |
| 145 | + if (EXPECT_SUCCESS()) { |
| 146 | + origLen = cert->derCert->length; |
| 147 | + cert->derCert->length = ((word32)INT_MAX) + 1U; |
| 148 | + ret = i2d_X509(cert, &overflowOut); |
| 149 | + cert->derCert->length = origLen; |
| 150 | + ExpectIntEQ(ret, MEMORY_E); |
| 151 | + ExpectNull(overflowOut); |
| 152 | + |
| 153 | + cert->derCert->length = ((word32)INT_MAX) + 1U; |
| 154 | + ret = i2d_X509(cert, &overflowCallerOut); |
| 155 | + cert->derCert->length = origLen; |
| 156 | + ExpectIntEQ(ret, MEMORY_E); |
| 157 | + ExpectPtrEq(overflowCallerOut, overflowBuf); |
| 158 | + ExpectIntEQ(XMEMCMP(overflowBuf, overflowExpected, |
| 159 | + sizeof(overflowBuf)), 0); |
| 160 | + |
| 161 | + cert->derCert->length = 0; |
| 162 | + ret = i2d_X509(cert, &zeroLenOut); |
| 163 | + cert->derCert->length = origLen; |
| 164 | + ExpectIntEQ(ret, MEMORY_E); |
| 165 | + ExpectNull(zeroLenOut); |
| 166 | + |
| 167 | + cert->derCert->length = 0; |
| 168 | + ret = i2d_X509(cert, &zeroLenCallerOut); |
| 169 | + cert->derCert->length = origLen; |
| 170 | + ExpectIntEQ(ret, MEMORY_E); |
| 171 | + ExpectPtrEq(zeroLenCallerOut, zeroLenBuf); |
| 172 | + ExpectIntEQ(XMEMCMP(zeroLenBuf, zeroLenExpected, |
| 173 | + sizeof(zeroLenBuf)), 0); |
| 174 | + |
| 175 | + ExpectIntGT(i2d_X509(cert, &successOut), 0); |
| 176 | + ExpectNotNull(successOut); |
| 177 | + } |
| 178 | + |
| 179 | + XFREE(overflowOut, NULL, DYNAMIC_TYPE_OPENSSL); |
| 180 | + XFREE(zeroLenOut, NULL, DYNAMIC_TYPE_OPENSSL); |
| 181 | + XFREE(successOut, NULL, DYNAMIC_TYPE_OPENSSL); |
| 182 | + X509_free(cert); |
| 183 | +#endif |
| 184 | + return EXPECT_RESULT(); |
| 185 | +} |
| 186 | + |
75 | 187 | int test_wolfSSL_PEM_read_X509(void) |
76 | 188 | { |
77 | 189 | EXPECT_DECLS; |
@@ -244,4 +356,3 @@ int test_wolfSSL_PEM_write_bio_X509(void) |
244 | 356 | #endif |
245 | 357 | return EXPECT_RESULT(); |
246 | 358 | } |
247 | | - |
|
0 commit comments