|
| 1 | +/* client-tls-cryptocb.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2020 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. (formerly known as CyaSSL) |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include "common.h" |
| 23 | + |
| 24 | +static void error_out(char* msg, int err) |
| 25 | +{ |
| 26 | + printf("Failed at %s with code %d\n", msg, err); |
| 27 | + exit(1); |
| 28 | +} |
| 29 | + |
| 30 | +int main(int argc, char** argv) |
| 31 | +{ |
| 32 | + int ret = 0; |
| 33 | +#ifdef WOLF_CRYPTO_CB |
| 34 | + int sockfd; |
| 35 | + struct sockaddr_in servAddr; |
| 36 | + char buff[256]; |
| 37 | + size_t len; |
| 38 | + |
| 39 | + /* declare wolfSSL objects */ |
| 40 | + WOLFSSL_CTX* ctx; |
| 41 | + WOLFSSL* ssl; |
| 42 | + |
| 43 | +#if defined(DEBUG_WOLFSSL) |
| 44 | + wolfSSL_Debugging_ON(); |
| 45 | +#endif |
| 46 | + |
| 47 | + /* Check for proper calling convention */ |
| 48 | + if (argc != 2) { |
| 49 | + printf("usage: %s <IPv4 address>\n", argv[0]); |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + /* Create a socket that uses an internet IPv4 address, |
| 54 | + * Sets the socket to be stream based (TCP), |
| 55 | + * 0 means choose the default protocol. */ |
| 56 | + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 57 | + fprintf(stderr, "ERROR: failed to create the socket\n"); |
| 58 | + ret = -1; |
| 59 | + goto end; |
| 60 | + } |
| 61 | + |
| 62 | + /* Initialize the server address struct with zeros */ |
| 63 | + memset(&servAddr, 0, sizeof(servAddr)); |
| 64 | + |
| 65 | + /* Fill in the server address */ |
| 66 | + servAddr.sin_family = AF_INET; /* using IPv4 */ |
| 67 | + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ |
| 68 | + |
| 69 | + /* Get the server IPv4 address from the command line call */ |
| 70 | + if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) { |
| 71 | + fprintf(stderr, "ERROR: invalid address\n"); |
| 72 | + ret = -1; |
| 73 | + goto end; |
| 74 | + } |
| 75 | + |
| 76 | + /* Connect to the server */ |
| 77 | + if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr))) |
| 78 | + == -1) { |
| 79 | + fprintf(stderr, "ERROR: failed to connect\n"); |
| 80 | + goto end; |
| 81 | + } |
| 82 | + |
| 83 | + /*---------------------------------*/ |
| 84 | + /* Start of wolfSSL initialization and configuration */ |
| 85 | + /*---------------------------------*/ |
| 86 | + /* Initialize wolfSSL */ |
| 87 | + if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) { |
| 88 | + fprintf(stderr, "ERROR: Failed to initialize the library\n"); |
| 89 | + goto socket_cleanup; |
| 90 | + } |
| 91 | + |
| 92 | + /* Create and initialize WOLFSSL_CTX */ |
| 93 | + if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { |
| 94 | + fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); |
| 95 | + ret = -1; |
| 96 | + goto socket_cleanup; |
| 97 | + } |
| 98 | + |
| 99 | + /* Load client certificates into WOLFSSL_CTX */ |
| 100 | + if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL)) |
| 101 | + != SSL_SUCCESS) { |
| 102 | + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", |
| 103 | + CA_FILE); |
| 104 | + goto ctx_cleanup; |
| 105 | + } |
| 106 | + |
| 107 | + /* Load client ecc certificates into WOLFSSL_CTX */ |
| 108 | + if ((ret = wolfSSL_CTX_use_certificate_chain_file(ctx, CLIENT_ECC_FILE)) != |
| 109 | + WOLFSSL_SUCCESS) { |
| 110 | + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", |
| 111 | + CLIENT_ECC_FILE); |
| 112 | + goto ctx_cleanup; |
| 113 | + } |
| 114 | + |
| 115 | + /* Load client ecc key into WOLFSSL_CTX */ |
| 116 | + if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, CLIENT_KEY_FILE, SSL_FILETYPE_PEM)) |
| 117 | + != WOLFSSL_SUCCESS) { |
| 118 | + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", |
| 119 | + CLIENT_KEY_FILE); |
| 120 | + goto ctx_cleanup; |
| 121 | + } |
| 122 | + |
| 123 | + if ((ret = wolfSSL_CTX_set_cipher_list(ctx, CIPHER_LIST)) |
| 124 | + != WOLFSSL_SUCCESS) { |
| 125 | + fprintf(stderr, "ERROR: failed to set cipher list\n"); |
| 126 | + goto ctx_cleanup; |
| 127 | + } |
| 128 | + |
| 129 | + if ((ret = wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1)) |
| 130 | + != WOLFSSL_SUCCESS) { |
| 131 | + fprintf(stderr, "ERROR: failed to set supported curve\n"); |
| 132 | + goto ctx_cleanup; |
| 133 | + } |
| 134 | + |
| 135 | + /* Create a WOLFSSL object */ |
| 136 | + if ((ssl = wolfSSL_new(ctx)) == NULL) { |
| 137 | + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); |
| 138 | + ret = -1; |
| 139 | + goto ctx_cleanup; |
| 140 | + } |
| 141 | + |
| 142 | + /* Attach wolfSSL to the socket */ |
| 143 | + if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) { |
| 144 | + fprintf(stderr, "ERROR: Failed to set the file descriptor\n"); |
| 145 | + goto cleanup; |
| 146 | + } |
| 147 | + |
| 148 | + /* Connect to wolfSSL on the server side */ |
| 149 | + if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) { |
| 150 | + fprintf(stderr, "ERROR: failed to connect to wolfSSL\n"); |
| 151 | + goto cleanup; |
| 152 | + } |
| 153 | + |
| 154 | + /* Get a message for the server from stdin */ |
| 155 | + printf("Message for server: "); |
| 156 | + memset(buff, 0, sizeof(buff)); |
| 157 | + if (fgets(buff, sizeof(buff), stdin) == NULL) { |
| 158 | + fprintf(stderr, "ERROR: failed to get message for server\n"); |
| 159 | + ret = -1; |
| 160 | + goto cleanup; |
| 161 | + } |
| 162 | + len = strnlen(buff, sizeof(buff)); |
| 163 | + |
| 164 | + /* Send the message to the server */ |
| 165 | + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { |
| 166 | + fprintf(stderr, "ERROR: failed to write entire message\n"); |
| 167 | + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); |
| 168 | + goto cleanup; |
| 169 | + } |
| 170 | + |
| 171 | + /* Read the server data into our buff array */ |
| 172 | + memset(buff, 0, sizeof(buff)); |
| 173 | + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { |
| 174 | + fprintf(stderr, "ERROR: failed to read\n"); |
| 175 | + goto cleanup; |
| 176 | + } |
| 177 | + |
| 178 | + /* Print to stdout any data the server sends */ |
| 179 | + printf("Server: %s\n", buff); |
| 180 | + |
| 181 | + ret = 0; |
| 182 | + |
| 183 | + /* Cleanup and return */ |
| 184 | +cleanup: |
| 185 | + if (ret != 0) { |
| 186 | + fprintf(stderr,"SSL Error: %s\n",wolfSSL_ERR_error_string(wolfSSL_get_error(ssl,0), NULL)); |
| 187 | + } |
| 188 | + wolfSSL_free(ssl); /* Free the wolfSSL object */ |
| 189 | +ctx_cleanup: |
| 190 | + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ |
| 191 | + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ |
| 192 | +socket_cleanup: |
| 193 | + close(sockfd); /* Close the connection to the server */ |
| 194 | +end: |
| 195 | + |
| 196 | +#else |
| 197 | + printf("Please configure wolfSSL with --enable-cryptocb and try again\n"); |
| 198 | +#endif /* WOLF_CRYPTO_CB */ |
| 199 | + return ret; /* Return reporting a success */ |
| 200 | +} |
0 commit comments