### `ntp-keygen` - generate public and private keys

from Alice's Adventures in Wonderland, Lewis Carroll

Alice holds the key.

Last update: 21:04 UTC Monday, December 09, 2002

#### Related Links

#### Table of Contents

- Introduction
- Running the program
- Trusted Hosts and Groups
- Identity Schemes
- Example
- Command Line Options
- Symmetric Key File
- Private Key Files
- Identification Parameter Files
- Certificates
- Leapseconds Table
- File Formats
- Bugs

#### Introduction

This program generates cryptographic data files used by the NTPv4 authentication and identification schemes. It generates MD5 key files used in symmetric key cryptography. In addition, if the OpenSSL software library has been installed, it generates key, certificate and parameter files used in public key cryptography. These files are used for cookie encryption, digital signature and challenge/response identification algorithms compatible with the Internet standard security infrastructure.

All files are in PEM-encoded printable ASCII format, so they can be embedded as MIME attachments in mail to other sites and certificate authorities. Files containing private values can be password encrypted. File names begin with the prefix `ntpkey_` and end with the postfix `_hostname.filestamp`, where `hostname` is the owner name, usually the string returned by the Unix `gethostname()` routine, and `filestamp` is the NTP seconds when the file was generated, in decimal digits. This both guarantees uniqueness and simplifies maintenance procedures, since all files can be quickly removed by a `rm ntpkey*` command or all files generated at a specific time can be removed by a `rm *filestamp` command. To further reduce the risk of misconfiguration, the first two lines of a file contain the file name and generation date and time as comments.

All files are installed by default in the keys directory `/usr/local/etc`, which is normally in a shared filesystem in NFS-mounted networks. The actual location of the keys directory or each file can be overridden by configuration commands, but this is not recommended. Normally, the files for each host are generated by that host and used only by that host, although exceptions exist as noted later on this page.

Normally, files containing private values, including the host key, sign key and identification parameters, are permitted root read/write-only; while others containing public values are permitted world readable. Alternatively, files containing private values can be encrypted with a chosen password and all files permitted world readable, which simplifies maintenance in shared file systems. Since uniqueness is insured by the hostname and file name extensions, the files for a NFS server and dependent clients can all be installed in the same shared directory.

The recommended practice is to keep the file name extensions when installing a file and to install a soft link from the generic names specified elsewhere on this page to the generated files. This allows new file generations to be activated simply by changing the link. However, `ntpd` parses the link name when present to extract the filestamp and sends it along with the public key and host name when requested. This allows clients to verify that the file and generation times are always current. The `ntp-keygen` program uses the same timestamp extension for all files generated at one time, so each generation is distinct and can be readily recognized in monitoring data.

As explained in the Authentication Options page, all cryptographically sound key generation schemes must have means to randomize the entropy seed used to initialize the internal random number generator. The OpenSSL library uses a designated random seed file for this purpose. The file must be available when starting the NTP daemon and `ntp-keygen` program. If a site supports OpenSSL or its companion OpenSSH, it is very likely that means to do this are already available.

#### Running the program

The safest way to run the `ntp-keygen` program is logged in directly as root. The recommended procedure is change to the keys directory, usually `/ust/local/etc`, then run the program. When run for the first time, or if all `ntpkey` files have been removed, the program generates a RSA host key file and matching RSA-MD5 certificate file, which is all that is necessary in many cases. The program also generates soft links from the generic names to the respective files. If run again, the program uses the same host key file, but generates a new certificate file and link.

The host key is used to encrypt the cookie when required and so must be RSA type. By default, the host key is also the sign key used to encrypt signatures. When necessary, a different sign key can be specified and this can be either RSA or DSA type. By default, the message digest type is MD5, but any combination of sign key type and message digest type supported by the OpenSSL library can be specified.

Running the program as other than root and using the Unix `su` command to assume root may not work properly, since by default the OpenSSL library looks for the random seed file `.rnd` in the user home directory. However, there should be only one `.rnd`, most conveniently in the root directory, so it is convenient to define the `$RANDFILE` environment variable used by the OpenSSL library as the path to `/.rnd`.

Installing the keys as root might not work in NFS-mounted shared file systems, as NFS clients may not be able to write to the shared keys directory, even as root. In this case, NFS clients can specify the files in another directory such as `/etc` using the `keysdir` command. There is no need for one client to read the keys and certificates of other clients or servers, as these data are obtained automatically by the Autokey protocol.

