Topic: [SOLVED] wolfSSL_new "Bad function argument" error.

I'm creating an application for the Nintendo Wii and i'm trying to write a simple code to establish and HTTPS connection and retrieve data from a website, however, when i call the wolfSSL_new function, it doesn't work, wolfSSL_get_error returns me -173 error code, which means "Bad function argument".

This is the code i'm using:

WOLFSSL_CTX* ctx = NULL;
WOLFSSL* sslSock = NULL;

wolfSSL_Init();

ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
    
if ( ctx == NULL ) 
{
        printf("ERRO: WOLFSSL_CTX\n");
    return("SSLERR");
}
    
sslSock = wolfSSL_new(ctx);
    
if ( sslSock == NULL ) 
{
    int error = wolfSSL_get_error(sslSock, 0);
    char *errStr="";
    wolfSSL_ERR_error_string(error, errStr);
        printf("ERRO: wolfSSL_new: %d , %s\n", error, errStr);
    wolfSSL_CTX_free(ctx);
    wolfSSL_Cleanup();
        return("SSLERR");
}

I appreciate any help.

Share

Re: [SOLVED] wolfSSL_new "Bad function argument" error.

Hi @Deltree,

When configuring the wolfSSL library we have to make sure the application includes the same configuration settings. I think you may just be missing a header include for either wolfssl/wolfcrypt/settings.h or wolfssl/options.h. NOTE: wolfssl/options.h is generated when you use ./configure && make to build the library. If you are using some other means to configure the library you should make sure that your settings get included in wolfssl/wolfcrypt/settings.h and then make sure to include wolfssl/wolfcrypt/settings.h in your application.

Your code actually looks fine in fact I tested it successfully, here is a the quick app I threw together using our example from https://github.com/wolfSSL/wolfssl-exam … ient-tls.c and marrying it with your code, it runs fine when tested with our example server

SERVER COMMAND:

cd wolfssl/
./examples/server/server -d

CLIENT CODE:

#include <stdio.h>

#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/random.h>
#define SSLERR -1

int main(int argc, char** argv)
{

/* WOLFSSL SUPPORT CODE: START */
    int                sockfd;
    struct sockaddr_in servAddr;
    char               buff[256];
    size_t             len;
    #define DEFAULT_PORT 11111

    /* Check for proper calling convention */
    if (argc != 2) {
        printf("usage: %s <IPv4 address>\n", argv[0]);
        return 0;
    }

    /* Create a socket that uses an internet IPv4 address,
     * Sets the socket to be stream based (TCP),
     * 0 means choose the default protocol. */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "ERROR: failed to create the socket\n");
        return -1;
    }

    /* Initialize the server address struct with zeros */
    memset(&servAddr, 0, sizeof(servAddr));

    /* Fill in the server address */
    servAddr.sin_family = AF_INET;             /* using IPv4      */
    servAddr.sin_port   = htons(DEFAULT_PORT); /* on DEFAULT_PORT */

    /* Get the server IPv4 address from the command line call */
    if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) {
        fprintf(stderr, "ERROR: invalid address\n");
        return -1;
    }



    /* Connect to the server */
    if (connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr))
        == -1) {
        fprintf(stderr, "ERROR: failed to connect\n");
        return -1;
    }
/* WOLFSSL SUPPORT CODE: BREAK */

/* USER CODE (@Deltree): START */
    WOLFSSL_CTX* ctx = NULL;
    WOLFSSL* sslSock = NULL;

    wolfSSL_Init();

    ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
    wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);

    if ( ctx == NULL ) 
    {
            printf("ERRO: WOLFSSL_CTX\n");
        return(SSLERR);
    }

    sslSock = wolfSSL_new(ctx);

    if ( sslSock == NULL ) 
    {
        int error = wolfSSL_get_error(sslSock, 0);
        char *errStr="";
        wolfSSL_ERR_error_string((unsigned long) error, errStr);
            printf("ERRO: wolfSSL_new: %d , %s\n", error, errStr);
        wolfSSL_CTX_free(ctx);
        wolfSSL_Cleanup();
            return(SSLERR);
    }
/* USER CODE (@Deltree): END */

