Creating an Identity

Most SSH implementations include a program for creating key pairs. We cover ssh-keygen from OpenSSH and Tectia.

OpenSSH uses the program ssh-keygen to create key pairs. [2.4.2] Let's go into more detail about this program for creating new keys or modifying existing keys.

When creating a new key, you must indicate the key type (DSA or RSA) using the -t flag:

    $ ssh-keygen -t dsa

You may also specify these options for creating keys:

If you use both -f (specify output file) and -N (specify passphrase), ssh-keygen issues no prompts. Therefore, you can automate key generation using these options (and perhaps redirecting output to /dev/null):

    $ ssh-keygen -f mykey -N secretword

You might use this technique to automate generation of a large number of keys for some purpose. Use it carefully, though, and always on a secure machine. The password on the command line is probably visible to other users on the same machine via ps or similar programs; and if you're scripting with this technique, obviously the passphrases shouldn't be kept in files for long.

In addition to creating keys, ssh-keygen can manipulate existing keys in the following ways:

When you make changes to a key, such as its passphrase or comment, the changes are applied to the key file only. If you have keys loaded in an SSH agent, the copies in the agent don't get changed. For instance, if you list the keys in the agent with ssh-add -l (lowercase L) after changing the comment, you still see the old comment in the agent. To make the changes take effect in the agent, unload and reload the affected keys.

Tectia also uses a program named ssh-keygen to create key pairs and manipulate existing keys.

In addition to creating keys, ssh-keygen can operate on keys in the following ways:

Option

Extract/convert from...

To...

a Handy if you ever lose your public-key file.

  

-1

SSH1 key

SECSH key

--import-public-key

OpenSSH public key

SECSH public key

--import-private-key

OpenSSH private key, unencrypted only

SECSH private key

--import-ssh1-authorized-keys

An OpenSSH or SSH1 authorized_keys file

Tectia authorization file, plus an individual file for each referenced public key

-D

SECSH private key

SECSH public key a

-x

X.509 private key

SECSH private key

-k

PKCS 12 file

SECSH certificate and private key

-7

PKCS 7 file

Certificates from that file

ssh-keygen also gives you some control over input, output, and diagnostics:

Finally, ssh-keygen has one guru-level advanced option, -r, for affecting the random numbers used for key generation. It causes ssh-keygen to modify ~/.ssh2/random_seed using data you enter on standard input. [3.6.4] The Tectia manpages call this "stirring data into the random pool." Note that the program doesn't prompt you to enter data, it just sits there looking like it's hung. When this occurs, type as much data as you like and press the EOF character (Control-D in most shells):

    $ ssh-keygen -r
    I am stirring the random pool.
    blah blah blah
    ^D
    Stirred in 46 bytes.

See Table 6-1 for a description of ssh-keygen options.

Table 6-1. ssh-keygen options

ssh-keygen feature

OpenSSH

Tectia

a Any illegal argument, such as -h, causes a help message to print.

b You might need to escape the question mark in your shell, e.g., -\?.

c The key file format used by SSH Tectia and several other implementations, but not OpenSSH.

Set number of bits

-b bits

-b bits

Set output file

-f file

final argument of the command

Overwrite output file if present

 

--overwrite [ yes|no ]

Set comment string

-C comment

-c comment

Change comment string

-c

-e file

Set (new) passphrase

-N phrase

-p phrase

Set empty passphrase

-N `'

-P

Specify current passphrase

-P

 

Change passphrase

-p

-e file

Set encryption algorithm

-t algorithm

-t algorithm

Change encryption algorithm

-u

 

Derive public key from private

 

-D file

Quieter output

-q

-q

Describe key

 

-i file

Set numeric base for printing key information

 

-B base

Print version number

-V

-V

Print help message

-h a

-h, -? b

Print debugging information

 

-d debug_spec

Use data from stdin for randomness

 

-r

Print a key's fingerprint

- l or -B

-F file

Convert from SSH-1 to SSH-2 format

 

-1 file

   

Convert OpenSSH private to Tectia public

-e

 

Convert OpenSSH private to Tectia private

 

--import-private-key

Convert Tectia public to OpenSSH public

-i

--import-public-key

Extract OpenSSH private to public

-y

 

Convert authorized_keys to authorization file

 

--import-ssh1-authorized-keys

Convert X.509 key to SECSH format c

 

-x file

Convert PKCS 12 file to SECSH format

 

-k file

Convert PKCS 7 file to SECSH format

 

-7 file

Choose your passphrases carefully. Make them at least 10 characters long, containing a mix of uppercase and lowercase letters, digits, and nonalphanumeric symbols. At the same time, you want the passphrase to be easy to remember, but hard for others to guess. Don't use your name, username, phone number, or other easily guessed information in the passphrase. Coming up with an effective passphrase can be a chore, but the added security is worth it.

If you forget a passphrase, you are out of luck: the corresponding SSH private key becomes unusable because you can't decrypt it. The same encryption that makes SSH so secure also makes passphrases impossible to recover. You have to abandon your SSH key, generate a new one, and choose a new passphrase for it. You must also install the new public key on every machine that holds the original one.

As we saw in Chapter 3, the SSH Transport Protocol uses the Diffie-Hellman key-agreement algorithm to generate cryptographic session keys for the SSH connection. [3.8.1.3] One parameter to this algorithm is a mathematical structure from algebra known as a "group"; specifically, a finite integer group with respect to multiplication modulo a prime. In the initial SSH protocol, a single fixed group was used for the key exchange. Due to concern over possible future attacks against this fixed parameter, an extension was created to allow the group to be negotiated, and this extension is now widely implemented.

The OpenSSH server selects the groups to be offered the client from the file /etc/moduli. OpenSSH comes with a moduli file defining a set of suitable groups, and for most people this is sufficient; there is no pressing need to regenerate them. On particularly slow systems, you might edit this file to select groups with a smaller prime modulus, to speed up the key exchange.

If you like, you can generate your own set of key-exchange groups using ssh-keygen -G. This usage is quite technical and infrequently used, so we won't delve further into it here; refer to the ssh-keygen manpage, in the section "MODULI GENERATION," for details. You can also see an example in OpenBSD's usr/src/etc/Makefile for OpenSSH, e.g.: