|
| 1 | +/* |
| 2 | + * client-dtls.c |
| 3 | + * |
| 4 | + * Copyright (C) 2006-2020 wolfSSL Inc. |
| 5 | + * |
| 6 | + * This file is part of wolfSSL. (formerly known as CyaSSL) |
| 7 | + * |
| 8 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * wolfSSL is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 21 | + * |
| 22 | + *============================================================================= |
| 23 | + * |
| 24 | + * Example of DTLS client sending data from a changing socket using |
| 25 | + * Connection ID. The client's socket is reset after every message sent so each |
| 26 | + * new datagram is sent from a different port. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <wolfssl/options.h> |
| 30 | +#include <unistd.h> |
| 31 | +#include <wolfssl/ssl.h> |
| 32 | +#include <netdb.h> |
| 33 | +#include <signal.h> |
| 34 | +#include <sys/socket.h> |
| 35 | +#include <arpa/inet.h> |
| 36 | +#include <netinet/in.h> |
| 37 | +#include <stdio.h> |
| 38 | +#include <stdlib.h> |
| 39 | +#include <string.h> |
| 40 | + |
| 41 | +#define MAXLINE 4096 |
| 42 | +#define SERV_PORT 11111 |
| 43 | + |
| 44 | + |
| 45 | +int main (int argc, char** argv) |
| 46 | +{ |
| 47 | + /* standard variables used in a dtls client*/ |
| 48 | + int n = 0; |
| 49 | + int sockfd = 0; |
| 50 | + int err1; |
| 51 | + int readErr; |
| 52 | + struct sockaddr_in servAddr; |
| 53 | + WOLFSSL* ssl = 0; |
| 54 | + WOLFSSL_CTX* ctx = 0; |
| 55 | + char cert_array[] = "../certs/ca-cert.pem"; |
| 56 | + char* certs = cert_array; |
| 57 | + char sendLine[MAXLINE]; |
| 58 | + char recvLine[MAXLINE - 1]; |
| 59 | + |
| 60 | + /* Program argument checking */ |
| 61 | + if (argc != 2) { |
| 62 | + printf("usage: %s <IP address>\n", argv[0]); |
| 63 | + return 1; |
| 64 | + } |
| 65 | + |
| 66 | + /* Initialize wolfSSL before assigning ctx */ |
| 67 | + wolfSSL_Init(); |
| 68 | + |
| 69 | + /* wolfSSL_Debugging_ON(); */ |
| 70 | + |
| 71 | + if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) { |
| 72 | + fprintf(stderr, "wolfSSL_CTX_new error.\n"); |
| 73 | + return 1; |
| 74 | + } |
| 75 | + |
| 76 | + /* Load certificates into ctx variable */ |
| 77 | + if (wolfSSL_CTX_load_verify_locations(ctx, certs, 0) |
| 78 | + != SSL_SUCCESS) { |
| 79 | + fprintf(stderr, "Error loading %s, please check the file.\n", certs); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + /* Assign ssl variable */ |
| 84 | + ssl = wolfSSL_new(ctx); |
| 85 | + if (ssl == NULL) { |
| 86 | + printf("unable to get ssl object"); |
| 87 | + return 1; |
| 88 | + } |
| 89 | + |
| 90 | + /* servAddr setup */ |
| 91 | + memset(&servAddr, 0, sizeof(servAddr)); |
| 92 | + servAddr.sin_family = AF_INET; |
| 93 | + servAddr.sin_port = htons(SERV_PORT); |
| 94 | + if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) { |
| 95 | + printf("Error and/or invalid IP address"); |
| 96 | + return 1; |
| 97 | + } |
| 98 | + |
| 99 | + if (wolfSSL_dtls_cid_use(ssl) != SSL_SUCCESS) { |
| 100 | + fprintf(stderr, "wolfSSL_dtls_cid_use error.\n"); |
| 101 | + return 1; |
| 102 | + } |
| 103 | + if (wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr)) != SSL_SUCCESS) { |
| 104 | + fprintf(stderr, "wolfSSL_dtls_set_peer error.\n"); |
| 105 | + return 1; |
| 106 | + } |
| 107 | + |
| 108 | + if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 109 | + printf("cannot create a socket."); |
| 110 | + return 1; |
| 111 | + } |
| 112 | + |
| 113 | + /* Set the file descriptor for ssl and connect with ssl variable */ |
| 114 | + wolfSSL_set_fd(ssl, sockfd); |
| 115 | + if (wolfSSL_connect(ssl) != SSL_SUCCESS) { |
| 116 | + err1 = wolfSSL_get_error(ssl, 0); |
| 117 | + printf("err = %d, %s\n", err1, wolfSSL_ERR_reason_error_string(err1)); |
| 118 | + printf("SSL_connect failed"); |
| 119 | + return 1; |
| 120 | + } |
| 121 | + |
| 122 | +/*****************************************************************************/ |
| 123 | +/* Code for sending datagram to server */ |
| 124 | + /* Loop until the user is finished */ |
| 125 | + while (fgets(sendLine, MAXLINE, stdin) != NULL) { |
| 126 | + if (strncmp(sendLine, "exit", strlen("exit")) == 0) |
| 127 | + break; |
| 128 | + |
| 129 | + /* Send sendLine to the server */ |
| 130 | + if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) |
| 131 | + != strlen(sendLine)) { |
| 132 | + printf("SSL_write failed"); |
| 133 | + } |
| 134 | + |
| 135 | + /* n is the # of bytes received */ |
| 136 | + n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1); |
| 137 | + |
| 138 | + if (n < 0) { |
| 139 | + readErr = wolfSSL_get_error(ssl, 0); |
| 140 | + if (readErr != SSL_ERROR_WANT_READ) { |
| 141 | + printf("wolfSSL_read failed"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /* Add a terminating character to the generic server message */ |
| 146 | + recvLine[n] = '\0'; |
| 147 | + fputs(recvLine, stdout); |
| 148 | + |
| 149 | + close(sockfd); |
| 150 | + if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 151 | + printf("cannot create a socket."); |
| 152 | + return 1; |
| 153 | + } |
| 154 | + /* Set the file descriptor for ssl and connect with ssl variable */ |
| 155 | + wolfSSL_set_fd(ssl, sockfd); |
| 156 | + } |
| 157 | +/* End code for sending datagram to server */ |
| 158 | +/*****************************************************************************/ |
| 159 | + |
| 160 | + /* Housekeeping */ |
| 161 | + wolfSSL_shutdown(ssl); |
| 162 | + wolfSSL_free(ssl); |
| 163 | + close(sockfd); |
| 164 | + wolfSSL_CTX_free(ctx); |
| 165 | + wolfSSL_Cleanup(); |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
0 commit comments