1 /* keyblob.c - Keyblob parser and builder.
2 * Copyright (C) 2009 Free Software Foundation, Inc.
3 * Copyright (C) 2015-2016 Werner Koch
5 * This file is part of GnuPG.
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.
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.
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/>.
34 #include "../common/sysutils.h"
38 /* Parse the header prefix and return the length of the entire header. */
40 parse_header (const char *filename,
41 const unsigned char *packet, size_t packetlen,
47 return gpg_error (GPG_ERR_BUG);
49 len = buf32_to_uint (packet+2);
50 if (packet[0] != (0xc0|61) || len < 26
51 || memcmp (packet+6, "GnuPG/G13", 10))
53 log_error ("file '%s' is not valid container\n", filename);
54 return gpg_error (GPG_ERR_INV_OBJ);
58 log_error ("unknown version %u of container '%s'\n",
59 (unsigned int)packet[16], filename);
60 return gpg_error (GPG_ERR_INV_OBJ);
62 if (packet[17] || packet[18]
63 || packet[26] || packet[27] || packet[28] || packet[29]
64 || packet[30] || packet[31])
65 log_info ("WARNING: unknown meta information in '%s'\n", filename);
67 log_info ("WARNING: OS flag is not supported in '%s'\n", filename);
69 log_info ("Note: meta data copies in '%s' are ignored\n", filename);
71 len = buf32_to_uint (packet+20);
73 /* Do a basic sanity check on the length. */
74 if (len < 32 || len > 1024*1024)
76 log_error ("bad length given in container '%s'\n", filename);
77 return gpg_error (GPG_ERR_INV_OBJ);
85 /* Read the prefix of the keyblob and do some basic parsing. On
86 success returns an open estream file at R_FP and the length of the
87 header at R_HEADERLEN. */
89 read_keyblob_prefix (const char *filename, estream_t *r_fp, size_t *r_headerlen)
93 unsigned char packet[32];
97 fp = es_fopen (filename, "rb");
100 err = gpg_error_from_syserror ();
101 log_error ("error reading '%s': %s\n", filename, gpg_strerror (err));
105 /* Read the header. It is defined as 32 bytes thus we read it in one go. */
106 if (es_fread (packet, 32, 1, fp) != 1)
108 err = gpg_error_from_syserror ();
109 log_error ("error reading the header of '%s': %s\n",
110 filename, gpg_strerror (err));
115 err = parse_header (filename, packet, 32, r_headerlen);
127 * Test whether the container with name FILENAME is a suitable G13
128 * container. This function may even be called on a mounted
132 g13_is_container (ctrl_t ctrl, const char *filename)
140 /* Read just the prefix of the header. */
141 err = read_keyblob_prefix (filename, &fp, &dummy);
149 * Read the keyblob at FILENAME. The caller should have acquired a
150 * lockfile and checked that the file exists.
153 g13_keyblob_read (const char *filename,
154 void **r_enckeyblob, size_t *r_enckeybloblen)
158 size_t headerlen = 0;
162 *r_enckeyblob = NULL;
163 *r_enckeybloblen = 0;
165 err = read_keyblob_prefix (filename, &fp, &headerlen);
170 log_info ("header length of '%s' is %zu\n", filename, headerlen);
172 /* Read everything including the padding. We should eventually do a
173 regular OpenPGP parsing to detect the padding packet and pass
174 only the actual used OpenPGP data to the engine. This is in
175 particular required when supporting CMS which will be
176 encapsulated in an OpenPGP packet. */
177 assert (headerlen >= 32);
178 msglen = headerlen - 32;
181 err = gpg_error (GPG_ERR_NO_DATA);
184 msg = xtrymalloc (msglen);
187 err = gpg_error_from_syserror ();
190 if (es_fread (msg, msglen, 1, fp) != 1)
192 err = gpg_error_from_syserror ();
193 log_error ("error reading keyblob of '%s': %s\n",
194 filename, gpg_strerror (err));
200 *r_enckeybloblen = msglen;