开发者

How to generate RSA private key using OpenSSL?

开发者 https://www.devze.com 2023-03-03 22:22 出处:网络
开发者_开发技巧I want to know how to generate RSA private key using OpenSSL library in my C source file. I know how to generate it using terminal command.
开发者_开发技巧

I want to know how to generate RSA private key using OpenSSL library in my C source file. I know how to generate it using terminal command.

Actually my server.c file will generate a private key and send to client.c Please help me with some source code if possible, otherwise any help will be appreciated.

I'm working on Linux machine.


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

const int kBits = 1024;
const int kExp = 3;

int keylen;
char *pem_key;

RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0);

/* To get the C-string PEM form: */
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);

keylen = BIO_pending(bio);
pem_key = calloc(keylen+1, 1); /* Null-terminate */
BIO_read(bio, pem_key, keylen);

printf("%s", pem_key);

BIO_free_all(bio);
RSA_free(rsa);
free(pem_key);


I want to know how to generate RSA private key using openssl library in my c source file?

Generating the key is easy. Just use RSA_generate_key_ex. The program below shows you how to do it.

Saving the public and private key is a different matter because you need to know the format. The program below shows you how to do it in a number of formats.


Here are the various functions and formats.

Related, see What is the differences between “BEGIN RSA PRIVATE KEY” and “BEGIN PRIVATE KEY”. It dicusses the difference between SubjectPublicKeyInfo, PrivateKeyInfo, and the public and private keys.

PEM_write_bio_RSAPublicKey (PKCS PEM format). Notice BEGIN RSA PUBLIC KEY:

$ cat rsa-public-1.pem
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAmkD+kwlbsmvyUYOBuVkyWDyBNxGQNJrKUZu97Z+hBG3OA85fY6oV
CdyoT+KDmtAFgTgY76KRkHVYQH3HYFpec5vNptLJNd6OVJCXR6NFUR2b7aHLkres
qyPMrAHNU16wp85qdHALNd72EoUPkVItHqaBOldx7J9vyUF3bpAWq4XxRlbiK9iv
Yuci4/jr4ppRoso6LZm9Inzm53zYt2AqHUZQ+3WgK+czZyk8tLGEN7rtLZ7opvVA
aAiRGWiUWACtRwmpAq2L5PuWZkYdQ7WQigkx5c3x8iBkeEh022zqYkGOTh2Ue4Uf
rIYuSqjLb2d+jkfZQ1S4LQLQPtMxNV/FqwIDAQAB
-----END RSA PUBLIC KEY-----

PEM_write_bio_PUBKEY (Traditional PEM format). Notice BEGIN PUBLIC KEY:

$ cat rsa-public-2.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmkD+kwlbsmvyUYOBuVky
WDyBNxGQNJrKUZu97Z+hBG3OA85fY6oVCdyoT+KDmtAFgTgY76KRkHVYQH3HYFpe
c5vNptLJNd6OVJCXR6NFUR2b7aHLkresqyPMrAHNU16wp85qdHALNd72EoUPkVIt
HqaBOldx7J9vyUF3bpAWq4XxRlbiK9ivYuci4/jr4ppRoso6LZm9Inzm53zYt2Aq
HUZQ+3WgK+czZyk8tLGEN7rtLZ7opvVAaAiRGWiUWACtRwmpAq2L5PuWZkYdQ7WQ
igkx5c3x8iBkeEh022zqYkGOTh2Ue4UfrIYuSqjLb2d+jkfZQ1S4LQLQPtMxNV/F
qwIDAQAB
-----END PUBLIC KEY-----

PEM_write_bio_PrivateKey (PEM). Notice BEGIN PRIVATE KEY:

