在C OpenSSL的命令得到了RSA公​​钥模 [英] OpenSSL command in c to get Modulus of a public Key in the RSA

查看:167
本文介绍了在C OpenSSL的命令得到了RSA公​​钥模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望找到C中的OpenSSL的方法,让我们只使用RSA公钥模量。然而,我们不能确定使用哪种方法。是否d2i_RSAPublicKey功能的工作,但我们不知道它需要什么参数?

什么是以下开放SSL命令C方法只提取模量:

  $ OpenSSL的SSL RSA -inform DER -pubin -text< 12120862.key
公钥:(1024位)
模数:
    00:81:1F:1D:00:7E:D0:C7:E2:2F:31:3D:0D:F0:A8:
    AB:C1:EA:66:BA:AF:1D:A4:EB:B3:FD:51:58:1C:1D:
    81:AE:F0:99:9e中:图5c:26:67:B5:41:14:28:79:C0:29:
    E5:56:96:06:B7:4B:A0:C9:7F:41:46:9A:7E:85:10:
    A0:91:EA:58:BD:78:78:6D:3C:07:2A:3D:61:F3:编辑:
    42:8B:1E:DC:6D:2D:21:41:7A:E8:15:51:0D:75:84:
    是:20:8C:76:43:8B:4B:67:6B:49:09:E9:20:A1:11:
    53:A0:D9:30:B1:C2:27:A6:09:E1:56:36:ED:7E:9B:
    23:E2:DF:5B:BD:C5:66:CA:C5
指数:65537(0x10001)
写RSA密钥----- BEGIN PUBLIC KEY -----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBHx0AftDH4i8xPQ3wqKvB6ma6
rx2k67P9UVgcHYGu8JmeXCZntUEUKHnAKeVWlga3S6DJf0FGmn6FEKCR6li9eHht
PAcqPWHz7UKLHtxtLSFBeugVUQ11hL4gjHZDi0tna0kJ6SChEVOg2TCxwiemCeFW
NU1 + myPi31u9xWbKxQIDAQAB
----- END PUBLIC KEY -----


解决方案

  

什么是以下开放SSL命令C方法只提取模量


简短的回答...给定一个 RSA 结构:

  RSA * RSA = ...;
BIGNUM * N = rsa-将N;

然后,使用 BN_print_fp

  BN_print_fp(标准输出,N);

或者使用 BN_bn2dec

  fprintf中(标准输出,%S,BN_bn2dec(N));

或者使用 ASN1_bn_print

  INT ASN1_bn_print(BIO *基点,为const char *号,常量BIGNUM * NUM,
                  无符号的char * buf中,整数关)

ASN1_bn_print 从下方长的答案,和它给你的格式,你在你的例子显示。


龙答案......我相信关键是印有 RSA_print_fp ,它最终与呼叫 ASN1_bn_print 对于各种RSA参数。这里的步道的一部分:

  $的grep -R RSA_print_fp *
加密/ RSA / rsa.h:INT RSA_print_fp(FILE * FP,常量RSA * R,诠释抵消);
加密/ RSA / rsa_err.c:{ERR_FUNC(RSA_F_RSA_PRINT_FP),RSA_print_fp},
加密/ RSA / rsa_prn.c:整数RSA_print_fp(FILE * FP,常量RSA * X,诠释了)
...

RSA_print_fp

  INT RSA_print_fp(FILE * FP,常量RSA * X,诠释了)
{
    BIO * B;
    INT RET;    如果((B = BIO_new(BIO_s_file()))== NULL)
    {
        RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
        返回(0);
    }
    BIO_set_fp(B,FP,BIO_NOCLOSE);
    RET = RSA_print(B,X,关闭);
    BIO_free(二);
    返回(RET);
}

RSA_print

  INT RSA_print(BIO *基点,常量RSA * X,诠释了)
{
    EVP_PKEY * PK;
    INT RET;
    PK = EVP_PKEY_new();
    如果(!PK ||!EVP_PKEY_set1_RSA(PK,(RSA *)X))
        返回0;
    RET = EVP_PKEY_print_private(BP,PK,关,NULL);
    EVP_PKEY_free(PK);
    返回RET;
}

EVP_PKEY_print_private 加密/执行副总裁/ p_lib.c

  INT EVP_PKEY_print_private(BIO *总分,常量EVP_PKEY * p键,
                           INT缩进,ASN1_PCTX * pctx)
{
    如果(pkey-> ameth和放大器;&安培; pkey-> ameth-> priv_print)
        返回pkey-> ameth-> priv_print(满分,p键,缩进,pctx);    返回unsup_alg(满分,p键,缩进,密码);
}

