123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include <wolfssl/wolfcrypt/settings.h>
- #include <wolfssl/ssl.h>
- #define DEFAULT_PORT 11111
- #define CERT_FILE "../certs/ca-cert.pem"
- int main(int argc, char** argv)
- {
- int sockfd;
- struct sockaddr_in servAddr;
- char buff[256];
- size_t len;
- int ret;
-
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
-
- if (argc != 2) {
- printf("usage: %s <IPv4 address>\n", argv[0]);
- return 0;
- }
-
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- fprintf(stderr, "ERROR: failed to create the socket\n");
- ret = -1;
- goto end;
- }
-
- memset(&servAddr, 0, sizeof(servAddr));
-
- servAddr.sin_family = AF_INET;
- servAddr.sin_port = htons(DEFAULT_PORT);
-
- if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr, sizeof(servAddr.sin_addr)) != 1) {
- fprintf(stderr, "ERROR: invalid address\n");
- ret = -1;
- goto end;
- }
-
- if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
- == -1) {
- fprintf(stderr, "ERROR: failed to connect\n");
- goto end;
- }
-
-
-
-
- if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
- fprintf(stderr, "ERROR: Failed to initialize the library\n");
- goto socket_cleanup;
- }
-
- if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
- fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
- ret = -1;
- goto socket_cleanup;
- }
-
- if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CERT_FILE, NULL))
- != SSL_SUCCESS) {
- fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
- CERT_FILE);
- goto ctx_cleanup;
- }
-
- if ((ssl = wolfSSL_new(ctx)) == NULL) {
- fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
- ret = -1;
- goto ctx_cleanup;
- }
-
- if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
- fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
- goto cleanup;
- }
-
- if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) {
- fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
- goto cleanup;
- }
-
- printf("Message for server: ");
- memset(buff, 0, sizeof(buff));
- if (fgets(buff, sizeof(buff), stdin) == NULL) {
- fprintf(stderr, "ERROR: failed to get message for server\n");
- ret = -1;
- goto cleanup;
- }
- len = strnlen(buff, sizeof(buff));
-
- if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
- fprintf(stderr, "ERROR: failed to write entire message\n");
- fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
- goto cleanup;
- }
-
- memset(buff, 0, sizeof(buff));
- if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
- fprintf(stderr, "ERROR: failed to read\n");
- goto cleanup;
- }
-
- printf("Server: %s\n", buff);
-
- cleanup:
- wolfSSL_free(ssl);
- ctx_cleanup:
- wolfSSL_CTX_free(ctx);
- wolfSSL_Cleanup();
- socket_cleanup:
- close(sockfd);
- end:
- return ret;
- }
|