Ordinarily, cryptographic files are generated by the host that uses them, but it is possible for a trusted agent (TA) to generate these files for other hosts; however, in such cases the private values should always be password-encrypted. The password is supplied as the `-p` option to the `ntp-keygen` program and as the `pw` subcommand in the `ntpd` configuration file. The owner name and trusted name default to the name of the host generating the files, but can be changed by command line options. It is convenient to designate the owner name and trusted name as the subject and issuer fields, respectively, of the certificate. The owner name is also used for the host and sign key files, while the trusted name is used for the identity files.

#### Trusted Hosts and Groups

Each cryptographic configuration involves selection of a signature scheme and identification scheme, called a cryptotype, as explained in the Authentication Options page. The default cryptotype uses RSA encryption, MD5 message digest and default (TC) identification. First, configure a NTP subnet including a number of low-stratum time servers from which all other subnet members derive synchronization directly or indirectly. These servers are considered trusted and will have trusted certificates. All others will automatically and dynamically build authoritative certificate trails to these servers.

On each trusted server as root, change to the keys directory. To insure a fresh fileset, remove all `ntpkey` files. Then run `ntp-keygen -T` to generate keys and a trusted certificate. On all other hosts do the same, but leave off the `-T` flag to generate keys and nontrusted certificates. When complete, start the NTP daemons beginning at the lowest stratum and working up the tree. It may take some time for Autokey to instantiate the certificate trails throughout the subnet, but setting up the environment is completely automatic. The `ntp-keygen` program can be run to refresh the certificate without affecting ongoing operations; however, if the keys are refreshed, the NTP daemon should be restarted.

NTP groups can be used to define cryptographic compartments, as described on the Autokey Protocol page. It is important that every host in the group be able to construct a certificate trail to one or more trusted hosts in the same group. Ordinarily, each group host runs the Autokey protocol to obtain the certificates for all hosts along the trail to one or more trusted hosts. This requires the configuration file in all hosts to be engineered so that, even under anticipated failure conditions, the NTP subnet will form such that every group host can find a trail to at least one trusted host. If the trail is broken so that no trusted certificate can be found, a host will loop forever (at low rate) until a trail becomes available.

A host can be a member of more than one group if it has the necessary credentials. It might be the case, for example, that the servers in one corporation be members of the corporate group, as well as clients of a national standards group from which it imports time. In such cases a trusted host exports server credentials when operating as a server and imports client credentials of the group specific to each association. For instance, trusted server alice exports her server credentials to dependent clients, but imports bob client credentials for the associations where the certificate trail ends at trusted host bob in a different group.

After setting up the environment it is advisable to update certificates from time to time, if only to extend the validity interval. Simply run `ntp-keygen` with the same flags as before to generate new certificates using existing keys. The next time `ntpd` is started, it will load the new certificate and restart the protocol. Since the keys have not changed, other dependent hosts will continue as usual until signatures are refreshed and the protocol is restarted, which occurs about once per day.

#### Identity Schemes

As mentioned on the Autonomous Authentication page, the default TC identity scheme is vulnerable to a middleman attack. However, there are more secure identity schemes available, including PC, IFF, GQ and MV described in the Identification Schemes page. These schemes are based on a TA and a group of hosts deriving trust from the TA. The TA is not necessarily one of the group hosts, but often is. Group membership is defined by private and public keys generated by the TA and distributed by secure means to all group hosts.

In some schemes there are separate keys for servers and clients. A server can also be a client of another server, but a client can never be a server for another client. In general, a trusted host or TA is always a server and never a client of the same group, so these hosts have only server keys. Note that the name of the group is the subject and issuer names in the trusted certificate; that is, the certificate is self-signed.

The PC scheme is set up as follows. On TA alice run `ntp-keygen -P -p password` to generate the host key file `ntpkey_RSAkey_alice.filestamp` and private certificate file `ntpkey_RSA-MD5_cert_alice.filestamp`. Do not generate keys and certificates on the other group hosts; however, be sure to specify `pw password` in the `crypto` command in all NTP configuration files. Note that, as with all encrypted files, the `ntp-keygen` program prompts for the password if it reads an encrypted file and the `-p` option is not present.

