1 (edited by andrea.raiola 2023-09-12 08:22:17)

Topic: Error 173 while using wc_SignatureGetSize

Hi i'm trying to use wolfSSL in my project, i'm build it with CMake.

I need to parse a pem private key to der and then get the signature size to allocate the buffer for the signature, at the end i need to apply this signature on some data.

But i have a problem, while calling the wc_SignatureGetSize method i have this error:

wc_SignatureGetSize: Invalid RsaKey key size

.

I noticed while debugging that the

sizeof(RsaKey)

differs when executed inside the wc_SignatureGetSize method, in particulare i get 8368 but in my main.cpp i get 8432.

Here some snippet of how currently the code works:

#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include "wolfssl/wolfcrypt/types.h"
#include <wolfssl/ssl.h>
#include "wolfssl/wolfcrypt/rsa.h"
#include "wolfssl/wolfcrypt/sha256.h"
#include "wolfssl/wolfcrypt/signature.h"
#include "curl/curl.h"
#include "resources.h"


#define DEBUGGING_RIGHT_NOW 1

const static int RSA_KEY_SIZE{4096};
const static int DER_FILE_BUFFER{4096}; /* max DER size */
const static word32 DER_FILE_BUFFER_SIZE{sizeof(byte) * DER_FILE_BUFFER};

int getFileContentAsByte(const std::string &fileName, unsigned char *buffer, size_t bufferSize, int *charsRead) {
    std::vector<unsigned char> bytes;
    std::ifstream file1(fileName, std::ios_base::in | std::ios_base::binary);
    unsigned char ch = file1.get();

    while (file1.good()) {
        bytes.push_back(ch);
        ch = file1.get();
    }

    bytes.push_back('\0');
    *charsRead = (int) bytes.size();
    XMEMCPY(buffer, bytes.data(), (bytes.size() * sizeof(uint8_t)));
    file1.close();

    return 0;
}

int testWolfSSLSignature() {

    //Variabili per metodi di wolf
    int ret{};

    //Leggo il file pem e ne estraggo i dati
    auto prvPemKey{(uint8_t *) XMALLOC(DER_FILE_BUFFER_SIZE, NULL, DYNAMIC_TYPE_IN_BUFFER)};
    int prvPemKeyLen{};

    //Pulisce l'area di memoria appena allocata
    XMEMSET(prvPemKey, 0, DER_FILE_BUFFER_SIZE);

    getFileContentAsByte(wrapi_resources::WRAPI_KEY_FILE_PATH, prvPemKey, DER_FILE_BUFFER_SIZE, &prvPemKeyLen);

    //Creo il buffer, memorizzo la size e il buffer stesso
    auto derBuffers = (byte *) XMALLOC(DER_FILE_BUFFER_SIZE, NULL, DYNAMIC_TYPE_DER);


#ifdef DEBUGGING_RIGHT_NOW
    std::cout << "PemKeyString: " << prvPemKey << '\n';
    std::cout << "PemKeyLength: " << prvPemKeyLen << '\n';
#endif


    //Pulisce l'area di memoria prima della scrittura
    XMEMSET(derBuffers, 0, DER_FILE_BUFFER_SIZE);

    //Effettuo la conversione PEM -> DER
    word32 writtenBytesInConversionPemToDer = wc_KeyPemToDer(prvPemKey,
                                                             prvPemKeyLen,
                                                             derBuffers,
                                                             DER_FILE_BUFFER_SIZE, NULL);


#ifdef DEBUGGING_RIGHT_NOW
    std::cout << "PEM -> DER Succesfull:\n ";
    //print_buf("DER:", derBuffers, writtenBytesInConversionPemToDer);
    std::cout << "Written bytes in conversion: " << writtenBytesInConversionPemToDer << '\n';
#endif


    RNG rng;
    wc_InitRng(&rng);

    RsaKey rsaPrivateKey;
    ret = wc_InitRsaKey(&rsaPrivateKey, nullptr);
    wc_RsaSetRNG(&rsaPrivateKey, &rng);


    word32 decodingIndex{0};

    ret = wc_RsaPrivateKeyDecode(derBuffers, &decodingIndex, &rsaPrivateKey, writtenBytesInConversionPemToDer);


    enum wc_SignatureType sig_type = WC_SIGNATURE_TYPE_RSA;
    word32 rsaKeySize = sizeof(rsaPrivateKey);

    ret = wc_SignatureGetSize(sig_type, &rsaPrivateKey, rsaKeySize);

/*
        DOESN'T WORK

    std::cout << "Sizeof of my key: " << sizeof(rsaPrivateKey) << '\n';


    word32 sigBufferLen = wc_RsaEncryptSize(&rsaPrivateKey);
    byte *sigBuffer = (byte *) XMALLOC(sigBufferLen, NULL, DYNAMIC_TYPE_SIGNATURE);


    const byte stringToHash[] = "Tue, 12 Sep 2023 10:27:12 GMT";

    wc_SignatureGenerate(WC_HASH_TYPE_SHA256,
                         WC_SIGNATURE_TYPE_RSA,
                         stringToHash,
                         strlen(reinterpret_cast<const char *>(stringToHash)),
                         sigBuffer,
                         &sigBufferLen,
                         &rsaPrivateKey,
                         rsaKeySize,
                         &rng);

*/

    std::cout << "Last ret result: " << ret << '\n';

    //Free memory
    //XFREE(sigBuffer, NULL, DYNAMIC_TYPE_SIGNATURE)
    XFREE(derBuffers, NULL, DYNAMIC_TYPE_DER);
    XFREE(prvPemKey, NULL, DYNAMIC_TYPE_IN_BUFFER);
    wc_FreeRng(&rng);
    wc_FreeRsaKey(&rsaPrivateKey);


    return ret;
}