$ cat rsa-private-1.pem
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaQP6TCVuya/JR
g4G5WTJYPIE3EZA0mspRm73tn6EEbc4Dzl9jqhUJ3KhP4oOa0AWBOBjvopGQdVhA
fcdgWl5zm82m0sk13o5UkJdHo0VRHZvtocuSt6yrI8ysAc1TXrCnzmp0cAs13vYS
hQ+RUi0epoE6V3Hsn2/JQXdukBarhfFGVuIr2K9i5yLj+OvimlGiyjotmb0ifObn
fNi3YCodRlD7daAr5zNnKTy0sYQ3uu0tnuim9UBoCJEZaJRYAK1HCakCrYvk+5Zm
Rh1DtZCKCTHlzfHyIGR4SHTbbOpiQY5OHZ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DJpXBX8eDt/8DyYHsK77WtHd8WD1qR
ketG/NpFLteIV702MtwPLYH92T4CbG1hmSxoCHzCtzeSNy2t6wNA6YvYfX4xt/ea
vIV5WeeTxEK2+3lzPYFD4O0CgYA+KGJsXM77I5bgowfFh1JnKpBLmBEy3B5h91E8
1jhjGSIptAI0qQ81hG4TgTrLNIA6d63jZZWlSpqjZndZsVmpHH+/IDjsXGIjuXs6
GF6WF6Tm9Txva7RAERY5oP+LIw1odum3qHfyw/D/IFjBPEhNworiJNk/JQgMt/G1
07j/AQKBgHF2AoovqkCVx0TyggVkwBxwrQvR4jmUYmLKYWYc+IkDQzESXIUme61b
OZ7OfbTKQHBTz7i8bXK5mWSbNgmhcVsKHnbPyXKIrL7p+j2b2gdSNAaP1c5XFI/l
BvQSuumzUXbzQHbsYjn/4phOv3AK1xGLhnVI69QpaCYYuuPyfm1X
-----END RSA PRIVATE KEY-----

i2d_RSAPublicKey_bio (ASN.1/DER):

$ dumpasn1 rsa-public.der 
  0 266: SEQUENCE {
  4 257:   INTEGER
       :     00 9A 40 FE 93 09 5B B2 6B F2 51 83 81 B9 59 32
       :     58 3C 81 37 11 90 34 9A CA 51 9B BD ED 9F A1 04
       :     6D CE 03 CE 5F 63 AA 15 09 DC A8 4F E2 83 9A D0
       :     05 81 38 18 EF A2 91 90 75 58 40 7D C7 60 5A 5E
       :     73 9B CD A6 D2 C9 35 DE 8E 54 90 97 47 A3 45 51
       :     1D 9B ED A1 CB 92 B7 AC AB 23 CC AC 01 CD 53 5E
       :     B0 A7 CE 6A 74 70 0B 35 DE F6 12 85 0F 91 52 2D
       :     1E A6 81 3A 57 71 EC 9F 6F C9 41 77 6E 90 16 AB
       :             [ Another 129 bytes skipped ]
265   3:   INTEGER 65537
       :   }

0 warnings, 0 errors.

i2d_RSAPrivateKey_bio (ASN.1/DER):