Copy the host key file and private certificate file to all group hosts. On each host bob install a soft link from the generic name `ntpkey_host_bob` to the host key file and soft link `ntpkey_cert_bob` to the private certificate file. Note the generic links are on bob, but point to files generated by alice as the trusted host. In this scheme it is not possible to refresh either the keys or certificates in any host without copying them to all other hosts in the group.

The IFF scheme is set up as follows. On TA alice run the `ntp-keygen` program with the `-I -p password` options to produce the server key file `ntpkey_IFFpar_alice.filestamp` and client key file `ntpkey_IFFkey_alice.filestamp`. The only difference between these files is the server has the private group key and the client does not; therefore, a client cannot masquerade as a rogue server. Proceed as in the TC scheme to generate keys and certificates for each group host. Then, copy the server key file to all group hosts that can operate as servers or clients of other servers and the client key file to group hosts that can operate only as clients. On all group servers and clients install a soft link from the generic `ntpkey_iff_alice` to the client key file. On each server bob install a soft link from generic `ntpkey_iff_bob` to the server key file. As the IFF scheme is independent of keys and certificates, these files can be refreshed as needed.

The GQ scheme is set up as follows. On TA alice run the `ntp-keygen` program with the `-G -p password` options to produce the server/client key file `ntpkey_GQpar_alice.filestamp`. Proceed as in the TC scheme to generate keys and certificates for each group host. Then, copy the server/client key file to all group hosts. On all group servers and clients install a soft link from the generic `ntpkey_gq_alice` to this file. On each server bob install a soft link from generic `ntpkey_gq_bob` to this file. As the GQ scheme updates the GQ parameters file and certificate at the same time, keys and certificates can be regenerated as needed.

The MV scheme is set up as follows. On TA Alice run the `ntp-keygen` program with the `-V n -p password` options, where n is the number of revokable keys (typically 5) to produce the server key file `ntpkeys_MVpar_alice.filestamp` and client key files `ntpkeys_MVkeyd_alice.filestamp` where `d` is the key number (0 < `d` < n). Then, distribute the client key files throughout the group. It doesn't matter which client key file goes to which group host; they all work the same. But, keep in mind it is possible for a TA-server conspiracy to selectively deactivate a key in case of compromise. On each client dave install a soft link from generic `ntpkey_mv_alice` to the client key file. On each server bob install a soft link from generic `ntpkey_mv_bob` to the server key file. Note that the MV scheme is independent of host key and certificates, these files can be refreshed as needed.

If it is necessary to use a different sign key or different digest/signature scheme than the default, run `ntp-keygen` with the `-S` option and either `RSA` or `DSA` argument as needed. The most often need to do this is when a DSA-signed certificate is used. If `ntp-keygen` is run again without the option, it will generate a new certificate using the existing sign key. If it is necessary to use a different certificate scheme than the default, run `ntp-keygen` with the `-c` option and selected scheme as needed. If `ntp-keygen` is run again without the option, it will generate a new certificate using the same scheme and existing sign key.

#### Example

First, generate the host key, trusted host certificate and IFF identity file for trusted host whimsy. The `ntp-keygen` program on whimsy produces the following typescript.

    whimsy:/usr/local/etc# ntp-keygen -T -I -pqqsv
    Using OpenSSL version 90607f
    Random seed file /.rnd 1024 bytes
    Generating IFF parameters (512 bits)...
    IFF 0 159 188   1 49 118        2 1 2           3 1 2
    Generating IFF keys (512 bits)...
    Confirm g^(q - b) g^b = 1 mod p: yes
    Confirm g^k = g^(k + b r) g^(q - b) r: yes
    Generating new iff file and link
    ntpkey_iff_whimsy.udel.edu->ntpkey_IFFpar_whimsy.udel.edu.3248306666
    Generating RSA keys (512 bits)...
    RSA 0 16 207    1 11 142                        3 1 4
    Generating new host file and link
    ntpkey_host_whimsy.udel.edu->ntpkey_RSAkey_whimsy.udel.edu.3248306666
    Using host key as sign key
    Generating certificate RSA-MD5
    X509v3 Basic Constraints: critical,CA:TRUE
    X509v3 Key Usage: digitalSignature,keyCertSign
    X509v3 Extended Key Usage: trustRoot
    Generating new cert file and link
    ntpkey_cert_whimsy.udel.edu->ntpkey_RSA-MD5cert_whimsy.udel.edu.3248306666

