/src/gnupg/kbx/keybox-openpgp.c
Line | Count | Source |
1 | | /* keybox-openpgp.c - OpenPGP key parsing |
2 | | * Copyright (C) 2001, 2003, 2011 Free Software Foundation, Inc. |
3 | | * |
4 | | * This file is part of GnuPG. |
5 | | * |
6 | | * GnuPG is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * GnuPG is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | /* This is a simple OpenPGP parser suitable for all OpenPGP key |
21 | | material. It just provides the functionality required to build and |
22 | | parse an KBX OpenPGP key blob. Thus it is not a complete parser. |
23 | | However it is self-contained and optimized for fast in-memory |
24 | | parsing. Note that we don't support old ElGamal v3 keys |
25 | | anymore. */ |
26 | | |
27 | | #include <config.h> |
28 | | #include <stdlib.h> |
29 | | #include <stdio.h> |
30 | | #include <string.h> |
31 | | #include <errno.h> |
32 | | #include <assert.h> |
33 | | |
34 | | #include "keybox-defs.h" |
35 | | |
36 | | #include <gcrypt.h> |
37 | | |
38 | | #include "../common/openpgpdefs.h" |
39 | | #include "../common/host2net.h" |
40 | | |
41 | | struct keyparm_s |
42 | | { |
43 | | const char *mpi; |
44 | | int len; /* int to avoid a cast in gcry_sexp_build. */ |
45 | | }; |
46 | | |
47 | | |
48 | | /* Assume a valid OpenPGP packet at the address pointed to by BUFBTR |
49 | | which has a maximum length as stored at BUFLEN. Return the header |
50 | | information of that packet and advance the pointer stored at BUFPTR |
51 | | to the next packet; also adjust the length stored at BUFLEN to |
52 | | match the remaining bytes. If there are no more packets, store NULL |
53 | | at BUFPTR. Return an non-zero error code on failure or the |
54 | | following data on success: |
55 | | |
56 | | R_DATAPKT = Pointer to the begin of the packet data. |
57 | | R_DATALEN = Length of this data. This has already been checked to fit |
58 | | into the buffer. |
59 | | R_PKTTYPE = The packet type. |
60 | | R_NTOTAL = The total number of bytes of this packet |
61 | | |
62 | | Note that these values are only updated on success. |
63 | | */ |
64 | | static gpg_error_t |
65 | | next_packet (unsigned char const **bufptr, size_t *buflen, |
66 | | unsigned char const **r_data, size_t *r_datalen, int *r_pkttype, |
67 | | size_t *r_ntotal) |
68 | 0 | { |
69 | 0 | const unsigned char *buf = *bufptr; |
70 | 0 | size_t len = *buflen; |
71 | 0 | int c, ctb, pkttype; |
72 | 0 | unsigned long pktlen; |
73 | |
|
74 | 0 | if (!len) |
75 | 0 | return gpg_error (GPG_ERR_NO_DATA); |
76 | | |
77 | 0 | ctb = *buf++; len--; |
78 | 0 | if ( !(ctb & 0x80) ) |
79 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* Invalid CTB. */ |
80 | | |
81 | 0 | if ((ctb & 0x40)) /* New style (OpenPGP) CTB. */ |
82 | 0 | { |
83 | 0 | pkttype = (ctb & 0x3f); |
84 | 0 | if (!len) |
85 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* No 1st length byte. */ |
86 | 0 | c = *buf++; len--; |
87 | 0 | if (pkttype == PKT_COMPRESSED) |
88 | 0 | return gpg_error (GPG_ERR_UNEXPECTED); /* ... packet in a keyblock. */ |
89 | 0 | if ( c < 192 ) |
90 | 0 | pktlen = c; |
91 | 0 | else if ( c < 224 ) |
92 | 0 | { |
93 | 0 | pktlen = (c - 192) * 256; |
94 | 0 | if (!len) |
95 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* No 2nd length byte. */ |
96 | 0 | c = *buf++; len--; |
97 | 0 | pktlen += c + 192; |
98 | 0 | } |
99 | 0 | else if (c == 255) |
100 | 0 | { |
101 | 0 | if (len <4 ) |
102 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* No length bytes. */ |
103 | 0 | pktlen = buf32_to_ulong (buf); |
104 | 0 | buf += 4; |
105 | 0 | len -= 4; |
106 | 0 | } |
107 | 0 | else /* Partial length encoding is not allowed for key packets. */ |
108 | 0 | return gpg_error (GPG_ERR_UNEXPECTED); |
109 | 0 | } |
110 | 0 | else /* Old style CTB. */ |
111 | 0 | { |
112 | 0 | int lenbytes; |
113 | |
|
114 | 0 | pktlen = 0; |
115 | 0 | pkttype = (ctb>>2)&0xf; |
116 | 0 | lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3)); |
117 | 0 | if (!lenbytes) /* Not allowed in key packets. */ |
118 | 0 | return gpg_error (GPG_ERR_UNEXPECTED); |
119 | 0 | if (len < lenbytes) |
120 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* Not enough length bytes. */ |
121 | 0 | for (; lenbytes; lenbytes--) |
122 | 0 | { |
123 | 0 | pktlen <<= 8; |
124 | 0 | pktlen |= *buf++; len--; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | | /* Do some basic sanity check. */ |
129 | 0 | switch (pkttype) |
130 | 0 | { |
131 | 0 | case PKT_SIGNATURE: |
132 | 0 | case PKT_SECRET_KEY: |
133 | 0 | case PKT_PUBLIC_KEY: |
134 | 0 | case PKT_SECRET_SUBKEY: |
135 | 0 | case PKT_MARKER: |
136 | 0 | case PKT_RING_TRUST: |
137 | 0 | case PKT_USER_ID: |
138 | 0 | case PKT_PUBLIC_SUBKEY: |
139 | 0 | case PKT_OLD_COMMENT: |
140 | 0 | case PKT_ATTRIBUTE: |
141 | 0 | case PKT_COMMENT: |
142 | 0 | case PKT_GPG_CONTROL: |
143 | 0 | break; /* Okay these are allowed packets. */ |
144 | 0 | default: |
145 | 0 | return gpg_error (GPG_ERR_UNEXPECTED); |
146 | 0 | } |
147 | | |
148 | 0 | if (pkttype == 63 && pktlen == 0xFFFFFFFF) |
149 | | /* Sometimes the decompressing layer enters an error state in |
150 | | which it simply outputs 0xff for every byte read. If we have a |
151 | | stream of 0xff bytes, then it will be detected as a new format |
152 | | packet with type 63 and a 4-byte encoded length that is 4G-1. |
153 | | Since packets with type 63 are private and we use them as a |
154 | | control packet, which won't be 4 GB, we reject such packets as |
155 | | invalid. */ |
156 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
157 | | |
158 | 0 | if (pktlen > len) |
159 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* Packet length header too long. */ |
160 | | |
161 | 0 | *r_data = buf; |
162 | 0 | *r_datalen = pktlen; |
163 | 0 | *r_pkttype = pkttype; |
164 | 0 | *r_ntotal = (buf - *bufptr) + pktlen; |
165 | |
|
166 | 0 | *bufptr = buf + pktlen; |
167 | 0 | *buflen = len - pktlen; |
168 | 0 | if (!*buflen) |
169 | 0 | *bufptr = NULL; |
170 | |
|
171 | 0 | return 0; |
172 | 0 | } |
173 | | |
174 | | |
175 | | /* Take a list of key parameters KP for the OpenPGP ALGO and compute |
176 | | * the keygrip which will be stored at GRIP. GRIP needs to be a |
177 | | * buffer of 20 bytes. */ |
178 | | static gpg_error_t |
179 | | keygrip_from_keyparm (int algo, struct keyparm_s *kp, unsigned char *grip) |
180 | 0 | { |
181 | 0 | gpg_error_t err; |
182 | 0 | gcry_sexp_t s_pkey = NULL; |
183 | |
|
184 | 0 | switch (algo) |
185 | 0 | { |
186 | 0 | case PUBKEY_ALGO_DSA: |
187 | 0 | err = gcry_sexp_build (&s_pkey, NULL, |
188 | 0 | "(public-key(dsa(p%b)(q%b)(g%b)(y%b)))", |
189 | 0 | kp[0].len, kp[0].mpi, |
190 | 0 | kp[1].len, kp[1].mpi, |
191 | 0 | kp[2].len, kp[2].mpi, |
192 | 0 | kp[3].len, kp[3].mpi); |
193 | 0 | break; |
194 | | |
195 | 0 | case PUBKEY_ALGO_ELGAMAL: |
196 | 0 | case PUBKEY_ALGO_ELGAMAL_E: |
197 | 0 | err = gcry_sexp_build (&s_pkey, NULL, |
198 | 0 | "(public-key(elg(p%b)(g%b)(y%b)))", |
199 | 0 | kp[0].len, kp[0].mpi, |
200 | 0 | kp[1].len, kp[1].mpi, |
201 | 0 | kp[2].len, kp[2].mpi); |
202 | 0 | break; |
203 | | |
204 | 0 | case PUBKEY_ALGO_RSA: |
205 | 0 | case PUBKEY_ALGO_RSA_S: |
206 | 0 | case PUBKEY_ALGO_RSA_E: |
207 | 0 | err = gcry_sexp_build (&s_pkey, NULL, |
208 | 0 | "(public-key(rsa(n%b)(e%b)))", |
209 | 0 | kp[0].len, kp[0].mpi, |
210 | 0 | kp[1].len, kp[1].mpi); |
211 | 0 | break; |
212 | | |
213 | 0 | case PUBKEY_ALGO_EDDSA: |
214 | 0 | case PUBKEY_ALGO_ECDSA: |
215 | 0 | case PUBKEY_ALGO_ECDH: |
216 | 0 | { |
217 | 0 | char *curve = openpgp_oidbuf_to_str (kp[0].mpi, kp[0].len); |
218 | 0 | if (!curve) |
219 | 0 | err = gpg_error_from_syserror (); |
220 | 0 | else |
221 | 0 | { |
222 | 0 | err = gcry_sexp_build |
223 | 0 | (&s_pkey, NULL, |
224 | 0 | (algo == PUBKEY_ALGO_EDDSA)? |
225 | 0 | "(public-key(ecc(curve%s)(flags eddsa)(q%b)))": |
226 | 0 | (algo == PUBKEY_ALGO_ECDH |
227 | 0 | && openpgp_oidbuf_is_cv25519 (kp[0].mpi, kp[0].len))? |
228 | 0 | "(public-key(ecc(curve%s)(flags djb-tweak)(q%b)))": |
229 | 0 | "(public-key(ecc(curve%s)(q%b)))", |
230 | 0 | curve, kp[1].len, kp[1].mpi); |
231 | 0 | xfree (curve); |
232 | 0 | } |
233 | 0 | } |
234 | 0 | break; |
235 | | |
236 | 0 | case PUBKEY_ALGO_KYBER: |
237 | | /* There is no space in the BLOB for a second grip, thus for now |
238 | | * we store only the ECC keygrip. */ |
239 | 0 | { |
240 | 0 | char *curve = openpgp_oidbuf_to_str (kp[0].mpi, kp[0].len); |
241 | 0 | if (!curve) |
242 | 0 | err = gpg_error_from_syserror (); |
243 | 0 | else |
244 | 0 | { |
245 | 0 | err = gcry_sexp_build |
246 | 0 | (&s_pkey, NULL, |
247 | 0 | openpgp_oidbuf_is_cv25519 (kp[0].mpi, kp[0].len) |
248 | 0 | ?"(public-key(ecc(curve%s)(flags djb-tweak)(q%b)))" |
249 | 0 | : "(public-key(ecc(curve%s)(q%b)))", |
250 | 0 | curve, kp[1].len, kp[1].mpi); |
251 | 0 | xfree (curve); |
252 | 0 | } |
253 | 0 | } |
254 | 0 | break; |
255 | | |
256 | 0 | default: |
257 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); |
258 | 0 | break; |
259 | 0 | } |
260 | | |
261 | 0 | if (!err && !gcry_pk_get_keygrip (s_pkey, grip)) |
262 | 0 | { |
263 | | /* Some Linux distributions remove certain curves from Libgcrypt |
264 | | * but not from GnuPG and thus the keygrip can't be computed. |
265 | | * Emit a better error message for this case. */ |
266 | 0 | if (!gcry_pk_get_curve (s_pkey, 0, NULL)) |
267 | 0 | err = gpg_error (GPG_ERR_UNKNOWN_CURVE); |
268 | 0 | else |
269 | 0 | { |
270 | 0 | log_info ("kbx: error computing keygrip\n"); |
271 | 0 | err = gpg_error (GPG_ERR_GENERAL); |
272 | 0 | } |
273 | 0 | } |
274 | |
|
275 | 0 | gcry_sexp_release (s_pkey); |
276 | |
|
277 | 0 | if (err) |
278 | 0 | memset (grip, 0, 20); |
279 | 0 | return err; |
280 | 0 | } |
281 | | |
282 | | |
283 | | /* Parse a key packet and store the information in KI. */ |
284 | | static gpg_error_t |
285 | | parse_key (const unsigned char *data, size_t datalen, |
286 | | struct _keybox_openpgp_key_info *ki) |
287 | 0 | { |
288 | 0 | gpg_error_t err; |
289 | 0 | const unsigned char *data_start = data; |
290 | 0 | int i, version, algorithm; |
291 | 0 | size_t n; |
292 | 0 | int npkey; |
293 | 0 | unsigned char hashbuffer[768]; |
294 | 0 | gcry_md_hd_t md; |
295 | 0 | int is_ecc = 0; |
296 | 0 | int is_kyber = 0; |
297 | 0 | int is_v5; |
298 | | /* unsigned int pkbytes; for v5: # of octets of the public key params. */ |
299 | 0 | struct keyparm_s keyparm[OPENPGP_MAX_NPKEY]; |
300 | 0 | unsigned char *helpmpibuf[OPENPGP_MAX_NPKEY] = { NULL }; |
301 | |
|
302 | 0 | if (datalen < 5) |
303 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
304 | 0 | version = *data++; datalen--; |
305 | 0 | if (version < 2 || version > 5 ) |
306 | 0 | return gpg_error (GPG_ERR_INV_PACKET); /* Invalid version. */ |
307 | 0 | is_v5 = version == 5; |
308 | | |
309 | | /*timestamp = ((data[0]<<24)|(data[1]<<16)|(data[2]<<8)|(data[3]));*/ |
310 | 0 | data +=4; datalen -=4; |
311 | |
|
312 | 0 | if (version < 4) |
313 | 0 | { |
314 | 0 | if (datalen < 2) |
315 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
316 | 0 | data +=2; datalen -= 2; |
317 | 0 | } |
318 | | |
319 | 0 | if (!datalen) |
320 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
321 | 0 | algorithm = *data++; datalen--; |
322 | |
|
323 | 0 | if (is_v5) |
324 | 0 | { |
325 | 0 | if (datalen < 4) |
326 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
327 | | /* pkbytes = buf32_to_uint (data); */ |
328 | 0 | data += 4; |
329 | 0 | datalen -= 4; |
330 | 0 | } |
331 | | |
332 | 0 | switch (algorithm) |
333 | 0 | { |
334 | 0 | case PUBKEY_ALGO_RSA: |
335 | 0 | case PUBKEY_ALGO_RSA_E: |
336 | 0 | case PUBKEY_ALGO_RSA_S: |
337 | 0 | npkey = 2; |
338 | 0 | break; |
339 | 0 | case PUBKEY_ALGO_ELGAMAL_E: |
340 | 0 | case PUBKEY_ALGO_ELGAMAL: |
341 | 0 | npkey = 3; |
342 | 0 | break; |
343 | 0 | case PUBKEY_ALGO_DSA: |
344 | 0 | npkey = 4; |
345 | 0 | break; |
346 | 0 | case PUBKEY_ALGO_ECDH: |
347 | 0 | npkey = 3; |
348 | 0 | is_ecc = 1; |
349 | 0 | break; |
350 | 0 | case PUBKEY_ALGO_ECDSA: |
351 | 0 | case PUBKEY_ALGO_EDDSA: |
352 | 0 | npkey = 2; |
353 | 0 | is_ecc = 1; |
354 | 0 | break; |
355 | 0 | case PUBKEY_ALGO_KYBER: |
356 | 0 | npkey = 3; |
357 | 0 | is_kyber = 1; |
358 | 0 | break; |
359 | 0 | default: /* Unknown algorithm. */ |
360 | 0 | return gpg_error (GPG_ERR_UNKNOWN_ALGORITHM); |
361 | 0 | } |
362 | | |
363 | 0 | ki->version = version; |
364 | 0 | ki->algo = algorithm; |
365 | |
|
366 | 0 | for (i=0; i < npkey; i++ ) |
367 | 0 | { |
368 | 0 | unsigned int nbits, nbytes; |
369 | |
|
370 | 0 | if (datalen < 2) |
371 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
372 | | |
373 | 0 | if ((is_ecc && (i == 0 || i == 2)) |
374 | 0 | || (is_kyber && i == 0 )) |
375 | 0 | { |
376 | 0 | nbytes = data[0]; |
377 | 0 | if (nbytes < 2 || nbytes > 254) |
378 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
379 | 0 | nbytes++; /* The size byte itself. */ |
380 | 0 | if (datalen < nbytes) |
381 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
382 | | |
383 | 0 | keyparm[i].mpi = data; |
384 | 0 | keyparm[i].len = nbytes; |
385 | 0 | } |
386 | 0 | else if (is_kyber && i == 2) |
387 | 0 | { |
388 | 0 | if (datalen < 4) |
389 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
390 | 0 | nbytes = ((data[0]<<24)|(data[1]<<16)|(data[2]<<8)|(data[3])); |
391 | 0 | data += 4; |
392 | 0 | datalen -= 4; |
393 | | /* (for the limit see also MAX_EXTERN_MPI_BITS in g10/gpg.h) */ |
394 | 0 | if (datalen < nbytes || nbytes > (32768*8)) |
395 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
396 | | |
397 | 0 | keyparm[i].mpi = data; |
398 | 0 | keyparm[i].len = nbytes; |
399 | 0 | } |
400 | 0 | else |
401 | 0 | { |
402 | 0 | nbits = ((data[0]<<8)|(data[1])); |
403 | 0 | data += 2; |
404 | 0 | datalen -= 2; |
405 | 0 | nbytes = (nbits+7) / 8; |
406 | 0 | if (datalen < nbytes) |
407 | 0 | return gpg_error (GPG_ERR_INV_PACKET); |
408 | | |
409 | 0 | keyparm[i].mpi = data; |
410 | 0 | keyparm[i].len = nbytes; |
411 | 0 | } |
412 | | |
413 | 0 | data += nbytes; datalen -= nbytes; |
414 | 0 | } |
415 | 0 | n = data - data_start; |
416 | | |
417 | | |
418 | | /* Note: Starting here we need to jump to leave on error. */ |
419 | | |
420 | | /* For non-ECC, make sure the MPIs are unsigned. */ |
421 | 0 | if (!is_ecc && !is_kyber) |
422 | 0 | for (i=0; i < npkey; i++) |
423 | 0 | { |
424 | 0 | if (!keyparm[i].len || (keyparm[i].mpi[0] & 0x80)) |
425 | 0 | { |
426 | 0 | helpmpibuf[i] = xtrymalloc (1+keyparm[i].len); |
427 | 0 | if (!helpmpibuf[i]) |
428 | 0 | { |
429 | 0 | err = gpg_error_from_syserror (); |
430 | 0 | goto leave; |
431 | 0 | } |
432 | 0 | helpmpibuf[i][0] = 0; |
433 | 0 | memcpy (helpmpibuf[i]+1, keyparm[i].mpi, keyparm[i].len); |
434 | 0 | keyparm[i].mpi = helpmpibuf[i]; |
435 | 0 | keyparm[i].len++; |
436 | 0 | } |
437 | 0 | } |
438 | | |
439 | 0 | err = keygrip_from_keyparm (algorithm, keyparm, ki->grip); |
440 | 0 | if (err) |
441 | 0 | goto leave; |
442 | | |
443 | 0 | if (version < 4) |
444 | 0 | { |
445 | | /* We do not support any other algorithm than RSA in v3 |
446 | | packets. */ |
447 | 0 | if (algorithm < 1 || algorithm > 3) |
448 | 0 | return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM); |
449 | | |
450 | 0 | err = gcry_md_open (&md, GCRY_MD_MD5, 0); |
451 | 0 | if (err) |
452 | 0 | return err; /* Oops */ |
453 | 0 | gcry_md_write (md, keyparm[0].mpi, keyparm[0].len); |
454 | 0 | gcry_md_write (md, keyparm[1].mpi, keyparm[1].len); |
455 | 0 | memcpy (ki->fpr, gcry_md_read (md, 0), 16); |
456 | 0 | gcry_md_close (md); |
457 | 0 | ki->fprlen = 16; |
458 | |
|
459 | 0 | if (keyparm[0].len < 8) |
460 | 0 | { |
461 | | /* Moduli less than 64 bit are out of the specs scope. Zero |
462 | | them out because this is what gpg does too. */ |
463 | 0 | memset (ki->keyid, 0, 8); |
464 | 0 | } |
465 | 0 | else |
466 | 0 | memcpy (ki->keyid, keyparm[0].mpi + keyparm[0].len - 8, 8); |
467 | 0 | } |
468 | 0 | else |
469 | 0 | { |
470 | | /* Its a pity that we need to prefix the buffer with the tag |
471 | | and a length header: We can't simply pass it to the fast |
472 | | hashing function for that reason. It might be a good idea to |
473 | | have a scatter-gather enabled hash function. What we do here |
474 | | is to use a static buffer if this one is large enough and |
475 | | only use the regular hash functions if this buffer is not |
476 | | large enough. |
477 | | FIXME: Factor this out to a shared fingerprint function. |
478 | | */ |
479 | 0 | if (version == 5) |
480 | 0 | { |
481 | 0 | if (5 + n < sizeof hashbuffer ) |
482 | 0 | { |
483 | 0 | hashbuffer[0] = 0x9a; /* CTB */ |
484 | 0 | hashbuffer[1] = (n >> 24);/* 4 byte length header. */ |
485 | 0 | hashbuffer[2] = (n >> 16); |
486 | 0 | hashbuffer[3] = (n >> 8); |
487 | 0 | hashbuffer[4] = (n ); |
488 | 0 | memcpy (hashbuffer + 5, data_start, n); |
489 | 0 | gcry_md_hash_buffer (GCRY_MD_SHA256, ki->fpr, hashbuffer, 5 + n); |
490 | 0 | } |
491 | 0 | else |
492 | 0 | { |
493 | 0 | err = gcry_md_open (&md, GCRY_MD_SHA256, 0); |
494 | 0 | if (err) |
495 | 0 | return err; /* Oops */ |
496 | 0 | gcry_md_putc (md, 0x9a ); /* CTB */ |
497 | 0 | gcry_md_putc (md, (n >> 24)); /* 4 byte length header. */ |
498 | 0 | gcry_md_putc (md, (n >> 16)); |
499 | 0 | gcry_md_putc (md, (n >> 8)); |
500 | 0 | gcry_md_putc (md, (n )); |
501 | 0 | gcry_md_write (md, data_start, n); |
502 | 0 | memcpy (ki->fpr, gcry_md_read (md, 0), 32); |
503 | 0 | gcry_md_close (md); |
504 | 0 | } |
505 | 0 | ki->fprlen = 32; |
506 | 0 | memcpy (ki->keyid, ki->fpr, 8); |
507 | 0 | } |
508 | 0 | else |
509 | 0 | { |
510 | 0 | if ( 3 + n < sizeof hashbuffer ) |
511 | 0 | { |
512 | 0 | hashbuffer[0] = 0x99; /* CTB */ |
513 | 0 | hashbuffer[1] = (n >> 8); /* 2 byte length header. */ |
514 | 0 | hashbuffer[2] = (n ); |
515 | 0 | memcpy (hashbuffer + 3, data_start, n); |
516 | 0 | gcry_md_hash_buffer (GCRY_MD_SHA1, ki->fpr, hashbuffer, 3 + n); |
517 | 0 | } |
518 | 0 | else |
519 | 0 | { |
520 | 0 | err = gcry_md_open (&md, GCRY_MD_SHA1, 0); |
521 | 0 | if (err) |
522 | 0 | return err; /* Oops */ |
523 | 0 | gcry_md_putc (md, 0x99 ); /* CTB */ |
524 | 0 | gcry_md_putc (md, (n >> 8)); /* 2 byte length header. */ |
525 | 0 | gcry_md_putc (md, (n )); |
526 | 0 | gcry_md_write (md, data_start, n); |
527 | 0 | memcpy (ki->fpr, gcry_md_read (md, 0), 20); |
528 | 0 | gcry_md_close (md); |
529 | 0 | } |
530 | 0 | ki->fprlen = 20; |
531 | 0 | memcpy (ki->keyid, ki->fpr+12, 8); |
532 | 0 | } |
533 | 0 | } |
534 | | |
535 | 0 | leave: |
536 | 0 | for (i=0; i < npkey; i++) |
537 | 0 | xfree (helpmpibuf[i]); |
538 | |
|
539 | 0 | return err; |
540 | 0 | } |
541 | | |
542 | | |
543 | | |
544 | | /* The caller must pass the address of an INFO structure which will |
545 | | get filled on success with information pertaining to the OpenPGP |
546 | | keyblock IMAGE of length IMAGELEN. Note that a caller does only |
547 | | need to release this INFO structure if the function returns |
548 | | success. If NPARSED is not NULL the actual number of bytes parsed |
549 | | will be stored at this address. If ONLY_PRIMARY is set the parsing |
550 | | stops right after the primart key packet. */ |
551 | | gpg_error_t |
552 | | _keybox_parse_openpgp (const unsigned char *image, size_t imagelen, |
553 | | int only_primary, |
554 | | size_t *nparsed, keybox_openpgp_info_t info) |
555 | 0 | { |
556 | 0 | gpg_error_t err = 0; |
557 | 0 | const unsigned char *image_start, *data; |
558 | 0 | size_t n, datalen; |
559 | 0 | int pkttype; |
560 | 0 | int first = 1; |
561 | 0 | int read_error = 0; |
562 | 0 | struct _keybox_openpgp_key_info *k, **ktail = NULL; |
563 | 0 | struct _keybox_openpgp_uid_info *u, **utail = NULL; |
564 | |
|
565 | 0 | memset (info, 0, sizeof *info); |
566 | 0 | if (nparsed) |
567 | 0 | *nparsed = 0; |
568 | |
|
569 | 0 | image_start = image; |
570 | 0 | while (image) |
571 | 0 | { |
572 | 0 | err = next_packet (&image, &imagelen, &data, &datalen, &pkttype, &n); |
573 | 0 | if (err) |
574 | 0 | { |
575 | 0 | read_error = 1; |
576 | 0 | break; |
577 | 0 | } |
578 | | |
579 | 0 | if (first) |
580 | 0 | { |
581 | 0 | if (pkttype == PKT_PUBLIC_KEY) |
582 | 0 | ; |
583 | 0 | else if (pkttype == PKT_SECRET_KEY) |
584 | 0 | info->is_secret = 1; |
585 | 0 | else |
586 | 0 | { |
587 | 0 | err = gpg_error (GPG_ERR_UNEXPECTED); |
588 | 0 | if (nparsed) |
589 | 0 | *nparsed += n; |
590 | 0 | break; |
591 | 0 | } |
592 | 0 | first = 0; |
593 | 0 | } |
594 | 0 | else if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY) |
595 | 0 | break; /* Next keyblock encountered - ready. */ |
596 | | |
597 | 0 | if (nparsed) |
598 | 0 | *nparsed += n; |
599 | |
|
600 | 0 | if (pkttype == PKT_SIGNATURE) |
601 | 0 | { |
602 | | /* For now we only count the total number of signatures. */ |
603 | 0 | info->nsigs++; |
604 | 0 | } |
605 | 0 | else if (pkttype == PKT_USER_ID) |
606 | 0 | { |
607 | 0 | info->nuids++; |
608 | 0 | if (info->nuids == 1) |
609 | 0 | { |
610 | 0 | info->uids.off = data - image_start; |
611 | 0 | info->uids.len = datalen; |
612 | 0 | utail = &info->uids.next; |
613 | 0 | } |
614 | 0 | else |
615 | 0 | { |
616 | 0 | u = xtrycalloc (1, sizeof *u); |
617 | 0 | if (!u) |
618 | 0 | { |
619 | 0 | err = gpg_error_from_syserror (); |
620 | 0 | break; |
621 | 0 | } |
622 | 0 | u->off = data - image_start; |
623 | 0 | u->len = datalen; |
624 | 0 | *utail = u; |
625 | 0 | utail = &u->next; |
626 | 0 | } |
627 | 0 | } |
628 | 0 | else if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY) |
629 | 0 | { |
630 | 0 | err = parse_key (data, datalen, &info->primary); |
631 | 0 | if (err || only_primary) |
632 | 0 | break; |
633 | 0 | } |
634 | 0 | else if( pkttype == PKT_PUBLIC_SUBKEY && datalen && *data == '#' ) |
635 | 0 | { |
636 | | /* Early versions of GnuPG used old PGP comment packets; |
637 | | * luckily all those comments are prefixed by a hash |
638 | | * sign - ignore these packets. */ |
639 | 0 | } |
640 | 0 | else if (pkttype == PKT_PUBLIC_SUBKEY || pkttype == PKT_SECRET_SUBKEY) |
641 | 0 | { |
642 | 0 | info->nsubkeys++; |
643 | 0 | if (info->nsubkeys == 1) |
644 | 0 | { |
645 | 0 | err = parse_key (data, datalen, &info->subkeys); |
646 | 0 | if (err) |
647 | 0 | { |
648 | 0 | info->nsubkeys--; |
649 | | /* We ignore subkeys with unknown algorithms. */ |
650 | 0 | if (gpg_err_code (err) == GPG_ERR_UNKNOWN_ALGORITHM |
651 | 0 | || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM) |
652 | 0 | err = 0; |
653 | 0 | if (err) |
654 | 0 | break; |
655 | 0 | } |
656 | 0 | else |
657 | 0 | ktail = &info->subkeys.next; |
658 | 0 | } |
659 | 0 | else |
660 | 0 | { |
661 | 0 | k = xtrycalloc (1, sizeof *k); |
662 | 0 | if (!k) |
663 | 0 | { |
664 | 0 | err = gpg_error_from_syserror (); |
665 | 0 | break; |
666 | 0 | } |
667 | 0 | err = parse_key (data, datalen, k); |
668 | 0 | if (err) |
669 | 0 | { |
670 | 0 | xfree (k); |
671 | 0 | info->nsubkeys--; |
672 | | /* We ignore subkeys with unknown algorithms. */ |
673 | 0 | if (gpg_err_code (err) == GPG_ERR_UNKNOWN_ALGORITHM |
674 | 0 | || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM) |
675 | 0 | err = 0; |
676 | 0 | if (err) |
677 | 0 | break; |
678 | 0 | } |
679 | 0 | else |
680 | 0 | { |
681 | 0 | *ktail = k; |
682 | 0 | ktail = &k->next; |
683 | 0 | } |
684 | 0 | } |
685 | 0 | } |
686 | 0 | } |
687 | |
|
688 | 0 | if (err) |
689 | 0 | { |
690 | 0 | _keybox_destroy_openpgp_info (info); |
691 | 0 | if (!read_error) |
692 | 0 | { |
693 | | /* Packet parsing worked, thus we should be able to skip the |
694 | | rest of the keyblock. */ |
695 | 0 | while (image) |
696 | 0 | { |
697 | 0 | if (next_packet (&image, &imagelen, |
698 | 0 | &data, &datalen, &pkttype, &n) ) |
699 | 0 | break; /* Another error - stop here. */ |
700 | | |
701 | 0 | if (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY) |
702 | 0 | break; /* Next keyblock encountered - ready. */ |
703 | | |
704 | 0 | if (nparsed) |
705 | 0 | *nparsed += n; |
706 | 0 | } |
707 | 0 | } |
708 | 0 | } |
709 | |
|
710 | 0 | return err; |
711 | 0 | } |
712 | | |
713 | | |
714 | | /* Release any malloced data in INFO but not INFO itself! */ |
715 | | void |
716 | | _keybox_destroy_openpgp_info (keybox_openpgp_info_t info) |
717 | 0 | { |
718 | 0 | struct _keybox_openpgp_key_info *k, *k2; |
719 | 0 | struct _keybox_openpgp_uid_info *u, *u2; |
720 | |
|
721 | 0 | log_assert (!info->primary.next); |
722 | 0 | for (k=info->subkeys.next; k; k = k2) |
723 | 0 | { |
724 | 0 | k2 = k->next; |
725 | 0 | xfree (k); |
726 | 0 | } |
727 | |
|
728 | 0 | for (u=info->uids.next; u; u = u2) |
729 | 0 | { |
730 | 0 | u2 = u->next; |
731 | 0 | xfree (u); |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | | |
736 | | gpg_error_t |
737 | | kbx_get_first_opgp_keyid (const void *buffer, size_t len, u32 *kid) |
738 | 0 | { |
739 | 0 | struct _keybox_openpgp_info info; |
740 | 0 | gpg_error_t err; |
741 | |
|
742 | 0 | err = _keybox_parse_openpgp (buffer, len, 1 /*only primary*/, NULL, &info); |
743 | 0 | if (err) |
744 | 0 | return err; |
745 | | |
746 | 0 | kid[0] = buf32_to_u32 (info.primary.keyid); |
747 | 0 | kid[1] = buf32_to_u32 (info.primary.keyid+4); |
748 | 0 | _keybox_destroy_openpgp_info (&info); |
749 | 0 | return 0; |
750 | 0 | } |