Private key generated by openssl does not satisfy

2020-02-05 09:51发布

问题:

I've generated a key with openssl by using the following command

openssl genrsa 1024

I've got the following result

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC2Lh4HLcCR76Wv3oXl6sZ7pv8l9b/66H+I6Bb86tz8RhWCmTCG
xPVgtZ+w9WutU/rqBHHZOpotX4QDksRD8dRFh6a3HwkIFQdTcDoiD39yNP8F02Gd
dAl8p/URC5jNCLMSfUK38wMocmoI1I5vqdMNrzUnOup18rl4089Z+faMKQIDAQAB
AoGBAJrL4z5iWiengxqV8yETBeU8WcJft+n0dapXzHDNAUo8Izr+AIYEBp1Ot4se
f4igu6zuae80JJ45c2u14p/5dWzN9/URmhTP8xLGjGCjltOJLLyhBPP+ZsLjqu6l
57MNV6jDqDLdRC66w4NMRCN3FACxcldIC5L6B9OA7UvO1ugBAkEA8s1o6BvufTeD
ktyOMfW0ZeLGk/6EXp8nf7BIzYhAAPtSt8DegnfYFx2XDgKnYSB15dTDIDMe/KaA
GcuUb/ZjiQJBAMATb1fFdl7PGccVPgeTaupJayHPySc9PSsACV6VIAnpcU/3NZzd
MkdIL/JsOsD+1M9uQJqvRZO4qQdjcR5Om6ECQEJGgYlB/pJdcePHomTOvcRF55CE
G9u8M8rt8qFvvJDICWcxFUulrO16XT4syUWA1825it2iNqYeSL9By63YIokCQBnm
RPw71xM/r8UleyDAYwlGbxi3EPOmkUnsDldfmltby/ixZ9xIA1CTTkvNBjsh4YY6
4qE5AxPBMaGaahVhGiECQHyQvXUNE2IUco+ZwmmLnb2ey1s5tBg7i0lEZG47G7GV
o+yKdJHpToJD8eRJA/2D+pMhQIZm8X/XDIoi1AQ/TRY=
-----END RSA PRIVATE KEY-----

By using http://lapo.it/asn1js/ I've decompiled above pem and found out that

p = 12716592588957205057720227362856602359162165918588008158928004904780617816265357754968000322907349867949577092305558696339499022301055839537975977118688137

q = 10059832080410675679274931119486090017360564066559599189867309386706046720813347145402059918876964980032136051476889631968099168811652389989474036177869729

But in the mean time the modulus

n = 127931248913253271289716500205639992466284651287857358177145251825312810367993147780821624164062592337708505486275588028025122128928225266601591073331803580493341719724935049328478344297205955905466581637169109448199715137939448946445804542355907923908845024638480376219852266194827768486624319018352514599977

When it should equal:

N = 127926786079904340486324750015310657558743663036816604629670248604945247855865212421871439992814824964459404054462886419168595224468110358113599485755243718536935880977456835036272916419632947427885989469252242704342884476060911074859116149906854382812141105735147419775039435625802885168567201322191763704873

Why the hell N is not equal to p * q ?


EDIT:

Here is bunch of keys generated by my openssl, I'm using Mac OS X Mavericks, the first key has correct N, the second one incorrect, I haven't checked the others, but anyway this issue seems to be reproducing, I'm using the following openssl version "OpenSSL 0.9.8y 5 Feb 2013":

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDb4ZTDCASpFV+89RzD8MYR/xASV/piyXB3+qVq+PueDqq+45EL
Xt5UE2jXanfOh3uE3G3Fi76rm08XUC0CvFB7DxFJAMvchr5LbE9x2EMDmfpuZPOI
bDI+r+XZ+KJF++h296TOnncgnXILoD1gihkld7MS9g49TwsBPOtsMJzDRQIDAQAB
AoGBAMpGFfTormjhacZJcqHqIDwz+RP27TbtetJUuNHUpJw9f+PFMwxTxJFxnZP1
tKtAxWdu5GjDaq8xW6vlrNKEbD9jg2WU/myYffNeHxNVjivzypeQzBSmcE5LKFpM
objHkvXbBkkyX8Oh6ZF/3+NOhxOVZMH7CaZ90a+xPf7ENXJBAkEA+Zx/rwrx81+D
s79thCAUU4EnMVROGFPr/V7mt9AFXjAXHRuvg49Aon23bt48jdroakV5vk0ltKO8
Z5xnRWlGNQJBAOGCSOL+zFJd6zTM+CkbgGtml+DIunr7Avfu2mZyaMv4mLo+GTRN
ctZEKhknYfydewhORLKg51WdLThoe9VG6tECQCUyIvzMnr1rkE6qygEUXrMK3fJF
49VQPCxnhe4CO6qLdAZMZ90s7a7JGRqWV2O0KUoczEWFbYb7l0nBTspTI7kCQGmT
L0BmfY3KPoWXuE62e2bMvyoTj4pn3bnpbw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v9bHUTbwKIRbjU78qFkDP7iBpfjOITBcyJUSODWhadhXh15IxrJwJV2O
0aU0Tmfx0PkQEb+CWDfJAkEAzJPemI3yVYqXG2POBolG5gK2VkHEw7QCQOmMK60Z
+3H7U9F/AvoM3zpRujHBdRxdZlPSrCvbxhyavYhNXDqnjg==
-----END RSA PRIVATE KEY-----

回答1:

Why the hell N is not equal to p * q ?

Confirmed with the key provided. However, I could not duplicate when generating my own key with openssl genrsa 1024.

$ ./test-rsa.exe

***** P *****
12716592588957205057720227362856602359162165918588008158928004904780617816265357
754968000322907349867949577092305558696339499022301055839537975977118688137