The following files and links should be on whimsy:

    ntpkey_IFFkey_whimsy.udel.edu.3248306666
    ntpkey_IFFpar_whimsy.udel.edu.3248306666
    MD5cert_whimsy.udel.edu.3248306666
    ntpkey_RSAkey_whimsy.udel.edu.3248306666
    ntpkey_cert_whimsy.udel.edu->ntpkey_RSA-MD5cert_whimsy.udel.edu.3248306666
    ntpkey_host_whimsy.udel.edu->ntpkey_RSAkey_whimsy.udel.edu.3248306666
    ntpkey_iff_whimsy.udel.edu->ntpkey_IFFpar_whimsy.udel.edu.3248306666

Next, generate the host key and nontrusted host certificate for host mort. The `ntp-keygen` program on mort produces the following typescript.

    mort:/usr/local/etc# ntp-keygen
    Using OpenSSL version 90607f
    Random seed file /.rnd 1024 bytes
    Generating RSA keys (512 bits)...
    RSA                                             3 1 2
    Generating new host file and link
    ntpkey_host_mort.udel.edu->ntpkey_RSAkey_mort.udel.edu.3248306679
    Using host key as sign key
    Generating certificate RSA-MD5
    X509v3 Basic Constraints: critical,CA:TRUE
    X509v3 Key Usage: digitalSignature,keyCertSign
    Generating new cert file and link
    ntpkey_cert_mort.udel.edu->ntpkey_RSA-MD5cert_mort.udel.edu.3248306679

On whimsy, copy the file `ntpkey_IFFpar_whimsy.udel.edu.3248306666` to mort.

    whimsy:/usr/local/etc# scp ntpkey_IFFpar_whimsy.udel.edu.3248306666 mort:/usr/local/etc/ntpkey_IFFpar_whimsy.udel.edu.3248306666

On mort, install the links

    mort:/usr/local/etc# ln -s ntpkey_IFFpar_whimsy.udel.edu.3248306666 ntpkey_iff_whimsy.udel.edu
    mort:/usr/local/etc# ln -s ntpkey_IFFpar_whimsy.udel.edu.3248306666 ntpkey_iff_mort.udel.edu

Note the same file is linked from both hosts whimsy and mort if both are to be servers as well as clients. The following files and links should be on mort:

    ntpkey_IFFpar_whimsy.udel.edu.3248306666
    MD5cert_mort.udel.edu.3248306679
    ntpkey_RSAkey_mort.udel.edu.3248306679
    ntpkey_cert_mort.udel.edu->ntpkey_RSA-MD5cert_mort.udel.edu.3248306679
    ntpkey_host_mort.udel.edu->ntpkey_RSAkey_mort.udel.edu.3248306679
    ntpkey_iff_whimsy.udel.edu->ntpkey_IFFpar_whimsy.udel.edu.3248306666  ntpkey_iff_mort.udel.edu->ntpkey_IFFpar_whimsy.udel.edu.3248306666

Following is edited output from the `ntpq` program on whimsy with the `rv` command. Note the self-sgned certificate subject and issuer names are whimsy and the `0x2` confirms the certificate is trusted.

    hostname="whimsy.udel.edu", signature="md5WithRSAEncryption",
    flags=0x80021, hostkey=3248306666, refresh=3248306823,
    cert="whimsy.udel.edu whimsy.udel.edu 0x2 3248306666"

Following is edited output from the `ntpq` program on mort with the `rv` command. Note the self-sgned certificate subject and issuer names are whimsy and the `0x2` confirms the certificate is trusted. Note the Autokey protocol has fetched the trusted certificate for whimsy, confirmed whimsy identity with the IFF scheme and has its certificate signed by whimsy. The `0x3` confirms the certificates have all been validated as well.

    hostname="mort.udel.edu", signature="md5WithRSAEncryption",
    flags=0x80001, hostkey=3248306679, refresh=3248306943,
    cert="mort.udel.edu whimsy.udel.edu 0x3 3248306679",
    cert="whimsy.udel.edu whimsy.udel.edu 0x3 3248306666",
    cert="mort.udel.edu mort.udel.edu 0x3 3248306679"

Following is edited output from the `ntpq` program on mort with the `rv assoc` command. This reveals the association host name, signature scheme, flags (showing the IFF scheme) and trusted host name.

    hostname="whimsy.udel.edu", signature="md5WithRSAEncryption",
    flags=0x83f21, identity="whimsy.udel.edu"

#### Command Line Options

`-c [ RSA-MD2 | RSA-MD5 | RSA-SHA | RSA-SHA1 | RSA-MDC2 | RSA-RIPEMD160 | DSA-SHA | DSA-SHA1 ]`
  Select certificate message digest/signature encryption scheme. Note that RSA schemes must be used with a RSA sign key and DSA schemes must be used with a DSA sign key. The default without this option is `RSA-MD5`.
`-d`
  Enable debugging. This option displays the cryptographic data produced in eye-friendly billboards.
`-e`
  Insert arbitrary X509v3 extension field. Not for amateurs. Usually produces a coredump.
`-G`
  Generate parameters and keys for the GQ identification scheme, obsoleting any that may exist.
`-g`
  Generate keys for the GQ identification scheme using the existing GQ parameters. If the GQ parameters do not yet exist, create them first.
`-H`
  Generate new host keys, obsoleting any that may exist.
`-I`
  Generate parameters for the IFF identification scheme, obsoleting any that may exist.
`-i name`
  Set the trusted name to name. This is used as the subject field in certificates and the file name in host and sign keys.
`-M`
  Generate MD5 keys, obsoleting any that may exist.
`-P`
  Generate a private certificate. By default, the program generates public certificates.
`-p password```
  Encrypt generated files containing private data with the designated `password` and the DES-CBC algorithm.
`-S [ RSA | DSA ]`
  Generate a new sign key of the designated type, obsoleting any that may exist. By default, the program uses the host key as the sign key.
`-s name`
  Set the trusted name to name. This is used for the issuer field in certificates and the file name for identity files.
`-T`
  Generate a trusted certificate. By default, the program generates a non-trusted certificate.
`-V nkeys`
  Generate parameters and keys for the Mu-Varadharajan (MV) identification scheme.

#### Symmetric Key File

The `ntp-keygen` program generates a MD5 symmetric keys file `ntpkey_MD5key_hostname.filestamp`. Since the file contains private shared keys, it should be visible only to root and distributed by secure means to other subnet hosts. The NTP daemon loads the file `ntp.keys`, so `ntp-keygen` installs a soft link from this name to the generated file. Subsequently, similar soft links must be installed by manual or automated means on the other subnet hosts. While this file is not used with the Autokey Version 2 protocol, it is needed to authenticate some remote configuration commands used by the `ntpq` and `ntpdc` utilities.

The symmetric key file contains 16 MD5 keys. Each key consists of 16 characters randomized over the ASCII 93-character printing subset. The file is read by the daemon at the location specified by the `keys` configuration file command. Additional keys consisting of easily remembered passwords should be added by hand for use with the `ntpq` and `ntpdc` programs. While the key identifiers for MD5 keys must be in the range 1-65,535, inclusive, the `ntp-keygen` program uses only the identifiers from 1 to 16. The key identifier for each association is specified as the `key` subcommand with the `server` or `peer` configuration command.

#### Private Key Files

The Autokey protocol requires every host to have a private/public host key pair and an optional private/public sign key pair. The `ntp-keygen` program generates a private key file `ntpkey_keynamekey_hostname.filestamp`, where `keyname` is the name of the public key algorithm type, either `RSA` or `DSA`. Since the host key is used to encrypt some data, it must be RSA type; the sign key file can be a RSA or DSA type. These files contain private values, so should be encrypted or visible only to root. The NTP daemon loads the host key file `ntpkey_host_hostname` and the sign key file `ntpkey_sign_hostname`, so `ntp-keygen` installs soft links to direct these names to the generated files.

#### Certificates

The Internet security infrastructure requires every host to have a digitally signed public certificate. Security procedures require the public key and host credentials, such as host name, responsible person, mail address, etc., to be provided in the form of a X.509 certificate request to a trusted certificate authority or CA. The CA attaches a digital signature to the request and returns the certificate with signature to the requesting party. The integrity of these procedures depend on the certificate trail that ultimately must end on a self-signed certificate provided by a trusted CA. The OpenSSL software library provides routines that can automate this process.

