1 /* 2 * Copyright (c) 2015 Joel Sing <jsing@openbsd.org> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <openssl/ssl.h> 18 19 #include <err.h> 20 #include <stdio.h> 21 #include <string.h> 22 23 static int 24 get_put_test(const char *name, const SSL_METHOD *method) 25 { 26 STACK_OF(SSL_CIPHER) *ciphers; 27 const SSL_CIPHER *cipher; 28 unsigned char buf[2]; 29 SSL_CTX *ssl_ctx = NULL; 30 SSL *ssl = NULL; 31 int ret = 1; 32 int i, len; 33 34 if ((len = method->put_cipher_by_char(NULL, NULL)) != 2) { 35 fprintf(stderr, 36 "%s: put_cipher_by_char() returned len %i (want 2)\n", 37 name, len); 38 return (1); 39 } 40 41 if ((ssl_ctx = SSL_CTX_new(method)) == NULL) { 42 fprintf(stderr, "%s: SSL_CTX_new() returned NULL\n", name); 43 goto failure; 44 } 45 if ((ssl = SSL_new(ssl_ctx)) == NULL) { 46 fprintf(stderr, "%s: SSL_new() returned NULL\n", name); 47 goto failure; 48 } 49 50 if ((ciphers = SSL_get_ciphers(ssl)) == NULL) { 51 fprintf(stderr, "%s: no ciphers\n", name); 52 goto failure; 53 } 54 55 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 56 cipher = sk_SSL_CIPHER_value(ciphers, i); 57 if ((len = method->put_cipher_by_char(cipher, buf)) != 2) { 58 fprintf(stderr, 59 "%s: put_cipher_by_char() returned len %i for %s " 60 "(want 2)\n", 61 name, len, SSL_CIPHER_get_name(cipher)); 62 goto failure; 63 } 64 if ((cipher = method->get_cipher_by_char(buf)) == NULL) { 65 fprintf(stderr, 66 "%s: get_cipher_by_char() returned NULL for %s\n", 67 name, SSL_CIPHER_get_name(cipher)); 68 goto failure; 69 } 70 } 71 72 ret = 0; 73 74 failure: 75 SSL_CTX_free(ssl_ctx); 76 SSL_free(ssl); 77 78 return (ret); 79 } 80 81 static int 82 cipher_get_put_tests(void) 83 { 84 int failed = 0; 85 86 failed |= get_put_test("SSLv23", SSLv23_method()); 87 failed |= get_put_test("SSLv23_client", SSLv23_client_method()); 88 failed |= get_put_test("SSLv23_server", SSLv23_server_method()); 89 90 failed |= get_put_test("SSLv3", SSLv3_method()); 91 failed |= get_put_test("SSLv3_client", SSLv3_client_method()); 92 failed |= get_put_test("SSLv3_server", SSLv3_server_method()); 93 94 failed |= get_put_test("TLSv1", TLSv1_method()); 95 failed |= get_put_test("TLSv1_client", TLSv1_client_method()); 96 failed |= get_put_test("TLSv1_server", TLSv1_server_method()); 97 98 failed |= get_put_test("TLSv1_1", TLSv1_1_method()); 99 failed |= get_put_test("TLSv1_1_client", TLSv1_1_client_method()); 100 failed |= get_put_test("TLSv1_1_server", TLSv1_1_server_method()); 101 102 failed |= get_put_test("TLSv1_2", TLSv1_2_method()); 103 failed |= get_put_test("TLSv1_2_client", TLSv1_2_client_method()); 104 failed |= get_put_test("TLSv1_2_server", TLSv1_2_server_method()); 105 106 failed |= get_put_test("DTLSv1", DTLSv1_method()); 107 failed |= get_put_test("DTLSv1_client", DTLSv1_client_method()); 108 failed |= get_put_test("DTLSv1_server", DTLSv1_server_method()); 109 110 return failed; 111 } 112 113 static int 114 cipher_get_by_value_tests(void) 115 { 116 STACK_OF(SSL_CIPHER) *ciphers; 117 const SSL_CIPHER *cipher; 118 SSL_CTX *ssl_ctx = NULL; 119 SSL *ssl = NULL; 120 unsigned long id; 121 uint16_t value; 122 int ret = 1; 123 int i; 124 125 if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) { 126 fprintf(stderr, "SSL_CTX_new() returned NULL\n"); 127 goto failure; 128 } 129 if ((ssl = SSL_new(ssl_ctx)) == NULL) { 130 fprintf(stderr, "SSL_new() returned NULL\n"); 131 goto failure; 132 } 133 134 if ((ciphers = SSL_get_ciphers(ssl)) == NULL) { 135 fprintf(stderr, "no ciphers\n"); 136 goto failure; 137 } 138 139 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 140 cipher = sk_SSL_CIPHER_value(ciphers, i); 141 142 id = SSL_CIPHER_get_id(cipher); 143 if (SSL_CIPHER_get_by_id(id) == NULL) { 144 fprintf(stderr, "SSL_CIPHER_get_by_id() failed " 145 "for %s (0x%lx)\n", SSL_CIPHER_get_name(cipher), 146 id); 147 goto failure; 148 } 149 150 value = SSL_CIPHER_get_value(cipher); 151 if (SSL_CIPHER_get_by_value(value) == NULL) { 152 fprintf(stderr, "SSL_CIPHER_get_by_value() failed " 153 "for %s (0x%04hx)\n", SSL_CIPHER_get_name(cipher), 154 value); 155 goto failure; 156 } 157 } 158 159 ret = 0; 160 161 failure: 162 SSL_CTX_free(ssl_ctx); 163 SSL_free(ssl); 164 165 return (ret); 166 } 167 168 int 169 main(int argc, char **argv) 170 { 171 int failed = 0; 172 173 SSL_library_init(); 174 175 failed |= cipher_get_put_tests(); 176 failed |= cipher_get_by_value_tests(); 177 178 return (failed); 179 } 180