Next: certtool Invocation, Previous: OCSP stapling, Up: More on certificate authentication [Contents][Index]
Transferring or storing private keys in plain may not be a good idea, since any compromise is irreparable. Storing the keys in hardware security modules (see Smart cards and HSMs) could solve the storage problem but it is not always practical or efficient enough. This section describes ways to store and transfer encrypted private keys.
There are methods for key encryption, namely the PKCS #8, PKCS #12 and OpenSSL’s custom encrypted private key formats. The PKCS #8 and the OpenSSL’s method allow encryption of the private key, while the PKCS #12 method allows, in addition, the bundling of accompanying data into the structure. That is typically the corresponding certificate, as well as a trusted CA certificate.
Generic and higher level private key import functions are available, that import plain or encrypted keys and will auto-detect the encrypted key format.
pkey: The private key
data: The private key data to be imported
format: The format of the private key
password: A password (optional)
flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t
This function will import the given private key to the abstract
gnutls_privkey_t
type.
The supported formats are basic unencrypted key, PKCS8, PKCS12, TSS2, and the openssl format.
Returns: On success, GNUTLS_E_SUCCESS
(0) is returned, otherwise a
negative error value.
Since: 3.1.0
key: The data to store the parsed key
data: The DER or PEM encoded key.
format: One of DER or PEM
password: A password (optional)
flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t
This function will import the given DER or PEM encoded key, to
the native gnutls_x509_privkey_t
format, irrespective of the
input format. The input format is auto-detected.
The supported formats are basic unencrypted key, PKCS8, PKCS12, and the openssl format.
If the provided key is encrypted but no password was given, then
GNUTLS_E_DECRYPTION_FAILED
is returned. Since GnuTLS 3.4.0 this
function will utilize the PIN callbacks if any.
Returns: On success, GNUTLS_E_SUCCESS
(0) is returned, otherwise a
negative error value.
Any keys imported using those functions can be imported to a certificate credentials structure using gnutls_certificate_set_key, or alternatively they can be directly imported using gnutls_certificate_set_x509_key_file2.
PKCS #8 keys can be imported and exported as normal private keys using
the functions below. An addition to the normal import functions, are
a password and a flags argument. The flags can be any element of the gnutls_pkcs_encrypt_flags_t
enumeration. Note however, that GnuTLS only supports the PKCS #5 PBES2
encryption scheme. Keys encrypted with the obsolete PBES1 scheme cannot
be decrypted.
int gnutls_x509_privkey_import_pkcs8 (gnutls_x509_privkey_t key, const gnutls_datum_t * data, gnutls_x509_crt_fmt_t format, const char * password, unsigned int flags)
int gnutls_x509_privkey_export_pkcs8 (gnutls_x509_privkey_t key, gnutls_x509_crt_fmt_t format, const char * password, unsigned int flags, void * output_data, size_t * output_data_size)
int gnutls_x509_privkey_export2_pkcs8 (gnutls_x509_privkey_t key, gnutls_x509_crt_fmt_t format, const char * password, unsigned int flags, gnutls_datum_t * out)
GNUTLS_PKCS_PLAIN
Unencrypted private key.
GNUTLS_PKCS_PKCS12_3DES
PKCS-12 3DES.
GNUTLS_PKCS_PKCS12_ARCFOUR
PKCS-12 ARCFOUR.
GNUTLS_PKCS_PKCS12_RC2_40
PKCS-12 RC2-40.
GNUTLS_PKCS_PBES2_3DES
PBES2 3DES.
GNUTLS_PKCS_PBES2_AES_128
PBES2 AES-128.
GNUTLS_PKCS_PBES2_AES_192
PBES2 AES-192.
GNUTLS_PKCS_PBES2_AES_256
PBES2 AES-256.
GNUTLS_PKCS_NULL_PASSWORD
Some schemas distinguish between an empty and a NULL password.
GNUTLS_PKCS_PBES2_DES
PBES2 single DES.
GNUTLS_PKCS_PBES1_DES_MD5
PBES1 with single DES; for compatibility with openssl only.
GNUTLS_PKCS_PBES2_GOST_TC26Z
PBES2 GOST 28147-89 CFB with TC26-Z S-box.
GNUTLS_PKCS_PBES2_GOST_CPA
PBES2 GOST 28147-89 CFB with CryptoPro-A S-box.
GNUTLS_PKCS_PBES2_GOST_CPB
PBES2 GOST 28147-89 CFB with CryptoPro-B S-box.
GNUTLS_PKCS_PBES2_GOST_CPC
PBES2 GOST 28147-89 CFB with CryptoPro-C S-box.
GNUTLS_PKCS_PBES2_GOST_CPD
PBES2 GOST 28147-89 CFB with CryptoPro-D S-box.
A PKCS #12 structure [PKCS12] usually contains a user’s private keys and certificates. It is commonly used in browsers to export and import the user’s identities. A file containing such a key can be directly imported to a certificate credentials structure by using gnutls_certificate_set_x509_simple_pkcs12_file.
In GnuTLS the PKCS #12 structures are handled
using the gnutls_pkcs12_t
type. This is an abstract type that
may hold several gnutls_pkcs12_bag_t
types. The bag types are
the holders of the actual data, which may be certificates, private
keys or encrypted data. A bag of type encrypted should be decrypted
in order for its data to be accessed.
To reduce the complexity in parsing the structures the simple helper function gnutls_pkcs12_simple_parse is provided. For more advanced uses, manual parsing of the structure is required using the functions below.
int gnutls_pkcs12_get_bag (gnutls_pkcs12_t pkcs12, int indx, gnutls_pkcs12_bag_t bag)
int gnutls_pkcs12_verify_mac (gnutls_pkcs12_t pkcs12, const char * pass)
int gnutls_pkcs12_bag_decrypt (gnutls_pkcs12_bag_t bag, const char * pass)
int gnutls_pkcs12_bag_get_count (gnutls_pkcs12_bag_t bag)
p12: A pkcs12 type
password: optional password used to decrypt the structure, bags and keys.
key: a structure to store the parsed private key.
chain: the corresponding to key certificate chain (may be NULL
)
chain_len: will be updated with the number of additional (may be NULL
)
extra_certs: optional pointer to receive an array of additional
certificates found in the PKCS12 structure (may be NULL
).
extra_certs_len: will be updated with the number of additional
certs (may be NULL
).
crl: an optional structure to store the parsed CRL (may be NULL
).
flags: should be zero or one of GNUTLS_PKCS12_SP_*
This function parses a PKCS12 structure in pkcs12
and extracts the
private key, the corresponding certificate chain, any additional
certificates and a CRL. The structures in key
, chain
crl
, and extra_certs
must not be initialized.
The extra_certs
and extra_certs_len
parameters are optional
and both may be set to NULL
. If either is non-NULL
, then both must
be set. The value for extra_certs
is allocated
using gnutls_malloc()
.
Encrypted PKCS12 bags and PKCS8 private keys are supported, but only with password based security and the same password for all operations.
Note that a PKCS12 structure may contain many keys and/or certificates, and there is no way to identify which key/certificate pair you want. For this reason this function is useful for PKCS12 files that contain only one key/certificate pair and/or one CRL.
If the provided structure has encrypted fields but no password
is provided then this function returns GNUTLS_E_DECRYPTION_FAILED
.
Note that normally the chain constructed does not include self signed
certificates, to comply with TLS’ requirements. If, however, the flag
GNUTLS_PKCS12_SP_INCLUDE_SELF_SIGNED
is specified then
self signed certificates will be included in the chain.
Prior to using this function the PKCS 12
structure integrity must
be verified using gnutls_pkcs12_verify_mac()
.
Returns: On success, GNUTLS_E_SUCCESS
(0) is returned, otherwise a
negative error value.
Since: 3.1.0
int gnutls_pkcs12_bag_get_data (gnutls_pkcs12_bag_t bag, unsigned indx, gnutls_datum_t * data)
int gnutls_pkcs12_bag_get_key_id (gnutls_pkcs12_bag_t bag, unsigned indx, gnutls_datum_t * id)
int gnutls_pkcs12_bag_get_friendly_name (gnutls_pkcs12_bag_t bag, unsigned indx, char ** name)
The functions below are used to generate a PKCS #12 structure. An example of their usage is shown at PKCS12 structure generation example.
int gnutls_pkcs12_set_bag (gnutls_pkcs12_t pkcs12, gnutls_pkcs12_bag_t bag)
int gnutls_pkcs12_bag_encrypt (gnutls_pkcs12_bag_t bag, const char * pass, unsigned int flags)
int gnutls_pkcs12_generate_mac (gnutls_pkcs12_t pkcs12, const char * pass)
int gnutls_pkcs12_bag_set_data (gnutls_pkcs12_bag_t bag, gnutls_pkcs12_bag_type_t type, const gnutls_datum_t * data)
int gnutls_pkcs12_bag_set_crl (gnutls_pkcs12_bag_t bag, gnutls_x509_crl_t crl)
int gnutls_pkcs12_bag_set_crt (gnutls_pkcs12_bag_t bag, gnutls_x509_crt_t crt)
int gnutls_pkcs12_bag_set_key_id (gnutls_pkcs12_bag_t bag, unsigned indx, const gnutls_datum_t * id)
int gnutls_pkcs12_bag_set_friendly_name (gnutls_pkcs12_bag_t bag, unsigned indx, const char * name)
Unfortunately the structures discussed in the previous sections are not the only structures that may hold an encrypted private key. For example the OpenSSL library offers a custom key encryption method. Those structures are also supported in GnuTLS with gnutls_x509_privkey_import_openssl.
key: The data to store the parsed key
data: The DER or PEM encoded key.
password: the password to decrypt the key (if it is encrypted).
This function will convert the given PEM encrypted to
the native gnutls_x509_privkey_t format. The
output will be stored in key
.
The password
should be in ASCII. If the password is not provided
or wrong then GNUTLS_E_DECRYPTION_FAILED
will be returned.
If the Certificate is PEM encoded it should have a header of "PRIVATE KEY" and the "DEK-Info" header.
Returns: On success, GNUTLS_E_SUCCESS
(0) is returned, otherwise a
negative error value.
Next: certtool Invocation, Previous: OCSP stapling, Up: More on certificate authentication [Contents][Index]