123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <wolfssl/options.h>
- #include <wolfssl/ssl.h>
- static int convertDerToPem(int type, char* file, unsigned char **out)
- {
- int derSz, pemSz;
- unsigned char der[4096];
- unsigned char *pem;
- FILE* f;
- f = fopen(file, "rb");
- if (f == NULL) {
- fprintf(stderr, "unable to open cert file %s\n", file);
- return -1;
- }
- derSz = fread(der, 1, sizeof(der), f);
- fclose(f);
- pemSz = wc_DerToPemEx(der, derSz, NULL, 0, NULL, type);
- if (pemSz <= 0) {
- fprintf(stderr, "issue getting pem size needed\n");
- return -1;
- }
- pem = (unsigned char*)malloc(pemSz);
- if (pem == NULL) {
- fprintf(stderr, "issue malloc'ing pem size needed\n");
- return -1;
- }
- pemSz = wc_DerToPemEx(der, derSz, pem, pemSz, NULL, type);
- if (pemSz <= 0) {
- fprintf(stderr, "issue %d converting der to pem\n", pemSz);
- free(pem);
- return -1;
- }
- *out = pem;
- return pemSz;
- }
- int main(int argc, char** argv)
- {
- int sockfd;
- struct sockaddr_in servAddr;
- char buff[256];
- size_t len;
- int ret;
- int port;
- int pemSz;
- unsigned char *pem;
- FILE* f;
-
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
-
- if (argc != 6) {
- printf("usage: %s <port> <IPv4 address> <CA PEM> <cert DER> <key DER>\n",
- argv[0]);
- return 0;
- }
- port = atoi(argv[1]);
-
- 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(port);
-
- if (inet_pton(AF_INET, argv[2], &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(wolfSSLv23_client_method())) == NULL) {
- fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
- ret = -1;
- goto socket_cleanup;
- }
-
- pemSz = convertDerToPem(CERT_TYPE, argv[4], &pem);
- if (pemSz <= 0) {
- fprintf(stderr, "ERROR: converting DER cert to PEM\n");
- ret = -1;
- goto socket_cleanup;
- }
- if (wolfSSL_CTX_use_certificate_buffer(ctx, pem, pemSz,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- fprintf(stderr, "issue loading in pem cert\n");
- ret = -1;
- free(pem);
- goto socket_cleanup;
- }
- free(pem);
-
- pemSz = convertDerToPem(PRIVATEKEY_TYPE, argv[5], &pem);
- if (pemSz <= 0) {
- fprintf(stderr, "ERROR: converting DER key to PEM\n");
- ret = -1;
- goto socket_cleanup;
- }
- if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, pem, pemSz,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- fprintf(stderr, "issue loading in pem key\n");
- ret = -1;
- free(pem);
- goto socket_cleanup;
- }
- free(pem);
-
- f = fopen(argv[3], "rb");
- if (f == NULL) {
- fprintf(stderr, "unable to open %s\n", argv[3]);
- ret = -1;
- goto socket_cleanup;
- }
- fseek(f, 0, SEEK_END);
- pemSz = ftell(f);
- rewind(f);
- pem = malloc(pemSz);
- if (pem == NULL) {
- fclose(f);
- ret = -1;
- goto socket_cleanup;
- }
- pemSz = fread(pem, 1, pemSz, f);
- fclose(f);
- ret = wolfSSL_CTX_load_verify_buffer(ctx, pem, pemSz, WOLFSSL_FILETYPE_PEM);
- if (ret != SSL_SUCCESS) {
- fprintf(stderr, "ERROR %d: failed to load %s, please check the file.\n",
- ret, argv[3]);
- free(pem);
- goto ctx_cleanup;
- }
- free(pem);
-
- 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;
- }
|