8 * OpenPGP constants and structures from RFC-2440.
10 * Text from RFC-2440 in comments is
11 * Copyright (C) The Internet Society (1998). All Rights Reserved.
17 #include "endianness.h"
24 typedef /*@abstract@*/ struct DIGEST_CTX_s * DIGEST_CTX;
28 typedef const struct pgpValTbl_s {
30 /*@observer@*/ const char * str;
36 * The packet tag denotes what type of packet the body holds. Note that
37 * old format headers can only have tags less than 16, whereas new
38 * format headers can have tags as great as 63.
40 typedef enum pgpTag_e {
41 PGPTAG_RESERVED = 0, /*!< Reserved/Invalid */
42 PGPTAG_PUBLIC_SESSION_KEY = 1, /*!< Public-Key Encrypted Session Key */
43 PGPTAG_SIGNATURE = 2, /*!< Signature */
44 PGPTAG_SYMMETRIC_SESSION_KEY= 3, /*!< Symmetric-Key Encrypted Session Key*/
45 PGPTAG_ONEPASS_SIGNATURE = 4, /*!< One-Pass Signature */
46 PGPTAG_SECRET_KEY = 5, /*!< Secret Key */
47 PGPTAG_PUBLIC_KEY = 6, /*!< Public Key */
48 PGPTAG_SECRET_SUBKEY = 7, /*!< Secret Subkey */
49 PGPTAG_COMPRESSED_DATA = 8, /*!< Compressed Data */
50 PGPTAG_SYMMETRIC_DATA = 9, /*!< Symmetrically Encrypted Data */
51 PGPTAG_MARKER = 10, /*!< Marker */
52 PGPTAG_LITERAL_DATA = 11, /*!< Literal Data */
53 PGPTAG_TRUST = 12, /*!< Trust */
54 PGPTAG_USER_ID = 13, /*!< User ID */
55 PGPTAG_PUBLIC_SUBKEY = 14, /*!< Public Subkey */
56 PGPTAG_COMMENT_OLD = 16, /*!< Comment (from OpenPGP draft) */
57 PGPTAG_PHOTOID = 17, /*!< PGP's photo ID */
58 PGPTAG_ENCRYPTED_MDC = 18, /*!< Integrity protected encrypted data */
59 PGPTAG_MDC = 19, /*!< Manipulaion detection code packet */
60 PGPTAG_PRIVATE_60 = 60, /*!< Private or Experimental Values */
61 PGPTAG_COMMENT = 61, /*!< Comment */
62 PGPTAG_PRIVATE_62 = 62, /*!< Private or Experimental Values */
63 PGPTAG_CONTROL = 63 /*!< Control (GPG) */
68 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
69 extern struct pgpValTbl_s pgpTagTbl[];
72 * 5.1. Public-Key Encrypted Session Key Packets (Tag 1)
74 * A Public-Key Encrypted Session Key packet holds the session key used
75 * to encrypt a message. Zero or more Encrypted Session Key packets
76 * (either Public-Key or Symmetric-Key) may precede a Symmetrically
77 * Encrypted Data Packet, which holds an encrypted message. The message
78 * is encrypted with the session key, and the session key is itself
79 * encrypted and stored in the Encrypted Session Key packet(s). The
80 * Symmetrically Encrypted Data Packet is preceded by one Public-Key
81 * Encrypted Session Key packet for each OpenPGP key to which the
82 * message is encrypted. The recipient of the message finds a session
83 * key that is encrypted to their public key, decrypts the session key,
84 * and then uses the session key to decrypt the message.
86 * The body of this packet consists of:
87 * - A one-octet number giving the version number of the packet type.
88 * The currently defined value for packet version is 3. An
89 * implementation should accept, but not generate a version of 2,
90 * which is equivalent to V3 in all other respects.
91 * - An eight-octet number that gives the key ID of the public key
92 * that the session key is encrypted to.
93 * - A one-octet number giving the public key algorithm used.
94 * - A string of octets that is the encrypted session key. This string
95 * takes up the remainder of the packet, and its contents are
96 * dependent on the public key algorithm used.
98 * Algorithm Specific Fields for RSA encryption
99 * - multiprecision integer (MPI) of RSA encrypted value m**e mod n.
101 * Algorithm Specific Fields for Elgamal encryption:
102 * - MPI of Elgamal (Diffie-Hellman) value g**k mod p.
103 * - MPI of Elgamal (Diffie-Hellman) value m * y**k mod p.
105 typedef struct pgpPktPubkey_s {
106 byte version; /*!< version number (generate 3, accept 2). */
107 byte keyid[8]; /*!< key ID of the public key for session key. */
108 byte algo; /*!< public key algorithm used. */
113 * 5.2.1. Signature Types
115 * There are a number of possible meanings for a signature, which are
116 * specified in a signature type octet in any given signature.
119 typedef enum pgpSigType_e {
120 PGPSIGTYPE_BINARY = 0x00, /*!< Binary document */
121 PGPSIGTYPE_TEXT = 0x01, /*!< Canonical text document */
122 PGPSIGTYPE_STANDALONE = 0x02, /*!< Standalone */
123 PGPSIGTYPE_GENERIC_CERT = 0x10,
124 /*!< Generic certification of a User ID & Public Key */
125 PGPSIGTYPE_PERSONA_CERT = 0x11,
126 /*!< Persona certification of a User ID & Public Key */
127 PGPSIGTYPE_CASUAL_CERT = 0x12,
128 /*!< Casual certification of a User ID & Public Key */
129 PGPSIGTYPE_POSITIVE_CERT = 0x13,
130 /*!< Positive certification of a User ID & Public Key */
131 PGPSIGTYPE_SUBKEY_BINDING = 0x18, /*!< Subkey Binding */
132 PGPSIGTYPE_SIGNED_KEY = 0x1F, /*!< Signature directly on a key */
133 PGPSIGTYPE_KEY_REVOKE = 0x20, /*!< Key revocation */
134 PGPSIGTYPE_SUBKEY_REVOKE = 0x28, /*!< Subkey revocation */
135 PGPSIGTYPE_CERT_REVOKE = 0x30, /*!< Certification revocation */
136 PGPSIGTYPE_TIMESTAMP = 0x40 /*!< Timestamp */
142 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
143 extern struct pgpValTbl_s pgpSigTypeTbl[];
146 * 9.1. Public Key Algorithms
151 1 - RSA (Encrypt or Sign)
154 16 - Elgamal (Encrypt-Only), see [ELGAMAL]
155 17 - DSA (Digital Signature Standard)
156 18 - Reserved for Elliptic Curve
157 19 - Reserved for ECDSA
158 20 - Elgamal (Encrypt or Sign)
159 21 - Reserved for Diffie-Hellman (X9.42,
160 as defined for IETF-S/MIME)
161 100 to 110 - Private/Experimental algorithm.
164 * Implementations MUST implement DSA for signatures, and Elgamal for
165 * encryption. Implementations SHOULD implement RSA keys.
166 * Implementations MAY implement any other algorithm.
169 typedef enum pgpPubkeyAlgo_e {
170 PGPPUBKEYALGO_RSA = 1, /*!< RSA */
171 PGPPUBKEYALGO_RSA_ENCRYPT = 2, /*!< RSA(Encrypt-Only) */
172 PGPPUBKEYALGO_RSA_SIGN = 3, /*!< RSA(Sign-Only) */
173 PGPPUBKEYALGO_ELGAMAL_ENCRYPT= 16, /*!< Elgamal(Encrypt-Only) */
174 PGPPUBKEYALGO_DSA = 17, /*!< DSA */
175 PGPPUBKEYALGO_EC = 18, /*!< Elliptic Curve */
176 PGPPUBKEYALGO_ECDSA = 19, /*!< ECDSA */
177 PGPPUBKEYALGO_ELGAMAL = 20, /*!< Elgamal */
178 PGPPUBKEYALGO_DH = 21 /*!< Diffie-Hellman (X9.42) */
184 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
185 extern struct pgpValTbl_s pgpPubkeyTbl[];
188 * 9.2. Symmetric Key Algorithms
193 0 - Plaintext or unencrypted data
195 2 - Triple-DES (DES-EDE, as per spec -
196 168 bit key derived from 192)
197 3 - CAST5 (128 bit key, as per RFC 2144)
198 4 - Blowfish (128 bit key, 16 rounds) [BLOWFISH]
199 5 - SAFER-SK128 (13 rounds) [SAFER]
200 6 - Reserved for DES/SK
201 7 - Reserved for AES with 128-bit key
202 8 - Reserved for AES with 192-bit key
203 9 - Reserved for AES with 256-bit key
204 100 to 110 - Private/Experimental algorithm.
207 * Implementations MUST implement Triple-DES. Implementations SHOULD
208 * implement IDEA and CAST5. Implementations MAY implement any other
212 typedef enum pgpSymkeyAlgo_e {
213 PGPSYMKEYALGO_PLAINTEXT = 0, /*!< Plaintext */
214 PGPSYMKEYALGO_IDEA = 1, /*!< IDEA */
215 PGPSYMKEYALGO_TRIPLE_DES = 2, /*!< 3DES */
216 PGPSYMKEYALGO_CAST5 = 3, /*!< CAST5 */
217 PGPSYMKEYALGO_BLOWFISH = 4, /*!< BLOWFISH */
218 PGPSYMKEYALGO_SAFER = 5, /*!< SAFER */
219 PGPSYMKEYALGO_DES_SK = 6, /*!< DES/SK */
220 PGPSYMKEYALGO_AES_128 = 7, /*!< AES(128-bit key) */
221 PGPSYMKEYALGO_AES_192 = 8, /*!< AES(192-bit key) */
222 PGPSYMKEYALGO_AES_256 = 9, /*!< AES(256-bit key) */
223 PGPSYMKEYALGO_TWOFISH = 10 /*!< TWOFISH */
229 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
230 extern struct pgpValTbl_s pgpSymkeyTbl[];
233 * 9.3. Compression Algorithms
241 100 to 110 - Private/Experimental algorithm.
244 * Implementations MUST implement uncompressed data. Implementations
245 * SHOULD implement ZIP. Implementations MAY implement ZLIB.
248 typedef enum pgpCompressAlgo_e {
249 PGPCOMPRESSALGO_NONE = 0, /*!< Uncompressed */
250 PGPCOMPRESSALGO_ZIP = 1, /*!< ZIP */
251 PGPCOMPRESSALGO_ZLIB = 2 /*!< ZLIB */
257 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
258 extern struct pgpValTbl_s pgpCompressionTbl[];
261 * 9.4. Hash Algorithms
264 ID Algorithm Text Name
265 -- --------- ---- ----
268 3 - RIPE-MD/160 "RIPEMD160"
269 4 - Reserved for double-width SHA (experimental)
271 6 - Reserved for TIGER/192 "TIGER192"
272 7 - Reserved for HAVAL (5 pass, 160-bit)
274 100 to 110 - Private/Experimental algorithm.
277 * Implementations MUST implement SHA-1. Implementations SHOULD
281 typedef enum pgpHashAlgo_e {
282 PGPHASHALGO_MD5 = 1, /*!< MD5 */
283 PGPHASHALGO_SHA1 = 2, /*!< SHA1 */
284 PGPHASHALGO_RIPEMD160 = 3, /*!< RIPEMD160 */
285 PGPHASHALGO_MD2 = 5, /*!< MD2 */
286 PGPHASHALGO_TIGER192 = 6, /*!< TIGER192 */
287 PGPHASHALGO_HAVAL_5_160 = 7 /*!< HAVAL-5-160 */
292 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
293 extern struct pgpValTbl_s pgpHashTbl[];
296 * 5.2.2. Version 3 Signature Packet Format
298 * The body of a version 3 Signature Packet contains:
299 * - One-octet version number (3).
300 * - One-octet length of following hashed material. MUST be 5.
301 * - One-octet signature type.
302 * - Four-octet creation time.
303 * - Eight-octet key ID of signer.
304 * - One-octet public key algorithm.
305 * - One-octet hash algorithm.
306 * - Two-octet field holding left 16 bits of signed hash value.
307 * - One or more multi-precision integers comprising the signature.
309 * Algorithm Specific Fields for RSA signatures:
310 * - multiprecision integer (MPI) of RSA signature value m**d.
312 * Algorithm Specific Fields for DSA signatures:
313 * - MPI of DSA value r.
314 * - MPI of DSA value s.
316 typedef struct pgpPktSigV3_s {
317 byte version; /*!< version number (3). */
318 byte hashlen; /*!< length of following hashed material. MUST be 5. */
319 byte sigtype; /*!< signature type. */
320 byte time[4]; /*!< 4 byte creation time. */
321 byte signid[8]; /*!< key ID of signer. */
322 byte pubkey_algo; /*!< public key algorithm. */
323 byte hash_algo; /*!< hash algorithm. */
324 byte signhash16[2]; /*!< left 16 bits of signed hash value. */
328 * 5.2.3. Version 4 Signature Packet Format
330 * The body of a version 4 Signature Packet contains:
331 * - One-octet version number (4).
332 * - One-octet signature type.
333 * - One-octet public key algorithm.
334 * - One-octet hash algorithm.
335 * - Two-octet scalar octet count for following hashed subpacket
336 * data. Note that this is the length in octets of all of the hashed
337 * subpackets; a pointer incremented by this number will skip over
338 * the hashed subpackets.
339 * - Hashed subpacket data. (zero or more subpackets)
340 * - Two-octet scalar octet count for following unhashed subpacket
341 * data. Note that this is the length in octets of all of the
342 * unhashed subpackets; a pointer incremented by this number will
343 * skip over the unhashed subpackets.
344 * - Unhashed subpacket data. (zero or more subpackets)
345 * - Two-octet field holding left 16 bits of signed hash value.
346 * - One or more multi-precision integers comprising the signature.
348 typedef struct pgpPktSigV4_s {
349 byte version; /*!< version number (4). */
350 byte sigtype; /*!< signature type. */
351 byte pubkey_algo; /*!< public key algorithm. */
352 byte hash_algo; /*!< hash algorithm. */
353 byte hashlen[2]; /*!< length of following hashed material. */
357 * 5.2.3.1. Signature Subpacket Specification
359 * The subpacket fields consist of zero or more signature subpackets.
360 * Each set of subpackets is preceded by a two-octet scalar count of the
361 * length of the set of subpackets.
363 * Each subpacket consists of a subpacket header and a body. The header
365 * - the subpacket length (1, 2, or 5 octets)
366 * - the subpacket type (1 octet)
367 * and is followed by the subpacket specific data.
369 * The length includes the type octet but not this length. Its format is
370 * similar to the "new" format packet header lengths, but cannot have
371 * partial body lengths. That is:
373 if the 1st octet < 192, then
375 subpacketLen = 1st_octet
377 if the 1st octet >= 192 and < 255, then
379 subpacketLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192
381 if the 1st octet = 255, then
383 subpacket length = [four-octet scalar starting at 2nd_octet]
386 * The value of the subpacket type octet may be:
389 2 = signature creation time
390 3 = signature expiration time
391 4 = exportable certification
393 6 = regular expression
395 9 = key expiration time
396 10 = placeholder for backward compatibility
397 11 = preferred symmetric algorithms
401 21 = preferred hash algorithms
402 22 = preferred compression algorithms
403 23 = key server preferences
404 24 = preferred key server
408 28 = signer's user id
409 29 = reason for revocation
410 100 to 110 = internal or user-defined
413 * An implementation SHOULD ignore any subpacket of a type that it does
416 * Bit 7 of the subpacket type is the "critical" bit. If set, it
417 * denotes that the subpacket is one that is critical for the evaluator
418 * of the signature to recognize. If a subpacket is encountered that is
419 * marked critical but is unknown to the evaluating software, the
420 * evaluator SHOULD consider the signature to be in error.
423 typedef enum pgpSubType_e {
424 PGPSUBTYPE_SIG_CREATE_TIME = 2, /*!< signature creation time */
425 PGPSUBTYPE_SIG_EXPIRE_TIME = 3, /*!< signature expiration time */
426 PGPSUBTYPE_EXPORTABLE_CERT = 4, /*!< exportable certification */
427 PGPSUBTYPE_TRUST_SIG = 5, /*!< trust signature */
428 PGPSUBTYPE_REGEX = 6, /*!< regular expression */
429 PGPSUBTYPE_REVOCABLE = 7, /*!< revocable */
430 PGPSUBTYPE_KEY_EXPIRE_TIME = 9, /*!< key expiration time */
431 PGPSUBTYPE_BACKWARD_COMPAT = 10, /*!< placeholder for backward compatibility */
432 PGPSUBTYPE_PREFER_SYMKEY = 11, /*!< preferred symmetric algorithms */
433 PGPSUBTYPE_REVOKE_KEY = 12, /*!< revocation key */
434 PGPSUBTYPE_ISSUER_KEYID = 16, /*!< issuer key ID */
435 PGPSUBTYPE_NOTATION = 20, /*!< notation data */
436 PGPSUBTYPE_PREFER_HASH = 21, /*!< preferred hash algorithms */
437 PGPSUBTYPE_PREFER_COMPRESS = 22, /*!< preferred compression algorithms */
438 PGPSUBTYPE_KEYSERVER_PREFERS= 23, /*!< key server preferences */
439 PGPSUBTYPE_PREFER_KEYSERVER = 24, /*!< preferred key server */
440 PGPSUBTYPE_PRIMARY_USERID = 25, /*!< primary user id */
441 PGPSUBTYPE_POLICY_URL = 26, /*!< policy URL */
442 PGPSUBTYPE_KEY_FLAGS = 27, /*!< key flags */
443 PGPSUBTYPE_SIGNER_USERID = 28, /*!< signer's user id */
444 PGPSUBTYPE_REVOKE_REASON = 29, /*!< reason for revocation */
445 PGPSUBTYPE_INTERNAL_100 = 100, /*!< internal or user-defined */
446 PGPSUBTYPE_INTERNAL_101 = 101, /*!< internal or user-defined */
447 PGPSUBTYPE_INTERNAL_102 = 102, /*!< internal or user-defined */
448 PGPSUBTYPE_INTERNAL_103 = 103, /*!< internal or user-defined */
449 PGPSUBTYPE_INTERNAL_104 = 104, /*!< internal or user-defined */
450 PGPSUBTYPE_INTERNAL_105 = 105, /*!< internal or user-defined */
451 PGPSUBTYPE_INTERNAL_106 = 106, /*!< internal or user-defined */
452 PGPSUBTYPE_INTERNAL_107 = 107, /*!< internal or user-defined */
453 PGPSUBTYPE_INTERNAL_108 = 108, /*!< internal or user-defined */
454 PGPSUBTYPE_INTERNAL_109 = 109, /*!< internal or user-defined */
455 PGPSUBTYPE_INTERNAL_110 = 110 /*!< internal or user-defined */
461 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
462 extern struct pgpValTbl_s pgpSubTypeTbl[];
465 * 5.2. Signature Packet (Tag 2)
467 * A signature packet describes a binding between some public key and
468 * some data. The most common signatures are a signature of a file or a
469 * block of text, and a signature that is a certification of a user ID.
471 * Two versions of signature packets are defined. Version 3 provides
472 * basic signature information, while version 4 provides an expandable
473 * format with subpackets that can specify more information about the
474 * signature. PGP 2.6.x only accepts version 3 signatures.
476 * Implementations MUST accept V3 signatures. Implementations SHOULD
477 * generate V4 signatures. Implementations MAY generate a V3 signature
478 * that can be verified by PGP 2.6.x.
480 * Note that if an implementation is creating an encrypted and signed
481 * message that is encrypted to a V3 key, it is reasonable to create a
484 typedef union pgpPktSig_u {
485 struct pgpPktSigV3_s v3;
486 struct pgpPktSigV4_s v4;
490 * 5.3. Symmetric-Key Encrypted Session-Key Packets (Tag 3)
492 * The Symmetric-Key Encrypted Session Key packet holds the symmetric-
493 * key encryption of a session key used to encrypt a message. Zero or
494 * more Encrypted Session Key packets and/or Symmetric-Key Encrypted
495 * Session Key packets may precede a Symmetrically Encrypted Data Packet
496 * that holds an encrypted message. The message is encrypted with a
497 * session key, and the session key is itself encrypted and stored in
498 * the Encrypted Session Key packet or the Symmetric-Key Encrypted
499 * Session Key packet.
501 * If the Symmetrically Encrypted Data Packet is preceded by one or more
502 * Symmetric-Key Encrypted Session Key packets, each specifies a
503 * passphrase that may be used to decrypt the message. This allows a
504 * message to be encrypted to a number of public keys, and also to one
505 * or more pass phrases. This packet type is new, and is not generated
506 * by PGP 2.x or PGP 5.0.
508 * The body of this packet consists of:
509 * - A one-octet version number. The only currently defined version
511 * - A one-octet number describing the symmetric algorithm used.
512 * - A string-to-key (S2K) specifier, length as defined above.
513 * - Optionally, the encrypted session key itself, which is decrypted
514 * with the string-to-key object.
517 typedef struct pgpPktSymkey_s {
518 byte version; /*!< version number (4). */
524 * 5.4. One-Pass Signature Packets (Tag 4)
526 * The One-Pass Signature packet precedes the signed data and contains
527 * enough information to allow the receiver to begin calculating any
528 * hashes needed to verify the signature. It allows the Signature
529 * Packet to be placed at the end of the message, so that the signer can
530 * compute the entire signed message in one pass.
532 * A One-Pass Signature does not interoperate with PGP 2.6.x or earlier.
534 * The body of this packet consists of:
535 * - A one-octet version number. The current version is 3.
536 * - A one-octet signature type. Signature types are described in
538 * - A one-octet number describing the hash algorithm used.
539 * - A one-octet number describing the public key algorithm used.
540 * - An eight-octet number holding the key ID of the signing key.
541 * - A one-octet number holding a flag showing whether the signature
542 * is nested. A zero value indicates that the next packet is
543 * another One-Pass Signature packet that describes another
544 * signature to be applied to the same message data.
546 * Note that if a message contains more than one one-pass signature,
547 * then the signature packets bracket the message; that is, the first
548 * signature packet after the message corresponds to the last one-pass
549 * packet and the final signature packet corresponds to the first one-
552 typedef struct pgpPktOnepass_s {
553 byte version; /*!< version number (3). */
554 byte sigtype; /*!< signature type. */
555 byte hash_algo; /*!< hash algorithm. */
556 byte pubkey_algo; /*!< public key algorithm. */
557 byte signid[8]; /*!< key ID of signer. */
562 * 5.5.1. Key Packet Variants
564 * 5.5.1.1. Public Key Packet (Tag 6)
566 * A Public Key packet starts a series of packets that forms an OpenPGP
567 * key (sometimes called an OpenPGP certificate).
569 * 5.5.1.2. Public Subkey Packet (Tag 14)
571 * A Public Subkey packet (tag 14) has exactly the same format as a
572 * Public Key packet, but denotes a subkey. One or more subkeys may be
573 * associated with a top-level key. By convention, the top-level key
574 * provides signature services, and the subkeys provide encryption
577 * Note: in PGP 2.6.x, tag 14 was intended to indicate a comment packet.
578 * This tag was selected for reuse because no previous version of PGP
579 * ever emitted comment packets but they did properly ignore them.
580 * Public Subkey packets are ignored by PGP 2.6.x and do not cause it to
581 * fail, providing a limited degree of backward compatibility.
583 * 5.5.1.3. Secret Key Packet (Tag 5)
585 * A Secret Key packet contains all the information that is found in a
586 * Public Key packet, including the public key material, but also
587 * includes the secret key material after all the public key fields.
589 * 5.5.1.4. Secret Subkey Packet (Tag 7)
591 * A Secret Subkey packet (tag 7) is the subkey analog of the Secret Key
592 * packet, and has exactly the same format.
594 * 5.5.2. Public Key Packet Formats
596 * There are two versions of key-material packets. Version 3 packets
597 * were first generated by PGP 2.6. Version 2 packets are identical in
598 * format to Version 3 packets, but are generated by PGP 2.5 or before.
599 * V2 packets are deprecated and they MUST NOT be generated. PGP 5.0
600 * introduced version 4 packets, with new fields and semantics. PGP
601 * 2.6.x will not accept key-material packets with versions greater than
604 * OpenPGP implementations SHOULD create keys with version 4 format. An
605 * implementation MAY generate a V3 key to ensure interoperability with
606 * old software; note, however, that V4 keys correct some security
607 * deficiencies in V3 keys. These deficiencies are described below. An
608 * implementation MUST NOT create a V3 key with a public key algorithm
611 * A version 3 public key or public subkey packet contains:
612 * - A one-octet version number (3).
613 * - A four-octet number denoting the time that the key was created.
614 * - A two-octet number denoting the time in days that this key is
615 * valid. If this number is zero, then it does not expire.
616 * - A one-octet number denoting the public key algorithm of this key
617 * - A series of multi-precision integers comprising the key
619 * - a multiprecision integer (MPI) of RSA public modulus n;
620 * - an MPI of RSA public encryption exponent e.
622 * V3 keys SHOULD only be used for backward compatibility because of
623 * three weaknesses in them. First, it is relatively easy to construct a
624 * V3 key that has the same key ID as any other key because the key ID
625 * is simply the low 64 bits of the public modulus. Secondly, because
626 * the fingerprint of a V3 key hashes the key material, but not its
627 * length, which increases the opportunity for fingerprint collisions.
628 * Third, there are minor weaknesses in the MD5 hash algorithm that make
629 * developers prefer other algorithms. See below for a fuller discussion
630 * of key IDs and fingerprints.
633 typedef struct pgpPktKeyV3_s {
634 byte version; /*!< version number (3). */
635 byte time[4]; /*!< time that the key was created. */
636 byte valid[2]; /*!< time in days that this key is valid. */
637 byte pubkey_algo; /*!< public key algorithm. */
641 * The version 4 format is similar to the version 3 format except for
642 * the absence of a validity period. This has been moved to the
643 * signature packet. In addition, fingerprints of version 4 keys are
644 * calculated differently from version 3 keys, as described in section
645 * "Enhanced Key Formats."
647 * A version 4 packet contains:
648 * - A one-octet version number (4).
649 * - A four-octet number denoting the time that the key was created.
650 * - A one-octet number denoting the public key algorithm of this key
651 * - A series of multi-precision integers comprising the key
652 * material. This algorithm-specific portion is:
654 * Algorithm Specific Fields for RSA public keys:
655 * - multiprecision integer (MPI) of RSA public modulus n;
656 * - MPI of RSA public encryption exponent e.
658 * Algorithm Specific Fields for DSA public keys:
659 * - MPI of DSA prime p;
660 * - MPI of DSA group order q (q is a prime divisor of p-1);
661 * - MPI of DSA group generator g;
662 * - MPI of DSA public key value y (= g**x where x is secret).
664 * Algorithm Specific Fields for Elgamal public keys:
665 * - MPI of Elgamal prime p;
666 * - MPI of Elgamal group generator g;
667 * - MPI of Elgamal public key value y (= g**x where x is
671 typedef struct pgpPktKeyV4_s {
672 byte version; /*!< version number (4). */
673 byte time[4]; /*!< time that the key was created. */
674 byte pubkey_algo; /*!< public key algorithm. */
678 * 5.5.3. Secret Key Packet Formats
680 * The Secret Key and Secret Subkey packets contain all the data of the
681 * Public Key and Public Subkey packets, with additional algorithm-
682 * specific secret key data appended, in encrypted form.
684 * The packet contains:
685 * - A Public Key or Public Subkey packet, as described above
686 * - One octet indicating string-to-key usage conventions. 0
687 * indicates that the secret key data is not encrypted. 255
688 * indicates that a string-to-key specifier is being given. Any
689 * other value is a symmetric-key encryption algorithm specifier.
690 * - [Optional] If string-to-key usage octet was 255, a one-octet
691 * symmetric encryption algorithm.
692 * - [Optional] If string-to-key usage octet was 255, a string-to-key
693 * specifier. The length of the string-to-key specifier is implied
694 * by its type, as described above.
695 * - [Optional] If secret data is encrypted, eight-octet Initial
697 * - Encrypted multi-precision integers comprising the secret key
698 * data. These algorithm-specific fields are as described below.
699 * - Two-octet checksum of the plaintext of the algorithm-specific
700 * portion (sum of all octets, mod 65536).
702 * Algorithm Specific Fields for RSA secret keys:
703 * - multiprecision integer (MPI) of RSA secret exponent d.
704 * - MPI of RSA secret prime value p.
705 * - MPI of RSA secret prime value q (p < q).
706 * - MPI of u, the multiplicative inverse of p, mod q.
708 * Algorithm Specific Fields for DSA secret keys:
709 * - MPI of DSA secret exponent x.
711 * Algorithm Specific Fields for Elgamal secret keys:
712 * - MPI of Elgamal secret exponent x.
714 * Secret MPI values can be encrypted using a passphrase. If a string-
715 * to-key specifier is given, that describes the algorithm for
716 * converting the passphrase to a key, else a simple MD5 hash of the
717 * passphrase is used. Implementations SHOULD use a string-to-key
718 * specifier; the simple hash is for backward compatibility. The cipher
719 * for encrypting the MPIs is specified in the secret key packet.
721 * Encryption/decryption of the secret data is done in CFB mode using
722 * the key created from the passphrase and the Initial Vector from the
723 * packet. A different mode is used with V3 keys (which are only RSA)
724 * than with other key formats. With V3 keys, the MPI bit count prefix
725 * (i.e., the first two octets) is not encrypted. Only the MPI non-
726 * prefix data is encrypted. Furthermore, the CFB state is
727 * resynchronized at the beginning of each new MPI value, so that the
728 * CFB block boundary is aligned with the start of the MPI data.
730 * With V4 keys, a simpler method is used. All secret MPI values are
731 * encrypted in CFB mode, including the MPI bitcount prefix.
733 * The 16-bit checksum that follows the algorithm-specific portion is
734 * the algebraic sum, mod 65536, of the plaintext of all the algorithm-
735 * specific octets (including MPI prefix and data). With V3 keys, the
736 * checksum is stored in the clear. With V4 keys, the checksum is
737 * encrypted like the algorithm-specific data. This value is used to
738 * check that the passphrase was correct.
741 typedef union pgpPktKey_u {
742 struct pgpPktKeyV3_s v3;
743 struct pgpPktKeyV4_s v4;
747 * 5.6. Compressed Data Packet (Tag 8)
749 * The Compressed Data packet contains compressed data. Typically, this
750 * packet is found as the contents of an encrypted packet, or following
751 * a Signature or One-Pass Signature packet, and contains literal data
754 * The body of this packet consists of:
755 * - One octet that gives the algorithm used to compress the packet.
756 * - The remainder of the packet is compressed data.
758 * A Compressed Data Packet's body contains an block that compresses
759 * some set of packets. See section "Packet Composition" for details on
760 * how messages are formed.
762 * ZIP-compressed packets are compressed with raw RFC 1951 DEFLATE
763 * blocks. Note that PGP V2.6 uses 13 bits of compression. If an
764 * implementation uses more bits of compression, PGP V2.6 cannot
767 * ZLIB-compressed packets are compressed with RFC 1950 ZLIB-style
770 typedef struct pgpPktCdata_s {
776 * 5.7. Symmetrically Encrypted Data Packet (Tag 9)
778 * The Symmetrically Encrypted Data packet contains data encrypted with
779 * a symmetric-key algorithm. When it has been decrypted, it will
780 * typically contain other packets (often literal data packets or
781 * compressed data packets).
783 * The body of this packet consists of:
784 * - Encrypted data, the output of the selected symmetric-key cipher
785 * operating in PGP's variant of Cipher Feedback (CFB) mode.
787 * The symmetric cipher used may be specified in an Public-Key or
788 * Symmetric-Key Encrypted Session Key packet that precedes the
789 * Symmetrically Encrypted Data Packet. In that case, the cipher
790 * algorithm octet is prefixed to the session key before it is
791 * encrypted. If no packets of these types precede the encrypted data,
792 * the IDEA algorithm is used with the session key calculated as the MD5
793 * hash of the passphrase.
795 * The data is encrypted in CFB mode, with a CFB shift size equal to the
796 * cipher's block size. The Initial Vector (IV) is specified as all
797 * zeros. Instead of using an IV, OpenPGP prefixes a 10-octet string to
798 * the data before it is encrypted. The first eight octets are random,
799 * and the 9th and 10th octets are copies of the 7th and 8th octets,
800 * respectively. After encrypting the first 10 octets, the CFB state is
801 * resynchronized if the cipher block size is 8 octets or less. The
802 * last 8 octets of ciphertext are passed through the cipher and the
803 * block boundary is reset.
805 * The repetition of 16 bits in the 80 bits of random data prefixed to
806 * the message allows the receiver to immediately check whether the
807 * session key is incorrect.
809 typedef struct pgpPktEdata_s {
814 * 5.8. Marker Packet (Obsolete Literal Packet) (Tag 10)
816 * An experimental version of PGP used this packet as the Literal
817 * packet, but no released version of PGP generated Literal packets with
818 * this tag. With PGP 5.x, this packet has been re-assigned and is
819 * reserved for use as the Marker packet.
821 * The body of this packet consists of:
822 * - The three octets 0x50, 0x47, 0x50 (which spell "PGP" in UTF-8).
824 * Such a packet MUST be ignored when received. It may be placed at the
825 * beginning of a message that uses features not available in PGP 2.6.x
826 * in order to cause that version to report that newer software is
827 * necessary to process the message.
830 * 5.9. Literal Data Packet (Tag 11)
832 * A Literal Data packet contains the body of a message; data that is
833 * not to be further interpreted.
835 * The body of this packet consists of:
836 * - A one-octet field that describes how the data is formatted.
838 * If it is a 'b' (0x62), then the literal packet contains binary data.
839 * If it is a 't' (0x74), then it contains text data, and thus may need
840 * line ends converted to local form, or other text-mode changes. RFC
841 * 1991 also defined a value of 'l' as a 'local' mode for machine-local
842 * conversions. This use is now deprecated.
843 * - File name as a string (one-octet length, followed by file name),
844 * if the encrypted data should be saved as a file.
846 * If the special name "_CONSOLE" is used, the message is considered to
847 * be "for your eyes only". This advises that the message data is
848 * unusually sensitive, and the receiving program should process it more
849 * carefully, perhaps avoiding storing the received data to disk, for
851 * - A four-octet number that indicates the modification date of the
852 * file, or the creation time of the packet, or a zero that
853 * indicates the present time.
854 * - The remainder of the packet is literal data.
856 * Text data is stored with <CR><LF> text endings (i.e. network-normal
857 * line endings). These should be converted to native line endings by
858 * the receiving software.
860 typedef struct pgpPktLdata_s {
867 * 5.10. Trust Packet (Tag 12)
869 * The Trust packet is used only within keyrings and is not normally
870 * exported. Trust packets contain data that record the user's
871 * specifications of which key holders are trustworthy introducers,
872 * along with other information that implementing software uses for
875 * Trust packets SHOULD NOT be emitted to output streams that are
876 * transferred to other users, and they SHOULD be ignored on any input
877 * other than local keyring files.
879 typedef struct pgpPktTrust_s {
884 * 5.11. User ID Packet (Tag 13)
886 * A User ID packet consists of data that is intended to represent the
887 * name and email address of the key holder. By convention, it includes
888 * an RFC 822 mail name, but there are no restrictions on its content.
889 * The packet length in the header specifies the length of the user id.
890 * If it is text, it is encoded in UTF-8.
893 typedef struct pgpPktUid_s {
900 pgpPktPubkey pubkey; /*!< 5.1. Public-Key Encrypted Session Key */
901 pgpPktSig sig; /*!< 5.2. Signature */
902 pgpPktSymkey symkey; /*!< 5.3. Symmetric-Key Encrypted Session-Key */
903 pgpPktOnepass onepass; /*!< 5.4. One-Pass Signature */
904 pgpPktKey key; /*!< 5.5. Key Material */
905 pgpPktCdata cdata; /*!< 5.6. Compressed Data */
906 pgpPktEdata edata; /*!< 5.7. Symmetrically Encrypted Data */
907 /*!< 5.8. Marker (obsolete) */
908 pgpPktLdata ldata; /*!< 5.9. Literal Data */
909 pgpPktTrust tdata; /*!< 5.10. Trust */
910 pgpPktUid uid; /*!< 5.11. User ID */
916 typedef enum pgpArmor_e {
919 PGPARMOR_MESSAGE = 1, /*!< MESSAGE */
920 PGPARMOR_PUBKEY = 2, /*!< PUBLIC KEY BLOCK */
921 PGPARMOR_SIGNATURE = 3, /*!< SIGNATURE */
922 PGPARMOR_SIGNED_MESSAGE = 4, /*!< SIGNED MESSAGE */
923 PGPARMOR_FILE = 5, /*!< ARMORED FILE */
924 PGPARMOR_PRIVKEY = 6, /*!< PRIVATE KEY BLOCK */
925 PGPARMOR_SECKEY = 7 /*!< SECRET KEY BLOCK */
931 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
932 extern struct pgpValTbl_s pgpArmorTbl[];
937 typedef enum pgpArmorKey_e {
938 PGPARMORKEY_VERSION = 1, /*!< Version: */
939 PGPARMORKEY_COMMENT = 2, /*!< Comment: */
940 PGPARMORKEY_MESSAGEID = 3, /*!< MessageID: */
941 PGPARMORKEY_HASH = 4, /*!< Hash: */
942 PGPARMORKEY_CHARSET = 5 /*!< Charset: */
948 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
949 extern struct pgpValTbl_s pgpArmorKeyTbl[];
953 struct pgpDigParams_s {
954 /*@only@*/ /*@null@*/
956 /*@only@*/ /*@null@*/
958 const char * params[4];
961 byte version; /*!< version number. */
962 byte time[4]; /*!< time that the key was created. */
963 byte pubkey_algo; /*!< public key algorithm. */
971 #define PGPDIG_SAVED_TIME (1 << 0)
972 #define PGPDIG_SAVED_ID (1 << 1)
979 struct pgpDigParams_s signature;
980 struct pgpDigParams_s pubkey;
982 size_t nbytes; /*!< No. bytes of plain text. */
984 /*@only@*/ /*@null@*/
985 DIGEST_CTX sha1ctx; /*!< (dsa) sha1 hash context. */
986 /*@only@*/ /*@null@*/
987 DIGEST_CTX hdrsha1ctx; /*!< (dsa) header sha1 hash context. */
988 /*@only@*/ /*@null@*/
989 void * sha1; /*!< (dsa) V3 signature hash. */
990 size_t sha1len; /*!< (dsa) V3 signature hash length. */
992 /*@only@*/ /*@null@*/
993 DIGEST_CTX md5ctx; /*!< (rsa) md5 hash context. */
994 /*@only@*/ /*@null@*/
995 void * md5; /*!< (rsa) V3 signature hash. */
996 size_t md5len; /*!< (rsa) V3 signature hash length. */
998 /* DSA parameters. */
1007 /* RSA parameters. */
1022 /*@unused@*/ static inline
1023 unsigned int pgpGrab(const byte *s, int nbytes)
1027 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
1029 i = (i << 8) | *s++;
1035 /*@unused@*/ static inline
1036 int pgpLen(const byte *s, /*@out@*/ unsigned int *lenp)
1037 /*@modifies *lenp @*/
1042 } else if (*s < 255) {
1043 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
1046 (*lenp) = pgpGrab(s+1, 4);
1053 /*@unused@*/ static inline
1054 unsigned int pgpMpiBits(const byte *p)
1057 return ((p[0] << 8) | p[1]);
1062 /*@unused@*/ static inline
1063 unsigned int pgpMpiLen(const byte *p)
1066 return (2 + ((pgpMpiBits(p)+7)>>3));
1071 /*@unused@*/ static inline
1072 char * pgpHexCvt(/*@returned@*/ char *t, const byte *s, int nbytes)
1075 static char hex[] = "0123456789abcdef";
1076 while (nbytes-- > 0) {
1079 *t++ = hex[ (i >> 4) & 0xf ];
1080 *t++ = hex[ (i ) & 0xf ];
1088 /*@unused@*/ static inline /*@observer@*/
1089 char * pgpHexStr(const byte *p, unsigned int plen)
1092 static char prbuf[2048];
1094 t = pgpHexCvt(t, p, plen);
1100 /*@unused@*/ static inline /*@observer@*/
1101 const char * pgpMpiStr(const byte *p)
1104 static char prbuf[2048];
1106 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
1108 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
1114 /*@unused@*/ static inline /*@observer@*/
1115 const char * pgpValStr(pgpValTbl vs, byte val)
1121 } while ((++vs)->val != -1);
1127 /*@unused@*/ static inline
1128 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
1132 int vlen = strlen(vs->str);
1133 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
1135 } while ((++vs)->val != -1);
1142 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
1143 /*@globals fileSystem @*/
1144 /*@modifies fileSystem @*/;
1148 int pgpPrtSubType(const byte *h, unsigned int hlen)
1149 /*@globals fileSystem @*/
1150 /*@modifies fileSystem @*/;
1154 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
1155 /*@globals fileSystem @*/
1156 /*@modifies fileSystem @*/;
1160 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
1161 /*@globals fileSystem @*/
1162 /*@modifies fileSystem @*/;
1166 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
1167 /*@globals fileSystem @*/
1168 /*@modifies fileSystem @*/;
1172 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
1173 /*@globals fileSystem @*/
1174 /*@modifies fileSystem @*/;
1178 int pgpPrtPkt(const byte *pkt)
1179 /*@globals fileSystem @*/
1180 /*@modifies fileSystem @*/;
1185 int pgpPrtPkts(const byte *pkts, unsigned int plen, struct pgpDig_s *dig, int printing)
1186 /*@globals fileSystem @*/
1187 /*@modifies fileSystem @*/;
1191 pgpArmor pgpReadPkts(const char * fn,
1192 /*@out@*/ const byte ** pkt, /*@out@*/ size_t * pktlen)
1193 /*@globals fileSystem @*/
1194 /*@modifies *pkt, *pktlen, fileSystem @*/;
1199 struct pgpDig_s * pgpNewDig(void)
1204 void pgpCleanDig(/*@null@*/ struct pgpDig_s * dig)
1205 /*@modifies dig @*/;
1209 /*@only@*/ /*@null@*/
1210 struct pgpDig_s * pgpFreeDig(/*@only@*/ /*@null@*/ struct pgpDig_s * dig)
1211 /*@modifies *dig @*/;
1215 /*@unused@*/ static inline
1216 int pgpIsPkt(const byte * p)
1219 unsigned int val = *p++;
1223 /* XXX can't deal with these. */
1230 tag = (val >> 2) & 0xf;
1234 case PGPTAG_SYMMETRIC_SESSION_KEY:
1235 case PGPTAG_ONEPASS_SIGNATURE:
1236 case PGPTAG_PUBLIC_KEY:
1237 case PGPTAG_SECRET_KEY:
1238 case PGPTAG_PUBLIC_SESSION_KEY:
1239 case PGPTAG_SIGNATURE:
1240 case PGPTAG_COMMENT:
1241 case PGPTAG_COMMENT_OLD:
1242 case PGPTAG_LITERAL_DATA:
1243 case PGPTAG_COMPRESSED_DATA:
1244 case PGPTAG_SYMMETRIC_DATA:
1247 case PGPTAG_PUBLIC_SUBKEY:
1248 case PGPTAG_SECRET_SUBKEY:
1249 case PGPTAG_USER_ID:
1250 case PGPTAG_RESERVED:
1252 case PGPTAG_PHOTOID:
1253 case PGPTAG_ENCRYPTED_MDC:
1255 case PGPTAG_PRIVATE_60:
1256 case PGPTAG_PRIVATE_62:
1257 case PGPTAG_CONTROL:
1266 #define CRC24_INIT 0xb704ce
1267 #define CRC24_POLY 0x1864cfb
1271 /*@unused@*/ static inline
1272 uint32 pgpCRC(const byte *octets, size_t len)
1275 uint32 crc = CRC24_INIT;
1279 crc ^= (*octets++) << 16;
1280 for (i = 0; i < 8; i++) {
1282 if (crc & 0x1000000)
1286 return crc & 0xffffff;
1295 #endif /* H_RPMPGP */