Normally, the `ntp-keygen` program generates certificates which contain only public values, so they can be transmitted and stored without cryptographic protection. With the `-P` option the program generates a certificate including a "X509v3 "Extended Key Usage" extension field with value `private` The intent when this field is present is never to disclose the certificate outside the host on which it is installed. Certificates generated without this option do not contain this field and are by default public.

The Autokey TC identification scheme automates the certificate trail construction and verification process with each server at stratum n operating as a CA for clients at stratum n + 1. The `ntp-keygen` program generates a self-signed X.509v3 public certificate `ntpkey_digestnamecert_hostname.filestamp`, where `digestname` is the name of the digest/signature scheme. The NTP daemon loads the certificate `ntpkey_cert_hostname`, so `ntp-keygen` installs soft links to direct this name to the generated file. The `ntp-keygen` program with the `-T` flag generates a trusted certificate including a "X509v3 Extended Key Usage" extension field with value `trustRoot`. Certificates generated without this option do not contain this field and are by default non-trusted.

Public certificates contain only public values and can be freely transmitted and stored anywhere without further cryptographic protection, although this is rarely necessary. The Autokey protocol retrieves the server certificate and requests the server sign and return the client certificate. Where security considerations require, certificates can be transmitted to an outside trusted certificate authority (CA) and returned as a signed public certificate for use in the Autokey protocol.

The Autokey protocol supports all digest/signature schemes available in the OpenSSL library, including those using the MD2, MD5, SHA, SHA1, MDC2 and RIPE160 message digest algorithms. However, the scheme specified in the certificate must be compatible with the sign key. Certificates using any digest algorithm are compatible with RSA sign keys; however, only SHA and SHA1 certificates are compatible with DSA sign keys.

#### Leapseconds Table

The NIST provides a file documenting the epoch for all historic occasions of leap second insertion since 1972. The leapsecond table shows each epoch of insertion along with the offset of International Atomic Time (TAI) with respect to Coordinated Universal Time (UTC), as disseminated by NTP. The table can be obtained directly from NIST national time servers using `ftp` as the ASCII file `pub/leap-seconds`.

While not strictly a security function, the Autokey protocol provides means to securely retrieve the leapsecond table from a server or peer. Servers load the leapsecond table directly from the file specified in the `crypto` command, with default `ntpkey_leap`, while clients can obtain the table indirectly from the servers using the Autokey protocol. Once loaded, the table can be provided on request to other clients and servers.

#### File Formats

The file formats begin with two lines. The first contains the file name, including the generated hostname and filestamp. The second contains the datestamp in conventional Unix `date` format. Lines beginning with `#` are considered comments and ignored by the daemon. In the `ntp.keys` file, the next 16 lines contain the MD5 keys. If necessary, this file can be further customized using an ordinary text editor. The format is described below. For all other files the cryptographic values are encoded first using ASN.1 encoding rules and then in PEM-encoded printable ASCII format preceded and followed by MIME content identifier lines.

Private/public key files and certificates are compatible with other OpenSSL applications and very likely other libraries as well. Certificates or certificate requests derived from them should be compatible with extant industry practice, although some users might find the interpretation of X509v3 extension fields somewhat liberal. However, the identification parameter files, although encoded as the other files, are probably not compatible with anything other than Autokey.

The format of the symmetric keys file is somewhat different than the other files in the interest of backward compatibility. Since DES-CBC is deprecated in NTPv4, the only key format of interest is MD5 alphanumeric strings. Keys are entered one per line in the format

`keyno type key`

where `keyno` is a positive integer in the range 1-65,535, `type` is the string `MD5` defining the key format and `key` is the key itself, which is a printable ASCII string 16 characters or less in length. Each character is chosen from the 93 printable characters in the range 0x21 through 0x7f excluding space and the '#' character.

Note that the keys used by the `ntpq` and `ntpdc` programs are checked against passwords requested by the programs and entered by hand, so it is generally appropriate to specify these keys in human readable ASCII format.

#### Bugs

It can take quite a while to generate some cryptographic values, from one to several minutes with modern architectures such as UltraSPARC and up to tens of minutes to an hour with older architectures such as SPARC IPC.