1 /* keybox-blob.c - KBX Blob handling
2 * Copyright (C) 2000, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* The keybox data formats
23 The KeyBox uses an augmented OpenPGP/X.509 key format. This makes
24 random access to a keyblock/certificate easier and also gives the
25 opportunity to store additional information (e.g. the fingerprint)
26 along with the key. All integers are stored in network byte order,
27 offsets are counted from the beginning of the Blob.
29 The first record of a plain KBX file has a special format:
31 u32 length of the first record
33 byte version number (1)
39 u32 last_maintenance_run
43 The OpenPGP and X.509 blob are very similiar, things which are
44 X.509 specific are noted like [X.509: xxx]
46 u32 length of this blob (including these 4 bytes)
47 byte Blob type (2) [X509: 3]
48 byte version number of this blob type (1)
50 bit 0 = contains secret key material
51 bit 1 = ephemeral blob (e.g. used while quering external resources)
53 u32 offset to the OpenPGP keyblock or X509 DER encoded certificate
55 u16 number of keys (at least 1!) [X509: always 1]
56 u16 size of additional key information
58 b20 The keys fingerprint
59 (fingerprints are always 20 bytes, MD5 left padded with zeroes)
60 u32 offset to the n-th key's keyID (a keyID is always 8 byte)
61 or 0 if not known which is the case only for X509.
63 bit 0 = qualified signature (not yet implemented}
65 u16 size of serialnumber(may be zero)
66 n u16 (see above) bytes of serial number
67 u16 number of user IDs
68 u16 size of additional user ID information
70 u32 offset to the n-th user ID
71 u32 length of this user ID.
72 u16 special user ID flags.
76 [For X509, the first user ID is the Issuer, the second the Subject
77 and the others are subjectAltNames]
78 u16 number of signatures
79 u16 size of signature information (4)
80 u32 expiration time of signature with some special values:
81 0x00000000 = not checked
82 0x00000001 = missing key
83 0x00000002 = bad signature
84 0x10000000 = valid and expires at some date in 1978.
85 0xffffffff = valid and does not expire
86 u8 assigned ownertrust [X509: not used]
88 OpenPGP: see ../g10/trustdb/TRUST_* [not yet used]
89 X509: Bit 4 set := key has been revoked. Note that this value
90 matches TRUST_FLAG_REVOKED
93 u32 Newest timestamp in the keyblock (useful for KS syncronsiation?)
95 u32 size of reserved space (not including this field)
98 Here we might want to put other data
100 Here comes the keyblock
102 maybe we put a signature here later.
104 b16 MD5 checksum (useful for KS syncronisation), we might also want to use
119 #include "keybox-defs.h"
122 #ifdef KEYBOX_WITH_OPENPGP
123 /* include stuff to parse the packets */
125 #ifdef KEYBOX_WITH_X509
130 #include "../common/gettime.h"
133 /* special values of the signature status */
134 #define SF_NONE(a) ( !(a) )
135 #define SF_NOKEY(a) ((a) & (1<<0))
136 #define SF_BAD(a) ((a) & (1<<1))
137 #define SF_VALID(a) ((a) & (1<<29))
148 /* #if MAX_FINGERPRINT_LEN < 20 */
149 /* #error fingerprints are 20 bytes */
152 struct keyboxblob_key {
158 struct keyboxblob_uid {
160 char *name; /* used only with x509 */
167 struct keyid_list *next;
173 struct fixup_list *next;
184 /* stuff used only by keybox_create_blob */
185 unsigned char *serialbuf;
186 const unsigned char *serial;
189 struct keyboxblob_key *keys;
191 struct keyboxblob_uid *uids;
194 struct fixup_list *fixups;
195 int fixup_out_of_core;
197 struct keyid_list *temp_kids;
198 struct membuf bufbuf; /* temporary store for the blob */
204 /* A simple implemention of a dynamic buffer. Use init_membuf() to
205 create a buffer, put_membuf to append bytes and get_membuf to
206 release and return the buffer. Allocation errors are detected but
207 only returned at the final get_membuf(), this helps not to clutter
208 the code with out of core checks. */
211 init_membuf (struct membuf *mb, int initiallen)
214 mb->size = initiallen;
216 mb->buf = xtrymalloc (initiallen);
222 put_membuf (struct membuf *mb, const void *buf, size_t len)
227 if (mb->len + len >= mb->size)
231 mb->size += len + 1024;
232 p = xtryrealloc (mb->buf, mb->size);
240 memcpy (mb->buf + mb->len, buf, len);
245 get_membuf (struct membuf *mb, size_t *len)
259 mb->out_of_core = 1; /* don't allow a reuse */
265 put8 (struct membuf *mb, byte a )
267 put_membuf (mb, &a, 1);
271 put16 (struct membuf *mb, u16 a )
273 unsigned char tmp[2];
276 put_membuf (mb, tmp, 2);
280 put32 (struct membuf *mb, u32 a )
282 unsigned char tmp[4];
287 put_membuf (mb, tmp, 4);
291 /* Store a value in the fixup list */
293 add_fixup (KEYBOXBLOB blob, u32 off, u32 val)
295 struct fixup_list *fl;
297 if (blob->fixup_out_of_core)
300 fl = xtrycalloc(1, sizeof *fl);
302 blob->fixup_out_of_core = 1;
307 fl->next = blob->fixups;
314 #ifdef KEYBOX_WITH_OPENPGP
316 OpenPGP specific stuff
321 We must store the keyid at some place because we can't calculate the
322 offset yet. This is only used for v3 keyIDs. Function returns an
323 index value for later fixup or -1 for out of core. The value must be
326 pgp_temp_store_kid (KEYBOXBLOB blob, PKT_public_key *pk)
328 struct keyid_list *k, *r;
330 k = xtrymalloc (sizeof *k);
333 k->kid[0] = pk->keyid[0] >> 24 ;
334 k->kid[1] = pk->keyid[0] >> 16 ;
335 k->kid[2] = pk->keyid[0] >> 8 ;
336 k->kid[3] = pk->keyid[0] ;
337 k->kid[4] = pk->keyid[0] >> 24 ;
338 k->kid[5] = pk->keyid[0] >> 16 ;
339 k->kid[6] = pk->keyid[0] >> 8 ;
340 k->kid[7] = pk->keyid[0] ;
342 k->next = blob->temp_kids;
344 for (r=k; r; r = r->next)
351 pgp_create_key_part (KEYBOXBLOB blob, KBNODE keyblock)
357 for (n=0, node = keyblock; node; node = node->next)
359 if ( node->pkt->pkttype == PKT_PUBLIC_KEY
360 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
362 PKT_public_key *pk = node->pkt->pkt.public_key;
365 fingerprint_from_pk (pk, tmp , &fprlen);
366 memcpy (blob->keys[n].fpr, tmp, 20);
367 if ( fprlen != 20 ) /*v3 fpr - shift right and fill with zeroes*/
369 assert (fprlen == 16);
370 memmove (blob->keys[n].fpr+4, blob->keys[n].fpr, 16);
371 memset (blob->keys[n].fpr, 0, 4);
372 blob->keys[n].off_kid = pgp_temp_store_kid (blob, pk);
376 blob->keys[n].off_kid = 0; /* will be fixed up later */
378 blob->keys[n].flags = 0;
381 else if ( node->pkt->pkttype == PKT_SECRET_KEY
382 || node->pkt->pkttype == PKT_SECRET_SUBKEY )
384 never_reached (); /* actually not yet implemented */
387 assert (n == blob->nkeys);
392 pgp_create_uid_part (KEYBOXBLOB blob, KBNODE keyblock)
397 for (n=0, node = keyblock; node; node = node->next)
399 if (node->pkt->pkttype == PKT_USER_ID)
401 PKT_user_id *u = node->pkt->pkt.user_id;
403 blob->uids[n].len = u->len;
404 blob->uids[n].flags = 0;
405 blob->uids[n].validity = 0;
409 assert (n == blob->nuids);
414 pgp_create_sig_part (KEYBOXBLOB blob, KBNODE keyblock)
419 for (n=0, node = keyblock; node; node = node->next)
421 if (node->pkt->pkttype == PKT_SIGNATURE)
423 PKT_signature *sig = node->pkt->pkt.signature;
425 blob->sigs[n] = 0; /* FIXME: check the signature here */
429 assert( n == blob->nsigs );
434 pgp_create_blob_keyblock (KEYBOXBLOB blob, KBNODE keyblock)
436 struct membuf *a = blob->buf;
440 u32 kbstart = a->len;
442 add_fixup (blob, kbstart);
444 for (n = 0, node = keyblock; node; node = node->next)
446 rc = build_packet ( a, node->pkt );
448 gpg_log_error ("build_packet(%d) for keyboxblob failed: %s\n",
449 node->pkt->pkttype, gpg_errstr(rc) );
450 return GPGERR_WRITE_FILE;
452 if ( node->pkt->pkttype == PKT_USER_ID )
454 PKT_user_id *u = node->pkt->pkt.user_id;
455 /* build_packet has set the offset of the name into u ;
456 * now we can do the fixup */
457 add_fixup (blob, blob->uids[n].off_addr, u->stored_at);
461 assert (n == blob->nuids);
463 add_fixup (blob, a->len - kbstart);
467 #endif /*KEYBOX_WITH_OPENPGP*/
470 #ifdef KEYBOX_WITH_X509
475 /* Write the raw certificate out */
477 x509_create_blob_cert (KEYBOXBLOB blob, ksba_cert_t cert)
479 struct membuf *a = blob->buf;
480 const unsigned char *image;
482 u32 kbstart = a->len;
484 /* Store our offset for later fixup */
485 add_fixup (blob, 8, kbstart);
487 image = ksba_cert_get_image (cert, &length);
489 return gpg_error (GPG_ERR_GENERAL);
490 put_membuf (a, image, length);
492 add_fixup (blob, 12, a->len - kbstart);
496 #endif /*KEYBOX_WITH_X509*/
498 /* Write a stored keyID out to the buffer */
500 write_stored_kid (KEYBOXBLOB blob, int seqno)
502 struct keyid_list *r;
504 for ( r = blob->temp_kids; r; r = r->next )
506 if (r->seqno == seqno )
508 put_membuf (blob->buf, r->kid, 8);
515 /* Release a list of key IDs */
517 release_kid_list (struct keyid_list *kl)
519 struct keyid_list *r, *r2;
521 for ( r = kl; r; r = r2 )
531 create_blob_header (KEYBOXBLOB blob, int blobtype, int as_ephemeral)
533 struct membuf *a = blob->buf;
536 put32 ( a, 0 ); /* blob length, needs fixup */
538 put8 ( a, 1 ); /* blob type version */
539 put16 ( a, as_ephemeral? 2:0 ); /* blob flags */
541 put32 ( a, 0 ); /* offset to the raw data, needs fixup */
542 put32 ( a, 0 ); /* length of the raw data, needs fixup */
544 put16 ( a, blob->nkeys );
545 put16 ( a, 20 + 4 + 2 + 2 ); /* size of key info */
546 for ( i=0; i < blob->nkeys; i++ )
548 put_membuf (a, blob->keys[i].fpr, 20);
549 blob->keys[i].off_kid_addr = a->len;
550 put32 ( a, 0 ); /* offset to keyid, fixed up later */
551 put16 ( a, blob->keys[i].flags );
552 put16 ( a, 0 ); /* reserved */
555 put16 (a, blob->seriallen); /*fixme: check that it fits into 16 bits*/
557 put_membuf (a, blob->serial, blob->seriallen);
559 put16 ( a, blob->nuids );
560 put16 ( a, 4 + 4 + 2 + 1 + 1 ); /* size of uid info */
561 for (i=0; i < blob->nuids; i++)
563 blob->uids[i].off_addr = a->len;
564 put32 ( a, 0 ); /* offset to userid, fixed up later */
565 put32 ( a, blob->uids[i].len );
566 put16 ( a, blob->uids[i].flags );
567 put8 ( a, 0 ); /* validity */
568 put8 ( a, 0 ); /* reserved */
571 put16 ( a, blob->nsigs );
572 put16 ( a, 4 ); /* size of sig info */
573 for (i=0; i < blob->nsigs; i++)
575 put32 ( a, blob->sigs[i]);
578 put8 ( a, 0 ); /* assigned ownertrust */
579 put8 ( a, 0 ); /* validity of all user IDs */
580 put16 ( a, 0 ); /* reserved */
581 put32 ( a, 0 ); /* time of next recheck */
582 put32 ( a, 0 ); /* newest timestamp (none) */
583 put32 ( a, make_timestamp() ); /* creation time */
584 put32 ( a, 0 ); /* size of reserved space */
585 /* reserved space (which is currently of size 0) */
587 /* space where we write keyIDs and and other stuff so that the
588 pointers can actually point to somewhere */
589 if (blobtype == BLOBTYPE_PGP)
591 /* We need to store the keyids for all pgp v3 keys because those key
592 IDs are not part of the fingerprint. While we are doing that, we
593 fixup all the keyID offsets */
594 for (i=0; i < blob->nkeys; i++ )
596 if (blob->keys[i].off_kid)
597 { /* this is a v3 one */
598 add_fixup (blob, blob->keys[i].off_kid_addr, a->len);
599 write_stored_kid (blob, blob->keys[i].off_kid);
602 { /* the better v4 key IDs - just store an offset 8 bytes back */
603 add_fixup (blob, blob->keys[i].off_kid_addr,
604 blob->keys[i].off_kid_addr - 8);
609 if (blobtype == BLOBTYPE_X509)
611 /* We don't want to point to ASN.1 encoded UserIDs (DNs) but to
612 the utf-8 string represenation of them */
613 for (i=0; i < blob->nuids; i++ )
615 if (blob->uids[i].name)
616 { /* this is a v3 one */
617 add_fixup (blob, blob->uids[i].off_addr, a->len);
618 put_membuf (blob->buf, blob->uids[i].name, blob->uids[i].len);
629 create_blob_trailer (KEYBOXBLOB blob)
637 create_blob_finish (KEYBOXBLOB blob)
639 struct membuf *a = blob->buf;
645 /* write a placeholder for the checksum */
646 for (i = 0; i < 16; i++ )
647 put32 (a, 0); /* Hmmm: why put32() ?? */
649 /* get the memory area */
650 n = 0; /* (Just to avoid compiler warning.) */
651 p = get_membuf (a, &n);
653 return gpg_error (GPG_ERR_ENOMEM);
656 /* fixup the length */
657 add_fixup (blob, 0, n);
660 if (blob->fixup_out_of_core)
661 return gpg_error (GPG_ERR_ENOMEM);
664 struct fixup_list *fl;
665 for (fl = blob->fixups; fl; fl = fl->next)
667 assert (fl->off+4 <= n);
668 p[fl->off+0] = fl->val >> 24;
669 p[fl->off+1] = fl->val >> 16;
670 p[fl->off+2] = fl->val >> 8;
671 p[fl->off+3] = fl->val;
675 /* calculate and store the MD5 checksum */
676 gcry_md_hash_buffer (GCRY_MD_MD5, p + n - 16, p, n - 16);
680 return gpg_error_from_syserror ();
689 #ifdef KEYBOX_WITH_OPENPGP
692 _keybox_create_pgp_blob (KEYBOXBLOB *r_blob, KBNODE keyblock, int as_ephemeral)
699 blob = xtrycalloc (1, sizeof *blob);
701 return gpg_error_from_syserror ();
703 /* fixme: Do some sanity checks on the keyblock */
705 /* count userids and keys so that we can allocate the arrays */
706 for (node = keyblock; node; node = node->next)
708 switch (node->pkt->pkttype)
712 case PKT_PUBLIC_SUBKEY:
713 case PKT_SECRET_SUBKEY: blob->nkeys++; break;
714 case PKT_USER_ID: blob->nuids++; break;
715 case PKT_SIGNATURE: blob->nsigs++; break;
720 blob->keys = xtrycalloc (blob->nkeys, sizeof *blob->keys );
721 blob->uids = xtrycalloc (blob->nuids, sizeof *blob->uids );
722 blob->sigs = xtrycalloc (blob->nsigs, sizeof *blob->sigs );
723 if (!blob->keys || !blob->uids || !blob->sigs)
725 rc = gpg_error (GPG_ERR_ENOMEM);
729 rc = pgp_create_key_part ( blob, keyblock );
732 rc = pgp_create_uid_part ( blob, keyblock );
735 rc = pgp_create_sig_part ( blob, keyblock );
739 init_membuf (&blob->bufbuf, 1024);
740 blob->buf = &blob->bufbuf;
741 rc = create_blob_header (blob, BLOBTYPE_OPENPGP, as_ephemeral);
744 rc = pgp_create_blob_keyblock (blob, keyblock);
747 rc = create_blob_trailer (blob);
750 rc = create_blob_finish ( blob );
756 release_kid_list (blob->temp_kids);
757 blob->temp_kids = NULL;
760 keybox_release_blob (blob);
769 #endif /*KEYBOX_WITH_OPENPGP*/
771 #ifdef KEYBOX_WITH_X509
773 /* Return an allocated string with the email address extracted from a
774 DN. Note hat we use this code also in ../sm/keylist.c. */
776 x509_email_kludge (const char *name)
778 const char *p, *string;
785 p = strstr (string, "1.2.840.113549.1.9.1=#");
788 if (p == name || (p > string+1 && p[-1] == ',' && p[-2] != '\\'))
797 /* This looks pretty much like an email address in the subject's DN
798 we use this to add an additional user ID entry. This way,
799 OpenSSL generated keys get a nicer and usable listing. */
800 for (n=0, p=name; hexdigitp (p) && hexdigitp (p+1); p +=2, n++)
804 buf = xtrymalloc (n+3);
806 return NULL; /* oops, out of core */
808 for (n=1, p=name; hexdigitp (p); p +=2, n++)
817 /* Note: We should move calculation of the digest into libksba and
818 remove that parameter */
820 _keybox_create_x509_blob (KEYBOXBLOB *r_blob, ksba_cert_t cert,
821 unsigned char *sha1_digest, int as_ephemeral)
831 blob = xtrycalloc (1, sizeof *blob);
833 return gpg_error_from_syserror ();
835 sn = ksba_cert_get_serial (cert);
839 n = gcry_sexp_canon_len (sn, 0, NULL, NULL);
843 return gpg_error (GPG_ERR_GENERAL);
845 blob->serialbuf = sn;
846 sn++; n--; /* skip '(' */
847 for (len=0; n && *sn && *sn != ':' && digitp (sn); n--, sn++)
848 len = len*10 + atoi_1 (sn);
851 xfree (blob->serialbuf);
852 blob->serialbuf = NULL;
853 return gpg_error (GPG_ERR_GENERAL);
857 blob->seriallen = len;
862 /* create list of names */
865 names = xtrymalloc (max_names * sizeof *names);
868 rc = gpg_error_from_syserror ();
872 p = ksba_cert_get_issuer (cert, 0);
875 rc = gpg_error (GPG_ERR_MISSING_VALUE);
878 names[blob->nuids++] = p;
879 for (i=0; (p = ksba_cert_get_subject (cert, i)); i++)
881 if (blob->nuids >= max_names)
886 tmp = xtryrealloc (names, max_names * sizeof *names);
889 rc = gpg_error_from_syserror ();
894 names[blob->nuids++] = p;
895 if (!i && (p=x509_email_kludge (p)))
896 names[blob->nuids++] = p; /* due to !i we don't need to check bounds*/
899 /* space for signature information */
902 blob->keys = xtrycalloc (blob->nkeys, sizeof *blob->keys );
903 blob->uids = xtrycalloc (blob->nuids, sizeof *blob->uids );
904 blob->sigs = xtrycalloc (blob->nsigs, sizeof *blob->sigs );
905 if (!blob->keys || !blob->uids || !blob->sigs)
907 rc = gpg_error (GPG_ERR_ENOMEM);
911 memcpy (blob->keys[0].fpr, sha1_digest, 20);
912 blob->keys[0].off_kid = 0; /* We don't have keyids */
913 blob->keys[0].flags = 0;
915 /* issuer and subject names */
916 for (i=0; i < blob->nuids; i++)
918 blob->uids[i].name = names[i];
919 blob->uids[i].len = strlen(names[i]);
921 blob->uids[i].flags = 0;
922 blob->uids[i].validity = 0;
928 blob->sigs[0] = 0; /* not yet checked */
930 /* Create a temporary buffer for further processing */
931 init_membuf (&blob->bufbuf, 1024);
932 blob->buf = &blob->bufbuf;
933 /* write out what we already have */
934 rc = create_blob_header (blob, BLOBTYPE_X509, as_ephemeral);
937 rc = x509_create_blob_cert (blob, cert);
940 rc = create_blob_trailer (blob);
943 rc = create_blob_finish ( blob );
949 release_kid_list (blob->temp_kids);
950 blob->temp_kids = NULL;
953 for (i=0; i < blob->nuids; i++)
959 _keybox_release_blob (blob);
968 #endif /*KEYBOX_WITH_X509*/
973 _keybox_new_blob (KEYBOXBLOB *r_blob,
974 unsigned char *image, size_t imagelen, off_t off)
979 blob = xtrycalloc (1, sizeof *blob);
981 return gpg_error_from_syserror ();
984 blob->bloblen = imagelen;
985 blob->fileoffset = off;
992 _keybox_release_blob (KEYBOXBLOB blob)
997 /* hmmm: release membuf here?*/
999 xfree (blob->serialbuf);
1000 for (i=0; i < blob->nuids; i++)
1001 xfree (blob->uids[i].name);
1002 xfree (blob->uids );
1003 xfree (blob->sigs );
1004 xfree (blob->blob );
1010 const unsigned char *
1011 _keybox_get_blob_image ( KEYBOXBLOB blob, size_t *n )
1018 _keybox_get_blob_fileoffset (KEYBOXBLOB blob)
1020 return blob->fileoffset;
1026 _keybox_update_header_blob (KEYBOXBLOB blob)
1028 if (blob->bloblen >= 32 && blob->blob[4] == BLOBTYPE_HEADER)
1030 u32 val = make_timestamp ();
1032 /* Update the last maintenance run times tamp. */
1033 blob->blob[20] = (val >> 24);
1034 blob->blob[20+1] = (val >> 16);
1035 blob->blob[20+2] = (val >> 8);
1036 blob->blob[20+3] = (val );