This is gnutls.info, produced by makeinfo version 6.8 from gnutls.texi.
This manual is last updated 9 February 2023 for version 3.7.9 of GnuTLS.
Copyright (C) 2001-2023 Free Software Foundation, Inc.\\ Copyright (C)
2001-2023 Nikos Mavrogiannopoulos
Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation License,
Version 1.3 or any later version published by the Free Software
Foundation; with no Invariant Sections, no Front-Cover Texts, and
no Back-Cover Texts. A copy of the license is included in the
section entitled "GNU Free Documentation License".
INFO-DIR-SECTION Software libraries
START-INFO-DIR-ENTRY
* GnuTLS: (gnutls). GNU Transport Layer Security Library.
END-INFO-DIR-ENTRY
INFO-DIR-SECTION System Administration
START-INFO-DIR-ENTRY
* certtool: (gnutls)certtool Invocation. Manipulate certificates and keys.
* gnutls-serv: (gnutls)gnutls-serv Invocation. GnuTLS test server.
* gnutls-cli: (gnutls)gnutls-cli Invocation. GnuTLS test client.
* gnutls-cli-debug: (gnutls)gnutls-cli-debug Invocation. GnuTLS debug client.
* psktool: (gnutls)psktool Invocation. Simple TLS-Pre-Shared-Keys manager.
* srptool: (gnutls)srptool Invocation. Simple SRP password tool.
END-INFO-DIR-ENTRY
File: gnutls.info, Node: Trusted Platform Module, Prev: Smart cards and HSMs, Up: Hardware security modules and abstract key types
5.4 Trusted Platform Module (TPM)
=================================
In this section we present the Trusted Platform Module (TPM) support in
GnuTLS. Note that we recommend against using TPM with this API because
it is restricted to TPM 1.2. We recommend instead to use PKCS#11
wrappers for TPM such as CHAPS(1) or opencryptoki(2). These will allow
using the standard smart card and HSM functionality (see *note Smart
cards and HSMs::) for TPM keys.
There was a big hype when the TPM chip was introduced into computers.
Briefly it is a co-processor in your PC that allows it to perform
calculations independently of the main processor. This has good and bad
side-effects. In this section we focus on the good ones; these are the
fact that you can use the TPM chip to perform cryptographic operations
on keys stored in it, without accessing them. That is very similar to
the operation of a PKCS #11 smart card. The chip allows for storage and
usage of RSA keys, but has quite some operational differences from PKCS
#11 module, and thus require different handling. The basic TPM
operations supported and used by GnuTLS, are key generation and signing.
That support is currently limited to TPM 1.2.
The next sections assume that the TPM chip in the system is already
initialized and in a operational state. If not, ensure that the TPM
chip is enabled by your BIOS, that the 'tcsd' daemon is running, and
that TPM ownership is set (by running 'tpm_takeownership').
In GnuTLS the TPM functionality is available in 'gnutls/tpm.h'.
* Menu:
* Keys in TPM::
* Key generation::
* Using keys::
* tpmtool Invocation::
---------- Footnotes ----------
(1) <https://github.com/google/chaps-linux>
(2) <https://sourceforge.net/projects/opencryptoki/>
File: gnutls.info, Node: Keys in TPM, Next: Key generation, Up: Trusted Platform Module
5.4.1 Keys in TPM
-----------------
The RSA keys in the TPM module may either be stored in a flash memory
within TPM or stored in a file in disk. In the former case the key can
provide operations as with PKCS #11 and is identified by a URL. The URL
is described in [*note TPMURI::] and is of the following form.
tpmkey:uuid=42309df8-d101-11e1-a89a-97bb33c23ad1;storage=user
It consists from a unique identifier of the key as well as the part of
the flash memory the key is stored at. The two options for the storage
field are 'user' and 'system'. The user keys are typically only
available to the generating user and the system keys to all users. The
stored in TPM keys are called registered keys.
The keys that are stored in the disk are exported from the TPM but in an
encrypted form. To access them two passwords are required. The first
is the TPM Storage Root Key (SRK), and the other is a key-specific
password. Also those keys are identified by a URL of the form:
tpmkey:file=/path/to/file
When objects require a PIN to be accessed the same callbacks as with
PKCS #11 objects are expected (see *note Accessing objects that require
a PIN::). Note that the PIN function may be called multiple times to
unlock the SRK and the specific key in use. The label in the key
function will then be set to 'SRK' when unlocking the SRK key, or to
'TPM' when unlocking any other key.
File: gnutls.info, Node: Key generation, Next: Using keys, Prev: Keys in TPM, Up: Trusted Platform Module
5.4.2 Key generation
--------------------
All keys used by the TPM must be generated by the TPM. This can be done
using *note gnutls_tpm_privkey_generate::.
-- Function: int gnutls_tpm_privkey_generate (gnutls_pk_algorithm_t PK,
unsigned int BITS, const char * SRK_PASSWORD, const char *
KEY_PASSWORD, gnutls_tpmkey_fmt_t FORMAT,
gnutls_x509_crt_fmt_t PUB_FORMAT, gnutls_datum_t * PRIVKEY,
gnutls_datum_t * PUBKEY, unsigned int FLAGS)
PK: the public key algorithm
BITS: the security bits
SRK_PASSWORD: a password to protect the exported key (optional)
KEY_PASSWORD: the password for the TPM (optional)
FORMAT: the format of the private key
PUB_FORMAT: the format of the public key
PRIVKEY: the generated key
PUBKEY: the corresponding public key (may be null)
FLAGS: should be a list of GNUTLS_TPM_* flags
This function will generate a private key in the TPM chip. The
private key will be generated within the chip and will be exported
in a wrapped with TPM's master key form. Furthermore the wrapped
key can be protected with the provided 'password' .
Note that bits in TPM is quantized value. If the input value is
not one of the allowed values, then it will be quantized to one of
512, 1024, 2048, 4096, 8192 and 16384.
Allowed flags are:
*Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
otherwise a negative error value.
*Since:* 3.1.0
'INT *note gnutls_tpm_get_registered:: (gnutls_tpm_key_list_t * LIST)'
'VOID *note gnutls_tpm_key_list_deinit:: (gnutls_tpm_key_list_t LIST)'
'INT *note gnutls_tpm_key_list_get_url:: (gnutls_tpm_key_list_t LIST, unsigned int IDX, char ** URL, unsigned int FLAGS)'
-- Function: int gnutls_tpm_privkey_delete (const char * URL, const
char * SRK_PASSWORD)
URL: the URL describing the key
SRK_PASSWORD: a password for the SRK key
This function will unregister the private key from the TPM chip.
*Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
otherwise a negative error value.
*Since:* 3.1.0
File: gnutls.info, Node: Using keys, Next: tpmtool Invocation, Prev: Key generation, Up: Trusted Platform Module
5.4.3 Using keys
----------------
Importing keys
..............
The TPM keys can be used directly by the abstract key types and do not
require any special structures. Moreover functions like *note
gnutls_certificate_set_x509_key_file2:: can access TPM URLs.
'INT *note gnutls_privkey_import_tpm_raw:: (gnutls_privkey_t PKEY, const gnutls_datum_t * FDATA, gnutls_tpmkey_fmt_t FORMAT, const char * SRK_PASSWORD, const char * KEY_PASSWORD, unsigned int FLAGS)'
'INT *note gnutls_pubkey_import_tpm_raw:: (gnutls_pubkey_t PKEY, const gnutls_datum_t * FDATA, gnutls_tpmkey_fmt_t FORMAT, const char * SRK_PASSWORD, unsigned int FLAGS)'
-- Function: int gnutls_privkey_import_tpm_url (gnutls_privkey_t PKEY,
const char * URL, const char * SRK_PASSWORD, const char *
KEY_PASSWORD, unsigned int FLAGS)
PKEY: The private key
URL: The URL of the TPM key to be imported
SRK_PASSWORD: The password for the SRK key (optional)
KEY_PASSWORD: A password for the key (optional)
FLAGS: One of the GNUTLS_PRIVKEY_* flags
This function will import the given private key to the abstract
'gnutls_privkey_t' type.
Note that unless 'GNUTLS_PRIVKEY_DISABLE_CALLBACKS' is specified,
if incorrect (or NULL) passwords are given the PKCS11 callback
functions will be used to obtain the correct passwords. Otherwise
if the SRK password is wrong 'GNUTLS_E_TPM_SRK_PASSWORD_ERROR' is
returned and if the key password is wrong or not provided then
'GNUTLS_E_TPM_KEY_PASSWORD_ERROR' is returned.
*Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
otherwise a negative error value.
*Since:* 3.1.0
-- Function: int gnutls_pubkey_import_tpm_url (gnutls_pubkey_t PKEY,
const char * URL, const char * SRK_PASSWORD, unsigned int
FLAGS)
PKEY: The public key
URL: The URL of the TPM key to be imported
SRK_PASSWORD: The password for the SRK key (optional)
FLAGS: should be zero
This function will import the given private key to the abstract
'gnutls_privkey_t' type.
Note that unless 'GNUTLS_PUBKEY_DISABLE_CALLBACKS' is specified, if
incorrect (or NULL) passwords are given the PKCS11 callback
functions will be used to obtain the correct passwords. Otherwise
if the SRK password is wrong 'GNUTLS_E_TPM_SRK_PASSWORD_ERROR' is
returned.
*Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
otherwise a negative error value.
*Since:* 3.1.0
Listing and deleting keys
.........................
The registered keys (that are stored in the TPM) can be listed using one
of the following functions. Those keys are unfortunately only
identified by their UUID and have no label or other human friendly
identifier. Keys can be deleted from permanent storage using *note
gnutls_tpm_privkey_delete::.
'INT *note gnutls_tpm_get_registered:: (gnutls_tpm_key_list_t * LIST)'
'VOID *note gnutls_tpm_key_list_deinit:: (gnutls_tpm_key_list_t LIST)'
'INT *note gnutls_tpm_key_list_get_url:: (gnutls_tpm_key_list_t LIST, unsigned int IDX, char ** URL, unsigned int FLAGS)'
-- Function: int gnutls_tpm_privkey_delete (const char * URL, const
char * SRK_PASSWORD)
URL: the URL describing the key
SRK_PASSWORD: a password for the SRK key
This function will unregister the private key from the TPM chip.
*Returns:* On success, 'GNUTLS_E_SUCCESS' (0) is returned,
otherwise a negative error value.
*Since:* 3.1.0
File: gnutls.info, Node: tpmtool Invocation, Prev: Using keys, Up: Trusted Platform Module
5.4.4 Invoking tpmtool
----------------------
Program that allows handling cryptographic data from the TPM chip.
tpmtool help/usage ('-?')
.........................
The text printed is the same whether selected with the 'help' option
('--help') or the 'more-help' option ('--more-help'). 'more-help' will
print the usage text by passing it through a pager program. 'more-help'
is disabled on platforms without a working 'fork(2)' function. The
'PAGER' environment variable is used to select the program, defaulting
to 'more'. Both will exit with a status code of 0.
tpmtool - GnuTLS TPM tool
Usage: tpmtool [ -<flag> [<val>] | --<name>[{=| }<val>] ]...
None:
-d, --debug=num Enable debugging
- it must be in the range:
0 to 9999
--infile=file Input file
- file must pre-exist
--outfile=str Output file
--generate-rsa Generate an RSA private-public key pair
--register Any generated key will be registered in the TPM
- requires the option 'generate-rsa'
--signing Any generated key will be a signing key
- prohibits the option 'legacy'
- requires the option 'generate-rsa'
--legacy Any generated key will be a legacy key
- prohibits the option 'signing'
- requires the option 'generate-rsa'
--user Any registered key will be a user key
- prohibits the option 'system'
- requires the option 'register'
--system Any registered key will be a system key
- prohibits the option 'user'
- requires the option 'register'
--pubkey=str Prints the public key of the provided key
--list Lists all stored keys in the TPM
--delete=str Delete the key identified by the given URL (UUID)
--test-sign=str Tests the signature operation of the provided object
--sec-param=str Specify the security level [low, legacy, medium, high, ultra]
--bits=num Specify the number of bits for key generate
--inder Use the DER format for keys
--outder Use DER format for output keys
--srk-well-known SRK has well known password (20 bytes of zeros)
Version, usage and configuration options:
-v, --version[=arg] output version information and exit
-h, --help display extended usage information and exit
-!, --more-help extended usage information passed thru pager
Options are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
Program that allows handling cryptographic data from the TPM chip.
Please send bug reports to: <bugs@gnutls.org>
debug option (-d).
..................
This is the "enable debugging" option. This option takes a
ArgumentType.NUMBER argument. Specifies the debug level.
generate-rsa option.
....................
This is the "generate an rsa private-public key pair" option. Generates
an RSA private-public key pair in the TPM chip. The key may be stored
in file system and protected by a PIN, or stored (registered) in the TPM
chip flash.
user option.
............
This is the "any registered key will be a user key" option.
This option has some usage constraints. It:
* must not appear in combination with any of the following options:
system.
* must appear in combination with the following options: register.
The generated key will be stored in a user specific persistent storage.
system option.
..............
This is the "any registered key will be a system key" option.
This option has some usage constraints. It:
* must not appear in combination with any of the following options:
user.
* must appear in combination with the following options: register.
The generated key will be stored in system persistent storage.
test-sign option.
.................
This is the "tests the signature operation of the provided object"
option. This option takes a ArgumentType.STRING argument 'url'. It can
be used to test the correct operation of the signature operation. This
operation will sign and verify the signed data.
sec-param option.
.................
This is the "specify the security level [low, legacy, medium, high,
ultra]" option. This option takes a ArgumentType.STRING argument
'Security parameter'. This is alternative to the bits option. Note
however that the values allowed by the TPM chip are quantized and given
values may be rounded up.
inder option.
.............
This is the "use the der format for keys" option. The input files will
be assumed to be in the portable DER format of TPM. The default format
is a custom format used by various TPM tools
outder option.
..............
This is the "use der format for output keys" option. The output will be
in the TPM portable DER format.
version option (-v).
....................
This is the "output version information and exit" option. This option
takes a ArgumentType.KEYWORD argument. Output version of program and
exit. The default mode is 'v', a simple version. The 'c' mode will
print copyright information and 'n' will print the full copyright
notice.
help option (-h).
.................
This is the "display extended usage information and exit" option.
Display usage information and exit.
more-help option (-!).
......................
This is the "extended usage information passed thru pager" option. Pass
the extended usage information through a pager.
tpmtool exit status
...................
One of the following exit values will be returned:
'0 (EXIT_SUCCESS)'
Successful program execution.
'1 (EXIT_FAILURE)'
The operation failed or the command syntax was not valid.
tpmtool See Also
................
p11tool (1), certtool (1)
tpmtool Examples
................
To generate a key that is to be stored in file system use:
$ tpmtool --generate-rsa --bits 2048 --outfile tpmkey.pem
To generate a key that is to be stored in TPM's flash use:
$ tpmtool --generate-rsa --bits 2048 --register --user
To get the public key of a TPM key use:
$ tpmtool --pubkey tpmkey:uuid=58ad734b-bde6-45c7-89d8-756a55ad1891;storage=user \
--outfile pubkey.pem
or if the key is stored in the file system:
$ tpmtool --pubkey tpmkey:file=tmpkey.pem --outfile pubkey.pem
To list all keys stored in TPM use:
$ tpmtool --list
File: gnutls.info, Node: How to use GnuTLS in applications, Next: GnuTLS application examples, Prev: Hardware security modules and abstract key types, Up: Top
6 How to use GnuTLS in applications
***********************************
* Menu:
* Introduction to the library::
* Preparation::
* Session initialization::
* Associating the credentials::
* Setting up the transport layer::
* TLS handshake::
* Data transfer and termination::
* Buffered data transfer::
* Handling alerts::
* Priority Strings::
* Selecting cryptographic key sizes::
* Advanced topics::
File: gnutls.info, Node: Introduction to the library, Next: Preparation, Up: How to use GnuTLS in applications
6.1 Introduction
================
This chapter tries to explain the basic functionality of the current
GnuTLS library. Note that there may be additional functionality not
discussed here but included in the library. Checking the header files
in '/usr/include/gnutls/' and the manpages is recommended.
* Menu:
* General idea::
* Error handling::
* Common types::
* Debugging and auditing::
* Thread safety::
* Running in a sandbox::
* Sessions and fork::
* Callback functions::
File: gnutls.info, Node: General idea, Next: Error handling, Up: Introduction to the library
6.1.1 General idea
------------------
A brief description of how GnuTLS sessions operate is shown at *note
Figure 6.1: fig-gnutls-design. This section will become more clear when
it is completely read. As shown in the figure, there is a read-only
global state that is initialized once by the global initialization
function. This global structure, among others, contains the memory
allocation functions used, structures needed for the ASN.1 parser and
depending on the system's CPU, pointers to hardware accelerated
encryption functions. This structure is never modified by any GnuTLS
function, except for the deinitialization function which frees all
allocated memory and must be called after the program has permanently
finished using GnuTLS.
[image src="gnutls-internals.png"