1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <openssl/crypto.h> 11e1051a39Sopenharmony_ci#include <string.h> 12e1051a39Sopenharmony_ci 13e1051a39Sopenharmony_ci#include "crypto/punycode.h" 14e1051a39Sopenharmony_ci#include "internal/nelem.h" 15e1051a39Sopenharmony_ci#include "testutil.h" 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_cistatic const struct puny_test { 19e1051a39Sopenharmony_ci unsigned int raw[50]; 20e1051a39Sopenharmony_ci const char *encoded; 21e1051a39Sopenharmony_ci} puny_cases[] = { 22e1051a39Sopenharmony_ci /* Test cases from RFC 3492 */ 23e1051a39Sopenharmony_ci { /* Arabic (Egyptian) */ 24e1051a39Sopenharmony_ci { 0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644, 25e1051a39Sopenharmony_ci 0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F 26e1051a39Sopenharmony_ci }, 27e1051a39Sopenharmony_ci "egbpdaj6bu4bxfgehfvwxn" 28e1051a39Sopenharmony_ci }, 29e1051a39Sopenharmony_ci { /* Chinese (simplified) */ 30e1051a39Sopenharmony_ci { 0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587 31e1051a39Sopenharmony_ci }, 32e1051a39Sopenharmony_ci "ihqwcrb4cv8a8dqg056pqjye" 33e1051a39Sopenharmony_ci }, 34e1051a39Sopenharmony_ci { /* Chinese (traditional) */ 35e1051a39Sopenharmony_ci { 0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587 36e1051a39Sopenharmony_ci }, 37e1051a39Sopenharmony_ci "ihqwctvzc91f659drss3x8bo0yb" 38e1051a39Sopenharmony_ci }, 39e1051a39Sopenharmony_ci { /* Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky */ 40e1051a39Sopenharmony_ci { 0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074, 41e1051a39Sopenharmony_ci 0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D, 42e1051a39Sopenharmony_ci 0x0065, 0x0073, 0x006B, 0x0079 43e1051a39Sopenharmony_ci }, 44e1051a39Sopenharmony_ci "Proprostnemluvesky-uyb24dma41a" 45e1051a39Sopenharmony_ci }, 46e1051a39Sopenharmony_ci { /* Hebrew */ 47e1051a39Sopenharmony_ci { 0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8, 48e1051a39Sopenharmony_ci 0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2, 49e1051a39Sopenharmony_ci 0x05D1, 0x05E8, 0x05D9, 0x05EA 50e1051a39Sopenharmony_ci }, 51e1051a39Sopenharmony_ci "4dbcagdahymbxekheh6e0a7fei0b" 52e1051a39Sopenharmony_ci }, 53e1051a39Sopenharmony_ci { /* Hindi (Devanagari) */ 54e1051a39Sopenharmony_ci { 0x092F, 0x0939, 0x0932, 0x094B, 0x0917, 0x0939, 0x093F, 0x0928, 0x094D, 55e1051a39Sopenharmony_ci 0x0926, 0x0940, 0x0915, 0x094D, 0x092F, 0x094B, 0x0902, 0x0928, 0x0939, 56e1051a39Sopenharmony_ci 0x0940, 0x0902, 0x092C, 0x094B, 0x0932, 0x0938, 0x0915, 0x0924, 0x0947, 57e1051a39Sopenharmony_ci 0x0939, 0x0948, 0x0902 58e1051a39Sopenharmony_ci }, 59e1051a39Sopenharmony_ci "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd" 60e1051a39Sopenharmony_ci }, 61e1051a39Sopenharmony_ci { /* Japanese (kanji and hiragana) */ 62e1051a39Sopenharmony_ci { 0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092, 63e1051a39Sopenharmony_ci 0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B 64e1051a39Sopenharmony_ci }, 65e1051a39Sopenharmony_ci "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa" 66e1051a39Sopenharmony_ci }, 67e1051a39Sopenharmony_ci { /* Korean (Hangul syllables) */ 68e1051a39Sopenharmony_ci { 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774, 69e1051a39Sopenharmony_ci 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74, 70e1051a39Sopenharmony_ci 0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C 71e1051a39Sopenharmony_ci }, 72e1051a39Sopenharmony_ci "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c" 73e1051a39Sopenharmony_ci }, 74e1051a39Sopenharmony_ci { /* Russian (Cyrillic) */ 75e1051a39Sopenharmony_ci { 0x043F, 0x043E, 0x0447, 0x0435, 0x043C, 0x0443, 0x0436, 0x0435, 0x043E, 76e1051a39Sopenharmony_ci 0x043D, 0x0438, 0x043D, 0x0435, 0x0433, 0x043E, 0x0432, 0x043E, 0x0440, 77e1051a39Sopenharmony_ci 0x044F, 0x0442, 0x043F, 0x043E, 0x0440, 0x0443, 0x0441, 0x0441, 0x043A, 78e1051a39Sopenharmony_ci 0x0438 79e1051a39Sopenharmony_ci }, 80e1051a39Sopenharmony_ci "b1abfaaepdrnnbgefbaDotcwatmq2g4l" 81e1051a39Sopenharmony_ci }, 82e1051a39Sopenharmony_ci { /* Spanish */ 83e1051a39Sopenharmony_ci { 0x0050, 0x006F, 0x0072, 0x0071, 0x0075, 0x00E9, 0x006E, 0x006F, 0x0070, 84e1051a39Sopenharmony_ci 0x0075, 0x0065, 0x0064, 0x0065, 0x006E, 0x0073, 0x0069, 0x006D, 0x0070, 85e1051a39Sopenharmony_ci 0x006C, 0x0065, 0x006D, 0x0065, 0x006E, 0x0074, 0x0065, 0x0068, 0x0061, 86e1051a39Sopenharmony_ci 0x0062, 0x006C, 0x0061, 0x0072, 0x0065, 0x006E, 0x0045, 0x0073, 0x0070, 87e1051a39Sopenharmony_ci 0x0061, 0x00F1, 0x006F, 0x006C 88e1051a39Sopenharmony_ci }, 89e1051a39Sopenharmony_ci "PorqunopuedensimplementehablarenEspaol-fmd56a" 90e1051a39Sopenharmony_ci }, 91e1051a39Sopenharmony_ci { /* Vietnamese */ 92e1051a39Sopenharmony_ci { 0x0054, 0x1EA1, 0x0069, 0x0073, 0x0061, 0x006F, 0x0068, 0x1ECD, 0x006B, 93e1051a39Sopenharmony_ci 0x0068, 0x00F4, 0x006E, 0x0067, 0x0074, 0x0068, 0x1EC3, 0x0063, 0x0068, 94e1051a39Sopenharmony_ci 0x1EC9, 0x006E, 0x00F3, 0x0069, 0x0074, 0x0069, 0x1EBF, 0x006E, 0x0067, 95e1051a39Sopenharmony_ci 0x0056, 0x0069, 0x1EC7, 0x0074 96e1051a39Sopenharmony_ci }, 97e1051a39Sopenharmony_ci "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g" 98e1051a39Sopenharmony_ci }, 99e1051a39Sopenharmony_ci { /* Japanese: 3<nen>B<gumi><kinpachi><sensei> */ 100e1051a39Sopenharmony_ci { 0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F 101e1051a39Sopenharmony_ci }, 102e1051a39Sopenharmony_ci "3B-ww4c5e180e575a65lsy2b" 103e1051a39Sopenharmony_ci }, 104e1051a39Sopenharmony_ci { /* Japanese: <amuro><namie>-with-SUPER-MONKEYS */ 105e1051a39Sopenharmony_ci { 0x5B89, 0x5BA4, 0x5948, 0x7F8E, 0x6075, 0x002D, 0x0077, 0x0069, 0x0074, 106e1051a39Sopenharmony_ci 0x0068, 0x002D, 0x0053, 0x0055, 0x0050, 0x0045, 0x0052, 0x002D, 0x004D, 107e1051a39Sopenharmony_ci 0x004F, 0x004E, 0x004B, 0x0045, 0x0059, 0x0053 108e1051a39Sopenharmony_ci }, 109e1051a39Sopenharmony_ci "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n" 110e1051a39Sopenharmony_ci }, 111e1051a39Sopenharmony_ci { /* Japanese: Hello-Another-Way-<sorezore><no><basho> */ 112e1051a39Sopenharmony_ci { 0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x002D, 0x0041, 0x006E, 0x006F, 113e1051a39Sopenharmony_ci 0x0074, 0x0068, 0x0065, 0x0072, 0x002D, 0x0057, 0x0061, 0x0079, 0x002D, 114e1051a39Sopenharmony_ci 0x305D, 0x308C, 0x305E, 0x308C, 0x306E, 0x5834, 0x6240 115e1051a39Sopenharmony_ci }, 116e1051a39Sopenharmony_ci "Hello-Another-Way--fc4qua05auwb3674vfr0b" 117e1051a39Sopenharmony_ci }, 118e1051a39Sopenharmony_ci { /* Japanese: <hitotsu><yane><no><shita>2 */ 119e1051a39Sopenharmony_ci { 0x3072, 0x3068, 0x3064, 0x5C4B, 0x6839, 0x306E, 0x4E0B, 0x0032 120e1051a39Sopenharmony_ci }, 121e1051a39Sopenharmony_ci "2-u9tlzr9756bt3uc0v" 122e1051a39Sopenharmony_ci }, 123e1051a39Sopenharmony_ci { /* Japanese: Maji<de>Koi<suru>5<byou><mae> */ 124e1051a39Sopenharmony_ci { 0x004D, 0x0061, 0x006A, 0x0069, 0x3067, 0x004B, 0x006F, 0x0069, 0x3059, 125e1051a39Sopenharmony_ci 0x308B, 0x0035, 0x79D2, 0x524D 126e1051a39Sopenharmony_ci }, 127e1051a39Sopenharmony_ci "MajiKoi5-783gue6qz075azm5e" 128e1051a39Sopenharmony_ci }, 129e1051a39Sopenharmony_ci { /* Japanese: <pafii>de<runba> */ 130e1051a39Sopenharmony_ci { 0x30D1, 0x30D5, 0x30A3, 0x30FC, 0x0064, 0x0065, 0x30EB, 0x30F3, 0x30D0 131e1051a39Sopenharmony_ci }, 132e1051a39Sopenharmony_ci "de-jg4avhby1noc0d" 133e1051a39Sopenharmony_ci }, 134e1051a39Sopenharmony_ci { /* Japanese: <sono><supiido><de> */ 135e1051a39Sopenharmony_ci { 0x305D, 0x306E, 0x30B9, 0x30D4, 0x30FC, 0x30C9, 0x3067 136e1051a39Sopenharmony_ci }, 137e1051a39Sopenharmony_ci "d9juau41awczczp" 138e1051a39Sopenharmony_ci }, 139e1051a39Sopenharmony_ci { /* -> $1.00 <- */ 140e1051a39Sopenharmony_ci { 0x002D, 0x003E, 0x0020, 0x0024, 0x0031, 0x002E, 0x0030, 0x0030, 0x0020, 141e1051a39Sopenharmony_ci 0x003C, 0x002D 142e1051a39Sopenharmony_ci }, 143e1051a39Sopenharmony_ci "-> $1.00 <--" 144e1051a39Sopenharmony_ci } 145e1051a39Sopenharmony_ci}; 146e1051a39Sopenharmony_ci 147e1051a39Sopenharmony_cistatic int test_punycode(int n) 148e1051a39Sopenharmony_ci{ 149e1051a39Sopenharmony_ci const struct puny_test *tc = puny_cases + n; 150e1051a39Sopenharmony_ci unsigned int buffer[50]; 151e1051a39Sopenharmony_ci unsigned int bsize = OSSL_NELEM(buffer); 152e1051a39Sopenharmony_ci size_t i; 153e1051a39Sopenharmony_ci 154e1051a39Sopenharmony_ci if (!TEST_true(ossl_punycode_decode(tc->encoded, strlen(tc->encoded), 155e1051a39Sopenharmony_ci buffer, &bsize))) 156e1051a39Sopenharmony_ci return 0; 157e1051a39Sopenharmony_ci for (i = 0; i < OSSL_NELEM(tc->raw); i++) 158e1051a39Sopenharmony_ci if (tc->raw[i] == 0) 159e1051a39Sopenharmony_ci break; 160e1051a39Sopenharmony_ci if (!TEST_mem_eq(buffer, bsize * sizeof(*buffer), 161e1051a39Sopenharmony_ci tc->raw, i * sizeof(*tc->raw))) 162e1051a39Sopenharmony_ci return 0; 163e1051a39Sopenharmony_ci return 1; 164e1051a39Sopenharmony_ci} 165e1051a39Sopenharmony_ci 166e1051a39Sopenharmony_cistatic int test_a2ulabel(void) 167e1051a39Sopenharmony_ci{ 168e1051a39Sopenharmony_ci char out[50]; 169e1051a39Sopenharmony_ci size_t outlen; 170e1051a39Sopenharmony_ci 171e1051a39Sopenharmony_ci /* 172e1051a39Sopenharmony_ci * Test that no buffer correctly returns the true length. 173e1051a39Sopenharmony_ci * The punycode being passed in and parsed is malformed but we're not 174e1051a39Sopenharmony_ci * verifying that behaviour here. 175e1051a39Sopenharmony_ci */ 176e1051a39Sopenharmony_ci if (!TEST_int_eq(ossl_a2ulabel("xn--a.b.c", NULL, &outlen), 0) 177e1051a39Sopenharmony_ci || !TEST_size_t_eq(outlen, 7) 178e1051a39Sopenharmony_ci || !TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, &outlen), 1)) 179e1051a39Sopenharmony_ci return 0; 180e1051a39Sopenharmony_ci /* Test that a short input length returns the true length */ 181e1051a39Sopenharmony_ci outlen = 1; 182e1051a39Sopenharmony_ci if (!TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, &outlen), 0) 183e1051a39Sopenharmony_ci || !TEST_size_t_eq(outlen, 7) 184e1051a39Sopenharmony_ci || !TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, &outlen), 1) 185e1051a39Sopenharmony_ci || !TEST_str_eq(out,"\xc2\x80.b.c")) 186e1051a39Sopenharmony_ci return 0; 187e1051a39Sopenharmony_ci /* Test for an off by one on the buffer size works */ 188e1051a39Sopenharmony_ci outlen = 6; 189e1051a39Sopenharmony_ci if (!TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, &outlen), 0) 190e1051a39Sopenharmony_ci || !TEST_size_t_eq(outlen, 7) 191e1051a39Sopenharmony_ci || !TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, &outlen), 1) 192e1051a39Sopenharmony_ci || !TEST_str_eq(out,"\xc2\x80.b.c")) 193e1051a39Sopenharmony_ci return 0; 194e1051a39Sopenharmony_ci return 1; 195e1051a39Sopenharmony_ci} 196e1051a39Sopenharmony_ci 197e1051a39Sopenharmony_cistatic int test_puny_overrun(void) 198e1051a39Sopenharmony_ci{ 199e1051a39Sopenharmony_ci static const unsigned int out[] = { 200e1051a39Sopenharmony_ci 0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F 201e1051a39Sopenharmony_ci }; 202e1051a39Sopenharmony_ci static const char *in = "3B-ww4c5e180e575a65lsy2b"; 203e1051a39Sopenharmony_ci unsigned int buf[OSSL_NELEM(out)]; 204e1051a39Sopenharmony_ci unsigned int bsize = OSSL_NELEM(buf) - 1; 205e1051a39Sopenharmony_ci 206e1051a39Sopenharmony_ci if (!TEST_false(ossl_punycode_decode(in, strlen(in), buf, &bsize))) { 207e1051a39Sopenharmony_ci if (TEST_mem_eq(buf, bsize * sizeof(*buf), out, sizeof(out))) 208e1051a39Sopenharmony_ci TEST_error("CRITICAL: buffer overrun detected!"); 209e1051a39Sopenharmony_ci return 0; 210e1051a39Sopenharmony_ci } 211e1051a39Sopenharmony_ci return 1; 212e1051a39Sopenharmony_ci} 213e1051a39Sopenharmony_ci 214e1051a39Sopenharmony_ciint setup_tests(void) 215e1051a39Sopenharmony_ci{ 216e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_punycode, OSSL_NELEM(puny_cases)); 217e1051a39Sopenharmony_ci ADD_TEST(test_a2ulabel); 218e1051a39Sopenharmony_ci ADD_TEST(test_puny_overrun); 219e1051a39Sopenharmony_ci return 1; 220e1051a39Sopenharmony_ci} 221