1 |
#ifndef GPGLIB_OPENPGP_H |
2 |
#define GPGLIB_OPENPGP_H |
3 |
|
4 |
#ifdef __cplusplus |
5 |
extern "C" { |
6 |
#endif |
7 |
|
8 |
#ifndef HAVE_BYTE_TYPEDEF |
9 |
typedef unsigned char byte; |
10 |
#define HAVE_BYTE_TYPEDEF |
11 |
#endif |
12 |
|
13 |
#ifndef HAVE_U32_TYPEDEF |
14 |
typedef unsigned long u32; |
15 |
#define HAVE_U32_TYPEDEF |
16 |
#endif |
17 |
|
18 |
#ifndef HAVE_ULONG_TYPEDEF |
19 |
typedef unsigned long ulong; |
20 |
#define SIZEOF_UNSIGNED_LONG 4 |
21 |
#define HAVE_ULONG_TYPEDEF |
22 |
#endif |
23 |
|
24 |
#ifndef HAVE_U16_TYPEDEF |
25 |
typedef unsigned short u16; |
26 |
#define HAVE_U16_TYPEDEF |
27 |
#endif |
28 |
|
29 |
#ifndef DIM |
30 |
#define DIM(v) (sizeof(v)/sizeof((v)[0])) |
31 |
#endif |
32 |
|
33 |
#ifndef safe_free |
34 |
#define safe_free( p ) do { \ |
35 |
if( p ) { free( p ); p = NULL; } \ |
36 |
} while( 0 ) |
37 |
#endif |
38 |
|
39 |
#define G10ERR_GENERAL 1 |
40 |
#define G10ERR_UNKNOWN_PACKET 2 |
41 |
#define G10ERR_UNKNOWN_VERSION 3 /* Unknown version (in packet) */ |
42 |
#define G10ERR_PUBKEY_ALGO 4 /* Unknown pubkey algorithm */ |
43 |
#define G10ERR_DIGEST_ALGO 5 /* Unknown digest algorithm */ |
44 |
#define G10ERR_BAD_PUBKEY 6 /* Bad public key */ |
45 |
#define G10ERR_BAD_SECKEY 7 /* Bad secret key */ |
46 |
#define G10ERR_BAD_SIGN 8 /* Bad signature */ |
47 |
#define G10ERR_NO_PUBKEY 9 /* public key not found */ |
48 |
#define G10ERR_CHECKSUM 10 /* checksum error */ |
49 |
#define G10ERR_BAD_PASS 11 /* Bad passphrase */ |
50 |
#define G10ERR_CIPHER_ALGO 12 /* Unknown cipher algorithm */ |
51 |
#define G10ERR_KEYRING_OPEN 13 |
52 |
#define G10ERR_INVALID_PACKET 14 |
53 |
#define G10ERR_INVALID_ARMOR 15 |
54 |
#define G10ERR_NO_USER_ID 16 |
55 |
#define G10ERR_NO_SECKEY 17 /* secret key not available */ |
56 |
#define G10ERR_WRONG_SECKEY 18 /* wrong seckey used */ |
57 |
#define G10ERR_UNSUPPORTED 19 |
58 |
#define G10ERR_BAD_KEY 20 /* bad (session) key */ |
59 |
#define G10ERR_READ_FILE 21 |
60 |
#define G10ERR_WRITE_FILE 22 |
61 |
#define G10ERR_COMPR_ALGO 23 /* Unknown compress algorithm */ |
62 |
#define G10ERR_OPEN_FILE 24 |
63 |
#define G10ERR_CREATE_FILE 25 |
64 |
#define G10ERR_PASSPHRASE 26 /* invalid passphrase */ |
65 |
#define G10ERR_NI_PUBKEY 27 |
66 |
#define G10ERR_NI_CIPHER 28 |
67 |
#define G10ERR_SIG_CLASS 29 |
68 |
#define G10ERR_BAD_MPI 30 |
69 |
#define G10ERR_RESOURCE_LIMIT 31 |
70 |
#define G10ERR_INV_KEYRING 32 |
71 |
#define G10ERR_TRUSTDB 33 /* a problem with the trustdb */ |
72 |
#define G10ERR_BAD_CERT 34 /* bad certicate */ |
73 |
#define G10ERR_INV_USER_ID 35 |
74 |
#define G10ERR_CLOSE_FILE 36 |
75 |
#define G10ERR_RENAME_FILE 37 |
76 |
#define G10ERR_DELETE_FILE 38 |
77 |
#define G10ERR_UNEXPECTED 39 |
78 |
#define G10ERR_TIME_CONFLICT 40 |
79 |
#define G10ERR_WR_PUBKEY_ALGO 41 /* unusabe pubkey algo */ |
80 |
#define G10ERR_FILE_EXISTS 42 |
81 |
#define G10ERR_WEAK_KEY 43 /* NOTE: hardcoded into the cipher modules */ |
82 |
#define G10ERR_WRONG_KEYLEN 44 /* NOTE: hardcoded into the cipher modules */ |
83 |
#define G10ERR_INV_ARG 45 |
84 |
#define G10ERR_BAD_URI 46 /* syntax error in URI */ |
85 |
#define G10ERR_INVALID_URI 47 /* e.g. unsupported scheme */ |
86 |
#define G10ERR_NETWORK 48 /* general network error */ |
87 |
#define G10ERR_UNKNOWN_HOST 49 |
88 |
#define G10ERR_SELFTEST_FAILED 50 |
89 |
#define G10ERR_NOT_ENCRYPTED 51 |
90 |
#define G10ERR_NOT_PROCESSED 52 |
91 |
#define G10ERR_UNU_PUBKEY 53 |
92 |
#define G10ERR_UNU_SECKEY 54 |
93 |
#define G10ERR_KEYSERVER 55 |
94 |
|
95 |
#define CIPHER_ALGO_NONE 0 |
96 |
#define CIPHER_ALGO_IDEA 1 |
97 |
#define CIPHER_ALGO_3DES 2 |
98 |
#define CIPHER_ALGO_CAST5 3 |
99 |
#define CIPHER_ALGO_BLOWFISH 4 /* blowfish 128 bit key */ |
100 |
#define CIPHER_ALGO_SAFER_SK128 5 |
101 |
#define CIPHER_ALGO_DES_SK 6 |
102 |
#define CIPHER_ALGO_AES 7 |
103 |
#define CIPHER_ALGO_AES192 8 |
104 |
#define CIPHER_ALGO_AES256 9 |
105 |
#define CIPHER_ALGO_RIJNDAEL CIPHER_ALGO_AES |
106 |
#define CIPHER_ALGO_RIJNDAEL192 CIPHER_ALGO_AES192 |
107 |
#define CIPHER_ALGO_RIJNDAEL256 CIPHER_ALGO_AES256 |
108 |
#define CIPHER_ALGO_TWOFISH 10 /* twofish 256 bit */ |
109 |
#define CIPHER_ALGO_SKIPJACK 101 /* experimental: skipjack */ |
110 |
#define CIPHER_ALGO_TWOFISH_OLD 102 /* experimental: twofish 128 bit */ |
111 |
#define CIPHER_ALGO_DUMMY 110 /* no encryption at all */ |
112 |
|
113 |
#define PUBKEY_ALGO_RSA 1 |
114 |
#define PUBKEY_ALGO_RSA_E 2 /* RSA encrypt only */ |
115 |
#define PUBKEY_ALGO_RSA_S 3 /* RSA sign only */ |
116 |
#define PUBKEY_ALGO_ELGAMAL_E 16 /* encrypt only ElGamal (but not for v3)*/ |
117 |
#define PUBKEY_ALGO_DSA 17 |
118 |
#define PUBKEY_ALGO_ELGAMAL 20 /* sign and encrypt elgamal */ |
119 |
|
120 |
#define PUBKEY_USAGE_SIG 1 /* key is good for signatures */ |
121 |
#define PUBKEY_USAGE_ENC 2 /* key is good for encryption */ |
122 |
#define PUBKEY_USAGE_CERT 4 /* key is also good to certify other keys*/ |
123 |
|
124 |
#define DIGEST_ALGO_MD5 1 |
125 |
#define DIGEST_ALGO_SHA1 2 |
126 |
#define DIGEST_ALGO_RMD160 3 |
127 |
#define DIGEST_ALGO_SHA256 8 |
128 |
#define DIGEST_ALGO_SHA384 9 |
129 |
#define DIGEST_ALGO_SHA512 10 |
130 |
|
131 |
#define MD_MD5 DIGEST_ALGO_MD5 |
132 |
#define MD_SHA1 DIGEST_ALGO_SHA1 |
133 |
#define MD_RMD160 DIGEST_ALGO_RMD160 |
134 |
#define MD_SHA256 DIGEST_ALGO_SHA256 |
135 |
#define MD_SHA384 DIGEST_ALGO_SHA384 |
136 |
#define MD_SHA512 DIGEST_ALGO_SHA512 |
137 |
|
138 |
#define is_RSA(a) ((a)==PUBKEY_ALGO_RSA || (a)==PUBKEY_ALGO_RSA_E \ |
139 |
|| (a)==PUBKEY_ALGO_RSA_S ) |
140 |
#define is_ELGAMAL(a) ((a)==PUBKEY_ALGO_ELGAMAL || (a)==PUBKEY_ALGO_ELGAMAL_E) |
141 |
|
142 |
|
143 |
#define PUBKEY_MAX_NPKEY 4 |
144 |
#define PUBKEY_MAX_NSKEY 6 |
145 |
#define PUBKEY_MAX_NSIG 2 |
146 |
#define PUBKEY_MAX_NENC 2 |
147 |
|
148 |
#define MAX_FINGERPRINT_LEN 20 |
149 |
|
150 |
#define IOBUFCTRL_INIT 1 |
151 |
#define IOBUFCTRL_FREE 2 |
152 |
#define IOBUFCTRL_UNDERFLOW 3 |
153 |
#define IOBUFCTRL_FLUSH 4 |
154 |
#define IOBUFCTRL_DESC 5 |
155 |
#define IOBUFCTRL_CANCEL 6 |
156 |
#define IOBUFCTRL_USER 16 |
157 |
|
158 |
|
159 |
/*-- md.c --*/ |
160 |
struct gpgm_md_s; |
161 |
typedef struct gpgm_md_s *gpg_md_t; |
162 |
|
163 |
gpg_md_t gpg_md_open( int algo ); |
164 |
void gpg_md_close( gpg_md_t md ); |
165 |
void gpg_md_final( gpg_md_t md ); |
166 |
void gpg_md_write( gpg_md_t md, byte *inbuf, size_t len ); |
167 |
void gpg_md_putc( gpg_md_t md, int c ); |
168 |
const byte * gpg_md_read( gpg_md_t md ); |
169 |
int gpg_md_selftest (void); |
170 |
|
171 |
|
172 |
/*-- iobuf.c --*/ |
173 |
struct gpg_iobuf_s { |
174 |
int use; /* 1 input , 2 output, 3 temp */ |
175 |
_off_t nlimit; |
176 |
_off_t nbytes; /* used together with nlimit */ |
177 |
_off_t ntotal; /* total bytes read (position of stream) */ |
178 |
int nofast; /* used by the iobuf_get() */ |
179 |
void *directfp; |
180 |
struct { |
181 |
size_t size; /* allocated size */ |
182 |
size_t start; /* number of invalid bytes at the begin of the buffer */ |
183 |
size_t len; /* currently filled to this size */ |
184 |
byte *buf; |
185 |
} d; |
186 |
int filter_eof; |
187 |
int error; |
188 |
int (*filter)( void *opaque, int control, |
189 |
struct gpg_iobuf_s *chain, byte *buf, size_t *len); |
190 |
void *filter_ov; /* value for opaque */ |
191 |
int filter_ov_owner; |
192 |
char *real_fname; |
193 |
struct gpg_iobuf_s *chain; /* next iobuf used for i/o if any (passed to filter) */ |
194 |
int no, subno; |
195 |
const char *desc; |
196 |
void *opaque; /* can be used to hold any information */ |
197 |
/* this value is copied to all instances */ |
198 |
struct { |
199 |
size_t size; /* allocated size */ |
200 |
size_t start; /* number of invalid bytes at the begin of the buffer */ |
201 |
size_t len; /* currently filled to this size */ |
202 |
byte *buf; |
203 |
} unget; |
204 |
}; |
205 |
typedef struct gpg_iobuf_s *gpg_iobuf_t; |
206 |
|
207 |
typedef struct { |
208 |
/* these fields may be initialized */ |
209 |
int what; /* what kind of armor headers to write */ |
210 |
int only_keyblocks; /* skip all headers but ".... key block" */ |
211 |
const char *hdrlines; /* write these headerlines */ |
212 |
|
213 |
/* these fileds must be initialized to zero */ |
214 |
int no_openpgp_data; /* output flag: "No valid OpenPGP data found" */ |
215 |
|
216 |
/* the following fields must be initialized to zero */ |
217 |
int inp_checked; /* set if the input has been checked */ |
218 |
int inp_bypass; /* set if the input is not armored */ |
219 |
int in_cleartext; /* clear text message */ |
220 |
int not_dash_escaped; /* clear text is not dash escaped */ |
221 |
int hashes; /* detected hash algorithms */ |
222 |
int faked; /* we are faking a literal data packet */ |
223 |
int truncated; /* number of truncated lines */ |
224 |
int qp_detected; |
225 |
int pgp2mode; |
226 |
|
227 |
byte *buffer; /* malloced buffer */ |
228 |
unsigned buffer_size; /* and size of this buffer */ |
229 |
unsigned buffer_len; /* used length of the buffer */ |
230 |
unsigned buffer_pos; /* read position */ |
231 |
|
232 |
byte radbuf[4]; |
233 |
int idx, idx2; |
234 |
u32 crc; |
235 |
|
236 |
int status; /* an internal state flag */ |
237 |
int cancel; |
238 |
int any_data; /* any valid armored data seen */ |
239 |
int pending_lf; /* used together with faked */ |
240 |
} armor_filter_context_t; |
241 |
|
242 |
struct unarmor_pump_s; |
243 |
typedef struct unarmor_pump_s *UnarmorPump; |
244 |
|
245 |
struct gpg_mpi_s { |
246 |
int alloced; /* array size (# of allocated limbs) */ |
247 |
int nlimbs; /* number of valid limbs */ |
248 |
int nbits; /* the real number of valid bits (info only) */ |
249 |
int sign; /* indicates a negative number */ |
250 |
unsigned flags; /* bit 0: array must be allocated in secure memory space */ |
251 |
/* bit 1: the mpi is encrypted */ |
252 |
/* bit 2: the limb is a pointer to some m_alloced data */ |
253 |
unsigned long *d; /* array with the limbs */ |
254 |
}; |
255 |
typedef struct gpg_mpi_s *gpg_mpi_t; |
256 |
|
257 |
typedef enum { |
258 |
PKT_NONE =0, |
259 |
PKT_PUBKEY_ENC =1, /* public key encrypted packet */ |
260 |
PKT_SIGNATURE =2, /* secret key encrypted packet */ |
261 |
PKT_SYMKEY_ENC =3, /* session key packet (OpenPGP)*/ |
262 |
PKT_ONEPASS_SIG =4, /* one pass sig packet (OpenPGP)*/ |
263 |
PKT_SECRET_KEY =5, /* secret key */ |
264 |
PKT_PUBLIC_KEY =6, /* public key */ |
265 |
PKT_SECRET_SUBKEY =7, /* secret subkey (OpenPGP) */ |
266 |
PKT_COMPRESSED =8, /* compressed data packet */ |
267 |
PKT_ENCRYPTED =9, /* conventional encrypted data */ |
268 |
PKT_MARKER =10, /* marker packet (OpenPGP) */ |
269 |
PKT_PLAINTEXT =11, /* plaintext data with filename and mode */ |
270 |
PKT_RING_TRUST =12, /* keyring trust packet */ |
271 |
PKT_USER_ID =13, /* user id packet */ |
272 |
PKT_PUBLIC_SUBKEY =14, /* public subkey (OpenPGP) */ |
273 |
PKT_OLD_COMMENT =16, /* comment packet from an OpenPGP draft */ |
274 |
PKT_ATTRIBUTE =17, /* PGP's attribute packet */ |
275 |
PKT_ENCRYPTED_MDC =18, /* integrity protected encrypted data */ |
276 |
PKT_MDC =19, /* manipulaion detection code packet */ |
277 |
PKT_COMMENT =61, /* new comment packet (private) */ |
278 |
PKT_GPG_CONTROL =63 /* internal control packet */ |
279 |
} pkttype_t; |
280 |
|
281 |
typedef struct packet_struct PACKET; |
282 |
|
283 |
/* PKT_GPG_CONTROL types */ |
284 |
typedef enum { |
285 |
CTRLPKT_CLEARSIGN_START = 1, |
286 |
CTRLPKT_PIPEMODE = 2, |
287 |
CTRLPKT_PLAINTEXT_MARK =3 |
288 |
} ctrlpkttype_t; |
289 |
|
290 |
typedef enum { |
291 |
PREFTYPE_NONE = 0, |
292 |
PREFTYPE_SYM = 1, |
293 |
PREFTYPE_HASH = 2, |
294 |
PREFTYPE_ZIP = 3 |
295 |
} preftype_t; |
296 |
|
297 |
typedef struct { |
298 |
byte type; |
299 |
byte value; |
300 |
} prefitem_t; |
301 |
|
302 |
typedef struct { |
303 |
int mode; |
304 |
byte hash_algo; |
305 |
byte salt[8]; |
306 |
u32 count; |
307 |
} STRING2KEY; |
308 |
|
309 |
typedef struct { |
310 |
byte version; |
311 |
byte cipher_algo; /* cipher algorithm used */ |
312 |
STRING2KEY s2k; |
313 |
byte seskeylen; /* keylength in byte or 0 for no seskey */ |
314 |
byte seskey[1]; |
315 |
} PKT_symkey_enc; |
316 |
|
317 |
typedef struct { |
318 |
u32 keyid[2]; /* 64 bit keyid */ |
319 |
byte version; |
320 |
byte pubkey_algo; /* algorithm used for public key scheme */ |
321 |
byte throw_keyid; |
322 |
gpg_mpi_t data[PUBKEY_MAX_NENC]; |
323 |
} PKT_pubkey_enc; |
324 |
|
325 |
|
326 |
typedef struct { |
327 |
u32 keyid[2]; /* 64 bit keyid */ |
328 |
byte sig_class; /* sig classification */ |
329 |
byte digest_algo; /* algorithm used for digest */ |
330 |
byte pubkey_algo; /* algorithm used for public key scheme */ |
331 |
byte last; /* a stupid flag */ |
332 |
} PKT_onepass_sig; |
333 |
|
334 |
|
335 |
typedef struct { |
336 |
size_t size; /* allocated */ |
337 |
size_t len; /* used */ |
338 |
byte data[1]; |
339 |
} subpktarea_t; |
340 |
|
341 |
struct revocation_key { |
342 |
byte rclass; |
343 |
byte algid; |
344 |
byte fpr[MAX_FINGERPRINT_LEN]; |
345 |
}; |
346 |
|
347 |
typedef struct { |
348 |
ulong local_id; /* internal use, valid if > 0 */ |
349 |
struct { |
350 |
unsigned checked:1; /* signature has been checked */ |
351 |
unsigned valid:1; /* signature is good (if checked is set) */ |
352 |
unsigned unknown_critical:1; |
353 |
unsigned exportable:1; |
354 |
unsigned revocable:1; |
355 |
unsigned policy_url:1; /* Policy URL is present */ |
356 |
unsigned notation:1; /* At least one notation is present */ |
357 |
unsigned expired:1; |
358 |
} flags; |
359 |
u32 keyid[2]; /* 64 bit keyid */ |
360 |
u32 timestamp; /* signature made */ |
361 |
u32 expiredate; /* expires at this date or 0 if not at all */ |
362 |
byte version; |
363 |
byte sig_class; /* sig classification, append for MD calculation*/ |
364 |
byte pubkey_algo; /* algorithm used for public key scheme */ |
365 |
/* (PUBKEY_ALGO_xxx) */ |
366 |
byte digest_algo; /* algorithm used for digest (DIGEST_ALGO_xxxx) */ |
367 |
struct revocation_key **revkey; |
368 |
int numrevkeys; |
369 |
subpktarea_t *hashed; /* all subpackets with hashed data (v4 only) */ |
370 |
subpktarea_t *unhashed; /* ditto for unhashed data */ |
371 |
byte digest_start[2]; /* first 2 bytes of the digest */ |
372 |
gpg_mpi_t data[PUBKEY_MAX_NSIG]; |
373 |
} PKT_signature; |
374 |
|
375 |
#define ATTRIB_IMAGE 1 |
376 |
|
377 |
/* This is the cooked form of attributes */ |
378 |
struct user_attribute { |
379 |
byte type; |
380 |
const byte *data; |
381 |
u32 len; |
382 |
}; |
383 |
|
384 |
typedef struct { |
385 |
int ref; /* reference counter */ |
386 |
int len; /* length of the name */ |
387 |
struct user_attribute *attribs; |
388 |
int numattribs; |
389 |
byte *attrib_data; /* if this is not NULL, the packet is an attribute */ |
390 |
unsigned long attrib_len; |
391 |
int help_key_usage; |
392 |
u32 help_key_expire; |
393 |
int is_primary; |
394 |
int is_revoked; |
395 |
int is_expired; |
396 |
u32 expiredate; /* expires at this date or 0 if not at all */ |
397 |
prefitem_t *prefs; /* list of preferences (may be NULL)*/ |
398 |
int mdc_feature; |
399 |
u32 created; /* according to the self-signature */ |
400 |
byte selfsigversion; |
401 |
char name[1]; |
402 |
} PKT_user_id; |
403 |
|
404 |
|
405 |
/**************** |
406 |
* Note about the pkey/skey elements: We assume that the secret keys |
407 |
* has the same elemts as the public key at the begin of the array, so |
408 |
* that npkey < nskey and it is possible to compare the secret and |
409 |
* public keys by comparing the first npkey elements of pkey againts skey. |
410 |
*/ |
411 |
typedef struct { |
412 |
u32 timestamp; /* key made */ |
413 |
u32 expiredate; /* expires at this date or 0 if not at all */ |
414 |
u32 max_expiredate; /* must not expire past this date */ |
415 |
byte hdrbytes; /* number of header bytes */ |
416 |
byte version; |
417 |
byte selfsigversion; /* highest version of all of the self-sigs */ |
418 |
byte pubkey_algo; /* algorithm used for public key scheme */ |
419 |
byte pubkey_usage; /* for now only used to pass it to getkey() */ |
420 |
byte req_usage; /* hack to pass a request to getkey() */ |
421 |
byte req_algo; /* Ditto */ |
422 |
u32 has_expired; /* set to the expiration date if expired */ |
423 |
int is_revoked; /* key has been revoked */ |
424 |
int is_valid; /* key (especially subkey) is valid */ |
425 |
int dont_cache; /* do not cache this */ |
426 |
ulong local_id; /* internal use, valid if > 0 */ |
427 |
u32 main_keyid[2]; /* keyid of the primary key */ |
428 |
u32 keyid[2]; /* calculated by keyid_from_pk() */ |
429 |
prefitem_t *prefs; /* list of preferences (may be NULL) */ |
430 |
int mdc_feature; /* mdc feature set */ |
431 |
byte *namehash; /* if != NULL: found by this name */ |
432 |
PKT_user_id *user_id; /* if != NULL: found by that uid */ |
433 |
struct revocation_key *revkey; |
434 |
int numrevkeys; |
435 |
int is_primary; |
436 |
gpg_mpi_t pkey[PUBKEY_MAX_NPKEY]; |
437 |
} PKT_public_key; |
438 |
|
439 |
typedef struct { |
440 |
u32 timestamp; /* key made */ |
441 |
u32 expiredate; /* expires at this date or 0 if not at all */ |
442 |
u32 max_expiredate; /* must not expire past this date */ |
443 |
byte hdrbytes; /* number of header bytes */ |
444 |
byte version; |
445 |
byte pubkey_algo; /* algorithm used for public key scheme */ |
446 |
byte pubkey_usage; |
447 |
byte req_usage; |
448 |
byte req_algo; |
449 |
u32 has_expired; /* set to the expiration date if expired */ |
450 |
int is_revoked; /* key has been revoked */ |
451 |
int is_valid; /* key (especially subkey) is valid */ |
452 |
u32 main_keyid[2]; /* keyid of the primary key */ |
453 |
u32 keyid[2]; |
454 |
byte is_primary; |
455 |
byte is_protected; /* The secret info is protected and must */ |
456 |
/* be decrypted before use, the protected */ |
457 |
/* GPG_MPIs are simply (void*) pointers to memory */ |
458 |
/* and should never be passed to a mpi_xxx() */ |
459 |
struct { |
460 |
byte algo; /* cipher used to protect the secret information*/ |
461 |
byte sha1chk; /* SHA1 is used instead of a 16 bit checksum */ |
462 |
STRING2KEY s2k; |
463 |
byte ivlen; /* used length of the iv */ |
464 |
byte iv[16]; /* initialization vector for CFB mode */ |
465 |
} protect; |
466 |
gpg_mpi_t skey[PUBKEY_MAX_NSKEY]; |
467 |
u16 csum; /* checksum */ |
468 |
} PKT_secret_key; |
469 |
|
470 |
|
471 |
typedef struct { |
472 |
int len; /* length of data */ |
473 |
char data[1]; |
474 |
} PKT_comment; |
475 |
|
476 |
typedef struct { |
477 |
u32 len; /* reserved */ |
478 |
byte new_ctb; |
479 |
byte algorithm; |
480 |
gpg_iobuf_t buf; /* gpg_iobuf_t reference */ |
481 |
} PKT_compressed; |
482 |
|
483 |
typedef struct { |
484 |
u32 len; /* length of encrypted data */ |
485 |
int extralen; /* this is (blocksize+2) */ |
486 |
byte new_ctb; /* uses a new CTB */ |
487 |
byte mdc_method; /* > 0: integrity protected encrypted data packet */ |
488 |
gpg_iobuf_t buf; /* gpg_iobuf_t reference */ |
489 |
} PKT_encrypted; |
490 |
|
491 |
typedef struct { |
492 |
byte hash[20]; |
493 |
} PKT_mdc; |
494 |
|
495 |
typedef struct { |
496 |
unsigned int trustval; |
497 |
unsigned int sigcache; |
498 |
} PKT_ring_trust; |
499 |
|
500 |
typedef struct { |
501 |
u32 len; /* length of encrypted data */ |
502 |
gpg_iobuf_t buf; /* gpg_iobuf_t reference */ |
503 |
byte new_ctb; |
504 |
byte is_partial; /* partial length encoded */ |
505 |
int mode; |
506 |
u32 timestamp; |
507 |
int namelen; |
508 |
char name[1]; |
509 |
} PKT_plaintext; |
510 |
|
511 |
typedef struct { |
512 |
int control; |
513 |
size_t datalen; |
514 |
char data[1]; |
515 |
} PKT_gpg_control; |
516 |
|
517 |
/* combine all packets into a union */ |
518 |
struct packet_struct { |
519 |
pkttype_t pkttype; |
520 |
union { |
521 |
void *generic; |
522 |
PKT_symkey_enc *symkey_enc; /* PKT_SYMKEY_ENC */ |
523 |
PKT_pubkey_enc *pubkey_enc; /* PKT_PUBKEY_ENC */ |
524 |
PKT_onepass_sig *onepass_sig; /* PKT_ONEPASS_SIG */ |
525 |
PKT_signature *signature; /* PKT_SIGNATURE */ |
526 |
PKT_public_key *public_key; /* PKT_PUBLIC_[SUB)KEY */ |
527 |
PKT_secret_key *secret_key; /* PKT_SECRET_[SUB]KEY */ |
528 |
PKT_comment *comment; /* PKT_COMMENT */ |
529 |
PKT_user_id *user_id; /* PKT_USER_ID */ |
530 |
PKT_compressed *compressed; /* PKT_COMPRESSED */ |
531 |
PKT_encrypted *encrypted; /* PKT_ENCRYPTED[_MDC] */ |
532 |
PKT_mdc *mdc; /* PKT_MDC */ |
533 |
PKT_ring_trust *ring_trust; /* PKT_RING_TRUST */ |
534 |
PKT_plaintext *plaintext; /* PKT_PLAINTEXT */ |
535 |
PKT_gpg_control *gpg_control; /* PKT_GPG_CONTROL */ |
536 |
} pkt; |
537 |
}; |
538 |
|
539 |
struct gpg_kbnode_s { |
540 |
struct gpg_kbnode_s *next; |
541 |
PACKET *pkt; |
542 |
int flag; |
543 |
int private_flag; |
544 |
ulong recno; /* used while updating the trustdb */ |
545 |
}; |
546 |
typedef struct gpg_kbnode_s *gpg_kbnode_t; |
547 |
|
548 |
#define gpg_init_packet(a) do { (a)->pkttype = (pkttype_t)0; \ |
549 |
(a)->pkt.generic = NULL; \ |
550 |
} while(0) |
551 |
|
552 |
typedef enum { |
553 |
SIGSUBPKT_TEST_CRITICAL=-3, |
554 |
SIGSUBPKT_LIST_UNHASHED=-2, |
555 |
SIGSUBPKT_LIST_HASHED =-1, |
556 |
SIGSUBPKT_NONE = 0, |
557 |
SIGSUBPKT_SIG_CREATED = 2, /* signature creation time */ |
558 |
SIGSUBPKT_SIG_EXPIRE = 3, /* signature expiration time */ |
559 |
SIGSUBPKT_EXPORTABLE = 4, /* exportable */ |
560 |
SIGSUBPKT_TRUST = 5, /* trust signature */ |
561 |
SIGSUBPKT_REGEXP = 6, /* regular expression */ |
562 |
SIGSUBPKT_REVOCABLE = 7, /* revocable */ |
563 |
SIGSUBPKT_KEY_EXPIRE = 9, /* key expiration time */ |
564 |
SIGSUBPKT_ARR =10, /* additional recipient request */ |
565 |
SIGSUBPKT_PREF_SYM =11, /* preferred symmetric algorithms */ |
566 |
SIGSUBPKT_REV_KEY =12, /* revocation key */ |
567 |
SIGSUBPKT_ISSUER =16, /* issuer key ID */ |
568 |
SIGSUBPKT_NOTATION =20, /* notation data */ |
569 |
SIGSUBPKT_PREF_HASH =21, /* preferred hash algorithms */ |
570 |
SIGSUBPKT_PREF_COMPR =22, /* preferred compression algorithms */ |
571 |
SIGSUBPKT_KS_FLAGS =23, /* key server preferences */ |
572 |
SIGSUBPKT_PREF_KS =24, /* preferred key server */ |
573 |
SIGSUBPKT_PRIMARY_UID =25, /* primary user id */ |
574 |
SIGSUBPKT_POLICY =26, /* policy URL */ |
575 |
SIGSUBPKT_KEY_FLAGS =27, /* key flags */ |
576 |
SIGSUBPKT_SIGNERS_UID =28, /* signer's user id */ |
577 |
SIGSUBPKT_REVOC_REASON =29, /* reason for revocation */ |
578 |
SIGSUBPKT_FEATURES =30, /* feature flags */ |
579 |
SIGSUBPKT_PRIV_VERIFY_CACHE =101, /* cache verification result (obsolete)*/ |
580 |
|
581 |
SIGSUBPKT_FLAG_CRITICAL=128 |
582 |
} sigsubpkttype_t; |
583 |
|
584 |
/*-- iobuf.c --*/ |
585 |
void gpg_iobuf_enable_special_filenames ( int yes ); |
586 |
gpg_iobuf_t gpg_iobuf_alloc(int use, size_t bufsize); |
587 |
gpg_iobuf_t gpg_iobuf_temp(void); |
588 |
gpg_iobuf_t gpg_iobuf_temp_with_content( const char *buffer, size_t length ); |
589 |
gpg_iobuf_t gpg_iobuf_open( const char *fname ); |
590 |
gpg_iobuf_t gpg_iobuf_fdopen( int fd, const char *mode ); |
591 |
gpg_iobuf_t gpg_iobuf_sockopen( int fd, const char *mode ); |
592 |
gpg_iobuf_t gpg_iobuf_create( const char *fname ); |
593 |
gpg_iobuf_t gpg_iobuf_append( const char *fname ); |
594 |
gpg_iobuf_t gpg_iobuf_openrw( const char *fname ); |
595 |
int gpg_iobuf_ioctl ( gpg_iobuf_t a, int cmd, int intval, void *ptrval ); |
596 |
int gpg_iobuf_close( gpg_iobuf_t iobuf ); |
597 |
int gpg_iobuf_cancel( gpg_iobuf_t iobuf ); |
598 |
|
599 |
int gpg_iobuf_push_filter( gpg_iobuf_t a, int (*f)(void *opaque, int control, |
600 |
gpg_iobuf_t chain, byte *buf, size_t *len), void *ov ); |
601 |
int gpg_iobuf_push_filter2( gpg_iobuf_t a, |
602 |
int (*f)(void *opaque, int control, |
603 |
gpg_iobuf_t chain, byte *buf, size_t *len), |
604 |
void *ov, int rel_ov ); |
605 |
int gpg_iobuf_flush(gpg_iobuf_t a); |
606 |
void gpg_iobuf_clear_eof(gpg_iobuf_t a); |
607 |
#define gpg_iobuf_set_error(a) do { (a)->error = 1; } while(0) |
608 |
#define gpg_iobuf_error(a) ((a)->error) |
609 |
|
610 |
void gpg_iobuf_set_limit( gpg_iobuf_t a, _off_t nlimit ); |
611 |
|
612 |
_off_t gpg_iobuf_tell( gpg_iobuf_t a ); |
613 |
int gpg_iobuf_seek( gpg_iobuf_t a, _off_t newpos ); |
614 |
|
615 |
int gpg_iobuf_readbyte(gpg_iobuf_t a); |
616 |
int gpg_iobuf_read(gpg_iobuf_t a, byte *buf, unsigned buflen ); |
617 |
unsigned gpg_iobuf_read_line( gpg_iobuf_t a, byte **addr_of_buffer, |
618 |
unsigned *length_of_buffer, unsigned *max_length ); |
619 |
int gpg_iobuf_peek(gpg_iobuf_t a, byte *buf, unsigned buflen ); |
620 |
int gpg_iobuf_writebyte(gpg_iobuf_t a, unsigned c); |
621 |
int gpg_iobuf_write(gpg_iobuf_t a, byte *buf, unsigned buflen ); |
622 |
int gpg_iobuf_writestr(gpg_iobuf_t a, const char *buf ); |
623 |
|
624 |
void gpg_iobuf_flush_temp( gpg_iobuf_t temp ); |
625 |
int gpg_iobuf_write_temp( gpg_iobuf_t a, gpg_iobuf_t temp ); |
626 |
size_t gpg_iobuf_temp_to_buffer( gpg_iobuf_t a, byte *buffer, size_t buflen ); |
627 |
void gpg_iobuf_unget_and_close_temp( gpg_iobuf_t a, gpg_iobuf_t temp ); |
628 |
|
629 |
off_t gpg_iobuf_get_filelength( gpg_iobuf_t a ); |
630 |
#define IOBUF_FILELENGTH_LIMIT 0xffffffff |
631 |
const char *gpg_iobuf_get_real_fname( gpg_iobuf_t a ); |
632 |
const char *gpg_iobuf_get_fname( gpg_iobuf_t a ); |
633 |
|
634 |
void gpg_iobuf_set_block_mode( gpg_iobuf_t a, size_t n ); |
635 |
void gpg_iobuf_set_partial_block_mode( gpg_iobuf_t a, size_t len ); |
636 |
int gpg_iobuf_in_block_mode( gpg_iobuf_t a ); |
637 |
|
638 |
int gpg_iobuf_translate_file_handle ( int fd, int for_write ); |
639 |
|
640 |
/* get a byte form the iobuf; must check for eof prior to this function |
641 |
* this function returns values in the range 0 .. 255 or -1 to indicate EOF |
642 |
* iobuf_get_noeof() does not return -1 to indicate EOF, but masks the |
643 |
* returned value to be in the range 0 ..255. |
644 |
*/ |
645 |
#define gpg_iobuf_get(a) \ |
646 |
( ((a)->nofast || (a)->d.start >= (a)->d.len )? \ |
647 |
gpg_iobuf_readbyte((a)) : ( (a)->nbytes++, (a)->d.buf[(a)->d.start++] ) ) |
648 |
#define gpg_iobuf_get_noeof(a) (gpg_iobuf_get((a))&0xff) |
649 |
|
650 |
/* write a byte to the iobuf and return true on write error |
651 |
* This macro does only write the low order byte |
652 |
*/ |
653 |
#define gpg_iobuf_put(a,c) gpg_iobuf_writebyte(a,c) |
654 |
|
655 |
#define gpg_iobuf_where(a) "[don't know]" |
656 |
#define gpg_iobuf_id(a) ((a)->no) |
657 |
|
658 |
#define gpg_iobuf_get_temp_buffer(a) ( (a)->d.buf ) |
659 |
#define gpg_iobuf_get_temp_length(a) ( (a)->d.len ) |
660 |
#define gpg_iobuf_is_temp(a) ( (a)->use == 3 ) |
661 |
|
662 |
/*-- parse-packet.c --*/ |
663 |
int gpg_parse_packet( gpg_iobuf_t inp, PACKET *ret_pkt ); |
664 |
int gpg_read_keyblock( gpg_iobuf_t a, PACKET **pending_pkt, |
665 |
gpg_kbnode_t *ret_root ); |
666 |
|
667 |
const byte * gpg_parse_sig_subpkt( const subpktarea_t * buffer, |
668 |
sigsubpkttype_t reqtype, |
669 |
size_t *ret_n ); |
670 |
const byte * gpg_parse_sig_subpkt2( PKT_signature * sig, |
671 |
sigsubpkttype_t reqtype, |
672 |
size_t *ret_n ); |
673 |
void gpg_free_packet( PACKET *pkt ); |
674 |
|
675 |
int pubkey_get_npkey( int algo ); |
676 |
int pubkey_get_nskey( int algo ); |
677 |
int pubkey_get_nsig( int algo ); |
678 |
int pubkey_get_nenc( int algo ); |
679 |
|
680 |
|
681 |
u32 gpg_keyid_from_sk( PKT_secret_key * sk, byte *fprint ); |
682 |
u32 gpg_keyid_from_pk( PKT_public_key * pk, byte *fprint ); |
683 |
|
684 |
/*-- armor.c --*/ |
685 |
int gpg_use_armor_filter( gpg_iobuf_t a ); |
686 |
int gpg_armor_filter( void *opaque, int control, gpg_iobuf_t a, byte *buf, |
687 |
size_t *ret_len); |
688 |
|
689 |
/*-- rand.c --*/ |
690 |
void gpg_randomize( unsigned char *buffer, size_t length, int level ); |
691 |
char gpg_random_char( int level ); |
692 |
int gpg_quick_random_gen( int onoff ); |
693 |
char gpg_random_char( int level ); |
694 |
|
695 |
/*-- kbnode.c --*/ |
696 |
gpg_kbnode_t gpg_new_kbnode( PACKET *pkt ); |
697 |
void gpg_release_kbnode( gpg_kbnode_t n ); |
698 |
void gpg_add_kbnode( gpg_kbnode_t root, gpg_kbnode_t node ); |
699 |
gpg_kbnode_t gpg_walk_kbnode( gpg_kbnode_t root, gpg_kbnode_t *context, int all ); |
700 |
|
701 |
#ifdef __cplusplus |
702 |
} |
703 |
#endif |
704 |
|
705 |
|
706 |
#endif /*GPGLIB_OPENPGP_H*/ |