AttestationResult ConvertJwkToRsaPubKey()

in client-library/src/Attestation/AttestationClient/lib/AttestationLibUtils.cpp [581:641]


    AttestationResult ConvertJwkToRsaPubKey(BIO* pkey_bio,
                                            const std::string& n,
                                            const std::string& e) {
        AttestationResult result(AttestationResult::ErrorCode::SUCCESS);
        if (pkey_bio == NULL ||
            n.empty() ||
            e.empty()) {
            return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_INVALID_INPUT_PARAMETER,
                                        "Invalid input parameter");
        }
        EVP_PKEY_CTX* genctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
        EVP_PKEY* pkey = NULL;
        try {
            auto n_bin = base64::base64url_to_binary(n);
            auto e_bin = base64::base64url_to_binary(e);
            const BIGNUM* modul = BN_bin2bn(n_bin.data(), n_bin.size(), NULL);
            const BIGNUM* expon = BN_bin2bn(e_bin.data(), e_bin.size(), NULL);
            OSSL_PARAM rsa_keygen_params[3] = {
                { OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER, n_bin.data(), BN_num_bytes(modul),  NULL},
                { OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, e_bin.data(), BN_num_bytes(expon), NULL},
                OSSL_PARAM_END
            };

            if (OSSL_PARAM_set_BN(&rsa_keygen_params[0], modul) <= 0) {
                EVP_PKEY_CTX_free(genctx);
                return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_CONVERTING_JWK_TO_RSA_PUB,
                    "OSSL_PARAM_set_BN failed");
            };

            if (OSSL_PARAM_set_BN(&rsa_keygen_params[1], expon) <= 0) {
                EVP_PKEY_CTX_free(genctx);
                return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_CONVERTING_JWK_TO_RSA_PUB,
                    "OSSL_PARAM_set_BN failed");
            }

            if (EVP_PKEY_fromdata_init(genctx) <= 0) {
                EVP_PKEY_CTX_free(genctx);
                return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_CONVERTING_JWK_TO_RSA_PUB,
                    "EVP_PKEY_fromdata_init failed");
            }

            if (EVP_PKEY_fromdata(genctx, &pkey, EVP_PKEY_PUBLIC_KEY, rsa_keygen_params) <= 0) {
                EVP_PKEY_CTX_free(genctx);
                return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_CONVERTING_JWK_TO_RSA_PUB,
                    "EVP_PKEY_fromdata failed");
            }

            if (PEM_write_bio_PUBKEY(pkey_bio, pkey) <= 0) {
                EVP_PKEY_CTX_free(genctx);
                return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_CONVERTING_JWK_TO_RSA_PUB,
                    "PEM_write_bio_PUBKEY failed");
            }
        }
        catch (...) {
            result = LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_CONVERTING_JWK_TO_RSA_PUB,
                                        "Error while converting JWK to RSA public key");
        }

        EVP_PKEY_CTX_free(genctx);
        return result;
    }