-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also added CID clients for dtls 1.2 and 1.3. The clients change port after sending each message.
- Loading branch information
1 parent
14dfeeb
commit 83ede0a
Showing
4 changed files
with
999 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* client-dtls.c | ||
* | ||
* Copyright (C) 2006-2020 wolfSSL Inc. | ||
* | ||
* This file is part of wolfSSL. (formerly known as CyaSSL) | ||
* | ||
* wolfSSL is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* wolfSSL is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
*============================================================================= | ||
* | ||
* Example of DTLS client sending data from a changing socket using | ||
* Connection ID. The client's socket is reset after every message sent so each | ||
* new datagram is sent from a different port. | ||
*/ | ||
|
||
#include <wolfssl/options.h> | ||
#include <unistd.h> | ||
#include <wolfssl/ssl.h> | ||
#include <netdb.h> | ||
#include <signal.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define MAXLINE 4096 | ||
#define SERV_PORT 11111 | ||
|
||
|
||
int main (int argc, char** argv) | ||
{ | ||
/* standard variables used in a dtls client*/ | ||
int n = 0; | ||
int sockfd = 0; | ||
int err1; | ||
int readErr; | ||
struct sockaddr_in servAddr; | ||
WOLFSSL* ssl = 0; | ||
WOLFSSL_CTX* ctx = 0; | ||
char cert_array[] = "../certs/ca-cert.pem"; | ||
char* certs = cert_array; | ||
char sendLine[MAXLINE]; | ||
char recvLine[MAXLINE - 1]; | ||
|
||
/* Program argument checking */ | ||
if (argc != 2) { | ||
printf("usage: %s <IP address>\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
/* Initialize wolfSSL before assigning ctx */ | ||
wolfSSL_Init(); | ||
|
||
/* wolfSSL_Debugging_ON(); */ | ||
|
||
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) { | ||
fprintf(stderr, "wolfSSL_CTX_new error.\n"); | ||
return 1; | ||
} | ||
|
||
/* Load certificates into ctx variable */ | ||
if (wolfSSL_CTX_load_verify_locations(ctx, certs, 0) | ||
!= SSL_SUCCESS) { | ||
fprintf(stderr, "Error loading %s, please check the file.\n", certs); | ||
return 1; | ||
} | ||
|
||
/* Assign ssl variable */ | ||
ssl = wolfSSL_new(ctx); | ||
if (ssl == NULL) { | ||
printf("unable to get ssl object"); | ||
return 1; | ||
} | ||
|
||
/* servAddr setup */ | ||
memset(&servAddr, 0, sizeof(servAddr)); | ||
servAddr.sin_family = AF_INET; | ||
servAddr.sin_port = htons(SERV_PORT); | ||
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) { | ||
printf("Error and/or invalid IP address"); | ||
return 1; | ||
} | ||
|
||
if (wolfSSL_dtls_cid_use(ssl) != SSL_SUCCESS) { | ||
fprintf(stderr, "wolfSSL_dtls_cid_use error.\n"); | ||
return 1; | ||
} | ||
if (wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr)) != SSL_SUCCESS) { | ||
fprintf(stderr, "wolfSSL_dtls_set_peer error.\n"); | ||
return 1; | ||
} | ||
|
||
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | ||
printf("cannot create a socket."); | ||
return 1; | ||
} | ||
|
||
/* Set the file descriptor for ssl and connect with ssl variable */ | ||
wolfSSL_set_fd(ssl, sockfd); | ||
if (wolfSSL_connect(ssl) != SSL_SUCCESS) { | ||
err1 = wolfSSL_get_error(ssl, 0); | ||
printf("err = %d, %s\n", err1, wolfSSL_ERR_reason_error_string(err1)); | ||
printf("SSL_connect failed"); | ||
return 1; | ||
} | ||
|
||
/*****************************************************************************/ | ||
/* Code for sending datagram to server */ | ||
/* Loop until the user is finished */ | ||
while (fgets(sendLine, MAXLINE, stdin) != NULL) { | ||
if (strncmp(sendLine, "exit", strlen("exit")) == 0) | ||
break; | ||
|
||
/* Send sendLine to the server */ | ||
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) | ||
!= strlen(sendLine)) { | ||
printf("SSL_write failed"); | ||
} | ||
|
||
/* n is the # of bytes received */ | ||
n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1); | ||
|
||
if (n < 0) { | ||
readErr = wolfSSL_get_error(ssl, 0); | ||
if (readErr != SSL_ERROR_WANT_READ) { | ||
printf("wolfSSL_read failed"); | ||
} | ||
} | ||
|
||
/* Add a terminating character to the generic server message */ | ||
recvLine[n] = '\0'; | ||
fputs(recvLine, stdout); | ||
|
||
close(sockfd); | ||
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | ||
printf("cannot create a socket."); | ||
return 1; | ||
} | ||
/* Set the file descriptor for ssl and connect with ssl variable */ | ||
wolfSSL_set_fd(ssl, sockfd); | ||
} | ||
/* End code for sending datagram to server */ | ||
/*****************************************************************************/ | ||
|
||
/* Housekeeping */ | ||
wolfSSL_shutdown(ssl); | ||
wolfSSL_free(ssl); | ||
close(sockfd); | ||
wolfSSL_CTX_free(ctx); | ||
wolfSSL_Cleanup(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* client-dtls.c | ||
* | ||
* Copyright (C) 2006-2020 wolfSSL Inc. | ||
* | ||
* This file is part of wolfSSL. (formerly known as CyaSSL) | ||
* | ||
* wolfSSL is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* wolfSSL is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
*============================================================================= | ||
* | ||
* Example of DTLS client sending data from a changing socket using | ||
* Connection ID. The client's socket is reset after every message sent so each | ||
* new datagram is sent from a different port. | ||
*/ | ||
|
||
#include <wolfssl/options.h> | ||
#include <unistd.h> | ||
#include <wolfssl/ssl.h> | ||
#include <netdb.h> | ||
#include <signal.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define MAXLINE 4096 | ||
#define SERV_PORT 11111 | ||
|
||
|
||
int main (int argc, char** argv) | ||
{ | ||
/* standard variables used in a dtls client*/ | ||
int n = 0; | ||
int sockfd = 0; | ||
int err1; | ||
int readErr; | ||
struct sockaddr_in servAddr; | ||
WOLFSSL* ssl = 0; | ||
WOLFSSL_CTX* ctx = 0; | ||
char cert_array[] = "../certs/ca-cert.pem"; | ||
char* certs = cert_array; | ||
char sendLine[MAXLINE]; | ||
char recvLine[MAXLINE - 1]; | ||
|
||
/* Program argument checking */ | ||
if (argc != 2) { | ||
printf("usage: %s <IP address>\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
/* Initialize wolfSSL before assigning ctx */ | ||
wolfSSL_Init(); | ||
|
||
/* wolfSSL_Debugging_ON(); */ | ||
|
||
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_3_client_method())) == NULL) { | ||
fprintf(stderr, "wolfSSL_CTX_new error.\n"); | ||
return 1; | ||
} | ||
|
||
/* Load certificates into ctx variable */ | ||
if (wolfSSL_CTX_load_verify_locations(ctx, certs, 0) | ||
!= SSL_SUCCESS) { | ||
fprintf(stderr, "Error loading %s, please check the file.\n", certs); | ||
return 1; | ||
} | ||
|
||
/* Assign ssl variable */ | ||
ssl = wolfSSL_new(ctx); | ||
if (ssl == NULL) { | ||
printf("unable to get ssl object"); | ||
return 1; | ||
} | ||
|
||
/* servAddr setup */ | ||
memset(&servAddr, 0, sizeof(servAddr)); | ||
servAddr.sin_family = AF_INET; | ||
servAddr.sin_port = htons(SERV_PORT); | ||
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) { | ||
printf("Error and/or invalid IP address"); | ||
return 1; | ||
} | ||
|
||
if (wolfSSL_dtls_cid_use(ssl) != SSL_SUCCESS) { | ||
fprintf(stderr, "wolfSSL_dtls_cid_use error.\n"); | ||
return 1; | ||
} | ||
if (wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr)) != SSL_SUCCESS) { | ||
fprintf(stderr, "wolfSSL_dtls_set_peer error.\n"); | ||
return 1; | ||
} | ||
|
||
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | ||
printf("cannot create a socket."); | ||
return 1; | ||
} | ||
|
||
/* Set the file descriptor for ssl and connect with ssl variable */ | ||
wolfSSL_set_fd(ssl, sockfd); | ||
if (wolfSSL_connect(ssl) != SSL_SUCCESS) { | ||
err1 = wolfSSL_get_error(ssl, 0); | ||
printf("err = %d, %s\n", err1, wolfSSL_ERR_reason_error_string(err1)); | ||
printf("SSL_connect failed"); | ||
return 1; | ||
} | ||
|
||
/*****************************************************************************/ | ||
/* Code for sending datagram to server */ | ||
/* Loop until the user is finished */ | ||
while (fgets(sendLine, MAXLINE, stdin) != NULL) { | ||
if (strncmp(sendLine, "exit", strlen("exit")) == 0) | ||
break; | ||
|
||
/* Send sendLine to the server */ | ||
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) | ||
!= strlen(sendLine)) { | ||
printf("SSL_write failed"); | ||
} | ||
|
||
/* n is the # of bytes received */ | ||
n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1); | ||
|
||
if (n < 0) { | ||
readErr = wolfSSL_get_error(ssl, 0); | ||
if (readErr != SSL_ERROR_WANT_READ) { | ||
printf("wolfSSL_read failed"); | ||
} | ||
} | ||
|
||
/* Add a terminating character to the generic server message */ | ||
recvLine[n] = '\0'; | ||
fputs(recvLine, stdout); | ||
|
||
close(sockfd); | ||
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | ||
printf("cannot create a socket."); | ||
return 1; | ||
} | ||
/* Set the file descriptor for ssl and connect with ssl variable */ | ||
wolfSSL_set_fd(ssl, sockfd); | ||
} | ||
/* End code for sending datagram to server */ | ||
/*****************************************************************************/ | ||
|
||
/* Housekeeping */ | ||
wolfSSL_shutdown(ssl); | ||
wolfSSL_free(ssl); | ||
close(sockfd); | ||
wolfSSL_CTX_free(ctx); | ||
wolfSSL_Cleanup(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.