一旦在办法的的后续 RSA_get_default_method 。相反,找到 priv_print

  $的grep -R priv_print * | grep的-i RSA
加密/ RSA / rsa_ameth.c:静态INT rsa_priv_print(BIO *基点,常量EVP_PKEY * p键,INT缩进,
加密/ RSA / rsa_ameth.c:rsa_priv_print,

rsa_priv_print

 静态INT rsa_priv_print(BIO *基点,常量EVP_PKEY * p键,INT缩进,
                          ASN1_PCTX * CTX)
{
    返回do_rsa_print(BP,pkey-> pkey.rsa,缩进,1);
}

接下来, do_rsa_print

 静态INT do_rsa_print(BIO *基点,常量RSA * X,诠释了,诠释私法)
{
    ...
    海峡=模数;
    S =指数;    如果(ASN1_bn_print(BP,STR,X-GT&;!N,M,关闭))转到ERR;
    如果(ASN1_bn_print(BP,S,X轴和GT;!E,M,关闭))转到ERR;    如果(私法)
    {
        如果(ASN1_bn_print(BPprivateExponent:中,X> D,M关闭))
            转到ERR;
        如果(ASN1_bn_print(BPprime1:中,X指p,男,关闭))
            转到ERR;
        如果(ASN1_bn_print(BPprime2:中,X> Q,M,关闭))
            转到ERR;
        如果(ASN1_bn_print(BPexponent1:中,X> DMP1,男,关闭))
            转到ERR;
        如果(ASN1_bn_print(BPexponent2:中,X> dmq1,男,关闭))
            转到ERR;
        如果(ASN1_bn_print(BP系数:,X-GT&; iqmp,男,关闭))
            转到ERR;
    }
    ...
}

我会留下最后的 ASN1_bn_print 跟踪给读者。它添加了冒号()和换行符( \\ n )。您可以在加密/ ASN 1 / t_pkey.c发现它


下面是你将如何使用 ASN1_bn_print

  RSA * RSA = RSA_new();
...BIO生物* = BIO_new_fp(标准输出,BIO_NOCLOSE);
...INT REQ = BN_num_bytes(rsa-将N)+ 4;
PTR = OPENSSL_malloc(REQ);RC = ASN1_bn_print(生物,模,rsa-> N,PTR,0);
ASSERT(RC == 1);
...

运行的程序像上面会产生:

  $ ./test-openssl.exe
模数:
    00:BB:BB:CF:AC:58:A9:25:2C:08:37:4D:4D:1D:0C:
    5B:7D:A7:BA DE:7B:31:9A:5E:40:61:1F:6D DE:F9:
    B4:48:15:A3:8C:2A:12:A9:10:FB:66:12:A4:3F:9C:
    0D:7F:80:94:B1:63:91:05:96:F0:48:E5:7D:76:8A:
    D0:26:DC:54:43

We wanted to find an OpenSSl method in C that gives us only the modulus from the public key using RSA. However we are not sure which method to use. Does d2i_RSAPublicKey function work, but we are not sure what arguments it takes in? '

What is the c method for the following open ssl command that only extracts the modulus:

$ openssl ssl rsa -inform der -pubin -text < 12120862.key 
Public-Key: (1024 bit)
Modulus:
    00:81:1f:1d:00:7e:d0:c7:e2:2f:31:3d:0d:f0:a8:
    ab:c1:ea:66:ba:af:1d:a4:eb:b3:fd:51:58:1c:1d:
    81:ae:f0:99:9e:5c:26:67:b5:41:14:28:79:c0:29:
    e5:56:96:06:b7:4b:a0:c9:7f:41:46:9a:7e:85:10:
    a0:91:ea:58:bd:78:78:6d:3c:07:2a:3d:61:f3:ed:
    42:8b:1e:dc:6d:2d:21:41:7a:e8:15:51:0d:75:84:
    be:20:8c:76:43:8b:4b:67:6b:49:09:e9:20:a1:11:
    53:a0:d9:30:b1:c2:27:a6:09:e1:56:36:ed:7e:9b:
    23:e2:df:5b:bd:c5:66:ca:c5
Exponent: 65537 (0x10001)
writing RSA key

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBHx0AftDH4i8xPQ3wqKvB6ma6
rx2k67P9UVgcHYGu8JmeXCZntUEUKHnAKeVWlga3S6DJf0FGmn6FEKCR6li9eHht
PAcqPWHz7UKLHtxtLSFBeugVUQ11hL4gjHZDi0tna0kJ6SChEVOg2TCxwiemCeFW
Nu1+myPi31u9xWbKxQIDAQAB
-----END PUBLIC KEY-----

解决方案

What is the c method for the following open ssl command that only extracts the modulus

Short answer... Given an RSA structure:

RSA* rsa = ...;
BIGNUM* n = rsa->n;

Then, use BN_print_fp:

BN_print_fp(stdout, n);

Or, use BN_bn2dec:

fprintf(stdout, "%s", BN_bn2dec(n));

Or, use ASN1_bn_print:

int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
                  unsigned char *buf, int off)