***** Q *****
10059832080410675679274931119486090017360564066559599189867309386706046720813347
145402059918876964980032136051476889631968099168811652389989474036177869729

***** N *****
12793124891325327128971650020563999246628465128785735817714525182531281036799314
77808216241640625923377085054862755880280251221289282252666015910733318035804933
41719724935049328478344297205955905466581637169109448199715137939448946445804542
355907923908845024638480376219852266194827768486624319018352514599977

*** Calc N ***
12792678607990434048632475001531065755874366303681660462967024860494524785586521
24218714399928148249644594040544628864191685952244681103581135994857552437185369
35880977456835036272916419632947427885989469252242704342884476060911074859116149
906854382812141105735147419775039435625802885168567201322191763704873

$ cat test-rsa.c

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

const char s_key[] = "-----BEGIN RSA PRIVATE KEY-----\n"
"MIICXAIBAAKBgQC2Lh4HLcCR76Wv3oXl6sZ7pv8l9b/66H+I6Bb86tz8RhWCmTCG\n"
"xPVgtZ+w9WutU/rqBHHZOpotX4QDksRD8dRFh6a3HwkIFQdTcDoiD39yNP8F02Gd\n"
"dAl8p/URC5jNCLMSfUK38wMocmoI1I5vqdMNrzUnOup18rl4089Z+faMKQIDAQAB\n"
"AoGBAJrL4z5iWiengxqV8yETBeU8WcJft+n0dapXzHDNAUo8Izr+AIYEBp1Ot4se\n"
"f4igu6zuae80JJ45c2u14p/5dWzN9/URmhTP8xLGjGCjltOJLLyhBPP+ZsLjqu6l\n"
"57MNV6jDqDLdRC66w4NMRCN3FACxcldIC5L6B9OA7UvO1ugBAkEA8s1o6BvufTeD\n"
"ktyOMfW0ZeLGk/6EXp8nf7BIzYhAAPtSt8DegnfYFx2XDgKnYSB15dTDIDMe/KaA\n"
"GcuUb/ZjiQJBAMATb1fFdl7PGccVPgeTaupJayHPySc9PSsACV6VIAnpcU/3NZzd\n"
"MkdIL/JsOsD+1M9uQJqvRZO4qQdjcR5Om6ECQEJGgYlB/pJdcePHomTOvcRF55CE\n"
"G9u8M8rt8qFvvJDICWcxFUulrO16XT4syUWA1825it2iNqYeSL9By63YIokCQBnm\n"
"RPw71xM/r8UleyDAYwlGbxi3EPOmkUnsDldfmltby/ixZ9xIA1CTTkvNBjsh4YY6\n"
"4qE5AxPBMaGaahVhGiECQHyQvXUNE2IUco+ZwmmLnb2ey1s5tBg7i0lEZG47G7GV\n"
"o+yKdJHpToJD8eRJA/2D+pMhQIZm8X/XDIoi1AQ/TRY=\n"
"-----END RSA PRIVATE KEY-----\n";

int main(int argc, char* argv[])
{
    UNUSED(argc), UNUSED(argv);

    int rc;
    FILE* fd = NULL;
    EVP_PKEY* pkey = NULL;
    RSA* rsa = NULL;
    BIGNUM* n = NULL;
    BN_CTX* ctx = NULL;

    fd = fopen("./key.pem", "w+");
    if(fd == NULL) exit(1);

    rc = fwrite(s_key, 1, sizeof(s_key), fd);
    if(rc != sizeof(s_key)) exit(2);

    rc = fseek(fd, 0, SEEK_SET);
    if(rc != 0) exit(3);

    pkey = PEM_read_PrivateKey(fd, NULL, NULL, NULL);
    if(pkey == NULL) exit(4);

    rsa = EVP_PKEY_get1_RSA(pkey);
    if(rsa == NULL) exit(5);

#if 0
    fprintf(stdout, "\n***** RSA *****\n");
    RSA_print_fp(stdout, rsa, 0);
#endif

    fprintf(stdout, "\n***** P *****\n");
    fprintf(stdout, "%s\n", BN_bn2dec(rsa->p));

    fprintf(stdout, "\n***** Q *****\n");
    fprintf(stdout, "%s\n", BN_bn2dec(rsa->q));

    fprintf(stdout, "\n***** N *****\n");
    fprintf(stdout, "%s\n", BN_bn2dec(rsa->n));

    n = BN_new();
    if(!n) exit(6);

    ctx = BN_CTX_new();
    if(ctx == NULL) exit(7);

    rc = BN_mul(n, rsa->p, rsa->q, ctx);
    if(rc != 1) exit(8);

    fprintf(stdout, "\n*** Calc N ***\n");
    fprintf(stdout, "%s\n", BN_bn2dec(n));

    BN_CTX_free(ctx);
    BN_free(n);
    RSA_free(rsa);
    EVP_PKEY_free(pkey);
    fclose(fd);

    return 0;
}


回答2:

Hi I've tested all 6 private keys you provided in EDIT with this command

$ openssl rsa -check -in privkey

and all of them returns RSA key ok. Except the first one, it returns RSA key error: n does not equal p q.

My answer to your question

Why the hell N is not equal to p * q ?

is: there's no way N is not equal to p*q. The first key has probably been modified, or has been changed during transmission, so it doesn't pass the rsa check test. Digging into the openssl source code, we can see that openssl genrsa command is carried out by

int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)

in /crypto/rsa/rsa_gen.c. At ln:289, the modulus n is calculated with

/* calculate n */
if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err;

which means n=p*q. You should look for other possibilities that cause your problem, rather than casting doubt on genrsa command.