OpenSL pem(3openssl)
NAME
PEM - PEM routines
SYNOPSIS
#include
EVPKEY *PEMreadbioPrivateKey(BIO *bp, EVPKEY **x,
pempasswordcb *cb, void *u);
EVPKEY *PEMreadPrivateKey(FILE *fp, EVPKEY **x,
pempasswordcb *cb, void *u);
int PEMwritebioPrivateKey(BIO *bp, EVPKEY *x, const EVPCIPHER *enc,
unsigned char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwritePrivateKey(FILE *fp, EVPKEY *x, const EVPCIPHER *enc,
unsigned char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwritebioPKCS8PrivateKey(BIO *bp, EVPKEY *x, const EVPCIPHER *enc,
char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwritePKCS8PrivateKey(FILE *fp, EVPKEY *x, const EVPCIPHER *enc,
char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwritebioPKCS8PrivateKeynid(BIO *bp, EVPKEY *x, int nid,
char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwritePKCS8PrivateKeynid(FILE *fp, EVPKEY *x, int nid,
char *kstr, int klen,
pempasswordcb *cb, void *u);
EVPKEY *PEMreadbioPUBKEY(BIO *bp, EVPKEY **x,
pempasswordcb *cb, void *u);
EVPKEY *PEMreadPUBKEY(FILE *fp, EVPKEY **x,
pempasswordcb *cb, void *u);
int PEMwritebioPUBKEY(BIO *bp, EVPKEY *x);
int PEMwritePUBKEY(FILE *fp, EVPKEY *x);
RSA *PEMreadbioRSAPrivateKey(BIO *bp, RSA **x,
pempasswordcb *cb, void *u);
RSA *PEMreadRSAPrivateKey(FILE *fp, RSA **x,
pempasswordcb *cb, void *u);
OpenSL-0.9.8 Last change: Oct 11 2005 1
OpenSL pem(3openssl)
int PEMwritebioRSAPrivateKey(BIO *bp, RSA *x, const EVPCIPHER *enc,
unsigned char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwriteRSAPrivateKey(FILE *fp, RSA *x, const EVPCIPHER *enc,
unsigned char *kstr, int klen,
pempasswordcb *cb, void *u);
RSA *PEMreadbioRSAPublicKey(BIO *bp, RSA **x,
pempasswordcb *cb, void *u);
RSA *PEMreadRSAPublicKey(FILE *fp, RSA **x,
pempasswordcb *cb, void *u);
int PEMwritebioRSAPublicKey(BIO *bp, RSA *x);
int PEMwriteRSAPublicKey(FILE *fp, RSA *x);
RSA *PEMreadbioRSAPUBKEY(BIO *bp, RSA **x,
pempasswordcb *cb, void *u);
RSA *PEMreadRSAPUBKEY(FILE *fp, RSA **x,
pempasswordcb *cb, void *u);
int PEMwritebioRSAPUBKEY(BIO *bp, RSA *x);
int PEMwriteRSAPUBKEY(FILE *fp, RSA *x);
DSA *PEMreadbioDSAPrivateKey(BIO *bp, DSA **x,
pempasswordcb *cb, void *u);
DSA *PEMreadDSAPrivateKey(FILE *fp, DSA **x,
pempasswordcb *cb, void *u);
int PEMwritebioDSAPrivateKey(BIO *bp, DSA *x, const EVPCIPHER *enc,
unsigned char *kstr, int klen,
pempasswordcb *cb, void *u);
int PEMwriteDSAPrivateKey(FILE *fp, DSA *x, const EVPCIPHER *enc,
unsigned char *kstr, int klen,
pempasswordcb *cb, void *u);
DSA *PEMreadbioDSAPUBKEY(BIO *bp, DSA **x,
pempasswordcb *cb, void *u);
DSA *PEMreadDSAPUBKEY(FILE *fp, DSA **x,
pempasswordcb *cb, void *u);
int PEMwritebioDSAPUBKEY(BIO *bp, DSA *x);
int PEMwriteDSAPUBKEY(FILE *fp, DSA *x);
OpenSL-0.9.8 Last change: Oct 11 2005 2
OpenSL pem(3openssl)
DSA *PEMreadbioDSAparams(BIO *bp, DSA **x, pempasswordcb *cb, void *u);
DSA *PEMreadDSAparams(FILE *fp, DSA **x, pempasswordcb *cb, void *u);
int PEMwritebioDSAparams(BIO *bp, DSA *x);
int PEMwriteDSAparams(FILE *fp, DSA *x);
DH *PEMreadbioDHparams(BIO *bp, DH **x, pempasswordcb *cb, void *u);
DH *PEMreadDHparams(FILE *fp, DH **x, pempasswordcb *cb, void *u);
int PEMwritebioDHparams(BIO *bp, DH *x);
int PEMwriteDHparams(FILE *fp, DH *x);
X509 *PEMreadbioX509(BIO *bp, X509 **x, pempasswordcb *cb, void *u);
X509 *PEMreadX509(FILE *fp, X509 **x, pempasswordcb *cb, void *u);
int PEMwritebioX509(BIO *bp, X509 *x);
int PEMwriteX509(FILE *fp, X509 *x);
X509 *PEMreadbioX509AUX(BIO *bp, X509 **x, pempasswordcb *cb, void *u);
X509 *PEMreadX509AUX(FILE *fp, X509 **x, pempasswordcb *cb, void *u);
int PEMwritebioX509AUX(BIO *bp, X509 *x);
int PEMwriteX509AUX(FILE *fp, X509 *x);
X509REQ *PEMreadbioX509REQ(BIO *bp, X509REQ **x,
pempasswordcb *cb, void *u);
X509REQ *PEMreadX509REQ(FILE *fp, X509REQ **x,
pempasswordcb *cb, void *u);
int PEMwritebioX509REQ(BIO *bp, X509REQ *x);
int PEMwriteX509REQ(FILE *fp, X509REQ *x);
int PEMwritebioX509REQNEW(BIO *bp, X509REQ *x);
int PEMwriteX509REQNEW(FILE *fp, X509REQ *x);
X509CRL *PEMreadbioX509CRL(BIO *bp, X509CRL **x,
pempasswordcb *cb, void *u);
X509CRL *PEMreadX509CRL(FILE *fp, X509CRL **x,
pempasswordcb *cb, void *u);
int PEMwritebioX509CRL(BIO *bp, X509CRL *x);
int PEMwriteX509CRL(FILE *fp, X509CRL *x);
OpenSL-0.9.8 Last change: Oct 11 2005 3
OpenSL pem(3openssl)
PKCS7 *PEMreadbioPKCS7(BIO *bp, PKCS7 **x, pempasswordcb *cb, void *u);
PKCS7 *PEMreadPKCS7(FILE *fp, PKCS7 **x, pempasswordcb *cb, void *u);
int PEMwritebioPKCS7(BIO *bp, PKCS7 *x);
int PEMwritePKCS7(FILE *fp, PKCS7 *x);
NETSCAPECERTSEQUENCE *PEMreadbioNETSCAPECERTSEQUENCE(BIO *bp,
NETSCAPECERTSEQUENCE **x,
pempasswordcb *cb, void *u);
NETSCAPECERTSEQUENCE *PEMreadNETSCAPECERTSEQUENCE(FILE *fp,
NETSCAPECERTSEQUENCE **x,
pempasswordcb *cb, void *u);
int PEMwritebioNETSCAPECERTSEQUENCE(BIO *bp, NETSCAPECERTSEQUENCE *x);
int PEMwriteNETSCAPECERTSEQUENCE(FILE *fp, NETSCAPECERTSEQUENCE *x);
DESCRIPTION
The PEM functions read or write structures in PEM format. In
this sense PEM format is simply base64 encoded data
surrounded by header lines.
For more details about the meaning of arguments see the PEM
FUNCTION ARGUMENTS section.
Each operation has four functions associated with it. For
clarity the term "foobar functions" will be used to
collectively refer to the PEMreadbiofoobar(),
PEMreadfoobar(), PEMwritebiofoobar() and
PEMwritefoobar() functions.
The PrivateKey functions read or write a private key in PEM
format using an EVPKEY 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.
PEMwritebioPKCS8PrivateKey() and
PEMwritePKCS8PrivateKey() write a private key in an
EVPKEY structure in PKCS#8 EncryptedPrivateKeyInfo format
using PKCS#5 v2.0 password based encryption algorithms. The
cipher argument specifies the encryption algoritm to use:
unlike all other PEM routines the encryption is applied at
the PKCS#8 level and not in the PEM headers. If cipher is
NUL then no encryption is used and a PKCS#8 PrivateKeyInfo
structure is used instead.
OpenSL-0.9.8 Last change: Oct 11 2005 4
OpenSL pem(3openssl)
PEMwritebioPKCS8PrivateKeynid() and
PEMwritePKCS8PrivateKeynid() also write out a private key
as a PKCS#8 EncryptedPrivateKeyInfo however it uses PKCS#5
v1.5 or PKCS#12 encryption algorithms instead. The algorithm
to use is specified in the nid parameter and should be the
NID of the corresponding OBJECT IDENTIFIER (see NOTES
section).
The PUBKEY functions process a public key using an EVPKEY
structure. The public key is encoded as a
SubjectPublicKeyInfo structure.
The RSAPrivateKey functions process an RSA private key using
an RSA structure. It handles 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 RSAPUBKEY functions also process an RSA public key
using an RSA structure. However the public key is encoded
using a 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. It handles the same formats as the
PrivateKey functions but an error occurs if the private key
is not DSA.
The DSAPUBKEY functions process a DSA public key using a
DSA structure. The public key is encoded using a
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 foobar
structure.
The DHparams functions process DH parameters using a DH
structure. The parameters are encoded using a PKCS#3
DHparameter 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 X509AUX functions process a trusted X509 certificate
using an X509 structure.
OpenSL-0.9.8 Last change: Oct 11 2005 5
OpenSL pem(3openssl)
The X509REQ and X509REQNEW functions process a PKCS#10
certificate request using an X509REQ structure. The
X509REQ write functions use CERTIFICATE REQUEST in the
header whereas the X509REQNEW functions use NEW
CERTIFICATE REQUEST (as required by some CAs). The X509REQ
read functions will handle either form so there are no
X509REQNEW read functions.
The X509CRL functions process an X509 CRL using an X509CRL
structure.
The PKCS7 functions process a PKCS#7 ContentInfo using a
PKCS7 structure.
The NETSCAPECERTSEQUENCE functions process a Netscape
Certificate Sequence using a NETSCAPECERTSEQUENCE
structure.
PEM FUNCTION ARGUMENTS
The PEM functions have many common arguments.
The bp BIO parameter (if present) specifies the BIO to read
from or write to.
The fp FILE parameter (if present) specifies the FILE
pointer to read from or write to.
The PEM read functions all take an argument TYPE **x and
return a TYPE * pointer. Where TYPE is whatever structure
the function uses. If x is NUL then the parameter is
ignored. If x is not NUL but *x is NUL then the structure
returned will be written to *x. If neither x nor *x is NUL
then an attempt is made to reuse the structure at *x (but
see BUGS and EXAMPLES sections). Irrespective of the value
of x a pointer to the structure is always returned (or NUL
if an error occurred).
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 NUL then the private key is written in unencrypted
form.
The cb argument is the callback to use when querying for the
pass phrase used for encrypted PEM structures (normally only
private keys).
For the PEM write routines if the kstr parameter is not NUL
then klen bytes at kstr are used as the passphrase and cb is
ignored.
OpenSL-0.9.8 Last change: Oct 11 2005 6
OpenSL pem(3openssl)
If the cb parameters is set to NUL and the u parameter is
not NUL then the u parameter is interpreted as a null
terminated string to use as the passphrase. If both cb and u
are NUL then the default callback routine is used which
will typically prompt for the passphrase on the current
terminal with echoing turned off.
The default passphrase callback is sometimes inappropriate
(for example in a GUI application) so an alternative can be
supplied. The callback routine has the following form:
int cb(char *buf, int size, int rwflag, void *u);
buf is the buffer to write the passphrase to. size is the
maximum length of the passphrase (i.e. the size of buf).
rwflag is a flag which is set to 0 when reading and 1 when
writing. A typical routine will ask the user to verify the
passphrase (for example by prompting for it twice) if rwflag
is 1. The u parameter has the same value as the u parameter
passed to the PEM routine. It allows arbitrary data to be
passed to the callback by the application (for example a
window handle in a GUI application). The callback must
return the number of characters in the passphrase or 0 if an
error occurred.
EXAMPLES
Although the PEM routines take several arguments in almost
all applications most of them are set to 0 or NUL.
Read a certificate in PEM format from a BIO:
X509 *x;
x = PEMreadbioX509(bp, NUL, 0, NUL);
if (x == NUL)
{
/* Error */
}
Alternative method:
X509 *x = NUL;
if (!PEMreadbioX509(bp, &x, 0, NUL))
{
/* Error */
}
Write a certificate to a BIO:
if (!PEMwritebioX509(bp, x))
{
/* Error */
}
OpenSL-0.9.8 Last change: Oct 11 2005 7
OpenSL pem(3openssl)
Write an unencrypted private key to a FILE pointer:
if (!PEMwritePrivateKey(fp, key, NUL, NUL, 0, 0, NUL))
{
/* Error */
}
Write a private key (using traditional format) to a BIO
using triple DES encryption, the pass phrase is prompted
for:
if (!PEMwritebioPrivateKey(bp, key, EVPdesede3cbc(), NUL, 0, 0, NUL))
{
/* Error */
}
Write a private key (using PKCS#8 format) to a BIO using
triple DES encryption, using the pass phrase "hello":
if (!PEMwritebioPKCS8PrivateKey(bp, key, EVPdesede3cbc(), NUL, 0, 0, "hello"))
{
/* Error */
}
Read a private key from a BIO using the pass phrase "hello":
key = PEMreadbioPrivateKey(bp, NUL, 0, "hello");
if (key == NUL)
{
/* Error */
}
Read a private key from a BIO using a pass phrase callback:
key = PEMreadbioPrivateKey(bp, NUL, passcb, "My Private Key");
if (key == NUL)
{
/* Error */
}
Skeleton pass phrase callback:
int passcb(char *buf, int size, int rwflag, void *u);
{
int len;
char *tmp;
/* We'd probably do something else if 'rwflag' is 1 */
printf("Enter pass phrase for \"%s\"\n", u);
/* get pass phrase, length 'len' into 'tmp' */
tmp = "hello";
len = strlen(tmp);
OpenSL-0.9.8 Last change: Oct 11 2005 8
OpenSL pem(3openssl)
if (len <= 0) return 0;
/* if too long, truncate */
if (len > size) len = size;
memcpy(buf, tmp, len);
return len;
}
NOTES
The old PrivateKey write routines are retained for
compatibility. New applications should write private keys
using the PEMwritebioPKCS8PrivateKey() or
PEMwritePKCS8PrivateKey() routines because they are more
secure (they use an iteration count of 2048 whereas the
traditional routines use a count of 1) unless compatibility
with older versions of OpenSL is important.
The PrivateKey read routines can be used in all applications
because they handle all formats transparently.
A frequent cause of problems is attempting to use the PEM
routines like this:
X509 *x;
PEMreadbioX509(bp, &x, 0, NUL);
this is a bug because an attempt will be made to reuse the
data at x which is an uninitialised pointer.
PEM ENCRYPTION FORMAT
This 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 DEK-Info contains two comma separated
pieces of information: the encryption algorithm name as
used by EVPgetcipherbyname() 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 EVPbytestokey(),
using salt and an iteration count of 1. The IV used is the
value of salt and *not* the IV returned by EVPbytestokey().
OpenSL-0.9.8 Last change: Oct 11 2005 9
OpenSL pem(3openssl)
BUGS
The PEM read routines in some versions of OpenSL will not
correctly reuse an existing structure. Therefore the
following:
PEMreadbioX509(bp, &x, 0, NUL);
where x already contains a valid certificate, may not work,
whereas:
X509free(x);
x = PEMreadbioX509(bp, NUL, 0, NUL);
is guaranteed to work.
RETURN CODES
The read routines return either a pointer to the structure
read or NUL if an error occurred.
The write routines return 1 for success or 0 for failure.
OpenSL-0.9.8 Last change: Oct 11 2005 10
|