Adam Langley | 659de41 | 2014-06-26 10:26:42 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <netinet/in.h> |
| 18 | #include <arpa/inet.h> |
| 19 | |
| 20 | #include <openssl/ssl.h> |
| 21 | #include <openssl/bio.h> |
| 22 | |
| 23 | |
| 24 | SSL *setup_test() { |
| 25 | if (!SSL_library_init()) { |
| 26 | return NULL; |
| 27 | } |
| 28 | |
| 29 | SSL_CTX *client_ctx = NULL; |
| 30 | SSL *client = NULL; |
| 31 | BIO *bio = NULL; |
| 32 | |
| 33 | client_ctx = SSL_CTX_new(SSLv23_client_method()); |
| 34 | if (client_ctx == NULL) { |
| 35 | goto err; |
| 36 | } |
| 37 | |
| 38 | if (!SSL_CTX_set_cipher_list(client_ctx, "ALL")) { |
| 39 | goto err; |
| 40 | } |
| 41 | |
| 42 | client = SSL_new(client_ctx); |
| 43 | if (client == NULL) { |
| 44 | goto err; |
| 45 | } |
| 46 | |
| 47 | bio = BIO_new_fd(3, 1 /* take ownership */); |
| 48 | if (bio == NULL) { |
| 49 | goto err; |
| 50 | } |
| 51 | |
| 52 | SSL_set_bio(client, bio, bio); |
| 53 | SSL_CTX_free(client_ctx); |
| 54 | |
| 55 | return client; |
| 56 | |
| 57 | err: |
| 58 | if (bio != NULL) { |
| 59 | BIO_free(bio); |
| 60 | } |
| 61 | if (client != NULL) { |
| 62 | SSL_free(client); |
| 63 | } |
| 64 | if (client_ctx != NULL) { |
| 65 | SSL_CTX_free(client_ctx); |
| 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 70 | int main(int argc, char **argv) { |
| 71 | int i; |
| 72 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 73 | SSL *client = setup_test(); |
| 74 | if (client == NULL) { |
| 75 | BIO_print_errors_fp(stdout); |
| 76 | return 1; |
| 77 | } |
| 78 | |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 79 | for (i = 1; i < argc; i++) { |
| 80 | if (strcmp(argv[i], "-fallback-scsv") == 0) { |
| 81 | SSL_enable_fallback_scsv(client); |
| 82 | } else { |
| 83 | fprintf(stderr, "Unknown argument: %s\n", argv[i]); |
| 84 | return 1; |
| 85 | } |
| 86 | } |
| 87 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 88 | if (SSL_connect(client) != 1) { |
| 89 | SSL_free(client); |
| 90 | BIO_print_errors_fp(stdout); |
| 91 | return 2; |
| 92 | } |
| 93 | |
| 94 | for (;;) { |
| 95 | uint8_t buf[512]; |
| 96 | int n = SSL_read(client, buf, sizeof(buf)); |
| 97 | if (n < 0) { |
| 98 | SSL_free(client); |
| 99 | BIO_print_errors_fp(stdout); |
| 100 | return 3; |
| 101 | } else if (n == 0) { |
| 102 | break; |
| 103 | } else { |
| 104 | for (int i = 0; i < n; i++) { |
| 105 | buf[i] ^= 0xff; |
| 106 | } |
| 107 | int w = SSL_write(client, buf, n); |
| 108 | if (w != n) { |
| 109 | SSL_free(client); |
| 110 | BIO_print_errors_fp(stdout); |
| 111 | return 4; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | SSL_free(client); |
| 117 | return 0; |
| 118 | } |