openssl
Llaves
Buscar..
Sintaxis
- 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 * key);
- int PEM_write_PrivateKey (FILE * fp, EVP_PKEY * x, const EVP_CIPHER * enc, unsigned char * kstr, int klen, pem_password_cb * cb, void * u);
- int PEM_write_bio_PrivateKey (BIO * bp, EVP_PKEY * x, const EVP_CIPHER * enc, unsigned char * kstr, int klen, pem_password_cb * cb, void * u);
- EVP_PKEY * PEM_read_PrivateKey (FILE * fp, EVP_PKEY ** x, pem_password_cb * cb, void * u);
- EVP_PKEY * PEM_read_bio_PrivateKey (BIO * bp, EVP_PKEY ** x, pem_password_cb * cb, void * u);
- nula EVP_PKEY_free (clave EVP_PKEY *);
Generar clave RSA
Con el fin de generar una clave RSA, un EVP_PKEY , habrá que situarlo con EVP_PKEY_new :
EVP_PKEY *pkey;
pkey = EVP_PKEY_new();
También se necesita un exponente para la clave, que requerirá asignar un BIGNUM con BN_new y luego asignar con BN_set_word :
BIGNUM *bn;
bn = BN_new();
BN_set_word(bn, RSA_F4);
Para generar la clave, cree un nuevo RSA con RSA_new y llame a 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 */
);
Para asignar la clave recién generada a la estructura EVP_PKEY , llame a EVP_PKEY_assign_RSA :
EVP_PKEY_assign_RSA(pkey, rsa);
La estructura RSA se liberará automáticamente cuando se EVP_PKEY estructura EVP_PKEY . Esto se hace con EVP_PKEY_free :
EVP_PKEY_free(pkey);
Guardar clave privada
Un EVP_PKEY se puede guardar directamente en el disco en varios formatos. PEM_write_PrivateKey se utiliza para guardar EVP_PKEY en un formato 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 */
);
Para guardar una clave privada en un BIO , use 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 */
);
Cargar clave privada
Para cargar una clave privada directamente desde el disco, use la función 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 */
);
Para cargar una clave privada desde un BIO , use 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 */
);