rpm  4.15.1
rpmpgp.h
Go to the documentation of this file.
1 #ifndef H_RPMPGP
2 #define H_RPMPGP
3 
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <rpm/rpmtypes.h>
17 #include <rpm/rpmstring.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
25 typedef struct DIGEST_CTX_s * DIGEST_CTX;
26 typedef struct rpmDigestBundle_s * rpmDigestBundle;
27 
30 typedef struct pgpDig_s * pgpDig;
31 
34 typedef struct pgpDigParams_s * pgpDigParams;
35 
36 typedef uint8_t pgpKeyID_t[8];
37 typedef uint8_t pgpTime_t[4];
38 
46 typedef enum pgpTag_e {
59  PGPTAG_TRUST = 12,
65  PGPTAG_MDC = 19,
70 } pgpTag;
71 
106 typedef struct pgpPktPubkey_s {
107  uint8_t version;
109  uint8_t algo;
110 } pgpPktPubkey;
111 
112 
119 typedef enum pgpSigType_e {
137 } pgpSigType;
138 
162 typedef enum pgpPubkeyAlgo_e {
172 } pgpPubkeyAlgo;
173 
198 typedef enum pgpSymkeyAlgo_e {
211 } pgpSymkeyAlgo;
212 
228 typedef enum pgpCompressAlgo_e {
234 
258 typedef enum pgpHashAlgo_e {
269 } pgpHashAlgo;
270 
292 typedef struct pgpPktSigV3_s {
293  uint8_t version;
294  uint8_t hashlen;
295  uint8_t sigtype;
298  uint8_t pubkey_algo;
299  uint8_t hash_algo;
300  uint8_t signhash16[2];
301 } * pgpPktSigV3;
302 
324 typedef struct pgpPktSigV4_s {
325  uint8_t version;
326  uint8_t sigtype;
327  uint8_t pubkey_algo;
328  uint8_t hash_algo;
329  uint8_t hashlen[2];
330 } * pgpPktSigV4;
331 
398 typedef enum pgpSubType_e {
437 } pgpSubType;
438 
459 typedef union pgpPktSig_u {
462 } * pgpPktSig;
463 
492 typedef struct pgpPktSymkey_s {
493  uint8_t version;
494  uint8_t symkey_algo;
495  uint8_t s2k[1];
496 } pgpPktSymkey;
497 
527 typedef struct pgpPktOnepass_s {
528  uint8_t version;
529  uint8_t sigtype;
530  uint8_t hash_algo;
531  uint8_t pubkey_algo;
533  uint8_t nested;
534 } * pgpPktOnepass;
535 
608 typedef struct pgpPktKeyV3_s {
609  uint8_t version;
611  uint8_t valid[2];
612  uint8_t pubkey_algo;
613 } * pgpPktKeyV3;
614 
646 typedef struct pgpPktKeyV4_s {
647  uint8_t version;
649  uint8_t pubkey_algo;
650 } * pgpPktKeyV4;
651 
716 typedef union pgpPktKey_u {
719 } pgpPktKey;
720 
721 /* \ingroup rpmpgp
722  * 5.6. Compressed Data Packet (Tag 8)
723  *
724  * The Compressed Data packet contains compressed data. Typically, this
725  * packet is found as the contents of an encrypted packet, or following
726  * a Signature or One-Pass Signature packet, and contains literal data
727  * packets.
728  *
729  * The body of this packet consists of:
730  * - One octet that gives the algorithm used to compress the packet.
731  * - The remainder of the packet is compressed data.
732  *
733  * A Compressed Data Packet's body contains an block that compresses
734  * some set of packets. See section "Packet Composition" for details on
735  * how messages are formed.
736  *
737  * ZIP-compressed packets are compressed with raw RFC 1951 DEFLATE
738  * blocks. Note that PGP V2.6 uses 13 bits of compression. If an
739  * implementation uses more bits of compression, PGP V2.6 cannot
740  * decompress it.
741  *
742  * ZLIB-compressed packets are compressed with RFC 1950 ZLIB-style
743  * blocks.
744  */
745 typedef struct pgpPktCdata_s {
746  uint8_t compressalgo;
747  uint8_t data[1];
748 } pgpPktCdata;
749 
750 /* \ingroup rpmpgp
751  * 5.7. Symmetrically Encrypted Data Packet (Tag 9)
752  *
753  * The Symmetrically Encrypted Data packet contains data encrypted with
754  * a symmetric-key algorithm. When it has been decrypted, it will
755  * typically contain other packets (often literal data packets or
756  * compressed data packets).
757  *
758  * The body of this packet consists of:
759  * - Encrypted data, the output of the selected symmetric-key cipher
760  * operating in PGP's variant of Cipher Feedback (CFB) mode.
761  *
762  * The symmetric cipher used may be specified in an Public-Key or
763  * Symmetric-Key Encrypted Session Key packet that precedes the
764  * Symmetrically Encrypted Data Packet. In that case, the cipher
765  * algorithm octet is prefixed to the session key before it is
766  * encrypted. If no packets of these types precede the encrypted data,
767  * the IDEA algorithm is used with the session key calculated as the MD5
768  * hash of the passphrase.
769  *
770  * The data is encrypted in CFB mode, with a CFB shift size equal to the
771  * cipher's block size. The Initial Vector (IV) is specified as all
772  * zeros. Instead of using an IV, OpenPGP prefixes a 10-octet string to
773  * the data before it is encrypted. The first eight octets are random,
774  * and the 9th and 10th octets are copies of the 7th and 8th octets,
775  * respectively. After encrypting the first 10 octets, the CFB state is
776  * resynchronized if the cipher block size is 8 octets or less. The
777  * last 8 octets of ciphertext are passed through the cipher and the
778  * block boundary is reset.
779  *
780  * The repetition of 16 bits in the 80 bits of random data prefixed to
781  * the message allows the receiver to immediately check whether the
782  * session key is incorrect.
783  */
784 typedef struct pgpPktEdata_s {
785  uint8_t data[1];
786 } pgpPktEdata;
787 
788 /* \ingroup rpmpgp
789  * 5.8. Marker Packet (Obsolete Literal Packet) (Tag 10)
790  *
791  * An experimental version of PGP used this packet as the Literal
792  * packet, but no released version of PGP generated Literal packets with
793  * this tag. With PGP 5.x, this packet has been re-assigned and is
794  * reserved for use as the Marker packet.
795  *
796  * The body of this packet consists of:
797  * - The three octets 0x50, 0x47, 0x50 (which spell "PGP" in UTF-8).
798  *
799  * Such a packet MUST be ignored when received. It may be placed at the
800  * beginning of a message that uses features not available in PGP 2.6.x
801  * in order to cause that version to report that newer software is
802  * necessary to process the message.
803  */
804 /* \ingroup rpmpgp
805  * 5.9. Literal Data Packet (Tag 11)
806  *
807  * A Literal Data packet contains the body of a message; data that is
808  * not to be further interpreted.
809  *
810  * The body of this packet consists of:
811  * - A one-octet field that describes how the data is formatted.
812  *
813  * If it is a 'b' (0x62), then the literal packet contains binary data.
814  * If it is a 't' (0x74), then it contains text data, and thus may need
815  * line ends converted to local form, or other text-mode changes. RFC
816  * 1991 also defined a value of 'l' as a 'local' mode for machine-local
817  * conversions. This use is now deprecated.
818  * - File name as a string (one-octet length, followed by file name),
819  * if the encrypted data should be saved as a file.
820  *
821  * If the special name "_CONSOLE" is used, the message is considered to
822  * be "for your eyes only". This advises that the message data is
823  * unusually sensitive, and the receiving program should process it more
824  * carefully, perhaps avoiding storing the received data to disk, for
825  * example.
826  * - A four-octet number that indicates the modification date of the
827  * file, or the creation time of the packet, or a zero that
828  * indicates the present time.
829  * - The remainder of the packet is literal data.
830  *
831  * Text data is stored with <CR><LF> text endings (i.e. network-normal
832  * line endings). These should be converted to native line endings by
833  * the receiving software.
834  */
835 typedef struct pgpPktLdata_s {
836  uint8_t format;
837  uint8_t filenamelen;
838  uint8_t filename[1];
839 } pgpPktLdata;
840 
841 /* \ingroup rpmpgp
842  * 5.10. Trust Packet (Tag 12)
843  *
844  * The Trust packet is used only within keyrings and is not normally
845  * exported. Trust packets contain data that record the user's
846  * specifications of which key holders are trustworthy introducers,
847  * along with other information that implementing software uses for
848  * trust information.
849  *
850  * Trust packets SHOULD NOT be emitted to output streams that are
851  * transferred to other users, and they SHOULD be ignored on any input
852  * other than local keyring files.
853  */
854 typedef struct pgpPktTrust_s {
855  uint8_t flag;
856 } pgpPktTrust;
857 
858 /* \ingroup rpmpgp
859  * 5.11. User ID Packet (Tag 13)
860  *
861  * A User ID packet consists of data that is intended to represent the
862  * name and email address of the key holder. By convention, it includes
863  * an RFC 822 mail name, but there are no restrictions on its content.
864  * The packet length in the header specifies the length of the user id.
865  * If it is text, it is encoded in UTF-8.
866  *
867  */
868 typedef struct pgpPktUid_s {
869  uint8_t userid[1];
870 } pgpPktUid;
871 
874 union pgpPktPre_u {
886 };
887 
890 typedef enum pgpArmor_e {
898 #define PGPARMOR_ERROR PGPARMOR_ERR_NO_BEGIN_PGP
907 } pgpArmor;
908 
911 typedef enum pgpArmorKey_e {
917 } pgpArmorKey;
918 
919 typedef enum pgpValType_e {
930 } pgpValType;
931 
937 };
938 
940 
947 const char * pgpValString(pgpValType type, uint8_t val);
948 
955 static inline
956 unsigned int pgpGrab(const uint8_t *s, size_t nbytes)
957 {
958  size_t i = 0;
959  size_t nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
960  while (nb--)
961  i = (i << 8) | *s++;
962  return i;
963 }
964 
971 char * pgpHexStr(const uint8_t *p, size_t plen);
972 
981 int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen,
982  uint8_t **fp, size_t *fplen);
983 
991 int pgpPubkeyKeyID(const uint8_t * pkt, size_t pktlen, pgpKeyID_t keyid);
992 
1001 int pgpPrtParams(const uint8_t *pkts, size_t pktlen, unsigned int pkttype,
1002  pgpDigParams * ret);
1003 
1013 int pgpPrtParamsSubkeys(const uint8_t *pkts, size_t pktlen,
1014  pgpDigParams mainkey, pgpDigParams **subkeys,
1015  int *subkeysCount);
1024 int pgpPrtPkts(const uint8_t *pkts, size_t pktlen, pgpDig dig, int printing);
1025 
1033 pgpArmor pgpReadPkts(const char * fn, uint8_t ** pkt, size_t * pktlen);
1034 
1042 pgpArmor pgpParsePkts(const char *armor, uint8_t ** pkt, size_t * pktlen);
1043 
1056 int pgpPubKeyCertLen(const uint8_t *pkts, size_t pktslen, size_t *certlen);
1057 
1065 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns);
1066 
1071 pgpDig pgpNewDig(void);
1072 
1077 void pgpCleanDig(pgpDig dig);
1078 
1084 pgpDig pgpFreeDig(pgpDig dig);
1085 
1092 pgpDigParams pgpDigGetParams(pgpDig dig, unsigned int pkttype);
1093 
1101 
1108 unsigned int pgpDigParamsAlgo(pgpDigParams digp, unsigned int algotype);
1109 
1116 
1125 
1134 rpmRC pgpVerifySig(pgpDig dig, DIGEST_CTX hashctx);
1135 
1141 char *pgpIdentItem(pgpDigParams digp);
1142 
1150 int rpmInitCrypto(void);
1151 
1155 int rpmFreeCrypto(void);
1156 
1163 
1169 size_t rpmDigestLength(int hashalgo);
1170 
1178 DIGEST_CTX rpmDigestInit(int hashalgo, rpmDigestFlags flags);
1179 
1187 int rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len);
1188 
1200 int rpmDigestFinal(DIGEST_CTX ctx,
1201  void ** datap,
1202  size_t * lenp, int asAscii);
1203 
1209 
1216 
1225 int rpmDigestBundleAdd(rpmDigestBundle bundle, int algo,
1226  rpmDigestFlags flags);
1227 
1236 int rpmDigestBundleAddID(rpmDigestBundle bundle, int algo, int id,
1237  rpmDigestFlags flags);
1238 
1246 int rpmDigestBundleUpdate(rpmDigestBundle bundle, const void *data, size_t len);
1247 
1258 int rpmDigestBundleFinal(rpmDigestBundle bundle, int id,
1259  void ** datap, size_t * lenp, int asAscii);
1260 
1268 
1269 #ifdef __cplusplus
1270 }
1271 #endif
1272 
1273 #endif /* H_RPMPGP */
uint8_t userid[1]
Definition: rpmpgp.h:869
struct pgpPktSigV3_s v3
Definition: rpmpgp.h:460
5.5.1.
Definition: rpmpgp.h:608
uint8_t hash_algo
Definition: rpmpgp.h:328
pgpArmor pgpParsePkts(const char *armor, uint8_t **pkt, size_t *pktlen)
Parse armored OpenPGP packets from memory.
enum pgpPubkeyAlgo_e pgpPubkeyAlgo
9.1.
pgpDig pgpNewDig(void)
Create a container for parsed OpenPGP packet(s).
uint8_t hash_algo
Definition: rpmpgp.h:530
struct pgpPktSigV4_s * pgpPktSigV4
5.2.3.
struct pgpPktPubkey_s pgpPktPubkey
5.1.
pgpPktCdata cdata
Definition: rpmpgp.h:880
uint8_t pubkey_algo
Definition: rpmpgp.h:649
struct pgpPktKeyV3_s * pgpPktKeyV3
5.5.1.
pgpValType_e
Definition: rpmpgp.h:919
enum pgpSymkeyAlgo_e pgpSymkeyAlgo
9.2.
pgpPktOnepass onepass
Definition: rpmpgp.h:878
pgpTime_t time
Definition: rpmpgp.h:648
int pgpDigParamsCmp(pgpDigParams p1, pgpDigParams p2)
Compare OpenPGP packet parameters param p1 1st parameter container param p2 2nd parameter container r...
pgpTag_e
4.3.
Definition: rpmpgp.h:46
pgpArmor pgpReadPkts(const char *fn, uint8_t **pkt, size_t *pktlen)
Parse armored OpenPGP packets from a file.
uint8_t version
Definition: rpmpgp.h:293
uint8_t version
Definition: rpmpgp.h:609
rpmDigestBundle rpmDigestBundleFree(rpmDigestBundle bundle)
Free a digest bundle and all contained digest contexts.
uint8_t signhash16[2]
Definition: rpmpgp.h:300
pgpPubkeyAlgo_e
9.1.
Definition: rpmpgp.h:162
uint8_t version
Definition: rpmpgp.h:528
struct pgpPktKeyV4_s * pgpPktKeyV4
The version 4 format is similar to the version 3 format except for the absence of a validity period.
int rpmDigestFinal(DIGEST_CTX ctx, void **datap, size_t *lenp, int asAscii)
Return digest and destroy context.
struct DIGEST_CTX_s * DIGEST_CTX
Definition: rpmpgp.h:25
5.2.3.
Definition: rpmpgp.h:324
enum pgpCompressAlgo_e pgpCompressAlgo
9.3.
DIGEST_CTX rpmDigestBundleDupCtx(rpmDigestBundle bundle, int id)
Duplicate a digest context from a bundle.
DIGEST_CTX rpmDigestInit(int hashalgo, rpmDigestFlags flags)
Initialize digest.
uint8_t version
Definition: rpmpgp.h:107
pgpPktSig sig
Definition: rpmpgp.h:876
enum pgpArmorKey_e pgpArmorKey
uint8_t version
Definition: rpmpgp.h:325
uint8_t valid[2]
Definition: rpmpgp.h:611
pgpArmorKey_e
Definition: rpmpgp.h:911
uint8_t pubkey_algo
Definition: rpmpgp.h:531
pgpDig pgpFreeDig(pgpDig dig)
Destroy a container for parsed OpenPGP packet(s).
uint8_t flag
Definition: rpmpgp.h:855
struct pgpDig_s * pgpDig
Definition: rpmpgp.h:30
rpmRC pgpVerifySignature(pgpDigParams key, pgpDigParams sig, DIGEST_CTX hashctx)
Verify a PGP signature.
5.2.2.
Definition: rpmpgp.h:292
int pgpPubkeyKeyID(const uint8_t *pkt, size_t pktlen, pgpKeyID_t keyid)
Calculate OpenPGP public key Key ID.
5.5.3.
Definition: rpmpgp.h:716
struct pgpPktTrust_s pgpPktTrust
struct pgpPktEdata_s pgpPktEdata
uint8_t pgpKeyID_t[8]
Definition: rpmpgp.h:36
uint8_t s2k[1]
Definition: rpmpgp.h:495
int rpmDigestBundleFinal(rpmDigestBundle bundle, int id, void **datap, size_t *lenp, int asAscii)
Return digest from a bundle and destroy context, see rpmDigestFinal().
static unsigned int pgpGrab(const uint8_t *s, size_t nbytes)
Return (native-endian) integer from big-endian representation.
Definition: rpmpgp.h:956
struct rpmDigestBundle_s * rpmDigestBundle
Definition: rpmpgp.h:26
uint8_t pubkey_algo
Definition: rpmpgp.h:327
uint8_t data[1]
Definition: rpmpgp.h:747
rpmFlags rpmDigestFlags
Definition: rpmpgp.h:939
uint32_t rpmFlags
Definition: rpmtypes.h:42
pgpArmor_e
Definition: rpmpgp.h:890
struct pgpPktLdata_s pgpPktLdata
pgpPktPubkey pubkey
Definition: rpmpgp.h:875
enum pgpHashAlgo_e pgpHashAlgo
9.4.
enum pgpValType_e pgpValType
uint8_t version
Definition: rpmpgp.h:493
struct pgpPktUid_s pgpPktUid
uint8_t symkey_algo
Definition: rpmpgp.h:494
uint8_t data[1]
Definition: rpmpgp.h:785
int rpmDigestUpdate(DIGEST_CTX ctx, const void *data, size_t len)
Update context with next plain text buffer.
DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
Duplicate a digest context.
uint8_t format
Definition: rpmpgp.h:836
uint8_t sigtype
Definition: rpmpgp.h:326
uint8_t hash_algo
Definition: rpmpgp.h:299
char * pgpHexStr(const uint8_t *p, size_t plen)
Return hex formatted representation of bytes.
pgpPktKey key
Definition: rpmpgp.h:879
pgpDigParams pgpDigParamsFree(pgpDigParams digp)
Destroy parsed OpenPGP packet parameter(s).
rpmDigestFlags_e
Bit(s) to control digest operation.
Definition: rpmpgp.h:935
pgpDigParams pgpDigGetParams(pgpDig dig, unsigned int pkttype)
Retrieve parameters for parsed OpenPGP packet(s).
union pgpPktKey_u pgpPktKey
5.5.3.
struct pgpPktCdata_s pgpPktCdata
rpmRC pgpVerifySig(pgpDig dig, DIGEST_CTX hashctx)
Verify a PGP signature.
int rpmDigestBundleUpdate(rpmDigestBundle bundle, const void *data, size_t len)
Update contexts within bundle with next plain text buffer.
rpmDigestBundle rpmDigestBundleNew(void)
Create a new digest bundle.
int pgpPubkeyFingerprint(const uint8_t *pkt, size_t pktlen, uint8_t **fp, size_t *fplen)
Calculate OpenPGP public key fingerprint.
enum pgpSubType_e pgpSubType
5.2.3.1.
uint8_t algo
Definition: rpmpgp.h:109
pgpTime_t time
Definition: rpmpgp.h:610
pgpSubType_e
5.2.3.1.
Definition: rpmpgp.h:398
pgpPktLdata ldata
Definition: rpmpgp.h:883
char * pgpArmorWrap(int atype, const unsigned char *s, size_t ns)
Wrap a OpenPGP packets in ascii armor for transport.
uint8_t nested
Definition: rpmpgp.h:533
uint8_t compressalgo
Definition: rpmpgp.h:746
pgpHashAlgo_e
9.4.
Definition: rpmpgp.h:258
uint8_t filename[1]
Definition: rpmpgp.h:838
pgpTime_t time
Definition: rpmpgp.h:296
uint8_t hashlen[2]
Definition: rpmpgp.h:329
struct pgpPktSigV3_s * pgpPktSigV3
5.2.2.
pgpKeyID_t signid
Definition: rpmpgp.h:297
pgpPktUid uid
Definition: rpmpgp.h:885
enum pgpTag_e pgpTag
4.3.
uint8_t pgpTime_t[4]
Definition: rpmpgp.h:37
unsigned int pgpDigParamsAlgo(pgpDigParams digp, unsigned int algotype)
Retrieve OpenPGP algorithm parameters param digp parameter container param algotype PGPVAL_HASHALGO /...
uint8_t version
Definition: rpmpgp.h:647
uint8_t pubkey_algo
Definition: rpmpgp.h:612
int rpmDigestBundleAddID(rpmDigestBundle bundle, int algo, int id, rpmDigestFlags flags)
Add a new type of digest to a bundle.
int rpmInitCrypto(void)
Perform cryptography initialization.
char * pgpIdentItem(pgpDigParams digp)
Return a string identification of a PGP signature/pubkey.
pgpPktEdata edata
Definition: rpmpgp.h:881
pgpSigType_e
5.2.1.
Definition: rpmpgp.h:119
struct pgpPktKeyV4_s v4
Definition: rpmpgp.h:718
uint8_t hashlen
Definition: rpmpgp.h:294
pgpPktTrust tdata
Definition: rpmpgp.h:884
uint8_t pubkey_algo
Definition: rpmpgp.h:298
5.2.
Definition: rpmpgp.h:459
struct pgpPktSigV4_s v4
Definition: rpmpgp.h:461
pgpKeyID_t keyid
Definition: rpmpgp.h:108
struct pgpDigParams_s * pgpDigParams
Definition: rpmpgp.h:34
int rpmFreeCrypto(void)
Shutdown cryptography.
int pgpPubKeyCertLen(const uint8_t *pkts, size_t pktslen, size_t *certlen)
Return a length of the first public key certificate in a buffer given by pkts that contains one or mo...
struct pgpPktSymkey_s pgpPktSymkey
5.3.
pgpSymkeyAlgo_e
9.2.
Definition: rpmpgp.h:198
int pgpPrtParamsSubkeys(const uint8_t *pkts, size_t pktlen, pgpDigParams mainkey, pgpDigParams **subkeys, int *subkeysCount)
Parse subkey parameters from OpenPGP packet(s).
struct pgpPktKeyV3_s v3
Definition: rpmpgp.h:717
const char * pgpValString(pgpValType type, uint8_t val)
Return string representation of am OpenPGP value.
enum pgpSigType_e pgpSigType
5.2.1.
int pgpPrtParams(const uint8_t *pkts, size_t pktlen, unsigned int pkttype, pgpDigParams *ret)
Parse a OpenPGP packet(s).
union pgpPktSig_u * pgpPktSig
5.2.
uint8_t sigtype
Definition: rpmpgp.h:295
int rpmDigestBundleAdd(rpmDigestBundle bundle, int algo, rpmDigestFlags flags)
Add a new type of digest to a bundle.
void pgpCleanDig(pgpDig dig)
Release (malloc'd) data from container.
pgpCompressAlgo_e
9.3.
Definition: rpmpgp.h:228
int pgpPrtPkts(const uint8_t *pkts, size_t pktlen, pgpDig dig, int printing)
Print/parse a OpenPGP packet(s).
The version 4 format is similar to the version 3 format except for the absence of a validity period.
Definition: rpmpgp.h:646
uint8_t sigtype
Definition: rpmpgp.h:529
pgpKeyID_t signid
Definition: rpmpgp.h:532
enum pgpArmor_e pgpArmor
uint8_t filenamelen
Definition: rpmpgp.h:837
enum rpmRC_e rpmRC
Package read return codes.
size_t rpmDigestLength(int hashalgo)
Obtain digest length in bytes.
pgpPktSymkey symkey
Definition: rpmpgp.h:877
struct pgpPktOnepass_s * pgpPktOnepass
5.4.