简体   繁体   中英

How to fetch RSA public key modulus and exponent in C/C++?

I'm using the BCrypt Windows library to handle the RSA algorithm in my application.

Problem: I need to fetch the RSA public key modulus and exponent .

In C# language, I was using the RSACryptoProvider class to fetch these informations (ExportParameters method).

In my C/C++ application, BCrypt seems to be unable to fetch the right data...

What I've done so far:

BOOL RSA_New(RSA_CTX_ST* rsa_ctx, const BYTE key_data[256], DWORD key_len, BOOL cipher)
{
    if (rsa_ctx == NULL || key_len != RSA_2048_BLOCK_SIZE)
    {
        return FALSE;
    }

    NTSTATUS status = STATUS_UNSUCCESSFUL;
    BCRYPT_ALG_HANDLE* algo_handle = NULL;
    BCRYPT_KEY_HANDLE* key_handle = NULL;

    algo_handle = malloc(sizeof(BCRYPT_ALG_HANDLE));
    key_handle = malloc(sizeof(BCRYPT_KEY_HANDLE));

    if (algo_handle == NULL || key_handle == NULL) goto END;

    //Creation handle Algo
    if (!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(algo_handle, BCRYPT_RSA_ALGORITHM, NULL, 0)))
    {
        UTL_Trace("SEC", VRB_MAJOR, "RSA:Algorithm handle opening error");
        if (status == STATUS_INVALID_HANDLE)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID HANDLE");
        }
        if (status == STATUS_INVALID_PARAMETER)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID PARAMS");
        }
        goto END;
    }

    // Key pair generation
    if (!NT_SUCCESS(status = BCryptGenerateKeyPair(algo_handle, key_handle, 2048, 0)))
    {
        UTL_Trace("SEC", VRB_MAJOR, "RSA:Key pair generating error");
        if (status == STATUS_INVALID_HANDLE)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID HANDLE");
        }
        if (status == STATUS_INVALID_PARAMETER)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID PARAMS");
        }
        goto END;
    }

    BCRYPT_RSAKEY_BLOB my_rsa_blob;
    ULONG* pcbResult = NULL;
    pcbResult = calloc(1, sizeof(ULONG));
    if (pcbResult == NULL)
    {
        UTL_Trace("SEC", VRB_INFO, "Allocating RSA result error");
        goto END;
    }

    // Export parameters of keys
    if (!NT_SUCCESS(status = BCryptExportKey(key_handle, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, &my_rsa_blob, sizeof(my_rsa_blob), pcbResult, 0)))
    {
        UTL_Trace("SEC", VRB_MAJOR, "RSA:Key pair exporting error");
        if (status == STATUS_INVALID_HANDLE)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID HANDLE");
        }
        if (status == STATUS_INVALID_PARAMETER)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID PARAMS");
        }
        goto END;
    }
    return TRUE;

END:
    if (algo_handle != NULL) free(algo_handle);
    if (key_handle != NULL) free(key_handle);

    return FALSE;
}

In my_rsa_blob, I should have the size of the modulus and exponent but not their values...

Does anyone have a solution to this problem?

In fact, I did find a solution !

With the call of BCryptExportKey , the variable my_rsa_blob should have been a PUCHAR variable, which is a pointer to a string.

With this, I found a link in Microsoft Docs while searching... The link is showing how the PUCHAR variable is designed:

https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wcce/540b7b8b-2232-45c8-9d7c-af7a5d5218ed

After, you can try to manipulate data like this:

static BOOL RSA_RecoverKeyInformation(RSA_CTX_ST* rsa_ctx, PUCHAR ptrKey)
{
    strncpy(rsa_ctx->key_information.header.Magic, ptrKey[0], 4);
    strncpy(rsa_ctx->key_information.header.BitLength, ptrKey[1 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbPublicExp, ptrKey[2 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbModulus, ptrKey[3 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbPrime1, ptrKey[4 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbPrime2, ptrKey[5 * 4], 4);

    size_t sizeOfModulus = rsa_ctx->key_information.header.cbModulus;
    size_t sizeOfExponent = rsa_ctx->key_information.header.cbPublicExp;

    rsa_ctx->key_information.keyExponent = malloc(sizeOfExponent);
    rsa_ctx->key_information.keyModulus = malloc(sizeOfModulus);
    if (rsa_ctx->key_information.keyExponent == NULL || rsa_ctx->key_information.keyModulus == NULL) goto END;

    strncpy(rsa_ctx->key_information.keyExponent, ptrKey[6 * 4], sizeOfExponent);
    strncpy(rsa_ctx->key_information.keyModulus, ptrKey[6 * 4 + sizeOfExponent], sizeOfModulus);

    return TRUE;

END:
    if (rsa_ctx->key_information.keyExponent != NULL) free(rsa_ctx->key_information.keyExponent);
    if (rsa_ctx->key_information.keyModulus != NULL) free(rsa_ctx->key_information.keyModulus);
    return FALSE;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM