
Format of "---with-colons" listings
===================================

sec::1024:17:6C7EE1B8621CC013:1998-07-07:0:::Werner Koch <werner.koch@guug.de>:
ssb::1536:20:5CE086B5B5A18FF4:1998-07-07:0:::

 1. Field:  Type of record
	    pub = public key
	    sub = subkey (secondary key)
	    sec = secret key
	    ssb = secret subkey (secondary key)
	    uid = user id (only field 10 is used).
	    fpr = fingerprint: (fingerprint is in field 10)
	    pkd = public key data (special field format, see below)

 2. Field:  A letter describing the calculated trust. This is a single
	    letter, but be prepared that additional information may follow
	    in some future versions. (not used for secret keys)
		o = Unknown (this key is new to the system)
		d = The key has been disabled
		r = The key has been revoked
		e = The key has expired
		q = Undefined (no value assigned)
		n = Don't trust this key at all
		m = There is marginal trust in this key
		f = The key is full trusted.
		u = The key is ultimately trusted; this is only used for
		    keys for which the secret key is also available.
 3. Field:  length of key in bits.
 4. Field:  Algorithm:	1 = RSA
		       16 = ElGamal (encrypt only)
		       17 = DSA (sometimes called DH, sign only)
		       20 = ElGamal (sign and encrypt)
	    (for other id's see include/cipher.h)
 5. Field:  KeyID
 6. Field:  Creation Date (in UTC)
 7. Field:  Key expiration date or empty if none.
 8. Field:  Local ID: record number of the dir record in the trustdb.
	    This value is only valid as long as the trustdb is not
	    deleted. You can use "#<local-id> as the user id when
	    specifying a key. This is needed because keyids may not be
	    unique - a program may use this number to access keys later.
 9. Field:  Ownertrust (primary public keys only)
	    This is a single letter, but be prepared that additional
	    information may follow in some future versions.
10. Field:  User-ID.  The value is quoted like a C string to avoid
	    control characters (the colon is quoted "\x3a").

More fields may be added later.

If field 1 has the tag "pkd", a listing looks like this:
pkd:0:1024:B665B1435F4C2 .... FF26ABB:
    !  !   !-- the value
    !  !------ for information number of bits in the value
    !--------- index (eg. DSA goes from 0 to 3: p,q,g,y)



Format of the "--status-fd" output
==================================
Every line is prefixed with "[GNUPG:] ", followed by a keyword with
the type of the status line and a some arguments depending on the
type (maybe none); an application should always be prepared to see
more arguments in future versions.


    GOODSIG	<long keyid>  <username>
	The signature with the keyid is good.

    BADSIG	<long keyid>  <username>
	The signature with the keyid has not been verified okay.

    ERRSIG  <long keyid>  <pubkey_algo> <hash_algo> \
	    <sig_class> <timestamp> <rc>
	It was not possible to check the signature.  This may be
	caused by a missing public key or an unsupported algorithm.
	A RC of 4 indicates unknown algorithm, a 9 indicates a missing
	public key. The other fields give more information about
	this signature.  sig_class is a 2 byte hex-value.

    VALIDSIG	<fingerprint in hex> <sig_creation_date> <sig-timestamp>
	The signature with the keyid is good. This is the same
	as GOODSIG but has the fingerprint as the argument. Both
	status lines ere emitted for a good signature.
	sig-timestamp is the signature creation time in seconds after
	the epoch.

    SIG_ID  <radix64_string>  <sig_creation_date>  <sig-timestamp>
	This is emitted only for signatures of class 0 or 1 which
	have been verified okay.  The string is a signature id
	and may be used in applications to detect replay attacks
	of signed messages.  Note that only DLP algorithms give
	unique ids - others may yield duplicated ones when they
	have been created in the same second.

    ENC_TO  <long keyid>  <keytype>  <keylength>
	The message is encrypted to this keyid.
	keytype is the numerical value of the public key algorithm,
	keylength is the length of the key or 0 if it is not known
	(which is currently always the case).

    NODATA  <what>
	No data has been found. Codes for what are:
	    1 - No armored data.
	    2 - Expected a packet but did not found one.
	    3 - Invalid packet found, this may indicate a non OpenPGP message.
	You may see more than one of these status lines.

    TRUST_UNDEFINED
    TRUST_NEVER
    TRUST_MARGINAL
    TRUST_FULLY
    TRUST_ULTIMATE
	For good signatures one of these status lines are emitted
	to indicate how trustworthy the signature is.  No arguments yet.

    SIGEXPIRED
	The signature key has expired.	No arguments yet.

    KEYREVOKED
	The used key has been revoked by his owner.  No arguments yet.

    BADARMOR
	The ASCII armor is corrupted.  No arguments yet.

    RSA_OR_IDEA
	The RSA or IDEA algorithms has been used in the data.  A
	program might want to fallback to another program to handle
	the data if GnuPG failed.

    SHM_INFO
    SHM_GET
    SHM_GET_BOOL
    SHM_GET_HIDDEN

    NEED_PASSPHRASE <long keyid>  <keytype>  <keylength>
	Issued whenever a passphrase is needed.
	keytype is the numerical value of the public key algorithm
	or 0 if this is not applicable, keylength is the length
	of the key or 0 if it is not known (this is currently always the case).

    NEED_PASSPHRASE_SYM <cipher_algo> <s2k_mode> <s2k_hash>
	Issued whenever a passphrase for symmetric encryption is needed.

    MISSING_PASSPHRASE
	No passphrase was supplied.  An application which encounters this
	message may want to stop parsing immediately because the next message
	will probably be a BAD_PASSPHRASE.  However, if the application
	is a wrapper around the key edit menu functionality it might not
	make sense to stop parsing but simply ignoring the following
	PAD_PASSPHRASE.

    BAD_PASSPHRASE <long keyid>
	The supplied passphrase was wrong or not given.  In the latter case
	you may have seen a MISSING_PASSPHRASE.

    GOOD_PASSPHRASE
	The supplied passphrase was good and the secret key material
	is therefore usable.

    DECRYPTION_FAILED
	The symmetric decryption failed - one reason could be a wrong
	passphrase for a symmetrical encrypted message.

    DECRYPTION_OKAY
	The decryption process succeeded.  This means, that either the
	correct secret key has been used or the correct passphrase
	for a conventional encrypted message was given.  The program
	itself may return an errorcode because it may not be possible to
	verify a signature for some reasons.

    NO_PUBKEY  <long keyid>
    NO_SECKEY  <long keyid>
	The key is not available

    IMPORTED   <long keyid>  <username>
	The keyid and name of the signature just imported

    IMPORTED_RES <count> <no_user_id> <imported> <imported_rsa> <unchanged>
	<n_uids> <n_subk> <n_sigs> <n_revoc> <sec_read> <sec_imported> <sec_dups>
	Final statistics on import process (this is one long line)

    FILE_START <what> <filename>
	Start processing a file <filename>.  <what> indicates the performed
	operation:
	    1 - verify

    FILE_DONE
	Marks the end of a file processing which has been started
	by FILE_START.



Key generation
==============
    Key generation shows progress by printing different characters to
    stderr:
	     "."  Last 10 Miller-Rabin tests failed
	     "+"  Miller-Rabin test succeeded
	     "!"  Reloading the pool with fresh prime numbers
	     "^"  Checking a new value for the generator
	     "<"  Size of one factor decreased
	     ">"  Size of one factor increased

    The prime number for ElGamal is generated this way:

    1) Make a prime number q of 160, 200, 240 bits (depending on the keysize)
    2) Select the length of the other prime factors to be at least the size
       of q and calculate the number of prime factors needed
    3) Make a pool of prime numbers, each of the length determined in step 2
    4) Get a new permutation out of the pool or continue with step 3
       if we have tested all permutations.
    5) Calculate a candidate prime p = 2 * q * p[1] * ... * p[n] + 1
    6) Check that this prime has the correct length (this may change q if
       it seems not to be possible to make a prime of the desired length)
    7) Check whether this is a prime using trial divisions and the
       Miller-Rabin test.
    8) Continue with step 4 if we did not find a prime in step 7.
    9) Find a generator for that prime.

    This algorithm is based on Lim and Lee's suggestion from the
    Crypto '97 proceedings p. 260.



Layout of the TrustDB
=====================
The TrustDB is built from fixed length records, where the first byte
describes the record type.  All numeric values are stored in network
byte order. The length of each record is 40 bytes. The first record of
the DB is always of type 1 and this is the only record of this type.

  Record type 0:
  --------------
    Unused record, can be reused for any purpose.

  Record type 1:
  --------------
    Version information for this TrustDB.  This is always the first
    record of the DB and the only one with type 1.
     1 byte value 1
     3 bytes 'gpg'  magic value
     1 byte Version of the TrustDB (2)
     1 byte marginals needed
     1 byte completes needed
     1 byte max_cert_depth
	    The three items are used to check whether the cached
	    validity value from the dir record can be used.
     1 u32  locked flags
     1 u32  timestamp of trustdb creation
     1 u32  timestamp of last modification which may affect the validity
	    of keys in the trustdb.  This value is checked against the
	    validity timestamp in the dir records.
     1 u32  timestamp of last validation
	    (Used to keep track of the time, when this TrustDB was checked
	     against the pubring)
     1 u32  record number of keyhashtable
     1 u32  first free record
     1 u32  record number of shadow directory hash table
	    It does not make sense to combine this table with the key table
	    because the keyid is not in every case a part of the fingerprint.
     4 bytes reserved for version extension record


  Record type 2: (directory record)
  --------------
    Informations about a public key certificate.
    These are static values which are never changed without user interaction.

     1 byte value 2
     1 byte  reserved
     1 u32   LID     .	(This is simply the record number of this record.)
     1 u32   List of key-records (the first one is the primary key)
     1 u32   List of uid-records
     1 u32   cache record
     1 byte  ownertrust
     1 byte  dirflag
     1 byte  maximum validity of all the user ids
     1 u32   time of last validity check.
     1 u32   Must check when this time has been reached.
	     (0 = no check required)


  Record type 3:  (key record)
  --------------
    Informations about a primary public key.
    (This is mainly used to lookup a trust record)

     1 byte value 3
     1 byte  reserved
     1 u32   LID
     1 u32   next   - next key record
     7 bytes reserved
     1 byte  keyflags
     1 byte  pubkey algorithm
     1 byte  length of the fingerprint (in bytes)
     20 bytes fingerprint of the public key
	      (This is the value we use to identify a key)

  Record type 4: (uid record)
  --------------
    Informations about a userid
    We do not store the userid but the hash value of the userid because that
    is sufficient.

     1 byte value 4
     1 byte reserved
     1 u32  LID  points to the directory record.
     1 u32  next   next userid
     1 u32  pointer to preference record
     1 u32  siglist  list of valid signatures
     1 byte uidflags
     1 byte validity of the key calculated over this user id
     20 bytes ripemd160 hash of the username.


  Record type 5: (pref record)
  --------------
    Informations about preferences

     1 byte value 5
     1 byte   reserved
     1 u32  LID; points to the directory record (and not to the uid record!).
	    (or 0 for standard preference record)
     1 u32  next
     30 byte preference data

  Record type 6  (sigrec)
  -------------
    Used to keep track of key signatures. Self-signatures are not
    stored.  If a public key is not in the DB, the signature points to
    a shadow dir record, which in turn has a list of records which
    might be interested in this key (and the signature record here
    is one).

     1 byte   value 6
     1 byte   reserved
     1 u32    LID	    points back to the dir record
     1 u32    next   next sigrec of this uid or 0 to indicate the
		     last sigrec.
     6 times
	1 u32  Local_id of signators dir or shadow dir record
	1 byte Flag: Bit 0 = checked: Bit 1 is valid (we have a real
			     directory record for this)
			 1 = valid is set (but my be revoked)



  Record type 8: (shadow directory record)
  --------------
    This record is used to reserved a LID for a public key.  We
    need this to create the sig records of other keys, even if we
    do not yet have the public key of the signature.
    This record (the record number to be more precise) will be reused
    as the dir record when we import the real public key.

     1 byte value 8
     1 byte  reserved
     1 u32   LID      (This is simply the record number of this record.)
     2 u32   keyid
     1 byte  pubkey algorithm
     3 byte reserved
     1 u32   hintlist	A list of records which have references to
			this key.  This is used for fast access to
			signature records which are not yet checked.
			Note, that this is only a hint and the actual records
			may not anymore hold signature records for that key
			but that the code cares about this.
    18 byte reserved



  Record Type 10 (hash table)
  --------------
    Due to the fact that we use fingerprints to lookup keys, we can
    implement quick access by some simple hash methods, and avoid
    the overhead of gdbm.  A property of fingerprints is that they can be
    used directly as hash values.  (They can be considered as strong
    random numbers.)
      What we use is a dynamic multilevel architecture, which combines
    hashtables, record lists, and linked lists.

    This record is a hashtable of 256 entries; a special property
    is that all these records are stored consecutively to make one
    big table. The hash value is simple the 1st, 2nd, ... byte of
    the fingerprint (depending on the indirection level).

    When used to hash shadow directory records, a different table is used
    and indexed by the keyid.

     1 byte value 10
     1 byte reserved
     n u32  recnum; n depends on the record length:
	    n = (reclen-2)/4  which yields 9 for the current record length
	    of 40 bytes.

    the total number of such record which makes up the table is:
	 m = (256+n-1) / n
    which is 29 for a record length of 40.

    To look up a key we use the first byte of the fingerprint to get
    the recnum from this hashtable and look up the addressed record:
       - If this record is another hashtable, we use 2nd byte
	 to index this hash table and so on.
       - if this record is a hashlist, we walk all entries
	 until we found one a matching one.
       - if this record is a key record, we compare the
	 fingerprint and to decide whether it is the requested key;


  Record type 11 (hash list)
  --------------
    see hash table for an explanation.
    This is also used for other purposes.

    1 byte value 11
    1 byte reserved
    1 u32  next 	 next hash list record
    n times		 n = (reclen-5)/5
	1 u32  recnum

    For the current record length of 40, n is 7



  Record type 254 (free record)
  ---------------
    All these records form a linked list of unused records.
     1 byte  value 254
     1 byte  reserved (0)
     1 u32   next_free



Packet Headers
===============

GNUPG uses PGP 2 packet headers and also understands OpenPGP packet header.
There is one enhancement used with the old style packet headers:

   CTB bits 10, the "packet-length length bits", have values listed in
   the following table:

      00 - 1-byte packet-length field
      01 - 2-byte packet-length field
      10 - 4-byte packet-length field
      11 - no packet length supplied, unknown packet length

   As indicated in this table, depending on the packet-length length
   bits, the remaining 1, 2, 4, or 0 bytes of the packet structure field
   are a "packet-length field".  The packet-length field is a whole
   number field.  The value of the packet-length field is defined to be
   the value of the whole number field.

   A value of 11 is currently used in one place: on compressed data.
   That is, a compressed data block currently looks like <A3 01 . .  .>,
   where <A3>, binary 10 1000 11, is an indefinite-length packet. The
   proper interpretation is "until the end of the enclosing structure",
   although it should never appear outermost (where the enclosing
   structure is a file).

+  This will be changed with another version, where the new meaning of
+  the value 11 (see below) will also take place.
+
+  A value of 11 for other packets enables a special length encoding,
+  which is used in case, where the length of the following packet can
+  not be determined prior to writing the packet; especially this will
+  be used if large amounts of data are processed in filter mode.
+
+  It works like this: After the CTB (with a length field of 11) a
+  marker field is used, which gives the length of the following datablock.
+  This is a simple 2 byte field (MSB first) containing the amount of data
+  following this field, not including this length field. After this datablock
+  another length field follows, which gives the size of the next datablock.
+  A value of 0 indicates the end of the packet. The maximum size of a
+  data block is limited to 65534, thereby reserving a value of 0xffff for
+  future extensions. These length markers must be inserted into the data
+  stream just before writing the data out.
+
+  This 2 byte filed is large enough, because the application must buffer
+  this amount of data to prepend the length marker before writing it out.
+  Data block sizes larger than about 32k doesn't make any sense. Note
+  that this may also be used for compressed data streams, but we must use
+  another packet version to tell the application that it can not assume,
+  that this is the last packet.


Usage of gdbm files for keyrings
================================
    The key to store the keyblock is it's fingerprint, other records
    are used for secondary keys.  fingerprints are always 20 bytes
    where 16 bit fingerprints are appended with zero.
    The first byte of the key gives some information on the type of the
    key.
      1 = key is a 20 bit fingerprint (16 bytes fpr are padded with zeroes)
	  data is the keyblock
      2 = key is the complete 8 byte keyid
	  data is a list of 20 byte fingerprints
      3 = key is the short 4 byte keyid
	  data is a list of 20 byte fingerprints
      4 = key is the email address
	  data is a list of 20 byte fingerprints

    Data is prepended with a type byte:
      1 = keyblock
      2 = list of 20 byte padded fingerprints
      3 = list of list fingerprints (but how to we key them?)




Other Notes
===========
    * For packet version 3 we calculate the keyids this way:
	RSA	:= low 64 bits of n
	ELGAMAL := build a v3 pubkey packet (with CTB 0x99) and calculate
		   a rmd160 hash value from it. This is used as the
		   fingerprint and the low 64 bits are the keyid.

    * Revocation certificates consist only of the signature packet;
      "import" knows how to handle this.  The rationale behind it is
      to keep them small.







Keyserver Message Format
=========================

The keyserver may be contacted by a Unix Domain socket or via TCP.

The format of a request is:

====
command-tag
"Content-length:" digits
CRLF
=======

Where command-tag is

NOOP
GET <user-name>
PUT
DELETE <user-name>


The format of a response is:

======
"GNUPG/1.0" status-code status-text
"Content-length:" digits
CRLF
============
followed by <digits> bytes of data


Status codes are:

     o	1xx: Informational - Request received, continuing process

     o	2xx: Success - The action was successfully received, understood,
	and accepted

     o	4xx: Client Error - The request contains bad syntax or cannot be
	fulfilled

     o	5xx: Server Error - The server failed to fulfill an apparently
	valid request



Documentation on HKP (the http keyserver protocol):

A minimalistic HTTP server on port 11371 recognizes a GET for /pks/lookup.
The standard http URL encoded query parameters are this (always key=value):

- op=index (like pgp -kv), op=vindex (like pgp -kvv) and op=get (like
  pgp -kxa)

- search=<stringlist>. This is a list of words that must occur in the key.
  The words are delimited with space, points, @ and so on. The delimiters
  are not searched for and the order of the words doesn't matter (but see
  next option).

- exact=on. This switch tells the hkp server to only report exact matching
  keys back. In this case the order and the "delimiters" are important.

- fingerprint=on. Also reports the fingerprints when used with 'index' or
  'vindex'

The keyserver also recognizes http-POSTs to /pks/add. Use this to upload
keys.


A better way to to this would be a request like:

   /pks/lookup/<gnupg_formatierte_user_id>?op=<operation>

this can be implemented using Hurd's translator mechanism.
However, I think the whole key server stuff has to be re-thought;
I have some ideas and probably create a white paper.

