在OpenSSL中验证RSA公钥

Verify a RSA public key in OpenSSL?

本文关键字:RSA 公钥 验证 OpenSSL      更新时间:2023-10-16

我有一个EVP_PKEY,其中只有RSA密钥的公共部分。我从DER编码的SubjectPublicKeyInfo结构中提取了公共部分。这是我现在的代码:

unsigned char publicKey[] = {0x30, 0x5a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, ...}
size_t publicKeyLength = 92;
unsigned char* publicKeyCopy = new unsigned char[publicKeyLength];
memcpy(publicKeyCopy, publicKey, publicKeyLength);
RSA *rsa;
rsa = d2i_RSA_PUBKEY(NULL, (unsigned char const **) &pubKey, pubKeyLen);
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, rsa);

我知道你可以使用RSA_check_key来验证RSA私钥,但是文档说"它不适用于只有模数和公共指数元素填充的RSA公钥"。

那么,有没有可能在没有私钥部分的情况下验证密钥呢?因为您可以看到,我只有 EVP_PKEY的公共部分。我想知道,这可能吗?你会在EVP_PKEY的公开部分验证什么?

您可以看到这个问题的答案,通过编程验证X509证书和私钥匹配,但在那里验证了完整的密钥(私有和公共部分)。

小心在这个问题中发布的原始代码有一个BUG。这是因为内部d2i_RSA_PUBKEY使用d2i_PUBKEY, d2i_PUBKEY使用d2i_X509_PUBKEY(在x_pubkey.c中)。如果您阅读了d2i_X509的文档,您将看到下一个"警告:使用临时变量是强制性的。一个常见的错误是试图直接使用缓冲区…"。因此,更正后的代码将不得不使用publicKeyCopy的临时副本,在使用之后,您可以安全地删除publicKeyCopy:

小心 在这个问题中发布的原始代码有一个BUG…

我将对此进行评论,并向您展示如何处理它。

unsigned char publicKey[] = {0x30, 0x5a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, ...}
size_t publicKeyLength = sizeof(publicKey);
unsigned char* t = publicKey;
rsa = d2i_RSA_PUBKEY(NULL, &t, pubKeyLen);

在内部,临时指针t是递增的,所以它被浪费了。如果一切正常,它将指向缓冲区之后的某个位置。在函数执行后,您应该找到的是(size_t)t - (size_t)publicKey == publicKeyLength

因为你使用了一个临时指针,原来的指针publicKey仍然是好的。如果内存中有连续的键,可以使用t来解析下一个键。

没有必要复制数据。


我认为第二个选择是使用内存BIOd2i_RSA_PUBKEY_bio。比如:

BIO* bio = BIO_new_mem_buf(publicKey, (int)publicKeyLength);
ASSERT(bio != NULL);
RSA* rsa = d2i_RSA_PUBKEY_bio(bio, NULL);
ASSERT(rsa != NULL);
/* ... */
RSA_free(rsa);
BIO_free(bio);

get1增加了引用计数,因此您需要在EVP_PKEY*RSA*上同时调用free

在@jww的帮助下回答https://stackoverflow.com/a/29885771/2692914。我想出了这个解决方案,我希望它是ok的:

bool isValidPublicKeyOnly(EVP_PKEY *pkey) {
    //EVP_PKEY_get_type from https://stackoverflow.com/a/29885771/2692914
    int type = EVP_PKEY_get_type(pkey); //checks nullptr
    if (type != EVP_PKEY_RSA && type != EVP_PKEY_RSA2) {
        //not RSA
        return false;
    }
    RSA *rsa = EVP_PKEY_get1_RSA(pkey);
    if (!rsa) {
        return false;
    }
    bool isValid = isValidRSAPublicKeyOnly(rsa);
    RSA_free(rsa);
    return isValid;
}
bool isValidRSAPublicKeyOnly(RSA *rsa) {
    //from rsa_ameth.c do_rsa_print : has a private key
    //from rsa_chk.c RSA_check_key : doesn't have n (modulus) and e (public exponent)
    if (!rsa || rsa->d || !rsa->n || !rsa->e) {
        return false;
    }
    //from http://rt.openssl.org/Ticket/Display.html?user=guest&pass=guest&id=1454
    //doesnt have a valid public exponent
    return BN_is_odd(rsa->e) && !BN_is_one(rsa->e);
}

我有一个类似的问题,我认为它可能是谨慎的显示我的解决方案,这个问题。与lmiguelmh的解决方案不同,这个解决方案在c中有效。

int checkRsaPublic(RSA *rsa, int debug) {
    if (!rsa) {
        printf("ERROR: RSA key not defined!n");
        return 0;
    }
    //key
    const BIGNUM *n;
    const BIGNUM *e;
    const BIGNUM *d;
    //factors
    const BIGNUM *p;
    const BIGNUM *q;
    //crt_params
    const BIGNUM *dmp1;
    const BIGNUM *dmq1;
    const BIGNUM *iqmp;
    RSA_get0_key(rsa, &n, &e, &d);
    RSA_get0_factors(rsa, &p, &q);
    RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
    if (debug) {
        if (n) {
            printf("n is %sn", BN_bn2hex(n));
        }
        if (e) {
            printf("e is %sn", BN_bn2hex(e));
        }
        if (d) {
            printf("d is %sn", BN_bn2hex(d));
        }
        if (p) {
            printf("p is %sn", BN_bn2hex(p));
        }
        if (q) {
            printf("q is %sn", BN_bn2hex(q));
        }
        if (dmp1) {
            printf("dmp1 is %sn", BN_bn2hex(dmp1));
        }
        if (dmq1) {
            printf("dmq1 is %sn", BN_bn2hex(dmq1));
        }
        if (iqmp) {
            printf("iqmp is %sn", BN_bn2hex(iqmp));
        }
    }
    //RSA_check_key : doesn't have n (modulus) and e (public exponent)
    if (d || !n || !e) {
        printf("ERROR: RSA public key not well defined!n");
        return 0;
    }
    if (BN_is_odd(e) && !BN_is_one(e)) {
        return 1;
    }
    printf("ERROR: Invalid public exponent.");
    return 0;
}