rpm  4.5
rpmpgp.h
Go to the documentation of this file.
1 #ifndef H_RPMPGP
2 #define H_RPMPGP
3 
13 #include <popt.h>
14 
15 #if !defined(_BEECRYPT_API_H)
16 /*@-redef@*/
17 typedef unsigned char byte;
18 /*@=redef@*/
19 #endif /* _BEECRYPT_API_H */
20 
23 typedef /*@abstract@*/ struct DIGEST_CTX_s * DIGEST_CTX;
24 
27 typedef /*@abstract@*/ struct pgpPkt_s * pgpPkt;
28 
31 typedef const struct pgpValTbl_s {
32  int val;
33 /*@observer@*/ const char * str;
34 } * pgpValTbl;
35 
43 typedef enum pgpTag_e {
56  PGPTAG_TRUST = 12,
62  PGPTAG_MDC = 19,
67 } pgpTag;
68 
71 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
72 extern struct pgpValTbl_s pgpTagTbl[];
73 
108 typedef struct pgpPktPubkey_s {
110  byte keyid[8];
112 } pgpPktPubkey;
113 
114 
121 /*@-typeuse@*/
122 typedef enum pgpSigType_e {
140 } pgpSigType;
141 /*@=typeuse@*/
142 
145 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
146 extern struct pgpValTbl_s pgpSigTypeTbl[];
147 
171 /*@-typeuse@*/
172 typedef enum pgpPubkeyAlgo_e {
182 } pgpPubkeyAlgo;
183 /*@=typeuse@*/
184 
187 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
188 extern struct pgpValTbl_s pgpPubkeyTbl[];
189 
214 /*@-typeuse@*/
215 typedef enum pgpSymkeyAlgo_e {
228 } pgpSymkeyAlgo;
229 /*@=typeuse@*/
230 
234 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
235 extern struct pgpValTbl_s pgpSymkeyTbl[];
236 
252 /*@-typeuse@*/
253 typedef enum pgpCompressAlgo_e {
259 /*@=typeuse@*/
260 
264 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
265 extern struct pgpValTbl_s pgpCompressionTbl[];
266 
288 typedef enum pgpHashAlgo_e {
313 } pgpHashAlgo;
314 
318 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
319 extern struct pgpValTbl_s pgpHashTbl[];
320 
342 typedef struct pgpPktSigV3_s {
346  byte time[4];
351 } * pgpPktSigV3;
352 
374 typedef struct pgpPktSigV4_s {
380 } * pgpPktSigV4;
381 
448 /*@-typeuse@*/
449 typedef enum pgpSubType_e {
488 } pgpSubType;
489 /*@=typeuse@*/
490 
494 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
495 extern struct pgpValTbl_s pgpSubTypeTbl[];
496 
517 typedef union pgpPktSig_u {
520 } * pgpPktSig;
521 
550 typedef struct pgpPktSymkey_s {
553  byte s2k[1];
554 } pgpPktSymkey;
555 
585 typedef struct pgpPktOnepass_s {
592 } * pgpPktOnepass;
593 
666 typedef struct pgpPktKeyV3_s {
668  byte time[4];
669  byte valid[2];
671 } * pgpPktKeyV3;
672 
704 typedef struct pgpPktKeyV4_s {
706  byte time[4];
708 } * pgpPktKeyV4;
709 
774 typedef union pgpPktKey_u {
777 } pgpPktKey;
778 
779 /*
780  * 5.6. Compressed Data Packet (Tag 8)
781  *
782  * The Compressed Data packet contains compressed data. Typically, this
783  * packet is found as the contents of an encrypted packet, or following
784  * a Signature or One-Pass Signature packet, and contains literal data
785  * packets.
786  *
787  * The body of this packet consists of:
788  * - One octet that gives the algorithm used to compress the packet.
789  * - The remainder of the packet is compressed data.
790  *
791  * A Compressed Data Packet's body contains an block that compresses
792  * some set of packets. See section "Packet Composition" for details on
793  * how messages are formed.
794  *
795  * ZIP-compressed packets are compressed with raw RFC 1951 DEFLATE
796  * blocks. Note that PGP V2.6 uses 13 bits of compression. If an
797  * implementation uses more bits of compression, PGP V2.6 cannot
798  * decompress it.
799  *
800  * ZLIB-compressed packets are compressed with RFC 1950 ZLIB-style
801  * blocks.
802  */
803 typedef struct pgpPktCdata_s {
805  byte data[1];
806 } pgpPktCdata;
807 
808 /*
809  * 5.7. Symmetrically Encrypted Data Packet (Tag 9)
810  *
811  * The Symmetrically Encrypted Data packet contains data encrypted with
812  * a symmetric-key algorithm. When it has been decrypted, it will
813  * typically contain other packets (often literal data packets or
814  * compressed data packets).
815  *
816  * The body of this packet consists of:
817  * - Encrypted data, the output of the selected symmetric-key cipher
818  * operating in PGP's variant of Cipher Feedback (CFB) mode.
819  *
820  * The symmetric cipher used may be specified in an Public-Key or
821  * Symmetric-Key Encrypted Session Key packet that precedes the
822  * Symmetrically Encrypted Data Packet. In that case, the cipher
823  * algorithm octet is prefixed to the session key before it is
824  * encrypted. If no packets of these types precede the encrypted data,
825  * the IDEA algorithm is used with the session key calculated as the MD5
826  * hash of the passphrase.
827  *
828  * The data is encrypted in CFB mode, with a CFB shift size equal to the
829  * cipher's block size. The Initial Vector (IV) is specified as all
830  * zeros. Instead of using an IV, OpenPGP prefixes a 10-octet string to
831  * the data before it is encrypted. The first eight octets are random,
832  * and the 9th and 10th octets are copies of the 7th and 8th octets,
833  * respectively. After encrypting the first 10 octets, the CFB state is
834  * resynchronized if the cipher block size is 8 octets or less. The
835  * last 8 octets of ciphertext are passed through the cipher and the
836  * block boundary is reset.
837  *
838  * The repetition of 16 bits in the 80 bits of random data prefixed to
839  * the message allows the receiver to immediately check whether the
840  * session key is incorrect.
841  */
842 typedef struct pgpPktEdata_s {
843  byte data[1];
844 } pgpPktEdata;
845 
846 /*
847  * 5.8. Marker Packet (Obsolete Literal Packet) (Tag 10)
848  *
849  * An experimental version of PGP used this packet as the Literal
850  * packet, but no released version of PGP generated Literal packets with
851  * this tag. With PGP 5.x, this packet has been re-assigned and is
852  * reserved for use as the Marker packet.
853  *
854  * The body of this packet consists of:
855  * - The three octets 0x50, 0x47, 0x50 (which spell "PGP" in UTF-8).
856  *
857  * Such a packet MUST be ignored when received. It may be placed at the
858  * beginning of a message that uses features not available in PGP 2.6.x
859  * in order to cause that version to report that newer software is
860  * necessary to process the message.
861  */
862 /*
863  * 5.9. Literal Data Packet (Tag 11)
864  *
865  * A Literal Data packet contains the body of a message; data that is
866  * not to be further interpreted.
867  *
868  * The body of this packet consists of:
869  * - A one-octet field that describes how the data is formatted.
870  *
871  * If it is a 'b' (0x62), then the literal packet contains binary data.
872  * If it is a 't' (0x74), then it contains text data, and thus may need
873  * line ends converted to local form, or other text-mode changes. RFC
874  * 1991 also defined a value of 'l' as a 'local' mode for machine-local
875  * conversions. This use is now deprecated.
876  * - File name as a string (one-octet length, followed by file name),
877  * if the encrypted data should be saved as a file.
878  *
879  * If the special name "_CONSOLE" is used, the message is considered to
880  * be "for your eyes only". This advises that the message data is
881  * unusually sensitive, and the receiving program should process it more
882  * carefully, perhaps avoiding storing the received data to disk, for
883  * example.
884  * - A four-octet number that indicates the modification date of the
885  * file, or the creation time of the packet, or a zero that
886  * indicates the present time.
887  * - The remainder of the packet is literal data.
888  *
889  * Text data is stored with <CR><LF> text endings (i.e. network-normal
890  * line endings). These should be converted to native line endings by
891  * the receiving software.
892  */
893 typedef struct pgpPktLdata_s {
897 } pgpPktLdata;
898 
899 /*
900  * 5.10. Trust Packet (Tag 12)
901  *
902  * The Trust packet is used only within keyrings and is not normally
903  * exported. Trust packets contain data that record the user's
904  * specifications of which key holders are trustworthy introducers,
905  * along with other information that implementing software uses for
906  * trust information.
907  *
908  * Trust packets SHOULD NOT be emitted to output streams that are
909  * transferred to other users, and they SHOULD be ignored on any input
910  * other than local keyring files.
911  */
912 typedef struct pgpPktTrust_s {
914 } pgpPktTrust;
915 
916 /*
917  * 5.11. User ID Packet (Tag 13)
918  *
919  * A User ID packet consists of data that is intended to represent the
920  * name and email address of the key holder. By convention, it includes
921  * an RFC 822 mail name, but there are no restrictions on its content.
922  * The packet length in the header specifies the length of the user id.
923  * If it is text, it is encoded in UTF-8.
924  *
925  */
926 typedef struct pgpPktUid_s {
928 } pgpPktUid;
929 
932 union pgpPktPre_u {
944 };
945 
948 /*@-typeuse@*/
949 typedef enum pgpArmor_e {
957 #define PGPARMOR_ERROR PGPARMOR_ERR_NO_BEGIN_PGP
966 } pgpArmor;
967 /*@=typeuse@*/
968 
972 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
973 extern struct pgpValTbl_s pgpArmorTbl[];
974 
977 /*@-typeuse@*/
978 typedef enum pgpArmorKey_e {
984 } pgpArmorKey;
985 /*@=typeuse@*/
986 
990 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
991 extern struct pgpValTbl_s pgpArmorKeyTbl[];
992 
996 typedef enum rpmDigestFlags_e {
999 
1000 /*@unchecked@*/
1002 
1003 /*@unchecked@*/ /*@observer@*/
1004 extern struct poptOption rpmDigestPoptTable[];
1005 
1006 /*@-fcnuse@*/
1007 #ifdef __cplusplus
1008 extern "C" {
1009 #endif
1010 
1017 /*@unused@*/ static inline
1018 unsigned int pgpGrab(const byte *s, int nbytes)
1019  /*@*/
1020 {
1021  unsigned int i = 0;
1022  int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
1023 /*@-boundsread@*/
1024  while (nb--)
1025  i = (i << 8) | *s++;
1026 /*@=boundsread@*/
1027  return i;
1028 }
1029 
1036 /*@unused@*/ static inline
1037 int pgpLen(const byte *s, /*@out@*/ unsigned int *lenp)
1038  /*@modifies *lenp @*/
1039 {
1040 /*@-boundswrite@*/
1041  if (*s < 192) {
1042  (*lenp) = *s++;
1043  return 1;
1044  } else if (*s < 255) {
1045  (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
1046  return 2;
1047  } else {
1048  (*lenp) = pgpGrab(s+1, 4);
1049  return 5;
1050  }
1051 /*@=boundswrite@*/
1052 }
1053 
1059 /*@unused@*/ static inline
1060 unsigned int pgpMpiBits(const byte *p)
1061  /*@requires maxRead(p) >= 1 @*/
1062  /*@*/
1063 {
1064  return ((p[0] << 8) | p[1]);
1065 }
1066 
1072 /*@unused@*/ static inline
1073 unsigned int pgpMpiLen(const byte *p)
1074  /*@requires maxRead(p) >= 1 @*/
1075  /*@*/
1076 {
1077  return (2 + ((pgpMpiBits(p)+7)>>3));
1078 }
1079 
1087 /*@unused@*/ static inline
1088 char * pgpHexCvt(/*@returned@*/ char *t, const byte *s, int nbytes)
1089  /*@modifies *t @*/
1090 {
1091  static char hex[] = "0123456789abcdef";
1092 /*@-boundswrite@*/
1093  while (nbytes-- > 0) {
1094  unsigned int i;
1095  i = *s++;
1096  *t++ = hex[ (i >> 4) & 0xf ];
1097  *t++ = hex[ (i ) & 0xf ];
1098  }
1099  *t = '\0';
1100 /*@=boundswrite@*/
1101  return t;
1102 }
1103 
1111 /*@unused@*/ static inline /*@observer@*/
1112 char * pgpHexStr(const byte *p, unsigned int plen)
1113  /*@*/
1114 {
1115  static char prbuf[8*BUFSIZ]; /* XXX ick */
1116  char *t = prbuf;
1117  t = pgpHexCvt(t, p, plen);
1118  return prbuf;
1119 }
1120 
1127 /*@unused@*/ static inline /*@observer@*/
1128 const char * pgpMpiStr(const byte *p)
1129  /*@requires maxRead(p) >= 3 @*/
1130  /*@*/
1131 {
1132  static char prbuf[8*BUFSIZ]; /* XXX ick */
1133  char *t = prbuf;
1134  sprintf(t, "[%4u]: ", pgpGrab(p, 2));
1135  t += strlen(t);
1136  t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
1137  return prbuf;
1138 }
1139 
1146 /*@unused@*/ static inline /*@observer@*/
1147 const char * pgpValStr(pgpValTbl vs, byte val)
1148  /*@*/
1149 {
1150  do {
1151  if (vs->val == val)
1152  break;
1153  } while ((++vs)->val != -1);
1154  return vs->str;
1155 }
1156 
1164 /*@unused@*/ static inline
1165 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
1166  /*@*/
1167 {
1168  do {
1169  int vlen = strlen(vs->str);
1170  if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
1171  break;
1172  } while ((++vs)->val != -1);
1173  return vs->val;
1174 }
1175 
1182 /*@-exportlocal@*/
1183 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
1184  /*@globals fileSystem @*/
1185  /*@modifies fileSystem @*/;
1186 /*@=exportlocal@*/
1187 
1195 /*@-exportlocal@*/
1196 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
1197  /*@globals fileSystem @*/
1198  /*@modifies fileSystem @*/;
1199 /*@=exportlocal@*/
1200 
1206 /*@-exportlocal@*/
1207 int pgpPrtSig(const pgpPkt pp)
1208  /*@globals fileSystem, internalState @*/
1209  /*@modifies fileSystem, internalState @*/;
1210 /*@=exportlocal@*/
1211 
1217 int pgpPrtKey(const pgpPkt pp)
1218  /*@globals fileSystem, internalState @*/
1219  /*@modifies fileSystem, internalState @*/;
1220 
1226 /*@-exportlocal@*/
1227 int pgpPrtUserID(const pgpPkt pp)
1228  /*@globals fileSystem, internalState @*/
1229  /*@modifies fileSystem, internalState @*/;
1230 /*@=exportlocal@*/
1231 
1237 /*@-exportlocal@*/
1238 int pgpPrtComment(const pgpPkt pp)
1239  /*@globals fileSystem @*/
1240  /*@modifies fileSystem @*/;
1241 /*@=exportlocal@*/
1242 
1251 /*@-exportlocal@*/
1252 int pgpPubkeyFingerprint(const byte * pkt, unsigned int pktlen,
1253  /*@out@*/ byte * keyid)
1254  /*@modifies *keyid @*/;
1255 /*@=exportlocal@*/
1256 
1264 int pgpExtractPubkeyFingerprint(const char * b64pkt, /*@out@*/ byte * keyid)
1265  /*@modifies *keyid @*/;
1266 
1274 int pgpPktLen(const byte *pkt, unsigned int pleft, /*@out@*/ pgpPkt pp)
1275  /*@modifies pp @*/;
1276 
1283 /*@-exportlocal@*/
1284 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
1285  /*@globals fileSystem, internalState @*/
1286  /*@modifies fileSystem, internalState @*/;
1287 /*@=exportlocal@*/
1288 
1297 int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
1298  /*@globals fileSystem, internalState @*/
1299  /*@modifies dig, fileSystem, internalState @*/;
1300 
1308 pgpArmor pgpReadPkts(const char * fn,
1309  /*@out@*/ const byte ** pkt, /*@out@*/ size_t * pktlen)
1310  /*@globals h_errno, fileSystem, internalState @*/
1311  /*@modifies *pkt, *pktlen, fileSystem, internalState @*/;
1312 
1320 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
1321  /*@*/;
1322 
1327 /*@only@*/
1328 pgpDig pgpNewDig(void)
1329  /*@*/;
1330 
1335 void pgpCleanDig(/*@null@*/ pgpDig dig)
1336  /*@modifies dig @*/;
1337 
1343 /*@only@*/ /*@null@*/
1344 pgpDig pgpFreeDig(/*@only@*/ /*@null@*/ pgpDig dig)
1345  /*@modifies dig @*/;
1346 
1352 /*@unused@*/ static inline
1353 int pgpIsPkt(const byte * p)
1354  /*@*/
1355 {
1356 /*@-boundsread@*/
1357  unsigned int val = *p++;
1358 /*@=boundsread@*/
1359  pgpTag tag;
1360  int rc;
1361 
1362  /* XXX can't deal with these. */
1363  if (!(val & 0x80))
1364  return 0;
1365 
1366  if (val & 0x40)
1367  tag = (val & 0x3f);
1368  else
1369  tag = (val >> 2) & 0xf;
1370 
1371  switch (tag) {
1372  case PGPTAG_MARKER:
1375  case PGPTAG_PUBLIC_KEY:
1376  case PGPTAG_SECRET_KEY:
1378  case PGPTAG_SIGNATURE:
1379  case PGPTAG_COMMENT:
1380  case PGPTAG_COMMENT_OLD:
1381  case PGPTAG_LITERAL_DATA:
1383  case PGPTAG_SYMMETRIC_DATA:
1384  rc = 1;
1385  break;
1386  case PGPTAG_PUBLIC_SUBKEY:
1387  case PGPTAG_SECRET_SUBKEY:
1388  case PGPTAG_USER_ID:
1389  case PGPTAG_RESERVED:
1390  case PGPTAG_TRUST:
1391  case PGPTAG_PHOTOID:
1392  case PGPTAG_ENCRYPTED_MDC:
1393  case PGPTAG_MDC:
1394  case PGPTAG_PRIVATE_60:
1395  case PGPTAG_PRIVATE_62:
1396  case PGPTAG_CONTROL:
1397  default:
1398  rc = 0;
1399  break;
1400  }
1401 
1402  return rc;
1403 }
1404 
1405 #define CRC24_INIT 0xb704ce
1406 #define CRC24_POLY 0x1864cfb
1407 
1414 /*@unused@*/ static inline
1415 unsigned int pgpCRC(const byte *octets, size_t len)
1416  /*@*/
1417 {
1418  unsigned int crc = CRC24_INIT;
1419  int i;
1420 
1421  while (len--) {
1422 /*@-boundsread@*/
1423  crc ^= (*octets++) << 16;
1424 /*@=boundsread@*/
1425  for (i = 0; i < 8; i++) {
1426  crc <<= 1;
1427  if (crc & 0x1000000)
1428  crc ^= CRC24_POLY;
1429  }
1430  }
1431  return crc & 0xffffff;
1432 }
1433 
1439 /*@only@*/
1440 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
1441  /*@*/;
1442 
1450 /*@only@*/ /*@null@*/
1451 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
1452  /*@*/;
1453 
1461 int rpmDigestUpdate(/*@null@*/ DIGEST_CTX ctx, const void * data, size_t len)
1462  /*@modifies ctx @*/;
1463 
1475 int rpmDigestFinal(/*@only@*/ /*@null@*/ DIGEST_CTX ctx,
1476  /*@null@*/ /*@out@*/ void * datap,
1477  /*@null@*/ /*@out@*/ size_t * lenp, int asAscii)
1478  /*@modifies *datap, *lenp @*/;
1479 
1480 #ifdef __cplusplus
1481 }
1482 #endif
1483 /*@=fcnuse@*/
1484 
1485 #endif /* H_RPMPGP */