Next: , Previous: , Up: Abstract key types   [Contents][Index]

5.1.2 Private keys

An abstract gnutls_privkey_t can be initialized and freed by using the functions below.

int gnutls_privkey_init (gnutls_privkey_t * key)
void gnutls_privkey_deinit (gnutls_privkey_t key)

After initialization its values can be imported from an existing structure like gnutls_x509_privkey_t, but unlike public keys it cannot be exported. That is to allow abstraction over keys stored in hardware that makes available only operations.

int gnutls_privkey_import_x509 (gnutls_privkey_t pkey, gnutls_x509_privkey_t key, unsigned int flags)
int gnutls_privkey_import_pkcs11 (gnutls_privkey_t pkey, gnutls_pkcs11_privkey_t key, unsigned int flags)

Other helper functions that allow directly importing from raw X.509 structures are shown below. Again, as with public keys, private keys can be imported from a hardware module using URLs.

Function: int gnutls_privkey_import_url (gnutls_privkey_t key, const char * url, unsigned int flags)

key: A key of type gnutls_privkey_t

url: A PKCS 11 url

flags: should be zero

This function will import a PKCS11 or TPM URL as a private key. The supported URL types can be checked using gnutls_url_is_supported() .

Returns: On success, GNUTLS_E_SUCCESS (0) is returned, otherwise a negative error value.

Since: 3.1.0

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)
int gnutls_privkey_get_pk_algorithm (gnutls_privkey_t key, unsigned int * bits)
gnutls_privkey_type_t gnutls_privkey_get_type (gnutls_privkey_t key)
int gnutls_privkey_status (gnutls_privkey_t key)

In order to support cryptographic operations using an external API, the following function is provided. This allows for a simple extensibility API without resorting to PKCS #11.

Function: int gnutls_privkey_import_ext4 (gnutls_privkey_t pkey, void * userdata, gnutls_privkey_sign_data_func sign_data_fn, gnutls_privkey_sign_hash_func sign_hash_fn, gnutls_privkey_decrypt_func decrypt_fn, gnutls_privkey_deinit_func deinit_fn, gnutls_privkey_info_func info_fn, unsigned int flags)

pkey: The private key

userdata: private data to be provided to the callbacks

sign_data_fn: callback for signature operations (may be NULL )

sign_hash_fn: callback for signature operations (may be NULL )

decrypt_fn: callback for decryption operations (may be NULL )

deinit_fn: a deinitialization function

info_fn: returns info about the public key algorithm (should not be NULL )

flags: Flags for the import

This function will associate the given callbacks with the gnutls_privkey_t type. At least one of the callbacks must be non-null. If a deinitialization function is provided then flags is assumed to contain GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE .

Note that in contrast with the signing function of gnutls_privkey_import_ext3() , the signing functions provided to this function take explicitly the signature algorithm as parameter and different functions are provided to sign the data and hashes.

The sign_hash_fn is to be called to sign pre-hashed data. The input to the callback is the output of the hash (such as SHA256) corresponding to the signature algorithm. For RSA PKCS1 signatures, the signature algorithm can be set to GNUTLS_SIGN_RSA_RAW , and in that case the data should be handled as if they were an RSA PKCS1 DigestInfo structure.

The sign_data_fn is to be called to sign data. The input data will be he data to be signed (and hashed), with the provided signature algorithm. This function is to be used for signature algorithms like Ed25519 which cannot take pre-hashed data as input.

When both sign_data_fn and sign_hash_fn functions are provided they must be able to operate on all the supported signature algorithms, unless prohibited by the type of the algorithm (e.g., as with Ed25519).

The info_fn must provide information on the signature algorithms supported by this private key, and should support the flags GNUTLS_PRIVKEY_INFO_PK_ALGO , GNUTLS_PRIVKEY_INFO_HAVE_SIGN_ALGO and GNUTLS_PRIVKEY_INFO_PK_ALGO_BITS . It must return -1 on unknown flags.

Returns: On success, GNUTLS_E_SUCCESS (0) is returned, otherwise a negative error value.

Since: 3.6.0

On the private keys where exporting of parameters is possible (i.e., software keys), the following functions are also available.

int gnutls_privkey_export_rsa_raw2 (gnutls_privkey_t key, gnutls_datum_t * m, gnutls_datum_t * e, gnutls_datum_t * d, gnutls_datum_t * p, gnutls_datum_t * q, gnutls_datum_t * u, gnutls_datum_t * e1, gnutls_datum_t * e2, unsigned int flags)
int gnutls_privkey_export_dsa_raw2 (gnutls_privkey_t key, gnutls_datum_t * p, gnutls_datum_t * q, gnutls_datum_t * g, gnutls_datum_t * y, gnutls_datum_t * x, unsigned int flags)
int gnutls_privkey_export_ecc_raw2 (gnutls_privkey_t key, gnutls_ecc_curve_t * curve, gnutls_datum_t * x, gnutls_datum_t * y, gnutls_datum_t * k, unsigned int flags)

Next: , Previous: , Up: Abstract key types   [Contents][Index]