|
25 | 25 | import org.junit.BeforeClass; |
26 | 26 | import static org.junit.Assert.*; |
27 | 27 |
|
| 28 | +import java.nio.charset.StandardCharsets; |
28 | 29 | import java.util.Arrays; |
29 | 30 | import java.util.List; |
30 | 31 |
|
@@ -56,6 +57,7 @@ public void testWolfSSL() throws WolfSSLException { |
56 | 57 | test_WolfSSL_Method_Allocators(lib); |
57 | 58 | test_WolfSSL_getLibVersionHex(); |
58 | 59 | test_WolfSSL_getErrno(); |
| 60 | + test_WolfSSL_getSNIFromBuffer(); |
59 | 61 | testGetCiphersAvailableIana(); |
60 | 62 | test_isLibraryLoadSkippedReturnsFalseByDefault(); |
61 | 63 | test_SystemPropertyNotSetByDefault(); |
@@ -236,6 +238,143 @@ public void test_WolfSSL_getErrno() { |
236 | 238 | System.out.println("\t\t\t... passed"); |
237 | 239 | } |
238 | 240 |
|
| 241 | + public void test_WolfSSL_getSNIFromBuffer() throws WolfSSLException { |
| 242 | + System.out.print("\tgetSNIFromBuffer()"); |
| 243 | + |
| 244 | + /* Minimal TLS 1.2 ClientHello with SNI extension for "www.example.com". |
| 245 | + * This is a hand crafted minimal valid ClientHello message. */ |
| 246 | + String hostname = "www.example.com"; |
| 247 | + byte[] hostBytes = hostname.getBytes(StandardCharsets.UTF_8); |
| 248 | + int hostLen = hostBytes.length; |
| 249 | + |
| 250 | + /* SNI extension: type(2) + len(2) + sni_list_len(2) + sni_type(1) + |
| 251 | + * sni_len(2) + sni_data */ |
| 252 | + int sniExtLen = 2 + 2 + 2 + 1 + 2 + hostLen; |
| 253 | + /* Extensions block: ext_len(2) + sni_ext */ |
| 254 | + int extBlockLen = 2 + sniExtLen; |
| 255 | + |
| 256 | + /* ClientHello body: |
| 257 | + * version(2) + random(32) + sessId_len(1) + cipher_suites_len(2) + |
| 258 | + * one_suite(2) + comp_len(1) + comp_null(1) + extensions */ |
| 259 | + int chBodyLen = 2 + 32 + 1 + 2 + 2 + 1 + 1 + extBlockLen; |
| 260 | + |
| 261 | + /* Handshake header: type(1) + length(3) */ |
| 262 | + int hsLen = 1 + 3 + chBodyLen; |
| 263 | + |
| 264 | + /* TLS record: type(1) + version(2) + length(2) */ |
| 265 | + int totalLen = 1 + 2 + 2 + hsLen; |
| 266 | + |
| 267 | + byte[] clientHello = new byte[totalLen]; |
| 268 | + int offset = 0; |
| 269 | + |
| 270 | + /* TLS record header */ |
| 271 | + clientHello[offset++] = 0x16; /* handshake */ |
| 272 | + clientHello[offset++] = 0x03; /* TLS 1.0 */ |
| 273 | + clientHello[offset++] = 0x01; |
| 274 | + clientHello[offset++] = (byte)((hsLen >> 8) & 0xFF); |
| 275 | + clientHello[offset++] = (byte)(hsLen & 0xFF); |
| 276 | + |
| 277 | + /* Handshake header */ |
| 278 | + clientHello[offset++] = 0x01; /* client_hello */ |
| 279 | + clientHello[offset++] = 0x00; /* length (3B) */ |
| 280 | + clientHello[offset++] = (byte)((chBodyLen >> 8) & 0xFF); |
| 281 | + clientHello[offset++] = (byte)(chBodyLen & 0xFF); |
| 282 | + |
| 283 | + /* ClientHello body */ |
| 284 | + clientHello[offset++] = 0x03; /* TLS 1.2 */ |
| 285 | + clientHello[offset++] = 0x03; |
| 286 | + |
| 287 | + /* 32 bytes random (zeros for test) */ |
| 288 | + offset += 32; |
| 289 | + |
| 290 | + /* Session ID length = 0 */ |
| 291 | + clientHello[offset++] = 0x00; |
| 292 | + |
| 293 | + /* Cipher suites: length=2, one suite */ |
| 294 | + clientHello[offset++] = 0x00; |
| 295 | + clientHello[offset++] = 0x02; |
| 296 | + clientHello[offset++] = (byte)0xC0; |
| 297 | + clientHello[offset++] = 0x2F; |
| 298 | + |
| 299 | + /* Compression: length=1, null */ |
| 300 | + clientHello[offset++] = 0x01; |
| 301 | + clientHello[offset++] = 0x00; |
| 302 | + |
| 303 | + /* Extensions length */ |
| 304 | + int extTotalLen = sniExtLen; |
| 305 | + clientHello[offset++] = (byte)((extTotalLen >> 8) & 0xFF); |
| 306 | + clientHello[offset++] = (byte)(extTotalLen & 0xFF); |
| 307 | + |
| 308 | + /* SNI extension type = 0x0000 */ |
| 309 | + clientHello[offset++] = 0x00; |
| 310 | + clientHello[offset++] = 0x00; |
| 311 | + |
| 312 | + /* SNI extension data length */ |
| 313 | + int sniDataLen = 2 + 1 + 2 + hostLen; |
| 314 | + clientHello[offset++] = (byte)((sniDataLen >> 8) & 0xFF); |
| 315 | + clientHello[offset++] = (byte)(sniDataLen & 0xFF); |
| 316 | + |
| 317 | + /* SNI list length */ |
| 318 | + int sniListLen = 1 + 2 + hostLen; |
| 319 | + clientHello[offset++] = (byte)((sniListLen >> 8) & 0xFF); |
| 320 | + clientHello[offset++] = (byte)(sniListLen & 0xFF); |
| 321 | + |
| 322 | + /* SNI type: host_name = 0 */ |
| 323 | + clientHello[offset++] = 0x00; |
| 324 | + |
| 325 | + /* SNI host name length */ |
| 326 | + clientHello[offset++] = (byte)((hostLen >> 8) & 0xFF); |
| 327 | + clientHello[offset++] = (byte)(hostLen & 0xFF); |
| 328 | + |
| 329 | + /* SNI host name data */ |
| 330 | + System.arraycopy(hostBytes, 0, clientHello, offset, hostLen); |
| 331 | + offset += hostLen; |
| 332 | + |
| 333 | + byte[] sniOut = new byte[256]; |
| 334 | + |
| 335 | + int ret = WolfSSL.getSNIFromBuffer(clientHello, |
| 336 | + (byte)WolfSSL.WOLFSSL_SNI_HOST_NAME, sniOut); |
| 337 | + |
| 338 | + if (ret == WolfSSL.NOT_COMPILED_IN) { |
| 339 | + System.out.println("\t\t... skipped"); |
| 340 | + return; |
| 341 | + } |
| 342 | + |
| 343 | + if (ret <= 0) { |
| 344 | + System.out.println("\t\t... failed"); |
| 345 | + fail("getSNIFromBuffer() returned: " + ret); |
| 346 | + } |
| 347 | + |
| 348 | + String extracted = new String(sniOut, 0, ret, StandardCharsets.UTF_8); |
| 349 | + if (!hostname.equals(extracted)) { |
| 350 | + System.out.println("\t\t... failed"); |
| 351 | + fail("getSNIFromBuffer() expected [" + hostname + "] got [" + |
| 352 | + extracted + "]"); |
| 353 | + } |
| 354 | + |
| 355 | + /* Test null clientHello throws exception */ |
| 356 | + try { |
| 357 | + WolfSSL.getSNIFromBuffer(null, (byte)WolfSSL.WOLFSSL_SNI_HOST_NAME, |
| 358 | + sniOut); |
| 359 | + System.out.println("\t\t... failed"); |
| 360 | + fail("Expected IllegalArgumentException for null clientHello"); |
| 361 | + } catch (IllegalArgumentException e) { |
| 362 | + /* expected */ |
| 363 | + } |
| 364 | + |
| 365 | + /* Test null sni output buffer throws exception */ |
| 366 | + try { |
| 367 | + WolfSSL.getSNIFromBuffer(clientHello, |
| 368 | + (byte)WolfSSL.WOLFSSL_SNI_HOST_NAME, null); |
| 369 | + System.out.println("\t\t... failed"); |
| 370 | + fail("Expected IllegalArgumentException for null sni"); |
| 371 | + } catch (IllegalArgumentException e) { |
| 372 | + /* expected */ |
| 373 | + } |
| 374 | + |
| 375 | + System.out.println("\t\t... passed"); |
| 376 | + } |
| 377 | + |
239 | 378 | public void test_isLibraryLoadSkippedReturnsFalseByDefault() { |
240 | 379 |
|
241 | 380 | System.out.print( |
|
0 commit comments