![]() |
![]()
| ![]() |
![]()
NAME
SYNOPSIS
EVP_PKEY *
EVP_PKEY *
int
int
int
int
int
int
X509_SIG *
X509_SIG *
int
int
PKCS8_PRIV_KEY_INFO *
PKCS8_PRIV_KEY_INFO *
int
int
EVP_PKEY *
EVP_PKEY *
int
int
RSA *
RSA *
int
int
RSA *
RSA *
int
int
RSA *
RSA *
int
int
DSA *
DSA *
int
int
DSA *
DSA *
int
int
DSA *
DSA *
int
int
DH *
DH *
int
int
EC_GROUP *
EC_GROUP *
int
int
EC_KEY *
EC_KEY *
int
int
EC_KEY *
EC_KEY *
int
int
X509 *
X509 *
int
int
X509 *
X509 *
int
int
X509_REQ *
X509_REQ *
int
int
int
int
X509_CRL *
X509_CRL *
int
int
PKCS7 *
PKCS7 *
int
int
CMS_ContentInfo *
CMS_ContentInfo *
int
int
DESCRIPTIONThe PEM functions read or write structures in PEM format. In this sense PEM format is simply base64-encoded data surrounded by header lines; see PEM_read(3) for more details. For more details about the meaning of arguments see the PEM function arguments section. Each operation has four functions
associated with it. For brevity the term “TYPE
functions” will be used to collectively refer
to the
The PrivateKey functions read or write a private key in PEM format using an EVP_PKEY structure. The write routines use "traditional" private key format and can handle both RSA and DSA private keys. The read functions can additionally transparently handle PKCS#8 format encrypted and unencrypted keys too.
The PKCS8 functions process an encrypted private key using an X509_SIG structure and the d2i_X509_SIG(3) function. The PKCS8_PRIV_KEY_INFO functions process a private key using a PKCS8_PRIV_KEY_INFO structure. The PUBKEY functions process a public key using an EVP_PKEY structure. The public key is encoded as an ASN.1 SubjectPublicKeyInfo structure. The RSAPrivateKey functions process an RSA private key using an RSA structure. They handle the same formats as the PrivateKey functions, but an error occurs if the private key is not RSA. The RSAPublicKey functions process an RSA public key using an RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey structure. The RSA_PUBKEY functions also process an RSA public key using an RSA structure. However the public key is encoded using an ASN.1 SubjectPublicKeyInfo structure and an error occurs if the public key is not RSA. The DSAPrivateKey functions process a DSA private key using a DSA structure. They handle the same formats as the PrivateKey functions but an error occurs if the private key is not DSA. The DSA_PUBKEY functions process a DSA public key using a DSA structure. The public key is encoded using an ASN.1 SubjectPublicKeyInfo structure and an error occurs if the public key is not DSA. The DSAparams functions process DSA parameters using a DSA structure. The parameters are encoded using a Dss-Parms structure as defined in RFC 2459. The DHparams functions process DH parameters using a DH structure. The parameters are encoded using a PKCS#3 DHparameter structure. The ECPKParameters functions process EC parameters using an EC_GROUP structure and the d2i_ECPKParameters(3) function. The ECPrivateKey functions process an EC private key using an EC_KEY structure. The EC_PUBKEY functions process an EC public key using an EC_KEY structure. The X509 functions process an X509 certificate using an X509 structure. They will also process a trusted X509 certificate but any trust settings are discarded. The X509_AUX functions process a trusted X509 certificate using an X509 structure. The X509_REQ and X509_REQ_NEW functions process a PKCS#10 certificate request using an X509_REQ structure. The X509_REQ write functions use CERTIFICATE REQUEST in the header whereas the X509_REQ_NEW functions use NEW CERTIFICATE REQUEST (as required by some CAs). The X509_REQ read functions will handle either form so there are no X509_REQ_NEW read functions. The X509_CRL functions process an X509 CRL using an X509_CRL structure. The PKCS7 functions process a PKCS#7 ContentInfo using a PKCS7 structure. The CMS functions process a CMS_ContentInfo structure. The old
PrivateKey write routines are retained for compatibility.
New applications should write private keys using the
The PrivateKey read routines can be used in all applications because they handle all formats transparently. PEM function argumentsThe PEM functions have many common arguments. The bp parameter specifies the BIO to read from or write to. The fp parameter specifies the FILE pointer to read from or write to. The PEM read functions all take a pointer to pointer argument
x and return a pointer of the same type. If
x is The PEM functions which write private keys take an
enc parameter, which specifies the encryption
algorithm to use. Encryption is done at the PEM level. If this parameter is
set to The optional arguments u and cb are a passphrase used for encrypting a PEM structure or a callback to obtain the passphrase; see pem_password_cb(3) for details. For the PEM write routines, if the kstr
parameter is not PEM encryption formatThese old PrivateKey routines use a non-standard technique for encryption. The private key (or other data) takes the following form: -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89 ...base64 encoded data... -----END RSA PRIVATE KEY----- The line beginning with “DEK-Info” contains two comma separated pieces of information: the encryption algorithm name as used by EVP_get_cipherbyname(3) and an 8-byte salt encoded as a set of hexadecimal digits. After this is the base64-encoded encrypted data. The encryption key is determined using EVP_BytesToKey(3), using the salt and an iteration count of 1. The IV used is the value of the salt and *not* the IV returned by EVP_BytesToKey(3). RETURN VALUESThe read routines return either a pointer to the structure read or
The write routines return 1 for success or 0 for failure. EXAMPLESAlthough the PEM routines take several arguments, in almost all
applications most of them are set to 0 or Read a certificate in PEM format from a BIO: X509 *x; x = PEM_read_bio_X509(bp, NULL, 0, NULL); if (x == NULL) { /* Error */ } Alternative method: X509 *x = NULL; if (!PEM_read_bio_X509(bp, &x, 0, NULL)) { /* Error */ } Write a certificate to a BIO: if (!PEM_write_bio_X509(bp, x)) { /* Error */ } Write an unencrypted private key to a FILE: if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL)) { /* Error */ } Write a private key (using traditional format) to a BIO using triple DES encryption; the pass phrase is prompted for: if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL)) { /* Error */ } Write a private key (using PKCS#8 format) to a BIO using triple DES encryption, using the pass phrase "hello": if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello")) { /* Error */ } Read a private key from a BIO using the pass phrase "hello": key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello"); if (key == NULL) { /* Error */ } Read a private key from a BIO using a pass phrase callback: key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key"); if (key == NULL) { /* Error */ } Skeleton pass phrase callback: int pass_cb(char *buf, int size, int rwflag, void *u) { char *tmp; size_t len; /* We'd probably do something else if 'rwflag' is 1 */ printf("Enter pass phrase for \"%s\"\n", u); /* * Instead of the following line, get the passphrase * from the user in some way. */ tmp = "hello"; if (tmp == NULL) /* An error occurred. */ return -1; len = strlen(tmp); if (len == 0) /* Treat an empty passphrase as an error, too. */ return -1; /* if too long, truncate */ if (len > size) len = size; memcpy(buf, tmp, len); return len; } SEE ALSOBIO_new(3), DSA_new(3), PEM_ASN1_read(3), PEM_bytes_read_bio(3), PEM_read(3), PEM_read_SSL_SESSION(3), PEM_write_bio_CMS_stream(3), PEM_write_bio_PKCS7_stream(3), PEM_X509_INFO_read(3), RSA_new(3), X509_CRL_new(3), X509_REQ_new(3), X509_SIG_new(3) HISTORY
CAVEATSA frequent cause of problems is attempting to use the PEM routines like this: X509 *x; PEM_read_bio_X509(bp, &x, 0, NULL); This is a bug because an attempt will be made to reuse the data at x, which is an uninitialised pointer. These functions make no assumption regarding the pass phrase received from the password callback. It will simply be treated as a byte sequence. BUGSThe PEM read routines in some versions of OpenSSL will not correctly reuse an existing structure. Therefore PEM_read_bio_X509(bp, &x, 0,
NULL); where x already contains a valid certificate may not work, whereas X509_free(x); x = PEM_read_bio_X509(bp, NULL, 0, NULL); is guaranteed to work.
|