/src/gnupg/g10/build-packet.c
Line | Count | Source |
1 | | /* build-packet.c - assemble packets and write them |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
3 | | * 2006, 2010, 2011 Free Software Foundation, Inc. |
4 | | * |
5 | | * This file is part of GnuPG. |
6 | | * |
7 | | * GnuPG is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * GnuPG is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include <config.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <ctype.h> |
26 | | |
27 | | #include "gpg.h" |
28 | | #include "../common/util.h" |
29 | | #include "packet.h" |
30 | | #include "../common/status.h" |
31 | | #include "../common/iobuf.h" |
32 | | #include "../common/i18n.h" |
33 | | #include "options.h" |
34 | | #include "../common/host2net.h" |
35 | | |
36 | | static gpg_error_t do_ring_trust (iobuf_t out, PKT_ring_trust *rt); |
37 | | static int do_user_id( IOBUF out, int ctb, PKT_user_id *uid ); |
38 | | static int do_key (iobuf_t out, int ctb, PKT_public_key *pk); |
39 | | static int do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc ); |
40 | | static int do_pubkey_enc( IOBUF out, int ctb, PKT_pubkey_enc *enc ); |
41 | | static int do_pubkey_enc_v6 (iobuf_t out, int ctb, PKT_pubkey_enc *enc); |
42 | | static u32 calc_plaintext( PKT_plaintext *pt ); |
43 | | static int do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt ); |
44 | | static int do_encrypted( IOBUF out, int ctb, PKT_encrypted *ed ); |
45 | | static int do_encrypted_mdc (iobuf_t out, int ctb, PKT_encrypted *ed ); |
46 | | static int do_encrypted_ocb (iobuf_t out, int ctb, PKT_encrypted *ed); |
47 | | static int do_compressed( IOBUF out, int ctb, PKT_compressed *cd ); |
48 | | static int do_signature( IOBUF out, int ctb, PKT_signature *sig ); |
49 | | static int do_onepass_sig( IOBUF out, int ctb, PKT_onepass_sig *ops ); |
50 | | |
51 | | static int calc_header_length( u32 len, int new_ctb ); |
52 | | static int write_16(IOBUF inp, u16 a); |
53 | | static int write_32(IOBUF inp, u32 a); |
54 | | static int write_header( IOBUF out, int ctb, u32 len ); |
55 | | static int write_sign_packet_header( IOBUF out, int ctb, u32 len ); |
56 | | static int write_header2( IOBUF out, int ctb, u32 len, int hdrlen ); |
57 | | static int write_new_header( IOBUF out, int ctb, u32 len, int hdrlen ); |
58 | | |
59 | | /* Returns 1 if CTB is a new format ctb and 0 if CTB is an old format |
60 | | ctb. */ |
61 | | static int |
62 | | ctb_new_format_p (int ctb) |
63 | 0 | { |
64 | | /* Bit 7 must always be set. */ |
65 | 0 | log_assert ((ctb & (1 << 7))); |
66 | | /* Bit 6 indicates whether the packet is a new format packet. */ |
67 | 0 | return (ctb & (1 << 6)); |
68 | 0 | } |
69 | | |
70 | | /* Extract the packet type from a CTB. */ |
71 | | static int |
72 | | ctb_pkttype (int ctb) |
73 | 0 | { |
74 | 0 | if (ctb_new_format_p (ctb)) |
75 | | /* Bits 0 through 5 are the packet type. */ |
76 | 0 | return (ctb & ((1 << 6) - 1)); |
77 | 0 | else |
78 | | /* Bits 2 through 5 are the packet type. */ |
79 | 0 | return (ctb & ((1 << 6) - 1)) >> 2; |
80 | 0 | } |
81 | | |
82 | | |
83 | | /* Build a keyblock image from KEYBLOCK. Returns 0 on success and |
84 | | * only then stores a new iobuf object at R_IOBUF; the returned iobuf |
85 | | * can be access with the iobuf_get_temp_buffer and |
86 | | * iobuf_get_temp_length macros. */ |
87 | | gpg_error_t |
88 | | build_keyblock_image (kbnode_t keyblock, iobuf_t *r_iobuf) |
89 | 0 | { |
90 | 0 | gpg_error_t err; |
91 | 0 | iobuf_t iobuf; |
92 | 0 | kbnode_t kbctx, node; |
93 | |
|
94 | 0 | *r_iobuf = NULL; |
95 | |
|
96 | 0 | iobuf = iobuf_temp (); |
97 | 0 | for (kbctx = NULL; (node = walk_kbnode (keyblock, &kbctx, 0));) |
98 | 0 | { |
99 | | /* Make sure to use only packets valid on a keyblock. */ |
100 | 0 | switch (node->pkt->pkttype) |
101 | 0 | { |
102 | 0 | case PKT_PUBLIC_KEY: |
103 | 0 | case PKT_PUBLIC_SUBKEY: |
104 | 0 | case PKT_SIGNATURE: |
105 | 0 | case PKT_USER_ID: |
106 | 0 | case PKT_ATTRIBUTE: |
107 | 0 | case PKT_RING_TRUST: |
108 | 0 | break; |
109 | 0 | default: |
110 | 0 | continue; |
111 | 0 | } |
112 | | |
113 | 0 | err = build_packet_and_meta (iobuf, node->pkt); |
114 | 0 | if (err) |
115 | 0 | { |
116 | 0 | iobuf_close (iobuf); |
117 | 0 | return err; |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | 0 | *r_iobuf = iobuf; |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | | |
126 | | /* Build a packet and write it to the stream OUT. |
127 | | * Returns: 0 on success or on an error code. */ |
128 | | int |
129 | | build_packet (IOBUF out, PACKET *pkt) |
130 | 0 | { |
131 | 0 | int rc = 0; |
132 | 0 | int new_ctb = 0; |
133 | 0 | int ctb, pkttype; |
134 | |
|
135 | 0 | if (DBG_PACKET) |
136 | 0 | log_debug ("build_packet() type=%d\n", pkt->pkttype); |
137 | 0 | log_assert (pkt->pkt.generic); |
138 | | |
139 | 0 | switch ((pkttype = pkt->pkttype)) |
140 | 0 | { |
141 | 0 | case PKT_PUBLIC_KEY: |
142 | 0 | if (pkt->pkt.public_key->seckey_info) |
143 | 0 | pkttype = PKT_SECRET_KEY; |
144 | 0 | break; |
145 | 0 | case PKT_PUBLIC_SUBKEY: |
146 | 0 | if (pkt->pkt.public_key->seckey_info) |
147 | 0 | pkttype = PKT_SECRET_SUBKEY; |
148 | 0 | break; |
149 | 0 | case PKT_PLAINTEXT: |
150 | 0 | new_ctb = pkt->pkt.plaintext->new_ctb; |
151 | 0 | break; |
152 | 0 | case PKT_ENCRYPTED: |
153 | 0 | case PKT_ENCRYPTED_MDC: |
154 | 0 | case PKT_ENCRYPTED_OCB: |
155 | 0 | new_ctb = pkt->pkt.encrypted->new_ctb; |
156 | 0 | break; |
157 | 0 | case PKT_COMPRESSED: |
158 | 0 | new_ctb = pkt->pkt.compressed->new_ctb; |
159 | 0 | break; |
160 | 0 | case PKT_USER_ID: |
161 | 0 | if (pkt->pkt.user_id->attrib_data) |
162 | 0 | pkttype = PKT_ATTRIBUTE; |
163 | 0 | break; |
164 | 0 | default: |
165 | 0 | break; |
166 | 0 | } |
167 | | |
168 | 0 | if (new_ctb || pkttype > 15) /* new format */ |
169 | 0 | ctb = (0xc0 | (pkttype & 0x3f)); |
170 | 0 | else |
171 | 0 | ctb = (0x80 | ((pkttype & 15)<<2)); |
172 | 0 | switch (pkttype) |
173 | 0 | { |
174 | 0 | case PKT_ATTRIBUTE: |
175 | 0 | case PKT_USER_ID: |
176 | 0 | rc = do_user_id (out, ctb, pkt->pkt.user_id); |
177 | 0 | break; |
178 | 0 | case PKT_OLD_COMMENT: |
179 | 0 | case PKT_COMMENT: |
180 | | /* Ignore these. Theoretically, this will never be called as we |
181 | | * have no way to output comment packets any longer, but just in |
182 | | * case there is some code path that would end up outputting a |
183 | | * comment that was written before comments were dropped (in the |
184 | | * public key?) this is a no-op. */ |
185 | 0 | break; |
186 | 0 | case PKT_PUBLIC_SUBKEY: |
187 | 0 | case PKT_PUBLIC_KEY: |
188 | 0 | case PKT_SECRET_SUBKEY: |
189 | 0 | case PKT_SECRET_KEY: |
190 | 0 | rc = do_key (out, ctb, pkt->pkt.public_key); |
191 | 0 | break; |
192 | 0 | case PKT_SYMKEY_ENC: |
193 | 0 | rc = do_symkey_enc (out, ctb, pkt->pkt.symkey_enc); |
194 | 0 | break; |
195 | 0 | case PKT_PUBKEY_ENC: |
196 | 0 | rc = do_pubkey_enc (out, ctb, pkt->pkt.pubkey_enc); |
197 | 0 | break; |
198 | 0 | case PKT_PLAINTEXT: |
199 | 0 | rc = do_plaintext (out, ctb, pkt->pkt.plaintext); |
200 | 0 | break; |
201 | 0 | case PKT_ENCRYPTED: |
202 | 0 | rc = do_encrypted (out, ctb, pkt->pkt.encrypted); |
203 | 0 | break; |
204 | 0 | case PKT_ENCRYPTED_MDC: |
205 | 0 | rc = do_encrypted_mdc (out, ctb, pkt->pkt.encrypted); |
206 | 0 | break; |
207 | 0 | case PKT_ENCRYPTED_OCB: |
208 | 0 | rc = do_encrypted_ocb (out, ctb, pkt->pkt.encrypted); |
209 | 0 | break; |
210 | 0 | case PKT_COMPRESSED: |
211 | 0 | rc = do_compressed (out, ctb, pkt->pkt.compressed); |
212 | 0 | break; |
213 | 0 | case PKT_SIGNATURE: |
214 | 0 | rc = do_signature (out, ctb, pkt->pkt.signature); |
215 | 0 | break; |
216 | 0 | case PKT_ONEPASS_SIG: |
217 | 0 | rc = do_onepass_sig (out, ctb, pkt->pkt.onepass_sig); |
218 | 0 | break; |
219 | 0 | case PKT_RING_TRUST: |
220 | | /* Ignore it (only written by build_packet_and_meta) */ |
221 | 0 | break; |
222 | 0 | case PKT_MDC: |
223 | | /* We write it directly, so we should never see it here. */ |
224 | 0 | default: |
225 | 0 | log_bug ("invalid packet type in build_packet()\n"); |
226 | 0 | break; |
227 | 0 | } |
228 | | |
229 | 0 | return rc; |
230 | 0 | } |
231 | | |
232 | | |
233 | | /* Build a packet and write it to the stream OUT. This variant also |
234 | | * writes the meta data using ring trust packets. Returns: 0 on |
235 | | * success or on error code. */ |
236 | | gpg_error_t |
237 | | build_packet_and_meta (iobuf_t out, PACKET *pkt) |
238 | 0 | { |
239 | 0 | gpg_error_t err; |
240 | 0 | PKT_ring_trust rt = {0}; |
241 | |
|
242 | 0 | err = build_packet (out, pkt); |
243 | 0 | if (err) |
244 | 0 | ; |
245 | 0 | else if (pkt->pkttype == PKT_SIGNATURE) |
246 | 0 | { |
247 | 0 | PKT_signature *sig = pkt->pkt.signature; |
248 | |
|
249 | 0 | rt.subtype = RING_TRUST_SIG; |
250 | | /* Note: trustval is not yet used. */ |
251 | 0 | if (sig->flags.checked) |
252 | 0 | { |
253 | 0 | rt.sigcache = 1; |
254 | 0 | if (sig->flags.valid) |
255 | 0 | rt.sigcache |= 2; |
256 | 0 | } |
257 | 0 | err = do_ring_trust (out, &rt); |
258 | 0 | } |
259 | 0 | else if (pkt->pkttype == PKT_USER_ID |
260 | 0 | || pkt->pkttype == PKT_ATTRIBUTE) |
261 | 0 | { |
262 | 0 | PKT_user_id *uid = pkt->pkt.user_id; |
263 | |
|
264 | 0 | rt.subtype = RING_TRUST_UID; |
265 | 0 | rt.keyorg = uid->keyorg; |
266 | 0 | rt.keyupdate = uid->keyupdate; |
267 | 0 | rt.url = uid->updateurl; |
268 | 0 | err = do_ring_trust (out, &rt); |
269 | 0 | rt.url = NULL; |
270 | 0 | } |
271 | 0 | else if (pkt->pkttype == PKT_PUBLIC_KEY |
272 | 0 | || pkt->pkttype == PKT_SECRET_KEY) |
273 | 0 | { |
274 | 0 | PKT_public_key *pk = pkt->pkt.public_key; |
275 | |
|
276 | 0 | rt.subtype = RING_TRUST_KEY; |
277 | 0 | rt.keyorg = pk->keyorg; |
278 | 0 | rt.keyupdate = pk->keyupdate; |
279 | 0 | rt.url = pk->updateurl; |
280 | 0 | err = do_ring_trust (out, &rt); |
281 | 0 | rt.url = NULL; |
282 | |
|
283 | 0 | } |
284 | |
|
285 | 0 | return err; |
286 | 0 | } |
287 | | |
288 | | |
289 | | /* |
290 | | * Write the mpi A to OUT. If R_NWRITTEN is not NULL the number of |
291 | | * bytes written is stored there. To only get the number of bytes |
292 | | * which would be written NULL may be passed for OUT. |
293 | | */ |
294 | | gpg_error_t |
295 | | gpg_mpi_write (iobuf_t out, gcry_mpi_t a, unsigned int *r_nwritten) |
296 | 0 | { |
297 | 0 | gpg_error_t err; |
298 | 0 | unsigned int nwritten = 0; |
299 | |
|
300 | 0 | if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
301 | 0 | { |
302 | 0 | unsigned int nbits; |
303 | 0 | const unsigned char *p; |
304 | 0 | unsigned char lenhdr[2]; |
305 | | |
306 | | /* gcry_log_debugmpi ("a", a); */ |
307 | 0 | p = gcry_mpi_get_opaque (a, &nbits); |
308 | 0 | if (p) |
309 | 0 | { |
310 | | /* First get nbits back to full bytes. */ |
311 | 0 | nbits = ((nbits + 7) / 8) * 8; |
312 | | /* Then strip leading zero bits. */ |
313 | 0 | for (; nbits >= 8 && !*p; p++, nbits -= 8) |
314 | 0 | ; |
315 | 0 | if (nbits >= 8 && !(*p & 0x80)) |
316 | 0 | if (--nbits >= 7 && !(*p & 0x40)) |
317 | 0 | if (--nbits >= 6 && !(*p & 0x20)) |
318 | 0 | if (--nbits >= 5 && !(*p & 0x10)) |
319 | 0 | if (--nbits >= 4 && !(*p & 0x08)) |
320 | 0 | if (--nbits >= 3 && !(*p & 0x04)) |
321 | 0 | if (--nbits >= 2 && !(*p & 0x02)) |
322 | 0 | if (--nbits >= 1 && !(*p & 0x01)) |
323 | 0 | --nbits; |
324 | 0 | } |
325 | | /* gcry_log_debug (" [%u bit]\n", nbits); */ |
326 | | /* gcry_log_debughex (" ", p, (nbits+7)/8); */ |
327 | 0 | lenhdr[0] = nbits >> 8; |
328 | 0 | lenhdr[1] = nbits; |
329 | 0 | err = out? iobuf_write (out, lenhdr, 2) : 0; |
330 | 0 | if (!err) |
331 | 0 | { |
332 | 0 | nwritten += 2; |
333 | 0 | if (p) |
334 | 0 | { |
335 | 0 | err = out? iobuf_write (out, p, (nbits+7)/8) : 0; |
336 | 0 | if (!err) |
337 | 0 | nwritten += (nbits+7)/8; |
338 | 0 | } |
339 | 0 | } |
340 | 0 | } |
341 | 0 | else |
342 | 0 | { |
343 | 0 | char buffer[(MAX_EXTERN_MPI_BITS+7)/8+2]; /* 2 is for the mpi length. */ |
344 | 0 | size_t nbytes; |
345 | |
|
346 | 0 | nbytes = DIM(buffer); |
347 | 0 | err = gcry_mpi_print (GCRYMPI_FMT_PGP, buffer, nbytes, &nbytes, a ); |
348 | 0 | if (!err) |
349 | 0 | { |
350 | 0 | err = out? iobuf_write (out, buffer, nbytes) : 0; |
351 | 0 | if (!err) |
352 | 0 | nwritten += nbytes; |
353 | 0 | } |
354 | 0 | else if (gpg_err_code (err) == GPG_ERR_TOO_SHORT ) |
355 | 0 | { |
356 | 0 | log_info ("mpi too large (%u bits)\n", gcry_mpi_get_nbits (a)); |
357 | | /* The buffer was too small. We better tell the user about |
358 | | * the MPI. */ |
359 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
360 | 0 | } |
361 | 0 | } |
362 | |
|
363 | 0 | if (r_nwritten) |
364 | 0 | *r_nwritten = nwritten; |
365 | 0 | return err; |
366 | 0 | } |
367 | | |
368 | | |
369 | | /* |
370 | | * Write the mpi A to the output stream OUT as "SOS" (Strange Octet |
371 | | * String). If R_NWRITTEN is not NULL the number of bytes written is |
372 | | * stored there. To only get the number of bytes which would be |
373 | | * written, NULL may be passed for OUT. |
374 | | */ |
375 | | static gpg_error_t |
376 | | sos_write (iobuf_t out, gcry_mpi_t a, unsigned int *r_nwritten) |
377 | 0 | { |
378 | 0 | gpg_error_t err; |
379 | 0 | unsigned int nwritten = 0; |
380 | |
|
381 | 0 | if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
382 | 0 | { |
383 | 0 | unsigned int nbits; |
384 | 0 | const unsigned char *p; |
385 | 0 | unsigned char lenhdr[2]; |
386 | | |
387 | | /* gcry_log_debugmpi ("a", a); */ |
388 | 0 | p = gcry_mpi_get_opaque (a, &nbits); |
389 | | /* gcry_log_debug (" [%u bit]\n", nbits); */ |
390 | | /* gcry_log_debughex (" ", p, (nbits+7)/8); */ |
391 | |
|
392 | 0 | if (p && *p) |
393 | 0 | { |
394 | 0 | nbits = ((nbits + 7) / 8) * 8; |
395 | |
|
396 | 0 | if (nbits >= 8 && !(*p & 0x80)) |
397 | 0 | if (--nbits >= 7 && !(*p & 0x40)) |
398 | 0 | if (--nbits >= 6 && !(*p & 0x20)) |
399 | 0 | if (--nbits >= 5 && !(*p & 0x10)) |
400 | 0 | if (--nbits >= 4 && !(*p & 0x08)) |
401 | 0 | if (--nbits >= 3 && !(*p & 0x04)) |
402 | 0 | if (--nbits >= 2 && !(*p & 0x02)) |
403 | 0 | if (--nbits >= 1 && !(*p & 0x01)) |
404 | 0 | --nbits; |
405 | 0 | } |
406 | |
|
407 | 0 | lenhdr[0] = nbits >> 8; |
408 | 0 | lenhdr[1] = nbits; |
409 | 0 | err = out? iobuf_write (out, lenhdr, 2) : 0; |
410 | 0 | if (!err) |
411 | 0 | { |
412 | 0 | nwritten += 2; |
413 | 0 | if (p) |
414 | 0 | { |
415 | 0 | err = out? iobuf_write (out, p, (nbits+7)/8) : 0; |
416 | 0 | if (!err) |
417 | 0 | nwritten += (nbits+7)/8; |
418 | 0 | } |
419 | 0 | } |
420 | 0 | } |
421 | 0 | else |
422 | 0 | { |
423 | 0 | log_info ("non-opaque MPI (%u bits) for SOS\n", gcry_mpi_get_nbits (a)); |
424 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
425 | 0 | } |
426 | |
|
427 | 0 | if (r_nwritten) |
428 | 0 | *r_nwritten = nwritten; |
429 | 0 | return err; |
430 | 0 | } |
431 | | |
432 | | |
433 | | /* |
434 | | * Write an opaque string to the output stream without length info. |
435 | | */ |
436 | | gpg_error_t |
437 | | gpg_mpi_write_opaque_nohdr (iobuf_t out, gcry_mpi_t a) |
438 | 0 | { |
439 | 0 | int rc; |
440 | |
|
441 | 0 | if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
442 | 0 | { |
443 | 0 | unsigned int nbits; |
444 | 0 | const void *p; |
445 | |
|
446 | 0 | p = gcry_mpi_get_opaque (a, &nbits); |
447 | 0 | rc = p ? iobuf_write (out, p, (nbits+7)/8) : 0; |
448 | 0 | } |
449 | 0 | else |
450 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
451 | |
|
452 | 0 | return rc; |
453 | 0 | } |
454 | | |
455 | | |
456 | | /* Write an opaque MPI string with a one-byte octet count to the |
457 | | * output stream. If R_NWRITTEN is not NULL the number of written |
458 | | * bytes is stored there. OUT may be NULL in which case only |
459 | | * R_NWRITTEN is updated and error checking is done. */ |
460 | | gpg_error_t |
461 | | gpg_mpi_write_opaque_8 (iobuf_t out, gcry_mpi_t a, unsigned int *r_nwritten) |
462 | 0 | { |
463 | 0 | gpg_error_t err; |
464 | |
|
465 | 0 | if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
466 | 0 | { |
467 | 0 | unsigned int nbits, nbytes; |
468 | 0 | const void *p; |
469 | |
|
470 | 0 | p = gcry_mpi_get_opaque (a, &nbits); |
471 | 0 | nbytes = (nbits + 7)/8; |
472 | 0 | if (nbytes > 255) |
473 | 0 | { |
474 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
475 | 0 | if (r_nwritten) |
476 | 0 | *r_nwritten = 0; |
477 | 0 | } |
478 | 0 | else |
479 | 0 | { |
480 | 0 | if (out) |
481 | 0 | { |
482 | 0 | iobuf_put (out, nbytes); |
483 | 0 | err = p ? iobuf_write (out, p, nbytes) : 0; |
484 | 0 | } |
485 | 0 | else |
486 | 0 | err = 0; |
487 | 0 | if (r_nwritten) |
488 | 0 | *r_nwritten = 4 + (p? nbytes : 0); |
489 | 0 | } |
490 | 0 | } |
491 | 0 | else |
492 | 0 | { |
493 | 0 | err = gpg_error (GPG_ERR_BAD_MPI); |
494 | 0 | if (r_nwritten) |
495 | 0 | *r_nwritten = 0; |
496 | 0 | } |
497 | |
|
498 | 0 | return err; |
499 | 0 | } |
500 | | |
501 | | |
502 | | /* |
503 | | * Write an opaque MPI string with a four-byte octet count to the |
504 | | * output stream. If R_NWRITTEN is not NULL the number of written |
505 | | * bytes is stored there. OUT may be NULL in which case only |
506 | | * R_NWRITTEN is updated and error checking is done. |
507 | | */ |
508 | | gpg_error_t |
509 | | gpg_mpi_write_opaque_32 (iobuf_t out, gcry_mpi_t a, unsigned int *r_nwritten) |
510 | 0 | { |
511 | 0 | gpg_error_t err; |
512 | |
|
513 | 0 | if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
514 | 0 | { |
515 | 0 | unsigned int nbits, nbytes; |
516 | 0 | const void *p; |
517 | |
|
518 | 0 | p = gcry_mpi_get_opaque (a, &nbits); |
519 | 0 | nbytes = (nbits + 7)/8; |
520 | 0 | if (out) |
521 | 0 | { |
522 | 0 | write_32 (out, nbytes); |
523 | 0 | err = p ? iobuf_write (out, p, nbytes) : 0; |
524 | 0 | } |
525 | 0 | else |
526 | 0 | err = 0; |
527 | 0 | if (r_nwritten) |
528 | 0 | *r_nwritten = 4 + (p? nbytes : 0); |
529 | 0 | } |
530 | 0 | else |
531 | 0 | { |
532 | 0 | err = gpg_error (GPG_ERR_BAD_MPI); |
533 | 0 | if (r_nwritten) |
534 | 0 | *r_nwritten = 0; |
535 | 0 | } |
536 | |
|
537 | 0 | return err; |
538 | 0 | } |
539 | | |
540 | | |
541 | | /* Calculate the length of a packet described by PKT. */ |
542 | | u32 |
543 | | calc_packet_length( PACKET *pkt ) |
544 | 0 | { |
545 | 0 | u32 n = 0; |
546 | 0 | int new_ctb = 0; |
547 | |
|
548 | 0 | log_assert (pkt->pkt.generic); |
549 | 0 | switch (pkt->pkttype) |
550 | 0 | { |
551 | 0 | case PKT_PLAINTEXT: |
552 | 0 | n = calc_plaintext (pkt->pkt.plaintext); |
553 | 0 | new_ctb = pkt->pkt.plaintext->new_ctb; |
554 | 0 | break; |
555 | 0 | case PKT_ATTRIBUTE: |
556 | 0 | case PKT_USER_ID: |
557 | 0 | case PKT_COMMENT: |
558 | 0 | case PKT_PUBLIC_KEY: |
559 | 0 | case PKT_SECRET_KEY: |
560 | 0 | case PKT_SYMKEY_ENC: |
561 | 0 | case PKT_PUBKEY_ENC: |
562 | 0 | case PKT_ENCRYPTED: |
563 | 0 | case PKT_SIGNATURE: |
564 | 0 | case PKT_ONEPASS_SIG: |
565 | 0 | case PKT_RING_TRUST: |
566 | 0 | case PKT_COMPRESSED: |
567 | 0 | default: |
568 | 0 | log_bug ("invalid packet type in calc_packet_length()"); |
569 | 0 | break; |
570 | 0 | } |
571 | | |
572 | 0 | n += calc_header_length (n, new_ctb); |
573 | 0 | return n; |
574 | 0 | } |
575 | | |
576 | | |
577 | | static gpg_error_t |
578 | | write_fake_data (IOBUF out, gcry_mpi_t a) |
579 | 0 | { |
580 | 0 | unsigned int n; |
581 | 0 | void *p; |
582 | |
|
583 | 0 | if (!a) |
584 | 0 | return 0; |
585 | 0 | if (!gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
586 | 0 | return 0; /* e.g. due to generating a key with wrong usage. */ |
587 | 0 | p = gcry_mpi_get_opaque ( a, &n); |
588 | 0 | if (!p) |
589 | 0 | return 0; /* For example due to a read error in |
590 | | parse-packet.c:read_rest. */ |
591 | 0 | return iobuf_write (out, p, (n+7)/8 ); |
592 | 0 | } |
593 | | |
594 | | |
595 | | /* Write a ring trust meta packet. */ |
596 | | static gpg_error_t |
597 | | do_ring_trust (iobuf_t out, PKT_ring_trust *rt) |
598 | 0 | { |
599 | 0 | unsigned int namelen = 0; |
600 | 0 | unsigned int pktlen = 6; |
601 | |
|
602 | 0 | if (rt->subtype == RING_TRUST_KEY || rt->subtype == RING_TRUST_UID) |
603 | 0 | { |
604 | 0 | if (rt->url) |
605 | 0 | namelen = strlen (rt->url); |
606 | 0 | pktlen += 1 + 4 + 1 + namelen; |
607 | 0 | } |
608 | |
|
609 | 0 | write_header (out, (0x80 | ((PKT_RING_TRUST & 15)<<2)), pktlen); |
610 | 0 | iobuf_put (out, rt->trustval); |
611 | 0 | iobuf_put (out, rt->sigcache); |
612 | 0 | iobuf_write (out, "gpg", 3); |
613 | 0 | iobuf_put (out, rt->subtype); |
614 | 0 | if (rt->subtype == RING_TRUST_KEY || rt->subtype == RING_TRUST_UID) |
615 | 0 | { |
616 | 0 | iobuf_put (out, rt->keyorg); |
617 | 0 | write_32 (out, rt->keyupdate); |
618 | 0 | iobuf_put (out, namelen); |
619 | 0 | if (namelen) |
620 | 0 | iobuf_write (out, rt->url, namelen); |
621 | 0 | } |
622 | |
|
623 | 0 | return 0; |
624 | 0 | } |
625 | | |
626 | | |
627 | | /* Serialize the user id (RFC 4880, Section 5.11) or the user |
628 | | * attribute UID (Section 5.12) and write it to OUT. |
629 | | * |
630 | | * CTB is the serialization's CTB. It specifies the header format and |
631 | | * the packet's type. The header length must not be set. */ |
632 | | static int |
633 | | do_user_id( IOBUF out, int ctb, PKT_user_id *uid ) |
634 | 0 | { |
635 | 0 | int rc; |
636 | 0 | int hdrlen; |
637 | |
|
638 | 0 | log_assert (ctb_pkttype (ctb) == PKT_USER_ID |
639 | 0 | || ctb_pkttype (ctb) == PKT_ATTRIBUTE); |
640 | | |
641 | | /* We need to take special care of a user ID with a length of 0: |
642 | | * Without forcing HDRLEN to 2 in this case an indeterminate length |
643 | | * packet would be written which is not allowed. Note that we are |
644 | | * always called with a CTB indicating an old packet header format, |
645 | | * so that forcing a 2 octet header works. We also check for the |
646 | | * maximum allowed packet size by the parser using an arbitrary |
647 | | * extra 10 bytes for header data. */ |
648 | 0 | if (uid->attrib_data) |
649 | 0 | { |
650 | 0 | if (uid->attrib_len > MAX_ATTR_PACKET_LENGTH - 10) |
651 | 0 | return gpg_error (GPG_ERR_TOO_LARGE); |
652 | 0 | hdrlen = uid->attrib_len? 0 : 2; |
653 | 0 | write_header2 (out, ctb, uid->attrib_len, hdrlen); |
654 | 0 | rc = iobuf_write( out, uid->attrib_data, uid->attrib_len ); |
655 | 0 | } |
656 | 0 | else |
657 | 0 | { |
658 | 0 | if (uid->len > MAX_UID_PACKET_LENGTH - 10) |
659 | 0 | return gpg_error (GPG_ERR_TOO_LARGE); |
660 | 0 | hdrlen = uid->len? 0 : 2; |
661 | 0 | write_header2 (out, ctb, uid->len, hdrlen); |
662 | 0 | rc = iobuf_write( out, uid->name, uid->len ); |
663 | 0 | } |
664 | 0 | return rc; |
665 | 0 | } |
666 | | |
667 | | |
668 | | /* Serialize the key (RFC 4880, Section 5.5) described by PK and write |
669 | | * it to OUT. |
670 | | * |
671 | | * This function serializes both primary keys and subkeys with or |
672 | | * without a secret part. |
673 | | * |
674 | | * CTB is the serialization's CTB. It specifies the header format and |
675 | | * the packet's type. The header length must not be set. |
676 | | * |
677 | | * PK->VERSION specifies the serialization format. A value of 0 means |
678 | | * to use the default version. Currently, only version 4 packets are |
679 | | * supported. |
680 | | */ |
681 | | static int |
682 | | do_key (iobuf_t out, int ctb, PKT_public_key *pk) |
683 | 0 | { |
684 | 0 | gpg_error_t err = 0; |
685 | 0 | iobuf_t a; |
686 | 0 | int i, nskey, npkey; |
687 | 0 | u32 pkbytes = 0; |
688 | 0 | int is_v5; |
689 | |
|
690 | 0 | log_assert (pk->version == 0 || pk->version == 4 || pk->version == 5 |
691 | 0 | || pk->version == 6); |
692 | 0 | log_assert (ctb_pkttype (ctb) == PKT_PUBLIC_KEY |
693 | 0 | || ctb_pkttype (ctb) == PKT_PUBLIC_SUBKEY |
694 | 0 | || ctb_pkttype (ctb) == PKT_SECRET_KEY |
695 | 0 | || ctb_pkttype (ctb) == PKT_SECRET_SUBKEY); |
696 | | |
697 | | /* The length of the body is stored in the packet's header, which |
698 | | * occurs before the body. Unfortunately, we don't know the length |
699 | | * of the packet's body until we've written all of the data! To |
700 | | * work around this, we first write the data into this temporary |
701 | | * buffer, then generate the header, and finally copy the content |
702 | | * of this buffer to OUT. */ |
703 | 0 | a = iobuf_temp(); |
704 | | |
705 | | /* Note that the Version number, Timestamp, Algo, and the v5 Key |
706 | | * material count are written at the end of the function. */ |
707 | |
|
708 | 0 | is_v5 = (pk->version >= 5); |
709 | | |
710 | | /* Get number of secret and public parameters. They are held in one |
711 | | array: the public ones followed by the secret ones. */ |
712 | 0 | nskey = pubkey_get_nskey (pk->pubkey_algo); |
713 | 0 | npkey = pubkey_get_npkey (pk->pubkey_algo); |
714 | | |
715 | | /* If we don't have any public parameters - which is for example the |
716 | | case if we don't know the algorithm used - the parameters are |
717 | | stored as one blob in a faked (opaque) MPI. */ |
718 | 0 | if (!npkey) |
719 | 0 | { |
720 | 0 | write_fake_data (a, pk->pkey[0]); |
721 | 0 | goto leave; |
722 | 0 | } |
723 | 0 | log_assert (npkey < nskey); |
724 | | |
725 | 0 | for (i=0; i < npkey; i++ ) |
726 | 0 | { |
727 | 0 | if ( (pk->pubkey_algo == PUBKEY_ALGO_ECDSA && (i == 0)) |
728 | 0 | || (pk->pubkey_algo == PUBKEY_ALGO_EDDSA && (i == 0)) |
729 | 0 | || (pk->pubkey_algo == PUBKEY_ALGO_ECDH && (i == 0 || i == 2)) |
730 | 0 | || (pk->pubkey_algo == PUBKEY_ALGO_KYBER && (i == 0))) |
731 | 0 | err = gpg_mpi_write_opaque_nohdr (a, pk->pkey[i]); |
732 | 0 | else if (pk->pubkey_algo == PUBKEY_ALGO_KYBER && i == 2) |
733 | 0 | { |
734 | | /* Write a four-octet count prefixed Kyber public key. */ |
735 | 0 | err = gpg_mpi_write_opaque_32 (a, pk->pkey[2], NULL); |
736 | 0 | } |
737 | 0 | else if (IS_PUBKEY_ALGO_MLK (pk->pubkey_algo) |
738 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ED25519 |
739 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_X25519) |
740 | 0 | { |
741 | 0 | err = gpg_mpi_write_opaque_nohdr (a, pk->pkey[i]); |
742 | 0 | } |
743 | 0 | else if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA |
744 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA |
745 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
746 | 0 | err = sos_write (a, pk->pkey[i], NULL); |
747 | 0 | else |
748 | 0 | err = gpg_mpi_write (a, pk->pkey[i], NULL); |
749 | 0 | if (err) |
750 | 0 | goto leave; |
751 | 0 | } |
752 | | |
753 | | /* Record the length of the public key part. */ |
754 | 0 | pkbytes = iobuf_get_temp_length (a); |
755 | |
|
756 | 0 | if (pk->seckey_info) |
757 | 0 | { |
758 | | /* This is a secret key packet. */ |
759 | 0 | struct seckey_info *ski = pk->seckey_info; |
760 | | |
761 | | /* Build the header for protected (encrypted) secret parameters. */ |
762 | 0 | if (ski->is_protected) |
763 | 0 | { |
764 | 0 | iobuf_put (a, ski->sha1chk? 0xfe : 0xff); /* S2k usage. */ |
765 | 0 | if (is_v5) |
766 | 0 | { |
767 | | /* For a v5 key determine the count of the following |
768 | | * key-protection material and write it. */ |
769 | 0 | int count = 1; /* Pubkey algo octet. */ |
770 | 0 | if (ski->s2k.mode >= 1000) |
771 | 0 | count += 6; /* GNU specific mode descriptor. */ |
772 | 0 | else |
773 | 0 | count += 2; /* Mode and hash algo. */ |
774 | 0 | if (ski->s2k.mode == 1 || ski->s2k.mode == 3) |
775 | 0 | count += 8; /* Salt. */ |
776 | 0 | if (ski->s2k.mode == 3) |
777 | 0 | count++; /* S2K.COUNT */ |
778 | 0 | if (ski->s2k.mode != 1001 && ski->s2k.mode != 1002 |
779 | 0 | && ski->s2k.mode != 1003) |
780 | 0 | count += ski->ivlen; |
781 | |
|
782 | 0 | iobuf_put (a, count); |
783 | 0 | } |
784 | 0 | iobuf_put (a, ski->algo); /* Pubkey algo octet. */ |
785 | 0 | if (ski->s2k.mode >= 1000) |
786 | 0 | { |
787 | | /* These modes are not possible in OpenPGP, we use them |
788 | | to implement our extensions, 101 can be viewed as a |
789 | | private/experimental extension (this is not specified |
790 | | in rfc2440 but the same scheme is used for all other |
791 | | algorithm identifiers). */ |
792 | 0 | iobuf_put (a, 101); |
793 | 0 | iobuf_put (a, ski->s2k.hash_algo); |
794 | 0 | iobuf_write (a, "GNU", 3 ); |
795 | 0 | iobuf_put (a, ski->s2k.mode - 1000); |
796 | 0 | } |
797 | 0 | else |
798 | 0 | { |
799 | 0 | iobuf_put (a, ski->s2k.mode); |
800 | 0 | iobuf_put (a, ski->s2k.hash_algo); |
801 | 0 | } |
802 | |
|
803 | 0 | if (ski->s2k.mode == 1 || ski->s2k.mode == 3) |
804 | 0 | iobuf_write (a, ski->s2k.salt, 8); |
805 | |
|
806 | 0 | if (ski->s2k.mode == 3) |
807 | 0 | iobuf_put (a, ski->s2k.count); |
808 | | |
809 | | /* For our special modes 1001..1003 we do not need an IV. */ |
810 | 0 | if (ski->s2k.mode != 1001 && ski->s2k.mode != 1002 |
811 | 0 | && ski->s2k.mode != 1003) |
812 | 0 | iobuf_write (a, ski->iv, ski->ivlen); |
813 | |
|
814 | 0 | } |
815 | 0 | else /* Not protected. */ |
816 | 0 | { |
817 | 0 | iobuf_put (a, 0 ); /* S2K usage = not protected. */ |
818 | 0 | if (is_v5) |
819 | 0 | iobuf_put (a, 0); /* Zero octets of key-protection |
820 | | * material follows. */ |
821 | 0 | } |
822 | |
|
823 | 0 | if (ski->s2k.mode == 1001) |
824 | 0 | { |
825 | | /* GnuPG extension - don't write a secret key at all. */ |
826 | 0 | if (is_v5) |
827 | 0 | write_32 (a, 0); /* Zero octets of key material. */ |
828 | 0 | } |
829 | 0 | else if (ski->s2k.mode == 1002) |
830 | 0 | { |
831 | | /* GnuPG extension - divert to OpenPGP smartcard. */ |
832 | 0 | if (is_v5) |
833 | 0 | write_32 (a, 1 + ski->ivlen); |
834 | | /* Length of the serial number or 0 for no serial number. */ |
835 | 0 | iobuf_put (a, ski->ivlen ); |
836 | | /* The serial number gets stored in the IV field. */ |
837 | 0 | iobuf_write (a, ski->iv, ski->ivlen); |
838 | 0 | } |
839 | 0 | else if (ski->s2k.mode == 1003) |
840 | 0 | { |
841 | | /* GnuPG extension - Store raw s-expression. */ |
842 | 0 | byte *p; |
843 | 0 | unsigned int ndatabits; |
844 | |
|
845 | 0 | log_assert (gcry_mpi_get_flag (pk->pkey[npkey], GCRYMPI_FLAG_OPAQUE)); |
846 | | |
847 | 0 | p = gcry_mpi_get_opaque (pk->pkey[npkey], &ndatabits); |
848 | | /* For v5 keys we first write the number of octets of the |
849 | | * following key material. */ |
850 | 0 | if (is_v5) |
851 | 0 | write_32 (a, p? (ndatabits+7)/8 : 0); |
852 | 0 | if (p) |
853 | 0 | iobuf_write (a, p, (ndatabits+7)/8 ); |
854 | 0 | } |
855 | 0 | else if (ski->is_protected) |
856 | 0 | { |
857 | | /* The secret key is protected - write it out as it is. */ |
858 | 0 | byte *p; |
859 | 0 | unsigned int ndatabits; |
860 | |
|
861 | 0 | log_assert (gcry_mpi_get_flag (pk->pkey[npkey], GCRYMPI_FLAG_OPAQUE)); |
862 | 0 | p = gcry_mpi_get_opaque (pk->pkey[npkey], &ndatabits); |
863 | | /* For v5 keys we first write the number of octets of the |
864 | | * following encrypted key material. */ |
865 | 0 | if (is_v5) |
866 | 0 | write_32 (a, p? (ndatabits+7)/8 : 0); |
867 | 0 | if (p) |
868 | 0 | iobuf_write (a, p, (ndatabits+7)/8 ); |
869 | 0 | } |
870 | 0 | else |
871 | 0 | { |
872 | | /* Non-protected key. */ |
873 | 0 | if (is_v5) |
874 | 0 | { |
875 | 0 | unsigned int skbytes = 0; |
876 | 0 | unsigned int n; |
877 | 0 | int j; |
878 | |
|
879 | 0 | for (j=i; j < nskey; j++ ) |
880 | 0 | { |
881 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_KYBER && j == 4) |
882 | 0 | { |
883 | 0 | if ((err=gpg_mpi_write_opaque_32 (NULL,pk->pkey[j], &n))) |
884 | 0 | goto leave; |
885 | 0 | } |
886 | 0 | else if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA |
887 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA |
888 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDH |
889 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_KYBER) |
890 | 0 | { |
891 | 0 | if ((err = sos_write (NULL, pk->pkey[j], &n))) |
892 | 0 | goto leave; |
893 | 0 | } |
894 | 0 | else |
895 | 0 | { |
896 | 0 | if ( (err = gpg_mpi_write (a, pk->pkey[i], NULL))) |
897 | 0 | goto leave; |
898 | 0 | } |
899 | 0 | skbytes += n; |
900 | 0 | } |
901 | | |
902 | 0 | write_32 (a, skbytes); |
903 | 0 | } |
904 | | |
905 | 0 | for ( ; i < nskey; i++ ) |
906 | 0 | { |
907 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_KYBER && i == 4) |
908 | 0 | { |
909 | 0 | err = gpg_mpi_write_opaque_32 (a, pk->pkey[i], NULL); |
910 | 0 | if (err) |
911 | 0 | goto leave; |
912 | 0 | } |
913 | 0 | else if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA |
914 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA |
915 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
916 | 0 | { |
917 | 0 | if ((err = sos_write (a, pk->pkey[i], NULL))) |
918 | 0 | goto leave; |
919 | 0 | } |
920 | 0 | else |
921 | 0 | { |
922 | 0 | if ((err = gpg_mpi_write (a, pk->pkey[i], NULL))) |
923 | 0 | goto leave; |
924 | 0 | } |
925 | 0 | } |
926 | | |
927 | 0 | write_16 (a, ski->csum ); |
928 | 0 | } |
929 | 0 | } |
930 | | |
931 | 0 | leave: |
932 | 0 | if (!err) |
933 | 0 | { |
934 | | /* Build the header of the packet - which we must do after |
935 | | * writing all the other stuff, so that we know the length of |
936 | | * the packet */ |
937 | 0 | u32 len = iobuf_get_temp_length (a); |
938 | 0 | len += 1; /* version number */ |
939 | 0 | len += 4; /* timestamp */ |
940 | 0 | len += 1; /* algo */ |
941 | 0 | if (is_v5) |
942 | 0 | len += 4; /* public key material count */ |
943 | |
|
944 | 0 | if (pk->version == 6) |
945 | 0 | write_new_header (out, (0xc0|(ctb_pkttype(ctb)&0x3f)), len, 0); |
946 | 0 | else |
947 | 0 | write_header2 (out, ctb, len, 0); |
948 | | /* And finally write it out to the real stream. */ |
949 | 0 | iobuf_put (out, pk->version? pk->version : 4); /* version number */ |
950 | 0 | write_32 (out, pk->timestamp ); |
951 | 0 | iobuf_put (out, pk->pubkey_algo); /* algo */ |
952 | 0 | if (is_v5) |
953 | 0 | write_32 (out, pkbytes); /* public key material count */ |
954 | 0 | err = iobuf_write_temp (out, a); /* pub and sec key material */ |
955 | 0 | } |
956 | |
|
957 | 0 | iobuf_close (a); /* Close the temporary buffer */ |
958 | 0 | return err; |
959 | 0 | } |
960 | | |
961 | | |
962 | | /* Serialize the symmetric-key encrypted session key packet (RFC 4880, |
963 | | * 5.3) described by ENC and write it to OUT. |
964 | | * |
965 | | * CTB is the serialization's CTB. It specifies the header format and |
966 | | * the packet's type. The header length must not be set. */ |
967 | | static int |
968 | | do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc ) |
969 | 0 | { |
970 | 0 | int rc = 0; |
971 | 0 | IOBUF a = iobuf_temp(); |
972 | |
|
973 | 0 | log_assert (ctb_pkttype (ctb) == PKT_SYMKEY_ENC); |
974 | 0 | log_assert (enc->version == 4 || enc->version == 5); |
975 | | |
976 | 0 | switch (enc->s2k.mode) |
977 | 0 | { |
978 | 0 | case 0: /* Simple S2K. */ |
979 | 0 | case 1: /* Salted S2K. */ |
980 | 0 | case 3: /* Iterated and salted S2K. */ |
981 | 0 | break; /* Reasonable values. */ |
982 | | |
983 | 0 | default: |
984 | 0 | log_bug ("do_symkey_enc: s2k=%d\n", enc->s2k.mode); |
985 | 0 | } |
986 | 0 | iobuf_put (a, enc->version); |
987 | 0 | iobuf_put (a, enc->cipher_algo); |
988 | 0 | if (enc->version == 5) |
989 | 0 | iobuf_put (a, enc->aead_algo); |
990 | 0 | iobuf_put (a, enc->s2k.mode); |
991 | 0 | iobuf_put (a, enc->s2k.hash_algo); |
992 | 0 | if (enc->s2k.mode == 1 || enc->s2k.mode == 3) |
993 | 0 | { |
994 | 0 | iobuf_write (a, enc->s2k.salt, 8); |
995 | 0 | if (enc->s2k.mode == 3) |
996 | 0 | iobuf_put (a, enc->s2k.count); |
997 | 0 | } |
998 | 0 | if (enc->seskeylen) |
999 | 0 | iobuf_write (a, enc->seskey, enc->seskeylen); |
1000 | |
|
1001 | 0 | write_header (out, ctb, iobuf_get_temp_length(a)); |
1002 | 0 | rc = iobuf_write_temp (out, a); |
1003 | |
|
1004 | 0 | iobuf_close (a); |
1005 | 0 | return rc; |
1006 | 0 | } |
1007 | | |
1008 | | |
1009 | | /* Serialize the public-key encrypted session key packet (RFC 4880, |
1010 | | 5.1) described by ENC and write it to OUT. |
1011 | | |
1012 | | CTB is the serialization's CTB. It specifies the header format and |
1013 | | the packet's type. The header length must not be set. */ |
1014 | | static int |
1015 | | do_pubkey_enc (iobuf_t out, int ctb, PKT_pubkey_enc *enc) |
1016 | 0 | { |
1017 | 0 | int rc = 0; |
1018 | 0 | int n, i; |
1019 | 0 | iobuf_t a; |
1020 | |
|
1021 | 0 | log_assert (ctb_pkttype (ctb) == PKT_PUBKEY_ENC); |
1022 | 0 | if (enc->version == 6) |
1023 | 0 | return do_pubkey_enc_v6 (out, ctb, enc); |
1024 | | |
1025 | 0 | a = iobuf_temp(); |
1026 | |
|
1027 | 0 | iobuf_put (a, 3); /* Version. */ |
1028 | |
|
1029 | 0 | if ( enc->throw_keyid ) |
1030 | 0 | { |
1031 | 0 | write_32(a, 0 ); /* Don't tell Eve who can decrypt the message. */ |
1032 | 0 | write_32(a, 0 ); |
1033 | 0 | } |
1034 | 0 | else |
1035 | 0 | { |
1036 | 0 | write_32(a, enc->keyid[0] ); |
1037 | 0 | write_32(a, enc->keyid[1] ); |
1038 | 0 | } |
1039 | 0 | iobuf_put(a,enc->pubkey_algo ); |
1040 | 0 | n = pubkey_get_nenc( enc->pubkey_algo ); |
1041 | 0 | if ( !n ) |
1042 | 0 | write_fake_data( a, enc->data[0] ); |
1043 | |
|
1044 | 0 | for (i=0; i < n && !rc ; i++ ) |
1045 | 0 | { |
1046 | | /* For Kyber we need to insert the algo before the final data |
1047 | | * element because it is not stored in the data array. */ |
1048 | 0 | if (enc->pubkey_algo == PUBKEY_ALGO_KYBER && i == 2) |
1049 | 0 | iobuf_put (a, enc->seskey_algo); |
1050 | |
|
1051 | 0 | if (i == 1 && enc->pubkey_algo == PUBKEY_ALGO_ECDH) |
1052 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, enc->data[i]); |
1053 | 0 | else if (i == 1 && enc->pubkey_algo == PUBKEY_ALGO_KYBER) |
1054 | 0 | rc = gpg_mpi_write_opaque_32 (a, enc->data[i], NULL); |
1055 | 0 | else if (i == 2 && enc->pubkey_algo == PUBKEY_ALGO_KYBER) |
1056 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, enc->data[i]); |
1057 | 0 | else if (enc->pubkey_algo == PUBKEY_ALGO_ECDH |
1058 | 0 | || enc->pubkey_algo == PUBKEY_ALGO_KYBER) |
1059 | 0 | rc = sos_write (a, enc->data[i], NULL); |
1060 | 0 | else |
1061 | 0 | rc = gpg_mpi_write (a, enc->data[i], NULL); |
1062 | 0 | } |
1063 | |
|
1064 | 0 | if (!rc) |
1065 | 0 | { |
1066 | 0 | write_header (out, ctb, iobuf_get_temp_length(a) ); |
1067 | 0 | rc = iobuf_write_temp (out, a); |
1068 | 0 | } |
1069 | 0 | iobuf_close(a); |
1070 | 0 | return rc; |
1071 | 0 | } |
1072 | | |
1073 | | |
1074 | | static int |
1075 | | do_pubkey_enc_v6 (iobuf_t out, int ctb, PKT_pubkey_enc *enc) |
1076 | 0 | { |
1077 | 0 | int rc = 0; |
1078 | 0 | iobuf_t a; |
1079 | |
|
1080 | 0 | a = iobuf_temp(); |
1081 | 0 | iobuf_put (a, 6); /* The version of this packet. */ |
1082 | |
|
1083 | 0 | if (enc->throw_keyid) |
1084 | 0 | iobuf_put (a, 0); /* No fingerprint given. */ |
1085 | 0 | else |
1086 | 0 | { |
1087 | | /* Note that we do not support the regular v5 keys here. v5 |
1088 | | * keys shall keep using the version 3 pubkey_enc packets |
1089 | | * because the v6 format is missing the session key algo and |
1090 | | * conveys the full fingerprint. */ |
1091 | 0 | iobuf_put (a, (enc->fprlen == 32? 32 : 20) + 1); |
1092 | 0 | iobuf_put (a, enc->fprlen == 32? 6 : 4); /* The _key_ version. */ |
1093 | 0 | iobuf_write (a, enc->fpr, sizeof enc->fpr); |
1094 | 0 | } |
1095 | |
|
1096 | 0 | iobuf_put (a, enc->pubkey_algo ); |
1097 | |
|
1098 | 0 | if (IS_PUBKEY_ALGO_MLK (enc->pubkey_algo)) |
1099 | 0 | { |
1100 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, enc->data[0]); |
1101 | 0 | if (!rc) |
1102 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, enc->data[1]); |
1103 | 0 | if (!rc) |
1104 | 0 | { |
1105 | 0 | iobuf_put (a, 40); /* size octet. */ |
1106 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, enc->data[2]); |
1107 | 0 | } |
1108 | 0 | } |
1109 | 0 | else if (enc->pubkey_algo == PUBKEY_ALGO_X25519) |
1110 | 0 | { |
1111 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, enc->data[0]); |
1112 | 0 | if (!rc) |
1113 | 0 | rc = gpg_mpi_write_opaque_8 (a, enc->data[1], NULL); |
1114 | 0 | } |
1115 | 0 | else |
1116 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); /* for this algo. */ |
1117 | |
|
1118 | 0 | if (!rc) |
1119 | 0 | { |
1120 | 0 | write_new_header (out, (0xc0|(ctb_pkttype(ctb)&0x3f)), |
1121 | 0 | iobuf_get_temp_length(a), 0); |
1122 | 0 | rc = iobuf_write_temp (out, a); |
1123 | 0 | } |
1124 | 0 | iobuf_close (a); |
1125 | 0 | return rc; |
1126 | 0 | } |
1127 | | |
1128 | | |
1129 | | /* Calculate the length of the serialized plaintext packet PT (RFC |
1130 | | 4480, Section 5.9). */ |
1131 | | static u32 |
1132 | | calc_plaintext( PKT_plaintext *pt ) |
1133 | 0 | { |
1134 | | /* Truncate namelen to the maximum 255 characters. Note this means |
1135 | | that a function that calls build_packet with an illegal literal |
1136 | | packet will get it back legalized. */ |
1137 | |
|
1138 | 0 | if(pt->namelen>255) |
1139 | 0 | pt->namelen=255; |
1140 | |
|
1141 | 0 | return pt->len? (1 + 1 + pt->namelen + 4 + pt->len) : 0; |
1142 | 0 | } |
1143 | | |
1144 | | /* Serialize the plaintext packet (RFC 4880, 5.9) described by PT and |
1145 | | write it to OUT. |
1146 | | |
1147 | | The body of the message is stored in PT->BUF. The amount of data |
1148 | | to write is PT->LEN. (PT->BUF should be configured to return EOF |
1149 | | after this much data has been read.) If PT->LEN is 0 and CTB |
1150 | | indicates that this is a new format packet, then partial block mode |
1151 | | is assumed to have been enabled on OUT. On success, partial block |
1152 | | mode is disabled. |
1153 | | |
1154 | | If PT->BUF is NULL, the caller must write out the data. In |
1155 | | this case, if PT->LEN was 0, then partial body length mode was |
1156 | | enabled and the caller must disable it by calling |
1157 | | iobuf_set_partial_body_length_mode (out, 0). */ |
1158 | | static int |
1159 | | do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt ) |
1160 | 0 | { |
1161 | 0 | int rc = 0; |
1162 | 0 | size_t nbytes; |
1163 | |
|
1164 | 0 | log_assert (ctb_pkttype (ctb) == PKT_PLAINTEXT); |
1165 | | |
1166 | 0 | write_header(out, ctb, calc_plaintext( pt ) ); |
1167 | 0 | log_assert (pt->mode == 'b' || pt->mode == 't' || pt->mode == 'u' |
1168 | 0 | || pt->mode == 'm' |
1169 | 0 | || pt->mode == 'l' || pt->mode == '1'); |
1170 | 0 | iobuf_put(out, pt->mode ); |
1171 | 0 | iobuf_put(out, pt->namelen ); |
1172 | 0 | iobuf_write (out, pt->name, pt->namelen); |
1173 | 0 | rc = write_32(out, pt->timestamp ); |
1174 | 0 | if (rc) |
1175 | 0 | return rc; |
1176 | | |
1177 | 0 | if (pt->buf) |
1178 | 0 | { |
1179 | 0 | nbytes = iobuf_copy (out, pt->buf); |
1180 | 0 | if (nbytes == (size_t)(-1) |
1181 | 0 | && (iobuf_error (out) || iobuf_error (pt->buf))) |
1182 | 0 | return iobuf_error (out)? iobuf_error (out):iobuf_error (pt->buf); |
1183 | 0 | else if (nbytes != (size_t)(-1) && iobuf_error (pt->buf)) |
1184 | 0 | rc = iobuf_error (pt->buf); /* Read error. */ |
1185 | 0 | else |
1186 | 0 | { |
1187 | | /* Always get the iobuf error to catch write errors |
1188 | | * because iobuf_copy returns (-1) only if there was a |
1189 | | * errors in the output stream when entering that |
1190 | | * function. */ |
1191 | 0 | rc = iobuf_error (out); |
1192 | 0 | } |
1193 | 0 | if(ctb_new_format_p (ctb) && !pt->len) |
1194 | | /* Turn off partial body length mode. */ |
1195 | 0 | iobuf_set_partial_body_length_mode (out, 0); |
1196 | 0 | if (pt->len && nbytes != pt->len) |
1197 | 0 | { |
1198 | 0 | log_error ("do_plaintext(): wrote %lu bytes" |
1199 | 0 | " but expected %lu bytes\n", |
1200 | 0 | (ulong)nbytes, (ulong)pt->len ); |
1201 | 0 | if (!rc) /* Just in case no error was set */ |
1202 | 0 | rc = gpg_error (GPG_ERR_EIO); |
1203 | 0 | } |
1204 | 0 | } |
1205 | | |
1206 | 0 | return rc; |
1207 | 0 | } |
1208 | | |
1209 | | |
1210 | | |
1211 | | /* Serialize the symmetrically encrypted data packet (RFC 4880, |
1212 | | Section 5.7) described by ED and write it to OUT. |
1213 | | |
1214 | | Note: this only writes the packets header! The call must then |
1215 | | follow up and write the initial random data and the body to OUT. |
1216 | | (If you use the encryption iobuf filter (cipher_filter), then this |
1217 | | is done automatically.) */ |
1218 | | static int |
1219 | | do_encrypted( IOBUF out, int ctb, PKT_encrypted *ed ) |
1220 | 0 | { |
1221 | 0 | int rc = 0; |
1222 | 0 | u32 n; |
1223 | |
|
1224 | 0 | log_assert (! ed->mdc_method); |
1225 | 0 | log_assert (ctb_pkttype (ctb) == PKT_ENCRYPTED); |
1226 | | |
1227 | 0 | n = ed->len ? (ed->len + ed->extralen) : 0; |
1228 | 0 | write_header(out, ctb, n ); |
1229 | | |
1230 | | /* This is all. The caller has to write the real data */ |
1231 | |
|
1232 | 0 | return rc; |
1233 | 0 | } |
1234 | | |
1235 | | /* Serialize the symmetrically encrypted integrity protected data |
1236 | | packet (RFC 4880, Section 5.13) described by ED and write it to |
1237 | | OUT. |
1238 | | |
1239 | | Note: this only writes the packet's header! The caller must then |
1240 | | follow up and write the initial random data, the body and the MDC |
1241 | | packet to OUT. (If you use the encryption iobuf filter |
1242 | | (cipher_filter), then this is done automatically.) */ |
1243 | | static int |
1244 | | do_encrypted_mdc (iobuf_t out, int ctb, PKT_encrypted *ed) |
1245 | 0 | { |
1246 | 0 | u32 n; |
1247 | |
|
1248 | 0 | log_assert (ctb_pkttype (ctb) == PKT_ENCRYPTED_MDC); |
1249 | | |
1250 | 0 | if (ed->version == 2) |
1251 | 0 | { |
1252 | | /* Take this header (4) in account. */ |
1253 | 0 | n = ed->len ? (ed->len + ed->extralen + 4) : 0; |
1254 | 0 | write_new_header (out, (0xc0|(ctb_pkttype(ctb)&0x3f)), n, 0); |
1255 | 0 | iobuf_writebyte (out, 2); |
1256 | 0 | iobuf_writebyte (out, ed->cipher_algo); |
1257 | 0 | iobuf_writebyte (out, ed->aead_algo); |
1258 | 0 | iobuf_writebyte (out, ed->chunkbyte); |
1259 | 0 | } |
1260 | 0 | else |
1261 | 0 | { |
1262 | 0 | log_assert (ed->mdc_method); |
1263 | | /* Take version number and the final MDC packet in account. */ |
1264 | 0 | n = ed->len ? (ed->len + ed->extralen + 1 + 22) : 0; |
1265 | 0 | write_header (out, ctb, n); |
1266 | 0 | iobuf_put (out, 1); /* version */ |
1267 | 0 | } |
1268 | | |
1269 | | /* This is all. The caller has to write the real data */ |
1270 | 0 | return 0; |
1271 | 0 | } |
1272 | | |
1273 | | |
1274 | | /* Serialize the symmetrically AEAD encrypted data packet |
1275 | | * (rfc4880bis-03, Section 5.16) described by ED and write it to OUT. |
1276 | | * |
1277 | | * Note: this only writes only packet's header. The caller must then |
1278 | | * follow up and write the actual encrypted data. This should be done |
1279 | | * by pushing the the cipher_filter_aead. */ |
1280 | | static int |
1281 | | do_encrypted_ocb (iobuf_t out, int ctb, PKT_encrypted *ed) |
1282 | 0 | { |
1283 | 0 | u32 n; |
1284 | |
|
1285 | 0 | log_assert (ctb_pkttype (ctb) == PKT_ENCRYPTED_OCB); |
1286 | | |
1287 | 0 | n = ed->len ? (ed->len + ed->extralen + 4) : 0; |
1288 | 0 | write_header (out, ctb, n ); |
1289 | 0 | iobuf_writebyte (out, 1); /* Version. */ |
1290 | 0 | iobuf_writebyte (out, ed->cipher_algo); |
1291 | 0 | iobuf_writebyte (out, ed->aead_algo); |
1292 | 0 | iobuf_writebyte (out, ed->chunkbyte); |
1293 | | |
1294 | | /* This is all. The caller has to write the encrypted data */ |
1295 | |
|
1296 | 0 | return 0; |
1297 | 0 | } |
1298 | | |
1299 | | |
1300 | | /* Serialize the compressed packet (RFC 4880, Section 5.6) described |
1301 | | by CD and write it to OUT. |
1302 | | |
1303 | | Note: this only writes the packet's header! The caller must then |
1304 | | follow up and write the body to OUT. */ |
1305 | | static int |
1306 | | do_compressed( IOBUF out, int ctb, PKT_compressed *cd ) |
1307 | 0 | { |
1308 | 0 | int rc = 0; |
1309 | |
|
1310 | 0 | log_assert (ctb_pkttype (ctb) == PKT_COMPRESSED); |
1311 | | |
1312 | | /* We must use the old convention and don't use blockmode for the |
1313 | | sake of PGP 2 compatibility. However if the new_ctb flag was |
1314 | | set, CTB is already formatted as new style and write_header2 |
1315 | | does create a partial length encoding using new the new |
1316 | | style. */ |
1317 | 0 | write_header2(out, ctb, 0, 0); |
1318 | 0 | iobuf_put(out, cd->algorithm ); |
1319 | | |
1320 | | /* This is all. The caller has to write the real data */ |
1321 | |
|
1322 | 0 | return rc; |
1323 | 0 | } |
1324 | | |
1325 | | |
1326 | | /**************** |
1327 | | * Delete all subpackets of type REQTYPE and return a bool whether a packet |
1328 | | * was deleted. |
1329 | | */ |
1330 | | int |
1331 | | delete_sig_subpkt (subpktarea_t *area, sigsubpkttype_t reqtype ) |
1332 | 0 | { |
1333 | 0 | int buflen; |
1334 | 0 | sigsubpkttype_t type; |
1335 | 0 | byte *buffer, *bufstart; |
1336 | 0 | size_t n; |
1337 | 0 | size_t unused = 0; |
1338 | 0 | int okay = 0; |
1339 | |
|
1340 | 0 | if( !area ) |
1341 | 0 | return 0; |
1342 | 0 | buflen = area->len; |
1343 | 0 | buffer = area->data; |
1344 | 0 | for(;;) { |
1345 | 0 | if( !buflen ) { |
1346 | 0 | okay = 1; |
1347 | 0 | break; |
1348 | 0 | } |
1349 | 0 | bufstart = buffer; |
1350 | 0 | n = *buffer++; buflen--; |
1351 | 0 | if( n == 255 ) { |
1352 | 0 | if( buflen < 4 ) |
1353 | 0 | break; |
1354 | 0 | n = buf32_to_size_t (buffer); |
1355 | 0 | buffer += 4; |
1356 | 0 | buflen -= 4; |
1357 | 0 | } |
1358 | 0 | else if( n >= 192 ) { |
1359 | 0 | if( buflen < 2 ) |
1360 | 0 | break; |
1361 | 0 | n = (( n - 192 ) << 8) + *buffer + 192; |
1362 | 0 | buffer++; |
1363 | 0 | buflen--; |
1364 | 0 | } |
1365 | 0 | if( buflen < n ) |
1366 | 0 | break; |
1367 | | |
1368 | 0 | type = *buffer & 0x7f; |
1369 | 0 | if( type == reqtype ) { |
1370 | 0 | buffer++; |
1371 | 0 | buflen--; |
1372 | 0 | n--; |
1373 | 0 | if( n > buflen ) |
1374 | 0 | break; |
1375 | 0 | buffer += n; /* point to next subpkt */ |
1376 | 0 | buflen -= n; |
1377 | 0 | memmove (bufstart, buffer, buflen); /* shift */ |
1378 | 0 | unused += buffer - bufstart; |
1379 | 0 | buffer = bufstart; |
1380 | 0 | } |
1381 | 0 | else { |
1382 | 0 | buffer += n; buflen -=n; |
1383 | 0 | } |
1384 | 0 | } |
1385 | |
|
1386 | 0 | if (!okay) |
1387 | 0 | log_error ("delete_subpkt: buffer shorter than subpacket\n"); |
1388 | 0 | log_assert (unused <= area->len); |
1389 | 0 | area->len -= unused; |
1390 | 0 | return !!unused; |
1391 | 0 | } |
1392 | | |
1393 | | |
1394 | | /**************** |
1395 | | * Create or update a signature subpacket for SIG of TYPE. This |
1396 | | * functions knows where to put the data (hashed or unhashed). The |
1397 | | * function may move data from the unhashed part to the hashed one. |
1398 | | * Note: All pointers into sig->[un]hashed (e.g. returned by |
1399 | | * parse_sig_subpkt) are not valid after a call to this function. The |
1400 | | * data to put into the subpaket should be in a buffer with a length |
1401 | | * of buflen. |
1402 | | */ |
1403 | | void |
1404 | | build_sig_subpkt (PKT_signature *sig, sigsubpkttype_t type, |
1405 | | const byte *buffer, size_t buflen ) |
1406 | 0 | { |
1407 | 0 | byte *p; |
1408 | 0 | int critical, hashed; |
1409 | 0 | subpktarea_t *oldarea, *newarea; |
1410 | 0 | size_t nlen, n, n0; |
1411 | |
|
1412 | 0 | critical = (type & SIGSUBPKT_FLAG_CRITICAL); |
1413 | 0 | type &= ~SIGSUBPKT_FLAG_CRITICAL; |
1414 | | |
1415 | | /* Sanity check buffer sizes */ |
1416 | 0 | if(parse_one_sig_subpkt(buffer,buflen,type)<0) |
1417 | 0 | BUG(); |
1418 | | |
1419 | 0 | switch(type) |
1420 | 0 | { |
1421 | 0 | case SIGSUBPKT_NOTATION: |
1422 | 0 | case SIGSUBPKT_POLICY: |
1423 | 0 | case SIGSUBPKT_REV_KEY: |
1424 | 0 | case SIGSUBPKT_SIGNATURE: |
1425 | | /* we do allow multiple subpackets */ |
1426 | 0 | break; |
1427 | | |
1428 | 0 | default: |
1429 | | /* we don't allow multiple subpackets */ |
1430 | 0 | delete_sig_subpkt(sig->hashed,type); |
1431 | 0 | delete_sig_subpkt(sig->unhashed,type); |
1432 | 0 | break; |
1433 | 0 | } |
1434 | | |
1435 | | /* Any special magic that needs to be done for this type so the |
1436 | | packet doesn't need to be reparsed? */ |
1437 | 0 | switch(type) |
1438 | 0 | { |
1439 | 0 | case SIGSUBPKT_NOTATION: |
1440 | 0 | sig->flags.notation=1; |
1441 | 0 | break; |
1442 | | |
1443 | 0 | case SIGSUBPKT_POLICY: |
1444 | 0 | sig->flags.policy_url=1; |
1445 | 0 | break; |
1446 | | |
1447 | 0 | case SIGSUBPKT_PREF_KS: |
1448 | 0 | sig->flags.pref_ks=1; |
1449 | 0 | break; |
1450 | | |
1451 | 0 | case SIGSUBPKT_EXPORTABLE: |
1452 | 0 | if(buffer[0]) |
1453 | 0 | sig->flags.exportable=1; |
1454 | 0 | else |
1455 | 0 | sig->flags.exportable=0; |
1456 | 0 | break; |
1457 | | |
1458 | 0 | case SIGSUBPKT_REVOCABLE: |
1459 | 0 | if(buffer[0]) |
1460 | 0 | sig->flags.revocable=1; |
1461 | 0 | else |
1462 | 0 | sig->flags.revocable=0; |
1463 | 0 | break; |
1464 | | |
1465 | 0 | case SIGSUBPKT_TRUST: |
1466 | 0 | sig->trust_depth=buffer[0]; |
1467 | 0 | sig->trust_value=buffer[1]; |
1468 | 0 | break; |
1469 | | |
1470 | 0 | case SIGSUBPKT_REGEXP: |
1471 | 0 | sig->trust_regexp=buffer; |
1472 | 0 | break; |
1473 | | |
1474 | | /* This should never happen since we don't currently allow |
1475 | | creating such a subpacket, but just in case... */ |
1476 | 0 | case SIGSUBPKT_SIG_EXPIRE: |
1477 | 0 | if(buf32_to_u32(buffer)+sig->timestamp<=make_timestamp()) |
1478 | 0 | sig->flags.expired=1; |
1479 | 0 | else |
1480 | 0 | sig->flags.expired=0; |
1481 | 0 | break; |
1482 | | |
1483 | 0 | default: |
1484 | 0 | break; |
1485 | 0 | } |
1486 | | |
1487 | 0 | if( (buflen+1) >= 8384 ) |
1488 | 0 | nlen = 5; /* write 5 byte length header */ |
1489 | 0 | else if( (buflen+1) >= 192 ) |
1490 | 0 | nlen = 2; /* write 2 byte length header */ |
1491 | 0 | else |
1492 | 0 | nlen = 1; /* just a 1 byte length header */ |
1493 | |
|
1494 | 0 | switch( type ) |
1495 | 0 | { |
1496 | | /* The issuer being unhashed is a historical oddity. It |
1497 | | should work equally as well hashed. Of course, if even an |
1498 | | unhashed issuer is tampered with, it makes it awfully hard |
1499 | | to verify the sig... */ |
1500 | 0 | case SIGSUBPKT_ISSUER: |
1501 | 0 | case SIGSUBPKT_SIGNATURE: |
1502 | 0 | hashed = 0; |
1503 | 0 | break; |
1504 | 0 | default: |
1505 | 0 | hashed = 1; |
1506 | 0 | break; |
1507 | 0 | } |
1508 | | |
1509 | 0 | if( critical ) |
1510 | 0 | type |= SIGSUBPKT_FLAG_CRITICAL; |
1511 | |
|
1512 | 0 | oldarea = hashed? sig->hashed : sig->unhashed; |
1513 | | |
1514 | | /* Calculate new size of the area and allocate */ |
1515 | 0 | n0 = oldarea? oldarea->len : 0; |
1516 | 0 | n = n0 + nlen + 1 + buflen; /* length, type, buffer */ |
1517 | 0 | if (oldarea && n <= oldarea->size) { /* fits into the unused space */ |
1518 | 0 | newarea = oldarea; |
1519 | | /*log_debug ("updating area for type %d\n", type );*/ |
1520 | 0 | } |
1521 | 0 | else if (oldarea) { |
1522 | 0 | newarea = xrealloc (oldarea, sizeof (*newarea) + n - 1); |
1523 | 0 | newarea->size = n; |
1524 | | /*log_debug ("reallocating area for type %d\n", type );*/ |
1525 | 0 | } |
1526 | 0 | else { |
1527 | 0 | newarea = xmalloc (sizeof (*newarea) + n - 1); |
1528 | 0 | newarea->size = n; |
1529 | | /*log_debug ("allocating area for type %d\n", type );*/ |
1530 | 0 | } |
1531 | 0 | newarea->len = n; |
1532 | |
|
1533 | 0 | p = newarea->data + n0; |
1534 | 0 | if (nlen == 5) { |
1535 | 0 | *p++ = 255; |
1536 | 0 | *p++ = (buflen+1) >> 24; |
1537 | 0 | *p++ = (buflen+1) >> 16; |
1538 | 0 | *p++ = (buflen+1) >> 8; |
1539 | 0 | *p++ = (buflen+1); |
1540 | 0 | *p++ = type; |
1541 | 0 | memcpy (p, buffer, buflen); |
1542 | 0 | } |
1543 | 0 | else if (nlen == 2) { |
1544 | 0 | *p++ = (buflen+1-192) / 256 + 192; |
1545 | 0 | *p++ = (buflen+1-192) % 256; |
1546 | 0 | *p++ = type; |
1547 | 0 | memcpy (p, buffer, buflen); |
1548 | 0 | } |
1549 | 0 | else { |
1550 | 0 | *p++ = buflen+1; |
1551 | 0 | *p++ = type; |
1552 | 0 | memcpy (p, buffer, buflen); |
1553 | 0 | } |
1554 | |
|
1555 | 0 | if (hashed) |
1556 | 0 | sig->hashed = newarea; |
1557 | 0 | else |
1558 | 0 | sig->unhashed = newarea; |
1559 | 0 | } |
1560 | | |
1561 | | /* |
1562 | | * Put all the required stuff from SIG into subpackets of sig. |
1563 | | * PKSK is the signing key. SIGNHINTS are various flags like |
1564 | | * SIGNHINT_ADSK. |
1565 | | * Hmmm, should we delete those subpackets which are in a wrong area? |
1566 | | */ |
1567 | | void |
1568 | | build_sig_subpkt_from_sig (PKT_signature *sig, PKT_public_key *pksk, |
1569 | | unsigned int signhints) |
1570 | 0 | { |
1571 | 0 | u32 u; |
1572 | 0 | byte buf[1+MAX_FINGERPRINT_LEN]; |
1573 | 0 | size_t fprlen; |
1574 | | |
1575 | | /* For v4 keys we need to write the ISSUER subpacket. We do not |
1576 | | * want that for a future v5 format. We also don't write it if |
1577 | | * only the new RENC keyflag is set (implementations with support |
1578 | | * for this key flag should understand the ISSUER_FPR). */ |
1579 | 0 | if (pksk->version < 5 && !(signhints & SIGNHINT_ADSK)) |
1580 | 0 | { |
1581 | 0 | u = sig->keyid[0]; |
1582 | 0 | buf[0] = (u >> 24) & 0xff; |
1583 | 0 | buf[1] = (u >> 16) & 0xff; |
1584 | 0 | buf[2] = (u >> 8) & 0xff; |
1585 | 0 | buf[3] = u & 0xff; |
1586 | 0 | u = sig->keyid[1]; |
1587 | 0 | buf[4] = (u >> 24) & 0xff; |
1588 | 0 | buf[5] = (u >> 16) & 0xff; |
1589 | 0 | buf[6] = (u >> 8) & 0xff; |
1590 | 0 | buf[7] = u & 0xff; |
1591 | 0 | build_sig_subpkt (sig, SIGSUBPKT_ISSUER, buf, 8); |
1592 | 0 | } |
1593 | | |
1594 | | /* Write the new ISSUER_FPR subpacket. */ |
1595 | 0 | fingerprint_from_pk (pksk, buf+1, &fprlen); |
1596 | 0 | if (fprlen == 20 || fprlen == 32) |
1597 | 0 | { |
1598 | 0 | buf[0] = pksk->version; |
1599 | 0 | build_sig_subpkt (sig, SIGSUBPKT_ISSUER_FPR, buf, fprlen + 1); |
1600 | 0 | } |
1601 | | |
1602 | | /* Write the timestamp. */ |
1603 | 0 | u = sig->timestamp; |
1604 | 0 | buf[0] = (u >> 24) & 0xff; |
1605 | 0 | buf[1] = (u >> 16) & 0xff; |
1606 | 0 | buf[2] = (u >> 8) & 0xff; |
1607 | 0 | buf[3] = u & 0xff; |
1608 | 0 | build_sig_subpkt( sig, SIGSUBPKT_SIG_CREATED, buf, 4 ); |
1609 | |
|
1610 | 0 | if(sig->expiredate) |
1611 | 0 | { |
1612 | 0 | if(sig->expiredate>sig->timestamp) |
1613 | 0 | u=sig->expiredate-sig->timestamp; |
1614 | 0 | else |
1615 | 0 | u=1; /* A 1-second expiration time is the shortest one |
1616 | | OpenPGP has */ |
1617 | |
|
1618 | 0 | buf[0] = (u >> 24) & 0xff; |
1619 | 0 | buf[1] = (u >> 16) & 0xff; |
1620 | 0 | buf[2] = (u >> 8) & 0xff; |
1621 | 0 | buf[3] = u & 0xff; |
1622 | | |
1623 | | /* Mark this CRITICAL, so if any implementation doesn't |
1624 | | understand sigs that can expire, it'll just disregard this |
1625 | | sig altogether. */ |
1626 | |
|
1627 | 0 | build_sig_subpkt( sig, SIGSUBPKT_SIG_EXPIRE | SIGSUBPKT_FLAG_CRITICAL, |
1628 | 0 | buf, 4 ); |
1629 | 0 | } |
1630 | | |
1631 | | /* Add fpr of the to-be-revoked key in an intended recipient fpr subpacket. */ |
1632 | 0 | if (IS_KEY_REV (sig)) |
1633 | 0 | { |
1634 | 0 | log_assert (sizeof buf >= sig->rev_subject_info->fprlen + 1); |
1635 | | |
1636 | 0 | buf[0] = sig->rev_subject_info->fprlen == 20? 4: |
1637 | 0 | sig->rev_subject_info->fprlen == 32? 5 : 0; |
1638 | |
|
1639 | 0 | if (buf[0] == 4 || buf[0] == 5) |
1640 | 0 | { |
1641 | 0 | memcpy (buf+1, sig->rev_subject_info->fpr, |
1642 | 0 | sig->rev_subject_info->fprlen); |
1643 | 0 | build_sig_subpkt (sig, SIGSUBPKT_INT_RCP_FPR, |
1644 | 0 | buf, sig->rev_subject_info->fprlen + 1); |
1645 | 0 | } |
1646 | 0 | } |
1647 | 0 | } |
1648 | | |
1649 | | void |
1650 | | build_attribute_subpkt(PKT_user_id *uid,byte type, |
1651 | | const void *buf,u32 buflen, |
1652 | | const void *header,u32 headerlen) |
1653 | 0 | { |
1654 | 0 | byte *attrib; |
1655 | 0 | int idx; |
1656 | |
|
1657 | 0 | if(1+headerlen+buflen>8383) |
1658 | 0 | idx=5; |
1659 | 0 | else if(1+headerlen+buflen>191) |
1660 | 0 | idx=2; |
1661 | 0 | else |
1662 | 0 | idx=1; |
1663 | | |
1664 | | /* realloc uid->attrib_data to the right size */ |
1665 | |
|
1666 | 0 | uid->attrib_data=xrealloc(uid->attrib_data, |
1667 | 0 | uid->attrib_len+idx+1+headerlen+buflen); |
1668 | |
|
1669 | 0 | attrib=&uid->attrib_data[uid->attrib_len]; |
1670 | |
|
1671 | 0 | if(idx==5) |
1672 | 0 | { |
1673 | 0 | attrib[0]=255; |
1674 | 0 | attrib[1]=(1+headerlen+buflen) >> 24; |
1675 | 0 | attrib[2]=(1+headerlen+buflen) >> 16; |
1676 | 0 | attrib[3]=(1+headerlen+buflen) >> 8; |
1677 | 0 | attrib[4]=1+headerlen+buflen; |
1678 | 0 | } |
1679 | 0 | else if(idx==2) |
1680 | 0 | { |
1681 | 0 | attrib[0]=(1+headerlen+buflen-192) / 256 + 192; |
1682 | 0 | attrib[1]=(1+headerlen+buflen-192) % 256; |
1683 | 0 | } |
1684 | 0 | else |
1685 | 0 | attrib[0]=1+headerlen+buflen; /* Good luck finding a JPEG this small! */ |
1686 | |
|
1687 | 0 | attrib[idx++]=type; |
1688 | | |
1689 | | /* Tack on our data at the end */ |
1690 | |
|
1691 | 0 | if(headerlen>0) |
1692 | 0 | memcpy(&attrib[idx],header,headerlen); |
1693 | 0 | memcpy(&attrib[idx+headerlen],buf,buflen); |
1694 | 0 | uid->attrib_len+=idx+headerlen+buflen; |
1695 | 0 | } |
1696 | | |
1697 | | /* Returns a human-readable string corresponding to the notation. |
1698 | | This ignores notation->value. The caller must free the result. */ |
1699 | | static char * |
1700 | | notation_value_to_human_readable_string (struct notation *notation) |
1701 | 0 | { |
1702 | 0 | if(notation->bdat) |
1703 | | /* Binary data. */ |
1704 | 0 | { |
1705 | 0 | size_t len = notation->blen; |
1706 | 0 | int i; |
1707 | 0 | char preview[20]; |
1708 | |
|
1709 | 0 | for (i = 0; i < len && i < sizeof (preview) - 1; i ++) |
1710 | 0 | if (isprint (notation->bdat[i])) |
1711 | 0 | preview[i] = notation->bdat[i]; |
1712 | 0 | else |
1713 | 0 | preview[i] = '?'; |
1714 | 0 | preview[i] = 0; |
1715 | |
|
1716 | 0 | return xasprintf (_("[ not human readable (%zu bytes: %s%s) ]"), |
1717 | 0 | len, preview, i < len ? "..." : ""); |
1718 | 0 | } |
1719 | 0 | else |
1720 | | /* The value is human-readable. */ |
1721 | 0 | return xstrdup (notation->value); |
1722 | 0 | } |
1723 | | |
1724 | | |
1725 | | /* Turn the notation described by the string STRING into a notation. |
1726 | | * |
1727 | | * STRING has the form: |
1728 | | * |
1729 | | * - -name - Delete the notation. |
1730 | | * - name@domain.name=value - Normal notation |
1731 | | * - !name@domain.name=value - Notation with critical bit set. |
1732 | | * |
1733 | | * The caller must free the result using free_notation(). */ |
1734 | | struct notation * |
1735 | | string_to_notation (const char *string, int is_utf8) |
1736 | 0 | { |
1737 | 0 | const char *s; |
1738 | 0 | int saw_at=0; |
1739 | 0 | struct notation *notation; |
1740 | |
|
1741 | 0 | notation=xmalloc_clear(sizeof(*notation)); |
1742 | |
|
1743 | 0 | if(*string=='-') |
1744 | 0 | { |
1745 | 0 | notation->flags.ignore=1; |
1746 | 0 | string++; |
1747 | 0 | } |
1748 | |
|
1749 | 0 | if(*string=='!') |
1750 | 0 | { |
1751 | 0 | notation->flags.critical=1; |
1752 | 0 | string++; |
1753 | 0 | } |
1754 | | |
1755 | | /* If and when the IETF assigns some official name tags, we'll have |
1756 | | to add them here. */ |
1757 | |
|
1758 | 0 | for( s=string ; *s != '='; s++ ) |
1759 | 0 | { |
1760 | 0 | if( *s=='@') |
1761 | 0 | saw_at++; |
1762 | | |
1763 | | /* -notationname is legal without an = sign */ |
1764 | 0 | if(!*s && notation->flags.ignore) |
1765 | 0 | break; |
1766 | | |
1767 | 0 | if( !*s || !isascii (*s) || (!isgraph(*s) && !isspace(*s)) ) |
1768 | 0 | { |
1769 | 0 | log_error(_("a notation name must have only printable characters" |
1770 | 0 | " or spaces, and end with an '='\n") ); |
1771 | 0 | goto fail; |
1772 | 0 | } |
1773 | 0 | } |
1774 | | |
1775 | 0 | notation->name=xmalloc((s-string)+1); |
1776 | 0 | memcpy(notation->name,string,s-string); |
1777 | 0 | notation->name[s-string]='\0'; |
1778 | |
|
1779 | 0 | if(!saw_at && !opt.expert) |
1780 | 0 | { |
1781 | 0 | log_error(_("a user notation name must contain the '@' character\n")); |
1782 | 0 | goto fail; |
1783 | 0 | } |
1784 | | |
1785 | 0 | if (saw_at > 1) |
1786 | 0 | { |
1787 | 0 | log_error(_("a notation name must not contain more than" |
1788 | 0 | " one '@' character\n")); |
1789 | 0 | goto fail; |
1790 | 0 | } |
1791 | | |
1792 | 0 | if(*s) |
1793 | 0 | { |
1794 | 0 | const char *i=s+1; |
1795 | 0 | int highbit=0; |
1796 | | |
1797 | | /* we only support printable text - therefore we enforce the use |
1798 | | of only printable characters (an empty value is valid) */ |
1799 | 0 | for(s++; *s ; s++ ) |
1800 | 0 | { |
1801 | 0 | if ( !isascii (*s) ) |
1802 | 0 | highbit=1; |
1803 | 0 | else if (iscntrl(*s)) |
1804 | 0 | { |
1805 | 0 | log_error(_("a notation value must not use any" |
1806 | 0 | " control characters\n")); |
1807 | 0 | goto fail; |
1808 | 0 | } |
1809 | 0 | } |
1810 | | |
1811 | 0 | if(!highbit || is_utf8) |
1812 | 0 | notation->value=xstrdup(i); |
1813 | 0 | else |
1814 | 0 | notation->value=native_to_utf8(i); |
1815 | 0 | } |
1816 | | |
1817 | 0 | return notation; |
1818 | | |
1819 | 0 | fail: |
1820 | 0 | free_notation(notation); |
1821 | 0 | return NULL; |
1822 | 0 | } |
1823 | | |
1824 | | |
1825 | | /* Turn the notation described by NAME and VALUE into a notation. |
1826 | | * This will be a human readble non-critical notation. |
1827 | | * The caller must free the result using free_notation(). */ |
1828 | | struct notation * |
1829 | | name_value_to_notation (const char *name, const char *value) |
1830 | 0 | { |
1831 | 0 | struct notation *notation; |
1832 | |
|
1833 | 0 | notation = xcalloc (1, sizeof *notation); |
1834 | 0 | notation->name = xstrdup (name); |
1835 | 0 | notation->value = xstrdup (value); |
1836 | 0 | return notation; |
1837 | 0 | } |
1838 | | |
1839 | | |
1840 | | /* Like string_to_notation, but store opaque data rather than human |
1841 | | readable data. */ |
1842 | | struct notation * |
1843 | | blob_to_notation(const char *name, const char *data, size_t len) |
1844 | 0 | { |
1845 | 0 | const char *s; |
1846 | 0 | int saw_at=0; |
1847 | 0 | struct notation *notation; |
1848 | |
|
1849 | 0 | notation=xmalloc_clear(sizeof(*notation)); |
1850 | |
|
1851 | 0 | if(*name=='-') |
1852 | 0 | { |
1853 | 0 | notation->flags.ignore=1; |
1854 | 0 | name++; |
1855 | 0 | } |
1856 | |
|
1857 | 0 | if(*name=='!') |
1858 | 0 | { |
1859 | 0 | notation->flags.critical=1; |
1860 | 0 | name++; |
1861 | 0 | } |
1862 | | |
1863 | | /* If and when the IETF assigns some official name tags, we'll have |
1864 | | to add them here. */ |
1865 | |
|
1866 | 0 | for( s=name ; *s; s++ ) |
1867 | 0 | { |
1868 | 0 | if( *s=='@') |
1869 | 0 | saw_at++; |
1870 | | |
1871 | | /* -notationname is legal without an = sign */ |
1872 | 0 | if(!*s && notation->flags.ignore) |
1873 | 0 | break; |
1874 | | |
1875 | 0 | if (*s == '=') |
1876 | 0 | { |
1877 | 0 | log_error(_("a notation name may not contain an '=' character\n")); |
1878 | 0 | goto fail; |
1879 | 0 | } |
1880 | | |
1881 | 0 | if (!isascii (*s) || (!isgraph(*s) && !isspace(*s))) |
1882 | 0 | { |
1883 | 0 | log_error(_("a notation name must have only printable characters" |
1884 | 0 | " or spaces\n") ); |
1885 | 0 | goto fail; |
1886 | 0 | } |
1887 | 0 | } |
1888 | | |
1889 | 0 | notation->name=xstrdup (name); |
1890 | |
|
1891 | 0 | if(!saw_at && !opt.expert) |
1892 | 0 | { |
1893 | 0 | log_error(_("a user notation name must contain the '@' character\n")); |
1894 | 0 | goto fail; |
1895 | 0 | } |
1896 | | |
1897 | 0 | if (saw_at > 1) |
1898 | 0 | { |
1899 | 0 | log_error(_("a notation name must not contain more than" |
1900 | 0 | " one '@' character\n")); |
1901 | 0 | goto fail; |
1902 | 0 | } |
1903 | | |
1904 | 0 | notation->bdat = xmalloc (len); |
1905 | 0 | memcpy (notation->bdat, data, len); |
1906 | 0 | notation->blen = len; |
1907 | |
|
1908 | 0 | notation->value = notation_value_to_human_readable_string (notation); |
1909 | |
|
1910 | 0 | return notation; |
1911 | | |
1912 | 0 | fail: |
1913 | 0 | free_notation(notation); |
1914 | 0 | return NULL; |
1915 | 0 | } |
1916 | | |
1917 | | struct notation * |
1918 | | sig_to_notation(PKT_signature *sig) |
1919 | 0 | { |
1920 | 0 | const byte *p; |
1921 | 0 | size_t len; |
1922 | 0 | int seq = 0; |
1923 | 0 | int crit; |
1924 | 0 | notation_t list = NULL; |
1925 | | |
1926 | | /* See RFC 4880, 5.2.3.16 for the format of notation data. In |
1927 | | short, a notation has: |
1928 | | |
1929 | | - 4 bytes of flags |
1930 | | - 2 byte name length (n1) |
1931 | | - 2 byte value length (n2) |
1932 | | - n1 bytes of name data |
1933 | | - n2 bytes of value data |
1934 | | */ |
1935 | 0 | while((p=enum_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, &len, &seq, &crit))) |
1936 | 0 | { |
1937 | 0 | int n1,n2; |
1938 | 0 | struct notation *n=NULL; |
1939 | |
|
1940 | 0 | if(len<8) |
1941 | 0 | { |
1942 | 0 | log_info(_("WARNING: invalid notation data found\n")); |
1943 | 0 | continue; |
1944 | 0 | } |
1945 | | |
1946 | | /* name length. */ |
1947 | 0 | n1=(p[4]<<8)|p[5]; |
1948 | | /* value length. */ |
1949 | 0 | n2=(p[6]<<8)|p[7]; |
1950 | |
|
1951 | 0 | if(8+n1+n2!=len) |
1952 | 0 | { |
1953 | 0 | log_info(_("WARNING: invalid notation data found\n")); |
1954 | 0 | continue; |
1955 | 0 | } |
1956 | | |
1957 | 0 | n=xmalloc_clear(sizeof(*n)); |
1958 | 0 | n->name=xmalloc(n1+1); |
1959 | |
|
1960 | 0 | memcpy(n->name,&p[8],n1); |
1961 | 0 | n->name[n1]='\0'; |
1962 | |
|
1963 | 0 | if(p[0]&0x80) |
1964 | | /* The value is human-readable. */ |
1965 | 0 | { |
1966 | 0 | n->value=xmalloc(n2+1); |
1967 | 0 | memcpy(n->value,&p[8+n1],n2); |
1968 | 0 | n->value[n2]='\0'; |
1969 | 0 | n->flags.human = 1; |
1970 | 0 | } |
1971 | 0 | else |
1972 | | /* Binary data. */ |
1973 | 0 | { |
1974 | 0 | n->bdat=xmalloc(n2); |
1975 | 0 | n->blen=n2; |
1976 | 0 | memcpy(n->bdat,&p[8+n1],n2); |
1977 | |
|
1978 | 0 | n->value = notation_value_to_human_readable_string (n); |
1979 | 0 | } |
1980 | |
|
1981 | 0 | n->flags.critical=crit; |
1982 | |
|
1983 | 0 | n->next=list; |
1984 | 0 | list=n; |
1985 | 0 | } |
1986 | |
|
1987 | 0 | return list; |
1988 | 0 | } |
1989 | | |
1990 | | |
1991 | | /* Return a list of notation data matching NAME. The caller needs to |
1992 | | * to free the list using free_notation. Other than sig_to_notation |
1993 | | * this function does not return the notation in human readable format |
1994 | | * but always returns the raw data. The human readable flag is set |
1995 | | * anyway set but .value is always NULL. */ |
1996 | | struct notation * |
1997 | | search_sig_notations (PKT_signature *sig, const char *name) |
1998 | 0 | { |
1999 | 0 | const byte *p; |
2000 | 0 | size_t len; |
2001 | 0 | int seq = 0; |
2002 | 0 | int crit; |
2003 | 0 | notation_t list = NULL; |
2004 | |
|
2005 | 0 | while((p=enum_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, &len, &seq, &crit))) |
2006 | 0 | { |
2007 | 0 | int n1,n2; |
2008 | 0 | struct notation *n=NULL; |
2009 | |
|
2010 | 0 | if (len < 8) |
2011 | 0 | { |
2012 | 0 | log_info (_("WARNING: invalid notation data found\n")); |
2013 | 0 | continue; |
2014 | 0 | } |
2015 | | |
2016 | | /* name length. */ |
2017 | 0 | n1=(p[4]<<8)|p[5]; |
2018 | | /* value length. */ |
2019 | 0 | n2=(p[6]<<8)|p[7]; |
2020 | |
|
2021 | 0 | if (8 + n1 + n2 != len) |
2022 | 0 | { |
2023 | 0 | log_info (_("WARNING: invalid notation data found\n")); |
2024 | 0 | continue; |
2025 | 0 | } |
2026 | | |
2027 | 0 | if (!name) |
2028 | 0 | ; /* Return everything. */ |
2029 | 0 | else if (n1 != strlen (name) || memcmp (p+8, name, n1)) |
2030 | 0 | continue; /* Not the requested name. */ |
2031 | | |
2032 | | |
2033 | 0 | n = xmalloc_clear (sizeof *n); |
2034 | 0 | n->name = xmalloc (n1+1); |
2035 | |
|
2036 | 0 | memcpy (n->name,p + 8, n1); |
2037 | 0 | n->name[n1]='\0'; |
2038 | | |
2039 | | /* In any case append a Nul. */ |
2040 | 0 | n->bdat = xmalloc (n2+1); |
2041 | 0 | memcpy (n->bdat, p + 8 + n1, n2); |
2042 | 0 | n->bdat[n2] = '\0'; |
2043 | 0 | n->blen = n2; |
2044 | 0 | n->flags.human = !!(p[0] & 0x80); |
2045 | |
|
2046 | 0 | n->flags.critical = crit; |
2047 | |
|
2048 | 0 | n->next = list; |
2049 | 0 | list = n; |
2050 | 0 | } |
2051 | |
|
2052 | 0 | return list; |
2053 | 0 | } |
2054 | | |
2055 | | |
2056 | | /* Release the resources associated with the *list* of notations. To |
2057 | | release a single notation, make sure that notation->next is |
2058 | | NULL. */ |
2059 | | void |
2060 | | free_notation(struct notation *notation) |
2061 | 0 | { |
2062 | 0 | while(notation) |
2063 | 0 | { |
2064 | 0 | struct notation *n=notation; |
2065 | |
|
2066 | 0 | xfree(n->name); |
2067 | 0 | xfree(n->value); |
2068 | 0 | xfree(n->altvalue); |
2069 | 0 | xfree(n->bdat); |
2070 | 0 | notation=n->next; |
2071 | 0 | xfree(n); |
2072 | 0 | } |
2073 | 0 | } |
2074 | | |
2075 | | /* Serialize the signature packet (RFC 4880, Section 5.2) described by |
2076 | | SIG and write it to OUT. */ |
2077 | | static int |
2078 | | do_signature( IOBUF out, int ctb, PKT_signature *sig ) |
2079 | 0 | { |
2080 | 0 | int rc = 0; |
2081 | 0 | int n, i; |
2082 | 0 | IOBUF a = iobuf_temp(); |
2083 | |
|
2084 | 0 | log_assert (ctb_pkttype (ctb) == PKT_SIGNATURE); |
2085 | | |
2086 | 0 | if ( !sig->version || sig->version == 3) |
2087 | 0 | { |
2088 | 0 | iobuf_put( a, 3 ); |
2089 | | |
2090 | | /* Version 3 packets don't support subpackets. Actually we |
2091 | | * should never get to here but real life is different and thus |
2092 | | * we now use a log_fatal instead of a log_assert here. */ |
2093 | 0 | if (sig->hashed || sig->unhashed) |
2094 | 0 | log_fatal ("trying to write a subpacket to a v3 signature (%d,%d)\n", |
2095 | 0 | !!sig->hashed, !!sig->unhashed); |
2096 | 0 | } |
2097 | 0 | else |
2098 | 0 | iobuf_put( a, sig->version ); |
2099 | 0 | if ( sig->version < 4 ) |
2100 | 0 | iobuf_put (a, 5 ); /* Constant used by pre-v4 signatures. */ |
2101 | 0 | iobuf_put (a, sig->sig_class ); |
2102 | 0 | if ( sig->version < 4 ) |
2103 | 0 | { |
2104 | 0 | write_32(a, sig->timestamp ); |
2105 | 0 | write_32(a, sig->keyid[0] ); |
2106 | 0 | write_32(a, sig->keyid[1] ); |
2107 | 0 | } |
2108 | 0 | iobuf_put(a, sig->pubkey_algo ); |
2109 | 0 | iobuf_put(a, sig->digest_algo ); |
2110 | 0 | if ( sig->version >= 4 ) |
2111 | 0 | { |
2112 | 0 | size_t nn; |
2113 | | /* Timestamp and keyid must have been packed into the subpackets |
2114 | | prior to the call of this function, because these subpackets |
2115 | | are hashed. */ |
2116 | 0 | nn = sig->hashed? sig->hashed->len : 0; |
2117 | 0 | if (sig->version >= 6) |
2118 | 0 | write_32(a, nn); |
2119 | 0 | else |
2120 | 0 | write_16(a, nn); |
2121 | 0 | if (nn) |
2122 | 0 | iobuf_write( a, sig->hashed->data, nn ); |
2123 | 0 | nn = sig->unhashed? sig->unhashed->len : 0; |
2124 | 0 | if (sig->version >= 6) |
2125 | 0 | write_32(a, nn); |
2126 | 0 | else |
2127 | 0 | write_16(a, nn); |
2128 | 0 | if (nn) |
2129 | 0 | iobuf_write( a, sig->unhashed->data, nn ); |
2130 | 0 | } |
2131 | 0 | iobuf_put(a, sig->digest_start[0] ); |
2132 | 0 | iobuf_put(a, sig->digest_start[1] ); |
2133 | |
|
2134 | 0 | if (sig->version == 6) |
2135 | 0 | { |
2136 | 0 | if (sig->salt) |
2137 | 0 | { |
2138 | 0 | rc = gpg_mpi_write_opaque_8 (a, sig->salt, NULL); |
2139 | 0 | if (rc) |
2140 | 0 | goto leave; |
2141 | 0 | } |
2142 | 0 | else |
2143 | 0 | iobuf_put (a, 0); |
2144 | 0 | } |
2145 | | |
2146 | 0 | n = pubkey_get_nsig( sig->pubkey_algo ); |
2147 | 0 | if ( !n ) |
2148 | 0 | write_fake_data( a, sig->data[0] ); |
2149 | 0 | if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA |
2150 | 0 | || sig->pubkey_algo == PUBKEY_ALGO_EDDSA) |
2151 | 0 | { |
2152 | 0 | for (i=0; i < n && !rc ; i++ ) |
2153 | 0 | rc = sos_write (a, sig->data[i], NULL); |
2154 | 0 | } |
2155 | 0 | else if (sig->pubkey_algo == PUBKEY_ALGO_ED25519) |
2156 | 0 | { |
2157 | 0 | rc = gpg_mpi_write_opaque_nohdr (a, sig->data[0]); |
2158 | 0 | } |
2159 | 0 | else |
2160 | 0 | { |
2161 | 0 | for (i=0; i < n && !rc ; i++ ) |
2162 | 0 | rc = gpg_mpi_write (a, sig->data[i], NULL); |
2163 | 0 | } |
2164 | |
|
2165 | 0 | if (!rc) |
2166 | 0 | { |
2167 | 0 | if ( is_RSA(sig->pubkey_algo) && sig->version < 4 ) |
2168 | 0 | write_sign_packet_header(out, ctb, iobuf_get_temp_length(a) ); |
2169 | 0 | else if (sig->version == 6) |
2170 | 0 | write_new_header (out, (0xc0|(ctb_pkttype(ctb)&0x3f)), |
2171 | 0 | iobuf_get_temp_length (a), 0); |
2172 | 0 | else |
2173 | 0 | write_header(out, ctb, iobuf_get_temp_length(a) ); |
2174 | 0 | rc = iobuf_write_temp( out, a ); |
2175 | 0 | } |
2176 | |
|
2177 | 0 | leave: |
2178 | 0 | iobuf_close(a); |
2179 | 0 | return rc; |
2180 | 0 | } |
2181 | | |
2182 | | |
2183 | | /* Serialize the one-pass signature packet (RFC 4880, Section 5.4) |
2184 | | described by OPS and write it to OUT. */ |
2185 | | static int |
2186 | | do_onepass_sig( IOBUF out, int ctb, PKT_onepass_sig *ops ) |
2187 | 0 | { |
2188 | 0 | log_assert (ctb_pkttype (ctb) == PKT_ONEPASS_SIG); |
2189 | | |
2190 | 0 | write_header(out, ctb, 4 + 8 + 1); |
2191 | |
|
2192 | 0 | iobuf_put (out, 3); /* Version. */ |
2193 | 0 | iobuf_put(out, ops->sig_class ); |
2194 | 0 | iobuf_put(out, ops->digest_algo ); |
2195 | 0 | iobuf_put(out, ops->pubkey_algo ); |
2196 | 0 | write_32(out, ops->keyid[0] ); |
2197 | 0 | write_32(out, ops->keyid[1] ); |
2198 | 0 | iobuf_put(out, ops->last ); |
2199 | |
|
2200 | 0 | return 0; |
2201 | 0 | } |
2202 | | |
2203 | | |
2204 | | /* Write a 16-bit quantity to OUT in big endian order. */ |
2205 | | static int |
2206 | | write_16(IOBUF out, u16 a) |
2207 | 0 | { |
2208 | 0 | iobuf_put(out, a>>8); |
2209 | 0 | if( iobuf_put(out,a) ) |
2210 | 0 | return -1; |
2211 | 0 | return 0; |
2212 | 0 | } |
2213 | | |
2214 | | /* Write a 32-bit quantity to OUT in big endian order. */ |
2215 | | static int |
2216 | | write_32(IOBUF out, u32 a) |
2217 | 0 | { |
2218 | 0 | iobuf_put(out, a>> 24); |
2219 | 0 | iobuf_put(out, a>> 16); |
2220 | 0 | iobuf_put(out, a>> 8); |
2221 | 0 | return iobuf_put(out, a); |
2222 | 0 | } |
2223 | | |
2224 | | |
2225 | | /**************** |
2226 | | * calculate the length of a header. |
2227 | | * |
2228 | | * LEN is the length of the packet's body. NEW_CTB is whether we are |
2229 | | * using a new or old format packet. |
2230 | | * |
2231 | | * This function does not handle indeterminate lengths or partial body |
2232 | | * lengths. (If you pass LEN as 0, then this function assumes you |
2233 | | * really mean an empty body.) |
2234 | | */ |
2235 | | static int |
2236 | | calc_header_length( u32 len, int new_ctb ) |
2237 | 0 | { |
2238 | 0 | if( new_ctb ) { |
2239 | 0 | if( len < 192 ) |
2240 | 0 | return 2; |
2241 | 0 | if( len < 8384 ) |
2242 | 0 | return 3; |
2243 | 0 | else |
2244 | 0 | return 6; |
2245 | 0 | } |
2246 | 0 | if( len < 256 ) |
2247 | 0 | return 2; |
2248 | 0 | if( len < 65536 ) |
2249 | 0 | return 3; |
2250 | | |
2251 | 0 | return 5; |
2252 | 0 | } |
2253 | | |
2254 | | /**************** |
2255 | | * Write the CTB and the packet length |
2256 | | */ |
2257 | | static int |
2258 | | write_header( IOBUF out, int ctb, u32 len ) |
2259 | 0 | { |
2260 | 0 | return write_header2( out, ctb, len, 0 ); |
2261 | 0 | } |
2262 | | |
2263 | | |
2264 | | static int |
2265 | | write_sign_packet_header (IOBUF out, int ctb, u32 len) |
2266 | 0 | { |
2267 | 0 | (void)ctb; |
2268 | | |
2269 | | /* Work around a bug in the pgp read function for signature packets, |
2270 | | which are not correctly coded and silently assume at some point 2 |
2271 | | byte length headers.*/ |
2272 | 0 | iobuf_put (out, 0x89 ); |
2273 | 0 | iobuf_put (out, len >> 8 ); |
2274 | 0 | return iobuf_put (out, len) == -1 ? -1:0; |
2275 | 0 | } |
2276 | | |
2277 | | /**************** |
2278 | | * Write a packet header to OUT. |
2279 | | * |
2280 | | * CTB is the ctb. It determines whether a new or old format packet |
2281 | | * header should be written. The length field is adjusted, but the |
2282 | | * CTB is otherwise written out as is. |
2283 | | * |
2284 | | * LEN is the length of the packet's body. |
2285 | | * |
2286 | | * If HDRLEN is set, then we don't necessarily use the most efficient |
2287 | | * encoding to store LEN, but the specified length. (If this is not |
2288 | | * possible, this is a bug.) In this case, LEN=0 means a 0 length |
2289 | | * packet. Note: setting HDRLEN is only supported for old format |
2290 | | * packets! |
2291 | | * |
2292 | | * If HDRLEN is not set, then the shortest encoding is used. In this |
2293 | | * case, LEN=0 means the body has an indeterminate length and a |
2294 | | * partial body length header (if a new format packet) or an |
2295 | | * indeterminate length header (if an old format packet) is written |
2296 | | * out. Further, if using partial body lengths, this enables partial |
2297 | | * body length mode on OUT. |
2298 | | */ |
2299 | | static int |
2300 | | write_header2( IOBUF out, int ctb, u32 len, int hdrlen ) |
2301 | 0 | { |
2302 | 0 | if (ctb_new_format_p (ctb)) |
2303 | 0 | return write_new_header( out, ctb, len, hdrlen ); |
2304 | | |
2305 | | /* An old format packet. Refer to RFC 4880, Section 4.2.1 to |
2306 | | understand how lengths are encoded in this case. */ |
2307 | | |
2308 | | /* The length encoding is stored in the two least significant bits. |
2309 | | Make sure they are cleared. */ |
2310 | 0 | log_assert ((ctb & 3) == 0); |
2311 | | |
2312 | 0 | log_assert (hdrlen == 0 || hdrlen == 2 || hdrlen == 3 || hdrlen == 5); |
2313 | | |
2314 | 0 | if (hdrlen) |
2315 | | /* Header length is given. */ |
2316 | 0 | { |
2317 | 0 | if( hdrlen == 2 && len < 256 ) |
2318 | | /* 00 => 1 byte length. */ |
2319 | 0 | ; |
2320 | 0 | else if( hdrlen == 3 && len < 65536 ) |
2321 | | /* 01 => 2 byte length. If len < 256, this is not the most |
2322 | | compact encoding, but it is a correct encoding. */ |
2323 | 0 | ctb |= 1; |
2324 | 0 | else if (hdrlen == 5) |
2325 | | /* 10 => 4 byte length. If len < 65536, this is not the most |
2326 | | compact encoding, but it is a correct encoding. */ |
2327 | 0 | ctb |= 2; |
2328 | 0 | else |
2329 | 0 | log_bug ("Can't encode length=%d in a %d byte header!\n", |
2330 | 0 | len, hdrlen); |
2331 | 0 | } |
2332 | 0 | else |
2333 | 0 | { |
2334 | 0 | if( !len ) |
2335 | | /* 11 => Indeterminate length. */ |
2336 | 0 | ctb |= 3; |
2337 | 0 | else if( len < 256 ) |
2338 | | /* 00 => 1 byte length. */ |
2339 | 0 | ; |
2340 | 0 | else if( len < 65536 ) |
2341 | | /* 01 => 2 byte length. */ |
2342 | 0 | ctb |= 1; |
2343 | 0 | else |
2344 | | /* 10 => 4 byte length. */ |
2345 | 0 | ctb |= 2; |
2346 | 0 | } |
2347 | | |
2348 | 0 | if( iobuf_put(out, ctb ) ) |
2349 | 0 | return -1; |
2350 | | |
2351 | 0 | if( len || hdrlen ) |
2352 | 0 | { |
2353 | 0 | if( ctb & 2 ) |
2354 | 0 | { |
2355 | 0 | if(iobuf_put(out, len >> 24 )) |
2356 | 0 | return -1; |
2357 | 0 | if(iobuf_put(out, len >> 16 )) |
2358 | 0 | return -1; |
2359 | 0 | } |
2360 | | |
2361 | 0 | if( ctb & 3 ) |
2362 | 0 | if(iobuf_put(out, len >> 8 )) |
2363 | 0 | return -1; |
2364 | | |
2365 | 0 | if( iobuf_put(out, len ) ) |
2366 | 0 | return -1; |
2367 | 0 | } |
2368 | | |
2369 | 0 | return 0; |
2370 | 0 | } |
2371 | | |
2372 | | |
2373 | | /* Write a new format header to OUT. |
2374 | | |
2375 | | CTB is the ctb. |
2376 | | |
2377 | | LEN is the length of the packet's body. If LEN is 0, then enables |
2378 | | partial body length mode (i.e., the body is of an indeterminant |
2379 | | length) on OUT. Note: this function cannot be used to generate a |
2380 | | header for a zero length packet. |
2381 | | |
2382 | | HDRLEN is the length of the packet's header. If HDRLEN is 0, the |
2383 | | shortest encoding is chosen based on the length of the packet's |
2384 | | body. Currently, values other than 0 are not supported. |
2385 | | |
2386 | | Returns 0 on success. */ |
2387 | | static int |
2388 | | write_new_header( IOBUF out, int ctb, u32 len, int hdrlen ) |
2389 | 0 | { |
2390 | 0 | if( hdrlen ) |
2391 | 0 | log_bug("can't cope with hdrlen yet\n"); |
2392 | | |
2393 | 0 | if( iobuf_put(out, ctb ) ) |
2394 | 0 | return -1; |
2395 | 0 | if( !len ) { |
2396 | 0 | iobuf_set_partial_body_length_mode(out, 512 ); |
2397 | 0 | } |
2398 | 0 | else { |
2399 | 0 | if( len < 192 ) { |
2400 | 0 | if( iobuf_put(out, len ) ) |
2401 | 0 | return -1; |
2402 | 0 | } |
2403 | 0 | else if( len < 8384 ) { |
2404 | 0 | len -= 192; |
2405 | 0 | if( iobuf_put( out, (len / 256) + 192) ) |
2406 | 0 | return -1; |
2407 | 0 | if( iobuf_put( out, (len % 256) ) ) |
2408 | 0 | return -1; |
2409 | 0 | } |
2410 | 0 | else { |
2411 | 0 | if( iobuf_put( out, 0xff ) ) |
2412 | 0 | return -1; |
2413 | 0 | if( iobuf_put( out, (len >> 24)&0xff ) ) |
2414 | 0 | return -1; |
2415 | 0 | if( iobuf_put( out, (len >> 16)&0xff ) ) |
2416 | 0 | return -1; |
2417 | 0 | if( iobuf_put( out, (len >> 8)&0xff ) ) |
2418 | 0 | return -1; |
2419 | 0 | if( iobuf_put( out, len & 0xff ) ) |
2420 | 0 | return -1; |
2421 | 0 | } |
2422 | 0 | } |
2423 | 0 | return 0; |
2424 | 0 | } |