サーチ…


構文

  • EVP_PKEY * EVP_PKEY_new(void);
  • RSA * RSA_new(void);
  • int RSA_generate_key_ex(RSA * rsa、int bits、BIGNUM * e、BN_GENCB * cb);
  • int EVP_PKEY_assign_RSA(EVP_PKEY * pkey、RSA *キー);
  • int PEM_write_PrivateKey(FILE * fp、EVP_PKEY * x、const EVP_CIPHER * enc、符号なしchar * kstr、int klen、pem_password_cb * cb、void * u);
  • int PEM_write_bio_PrivateKey(BIO * bp、EVP_PKEY * x、const EVP_CIPHER * enc、符号なしchar * kstr、int klen、pem_password_cb * cb、void * u);
  • EVP_PKEY * PEM_read_PrivateKey(FILE * fp、EVP_PKEY **、pem_password_cb * cb、void * u);
  • EVP_PKEY * PEM_read_bio_PrivateKey(BIO * bp、EVP_PKEY ** x、pem_password_cb * cb、void * u);
  • void EVP_PKEY_free(EVP_PKEY * key);

RSAキーを生成する

RSA鍵を生成するために、 EVP_PKEY最初で割り当てられなければならないEVP_PKEY_new

EVP_PKEY *pkey;
pkey = EVP_PKEY_new();

キーの指数も割り当てる必要となる、必要とされるBIGNUM有するBN_newし、次いで割り当てBN_set_word

BIGNUM *bn;
bn = BN_new();
BN_set_word(bn, RSA_F4);

キーを生成するには、 RSA_newを使用して新しいRSAを作成し、 RSA_newを呼び出しRSA_generate_key_ex

RSA *rsa;
rsa = RSA_new();
RSA_generate_key_ex(
    rsa,  /* pointer to the RSA structure */
    2048, /* number of bits for the key - 2048 is a good value */
    bn,   /* exponent allocated earlier */
    NULL, /* callback - can be NULL if progress isn't needed */
);

新しく生成されたキーをEVP_PKEY構造体に割り当てるには、 EVP_PKEYを呼び出しEVP_PKEY_assign_RSA

EVP_PKEY_assign_RSA(pkey, rsa);

EVP_PKEY構造体が解放されると、 RSA構造体は自動的に解放されます。これはEVP_PKEY_freeEVP_PKEY_freeます:

EVP_PKEY_free(pkey);

秘密鍵を保存する

EVP_PKEYは、複数の形式でディスクに直接保存できます。 PEM_write_PrivateKeyは、 EVP_PKEYをPEM形式で保存するために使用されます。

FILE *f;
f = fopen("key.pem", "wb");
PEM_write_PrivateKey(
    f,                  /* use the FILE* that was opened */
    pkey,               /* EVP_PKEY structure */
    EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
    "replace_me",       /* passphrase required for decrypting the key on disk */
    10,                 /* length of the passphrase string */
    NULL,               /* callback for requesting a password */
    NULL                /* data to pass to the callback */
);

プライベートキーをBIOに保存するには、 PEM_write_bio_PrivateKey使用しPEM_write_bio_PrivateKey

BIO *bio;
bio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(
    bio,                /* BIO to write the private key to */
    pkey,               /* EVP_PKEY structure */
    EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
    "replace_me",       /* passphrase required for decrypting the key on disk */
    10,                 /* length of the passphrase string */
    NULL,               /* callback for requesting a password */
    NULL                /* data to pass to the callback */
);

秘密鍵を読み込む

ディスクから直接秘密鍵をロードするには、 PEM_read_PrivateKey関数を使用します。

FILE *f;
EVP_PKEY *pkey;
f = fopen("key.pem", "rb");
PEM_read_PrivateKey(
    f,     /* use the FILE* that was opened */
    &pkey, /* pointer to EVP_PKEY structure */
    NULL,  /* password callback - can be NULL */
    NULL   /* parameter passed to callback or password if callback is NULL */
);

BIOから秘密鍵をロードするには、 PEM_read_bio_PrivateKey使用しPEM_read_bio_PrivateKey

BIO *bio;
bio = BIO_new_mem_buf((void *)input, input_len);
PEM_read_bio_PrivateKey(
    bio,   /* BIO to read the private key from */
    &pkey, /* pointer to EVP_PKEY structure */
    NULL,  /* password callback - can be NULL */
    NULL   /* parameter passed to callback or password if callback is NULL */
);


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow