[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.2.4 Managing encrypted keys
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.
High level functionality
Generic and higher level private key import functions are available, that import plain or encrypted keys and will auto-detect the encrypted key format.
- Function: int gnutls_privkey_import_x509_raw (gnutls_privkey_t pkey, const gnutls_datum_t * data, gnutls_x509_crt_fmt_t format, const char * password, unsigned int flags)
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
structure.The supported formats are basic unencrypted key, PKCS8, PKCS12, and the openssl format.
Returns: On success,
GNUTLS_E_SUCCESS
(0) is returned, otherwise a negative error value.Since: 3.1.0
- Function: int gnutls_x509_privkey_import2 (gnutls_x509_privkey_t key, const gnutls_datum_t * data, gnutls_x509_crt_fmt_t format, const char * password, unsigned int flags)
key: The structure 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.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 structures
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_USE_PKCS12_3DES
PKCS-12 3DES.
GNUTLS_PKCS_USE_PKCS12_ARCFOUR
PKCS-12 ARCFOUR.
GNUTLS_PKCS_USE_PKCS12_RC2_40
PKCS-12 RC2-40.
GNUTLS_PKCS_USE_PBES2_3DES
PBES2 3DES.
GNUTLS_PKCS_USE_PBES2_AES_128
PBES2 AES-128.
GNUTLS_PKCS_USE_PBES2_AES_192
PBES2 AES-192.
GNUTLS_PKCS_USE_PBES2_AES_256
PBES2 AES-256.
GNUTLS_PKCS_NULL_PASSWORD
Some schemas distinguish between an empty and a NULL password.
Figure 4.6: Encryption flags
PKCS #12 structures
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)
- Function: int gnutls_pkcs12_simple_parse (gnutls_pkcs12_t p12, const char * password, gnutls_x509_privkey_t * key, gnutls_x509_crt_t ** chain, unsigned int * chain_len, gnutls_x509_crt_t ** extra_certs, unsigned int * extra_certs_len, gnutls_x509_crl_t * crl, unsigned int flags)
p12: the PKCS12 blob.
password: optional password used to decrypt PKCS12 blob, 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 blob (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 blob in
p12blob
and extracts the private key, the corresponding certificate chain, and any additional certificates and a CRL.The
extra_certs_ret
andextra_certs_len
parameters are optional and both may be set toNULL
. If either is non-NULL
, then both must be set.Encrypted PKCS12 bags and PKCS8 private keys are supported. However, only password based security, and the same password for all operations, are supported.
A PKCS12 file may contain many keys and/or certificates, and there is no way to identify which key/certificate pair you want. You should make sure the PKCS12 file only contain one key/certificate pair and/or one CRL.
It is believed that the limitations of this function are acceptable for common usage, and that any more flexibility would introduce complexity that would make it harder to use this functionality at all.
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.Returns: On success,
GNUTLS_E_SUCCESS
(0) is returned, otherwise a negative error value.Since: 3.1
int gnutls_pkcs12_bag_get_data (gnutls_pkcs12_bag_t bag, int indx, gnutls_datum_t * data)
int gnutls_pkcs12_bag_get_key_id (gnutls_pkcs12_bag_t bag, int indx, gnutls_datum_t * id)
int gnutls_pkcs12_bag_get_friendly_name (gnutls_pkcs12_bag_t bag, int indx, char ** name)
The functions below are used to generate a PKCS #12 structure. An example of their usage is shown at PKCS #12 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, int indx, const gnutls_datum_t * id)
int gnutls_pkcs12_bag_set_friendly_name (gnutls_pkcs12_bag_t bag, int indx, const char * name)
OpenSSL encrypted keys
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.
- Function: int gnutls_x509_privkey_import_openssl (gnutls_x509_privkey_t key, const gnutls_datum_t * data, const char * password)
key: The structure 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 thenGNUTLS_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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on February 9, 2014 using texi2html 5.0.