ASN1_bn_print is from the long answer below, and it gives you the formatting you showed in your example.


Long answer... I believe the key is printed with RSA_print_fp, and it ultimately ends with calls to ASN1_bn_print for the various RSA parameters. Here's part of the trail:

$ grep -R RSA_print_fp *
crypto/rsa/rsa.h:int    RSA_print_fp(FILE *fp, const RSA *r,int offset);
crypto/rsa/rsa_err.c:{ERR_FUNC(RSA_F_RSA_PRINT_FP), "RSA_print_fp"},
crypto/rsa/rsa_prn.c:int RSA_print_fp(FILE *fp, const RSA *x, int off)
...

Following RSA_print_fp:

int RSA_print_fp(FILE *fp, const RSA *x, int off)
{
    BIO *b;
    int ret;

    if ((b=BIO_new(BIO_s_file())) == NULL)
    {
        RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
        return(0);
    }
    BIO_set_fp(b,fp,BIO_NOCLOSE);
    ret=RSA_print(b,x,off);
    BIO_free(b);
    return(ret);
}

And RSA_print:

int RSA_print(BIO *bp, const RSA *x, int off)
{
    EVP_PKEY *pk;
    int ret;
    pk = EVP_PKEY_new();
    if (!pk || !EVP_PKEY_set1_RSA(pk, (RSA *)x))
        return 0;
    ret = EVP_PKEY_print_private(bp, pk, off, NULL);
    EVP_PKEY_free(pk);
    return ret;
}

EVP_PKEY_print_private is in crypto/evp/p_lib.c:

int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
                           int indent, ASN1_PCTX *pctx)
{
    if (pkey->ameth && pkey->ameth->priv_print)
        return pkey->ameth->priv_print(out, pkey, indent, pctx);

    return unsup_alg(out, pkey, indent, "Private Key");
}

Once in the "methods", don't follow RSA_get_default_method. Rather, find priv_print:

$ grep -R priv_print * | grep -i RSA
crypto/rsa/rsa_ameth.c:static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
crypto/rsa/rsa_ameth.c:     rsa_priv_print,

And rsa_priv_print:

static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
                          ASN1_PCTX *ctx)
{
    return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
}

Next, do_rsa_print:

static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
{
    ...
    str = "Modulus:";
    s = "Exponent:";

    if (!ASN1_bn_print(bp,str,x->n,m,off)) goto err;
    if (!ASN1_bn_print(bp,s,x->e,m,off)) goto err;

    if (priv)
    {
        if (!ASN1_bn_print(bp,"privateExponent:",x->d,m,off))
            goto err;
        if (!ASN1_bn_print(bp,"prime1:",x->p,m,off))
            goto err;
        if (!ASN1_bn_print(bp,"prime2:",x->q,m,off))
            goto err;
        if (!ASN1_bn_print(bp,"exponent1:",x->dmp1,m,off))
            goto err;
        if (!ASN1_bn_print(bp,"exponent2:",x->dmq1,m,off))
            goto err;
        if (!ASN1_bn_print(bp,"coefficient:",x->iqmp,m,off))
            goto err;
    }
    ...
}

I'll leave the final ASN1_bn_print trace to the reader. It adds the colons (:) and line breaks (\n). You can find it in crypto/asn1/t_pkey.c.


Here's how you would use ASN1_bn_print:

RSA* rsa = RSA_new();
...

BIO* bio = BIO_new_fp(stdout, BIO_NOCLOSE);
...

int req = BN_num_bytes(rsa->n) + 4;
ptr = OPENSSL_malloc(req);

rc = ASN1_bn_print(bio, "Modulus:", rsa->n, ptr, 0);
ASSERT(rc == 1);
...

Running a program like above will produce:

$ ./test-openssl.exe
Modulus:
    00:bb:bb:cf:ac:58:a9:25:2c:08:37:4d:4d:1d:0c:
    5b:7d:a7:ba:de:7b:31:9a:5e:40:61:1f:6d:de:f9:
    b4:48:15:a3:8c:2a:12:a9:10:fb:66:12:a4:3f:9c:
    0d:7f:80:94:b1:63:91:05:96:f0:48:e5:7d:76:8a:
    d0:26:dc:54:43

这篇关于在C OpenSSL的命令得到了RSA公​​钥模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