This is the console log, with wolfssl debug enabled:

PemKeyLength: 1678
wolfSSL Entering wc_KeyPemToDer
wolfSSL Entering PemToDer
PEM -> DER Succesfull:
 Written bytes in conversion: 1192
wolfSSL Entering GetAlgoId
wc_SignatureGetSize: Invalid RsaKey key size
Last ret result: -173
*** stack smashing detected ***: terminated

This is the actual cmake configuration:

ExternalProject_Add(wolfssl
        SOURCE_DIR ${_source}
        BINARY_DIR ${_build}
        CMAKE_CACHE_ARGS
        ${WRAPI-SDK-CPP_DEFAULT_ARGS}
        CMAKE_ARGS
        "-DWOLFSSL_OPENSSLEXTRA=ON"
        "-DWOLFSSL_KEYGEN=ON"
        "-DWOLFSSL_DEBUG=ON"
        "-DWOLFSSL_CERTGEN=ON"
        "-DWOLFSSL_CERTEXT=ON"
)

The key is formatted in this way:

-----BEGIN RSA PRIVATE KEY-----
censored...pMQRUw==
-----END RSA PRIVATE KEY-----

The compiler i'm using is:

-- The C compiler identification is GNU 11.4.0
-- The ASM compiler identification is GNU

And i'm using the

wolfSSL v5.6.3-stable

.

I'm developing currently on Windows10 using the IDE CLion, and building on WSL.

Hope someone can help!

Share

Re: Error 173 while using wc_SignatureGetSize

Hi Andrea,

My name is Anthony Hu and I am a member of the wolfSSL team.

>  in particulare i get 8368 but in my main.cpp i get 8432.

This is a very big hint.  You can see the definition of

struct RsaKey

in

wolfssl/wolfcrypt/rsa.h 

It varies greatly based on what macros are defined. I noticed you have the following in your C file:


#include <wolfssl/options.h>


Can you please make sure it is exactly the same as the wolfssl/options.h  file in your wolfssl source build directory?

If you would like to further discuss this issue, please open a ticket on our Technical Support system by sending a message to support@wolfssl.com

Warm regards, Anthony

Share

Re: Error 173 while using wc_SignatureGetSize

Hi Anthony, thank you for your quick answer!

I managed to resolve the problem switching to autotool configuration, i noticed that CMake jumps some steps, i think related to the compiler options during the configuring phase.

I want to share this stackoverflow post where i managed to solve the problem, hope that can help someone else in the future:
https://stackoverflow.com/questions/597 … from-cmake

If you need some logs to check the problem, i will be glad to help!

Share

Re: Error 173 while using wc_SignatureGetSize

Hello andrea.raiola,

I'm glad to know that you have resolved this on your own.  I have enlisted our cmake expert to have a look at this thread and the stackoverflow link to better understand the issue you are seeing.

Thank you so much for pointing this out to us.

Warm regards, Anthony

Share