$ dumpasn1 rsa-private.der 
   0 1187: SEQUENCE {
   4    1:   INTEGER 0
   7  257:   INTEGER
         :     00 9A 40 FE 93 09 5B B2 6B F2 51 83 81 B9 59 32
         :     58 3C 81 37 11 90 34 9A CA 51 9B BD ED 9F A1 04
         :     6D CE 03 CE 5F 63 AA 15 09 DC A8 4F E2 83 9A D0
         :     05 81 38 18 EF A2 91 90 75 58 40 7D C7 60 5A 5E
         :     73 9B CD A6 D2 C9 35 DE 8E 54 90 97 47 A3 45 51
         :     1D 9B ED A1 CB 92 B7 AC AB 23 CC AC 01 CD 53 5E
         :     B0 A7 CE 6A 74 70 0B 35 DE F6 12 85 0F 91 52 2D
         :     1E A6 81 3A 57 71 EC 9F 6F C9 41 77 6E 90 16 AB
         :             [ Another 129 bytes skipped ]
 268    3:   INTEGER 65537
 273  256:   INTEGER
         :     5B 26 C3 08 92 44 1C 68 0B 76 3D B6 BF 97 44 D7
         :     BD 51 42 00 5F ED A5 F6 9E 48 E0 EF F5 1F 4A 25
         :     B4 D2 AC F5 D3 48 23 4C 83 96 F6 35 CD 3D 03 65
         :     ED 58 FF EB 43 6E AF 25 98 73 EB 9F 90 81 76 44
         :     EC B1 96 BE 02 66 91 D0 1B 8A A1 CA 94 61 96 B0
         :     5F 7F 09 D7 E9 28 1E A1 B6 F2 F0 45 D4 56 4A A3
         :     2B 56 1F B4 14 AD 9B 34 D8 C6 52 D8 C3 30 03 37
         :     94 7E D2 C1 EF B1 3A F9 EC BC 81 DD D4 C9 80 AB
         :             [ Another 128 bytes skipped ]
  ...

The program is written in C++, even though you have a C tag. It allows us to avoid a lot of error checking and cleanup because its automatic. And its easy enough to convert back to C.

#include <memory>
using std::unique_ptr;

#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/x509.h>

#include <cassert>
#define ASSERT assert

using BN_ptr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
using EVP_KEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
using BIO_FILE_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;

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

    RSA_ptr rsa(RSA_new(), ::RSA_free);
    BN_ptr bn(BN_new(), ::BN_free);

    BIO_FILE_ptr pem1(BIO_new_file("rsa-public-1.pem", "w"), ::BIO_free);
    BIO_FILE_ptr pem2(BIO_new_file("rsa-public-2.pem", "w"), ::BIO_free);
    BIO_FILE_ptr pem3(BIO_new_file("rsa-private-1.pem", "w"), ::BIO_free);
    BIO_FILE_ptr pem4(BIO_new_file("rsa-private-2.pem", "w"), ::BIO_free);
    BIO_FILE_ptr pem5(BIO_new_file("rsa-private-3.pem", "w"), ::BIO_free);
    BIO_FILE_ptr der1(BIO_new_file("rsa-public.der", "w"), ::BIO_free);
    BIO_FILE_ptr der2(BIO_new_file("rsa-private.der", "w"), ::BIO_free);

    rc = BN_set_word(bn.get(), RSA_F4);
    ASSERT(rc == 1);

    // Generate key
    rc = RSA_generate_key_ex(rsa.get(), 2048, bn.get(), NULL);
    ASSERT(rc == 1);

    // Convert RSA to PKEY
    EVP_KEY_ptr pkey(EVP_PKEY_new(), ::EVP_PKEY_free);
    rc = EVP_PKEY_set1_RSA(pkey.get(), rsa.get());
    ASSERT(rc == 1);

    //////////

    // Write public key in ASN.1/DER
    rc = i2d_RSAPublicKey_bio(der1.get(), rsa.get());
    ASSERT(rc == 1);

    // Write public key in PKCS PEM
    rc = PEM_write_bio_RSAPublicKey(pem1.get(), rsa.get());
    ASSERT(rc == 1);       

    // Write public key in Traditional PEM
    rc = PEM_write_bio_PUBKEY(pem2.get(), pkey.get());
    ASSERT(rc == 1);

    //////////

    // Write private key in ASN.1/DER
    rc = i2d_RSAPrivateKey_bio(der2.get(), rsa.get());
    ASSERT(rc == 1);

    // Write private key in PKCS PEM.
    rc = PEM_write_bio_PrivateKey(pem3.get(), pkey.get(), NULL, NULL, 0, NULL, NULL);
    ASSERT(rc == 1);

    // Write private key in PKCS PEM
    rc = PEM_write_bio_PKCS8PrivateKey(pem4.get(), pkey.get(), NULL, NULL, 0, NULL, NULL);
    ASSERT(rc == 1);

    // Write private key in Traditional PEM
    rc = PEM_write_bio_RSAPrivateKey(pem5.get(), rsa.get(), NULL, NULL, 0, NULL, NULL);
    ASSERT(rc == 1);

    return 0;
}


You would use RSA_generate_key_ex, after properly seeding the PRNG using RAND_add.

Edit: While calling RAND_add was needed when this answer was originally written, current versions of OpenSSL no longer require manual seeding of the PRNG.

0

精彩评论

暂无评论...
验证码 换一张
取 消