/* WOLFSSL SUPPORT CODE: RESUME */
    /* Attach wolfSSL to the socket */
    wolfSSL_set_fd(sslSock, sockfd);

    /* Connect to wolfSSL on the server side */
    if (wolfSSL_connect(sslSock) != SSL_SUCCESS) {
        fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
        return -1;
    }

    /* Get a message for the server from stdin */
    printf("Message for server: ");
    memset(buff, 0, sizeof(buff));
    fgets(buff, sizeof(buff), stdin);
    len = strnlen(buff, sizeof(buff));

    /* Send the message to the server */
    if (wolfSSL_write(sslSock, buff, len) != len) {
        fprintf(stderr, "ERROR: failed to write\n");
        return -1;
    }

    /* Read the server data into our buff array */
    memset(buff, 0, sizeof(buff));
    if (wolfSSL_read(sslSock, buff, sizeof(buff)-1) == -1) {
        fprintf(stderr, "ERROR: failed to read\n");
        return -1;
    }

    /* Print to stdout any data the server sends */
    printf("Server: %s\n", buff);

    /* Cleanup and return */
    wolfSSL_free(sslSock);      /* Free the wolfSSL object                  */
    wolfSSL_CTX_free(ctx);  /* Free the wolfSSL context object          */
    wolfSSL_Cleanup();      /* Cleanup the wolfSSL environment          */
    close(sockfd);          /* Close the connection to the server       */
/* WOLFSSL SUPPORT CODE: END */
    return 0;
}


RESULT:

kalebhimes$ ./run 127.0.0.1
Message for server: yo
Server: I hear you fa shizzle!

Regards,

K

Re: [SOLVED] wolfSSL_new "Bad function argument" error.

I have the headers you mentioned included, and even modified a line in settings.h, but all remains the same.

/* Uncomment next line if building wolfSSL for a game console */
/* #define WOLFSSL_GAME_BUILD*/

became

/* Uncomment next line if building wolfSSL for a game console */
#define WOLFSSL_GAME_BUILD

This doesn't seem to change anything, as i still get the same error, maybe there is something wrong with the commands i used to build the library, i found them in a 2016 post in devkitpro forums.

./configure --disable-shared CC=/opt/devkitpro/devkitPPC/bin/powerpc-eabi-gcc --host=ppc --enable-singlethreaded RANLIB=/opt/devkitpro/devkitPPC/bin/powerpc-eabi-ranlib CFLAGS="-DDEVKITPRO -DNO_WRITEV"

make src/libwolfssl.la

In the makefile everything looks fine and the compiler gives no errors.

CFLAGS    = -g -O2 -Wall $(MACHDEP) $(INCLUDE) -I/home/deltree/Downloads/Wii/DEV/wolfssl-4.0.0-stable/
LIBS    :=    /home/deltree/Downloads/Wii/DEV/wolfssl-4.0.0-stable/src/.libs/libwolfssl.a -lfat -lwiiuse -lbte -logc -lm 

Share

Re: [SOLVED] wolfSSL_new "Bad function argument" error.

Not sure if it will fix anything but could you change the makefile to this (it's more standard solution):

CFLAGS    = -g -O2 -Wall $(MACHDEP) $(INCLUDE) -I/home/deltree/Downloads/Wii/DEV/wolfssl-4.0.0-stable/
LIBS    :=    -L/home/deltree/Downloads/Wii/DEV/wolfssl-4.0.0-stable/src/.libs -lwolfssl -lfat -lwiiuse -lbte -logc -lm 

Can you turn on debugging with #define DEBUG_WOLFSSL  and then invoke wolfSSL_Debugging_ON(); in your application.

Regards,

K

Re: [SOLVED] wolfSSL_new "Bad function argument" error.

I turned debugging on, this is the output:

wolfSSL Entering SSL_new
RNG Init error
CTX ref count ot 0 yet, no free
wolfSSL Leaving SSL_new, return -199

I had forgotten to include random.h, but including it changes nothing.

Share

Re: [SOLVED] wolfSSL_new "Bad function argument" error.

Hi @Deltree,

It appears there might be some issue with the entropy source on the device. Can you review the porting guide section here:

https://www.wolfssl.com/docs/porting-gu … 2ipxp7p1y3

You may need to setup an alternate entropy source if the default one is failing to return random data.

Warm Regards,

K

Re: [SOLVED] wolfSSL_new "Bad function argument" error.

Thanks for the help! The problem was solved by creating a custom random seed generator, now everything is running fine.

Share