chiark / gitweb /
Import gnupg2_2.1.17.orig.tar.bz2
[gnupg2.git] / g13 / keyblob.c
1 /* keyblob.c - Keyblob parser and builder.
2  * Copyright (C) 2009 Free Software Foundation, Inc.
3  * Copyright (C) 2015-2016 Werner Koch
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 <errno.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <assert.h>
29
30 #include "g13.h"
31 #include "mount.h"
32
33 #include "keyblob.h"
34 #include "../common/sysutils.h"
35 #include "host2net.h"
36
37
38 /* Parse the header prefix and return the length of the entire header.  */
39 static gpg_error_t
40 parse_header (const char *filename,
41               const unsigned char *packet, size_t packetlen,
42               size_t *r_headerlen)
43 {
44   unsigned int len;
45
46   if (packetlen != 32)
47     return gpg_error (GPG_ERR_BUG);
48
49   len = buf32_to_uint (packet+2);
50   if (packet[0] != (0xc0|61) || len < 26
51       || memcmp (packet+6, "GnuPG/G13", 10))
52     {
53       log_error ("file '%s' is not valid container\n", filename);
54       return gpg_error (GPG_ERR_INV_OBJ);
55     }
56   if (packet[16] != 1)
57     {
58       log_error ("unknown version %u of container '%s'\n",
59                  (unsigned int)packet[16], filename);
60       return gpg_error (GPG_ERR_INV_OBJ);
61     }
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);
66   if (packet[19])
67     log_info ("WARNING: OS flag is not supported in '%s'\n", filename);
68   if (packet[24] > 1 )
69     log_info ("Note: meta data copies in '%s' are ignored\n", filename);
70
71   len = buf32_to_uint (packet+20);
72
73   /* Do a basic sanity check on the length.  */
74   if (len < 32 || len > 1024*1024)
75     {
76       log_error ("bad length given in container '%s'\n", filename);
77       return gpg_error (GPG_ERR_INV_OBJ);
78     }
79
80   *r_headerlen = len;
81   return 0;
82 }
83
84
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.  */
88 static gpg_error_t
89 read_keyblob_prefix (const char *filename, estream_t *r_fp, size_t *r_headerlen)
90 {
91   gpg_error_t err;
92   estream_t fp;
93   unsigned char packet[32];
94
95   *r_fp = NULL;
96
97   fp = es_fopen (filename, "rb");
98   if (!fp)
99     {
100       err = gpg_error_from_syserror ();
101       log_error ("error reading '%s': %s\n", filename, gpg_strerror (err));
102       return err;
103     }
104
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)
107     {
108       err = gpg_error_from_syserror ();
109       log_error ("error reading the header of '%s': %s\n",
110                  filename, gpg_strerror (err));
111       es_fclose (fp);
112       return err;
113     }
114
115   err = parse_header (filename, packet, 32, r_headerlen);
116   if (err)
117     es_fclose (fp);
118   else
119     *r_fp = fp;
120
121   return err;
122 }
123
124
125 \f
126 /*
127  * Test whether the container with name FILENAME is a suitable G13
128  * container.  This function may even be called on a mounted
129  * container.
130  */
131 gpg_error_t
132 g13_is_container (ctrl_t ctrl, const char *filename)
133 {
134   gpg_error_t err;
135   estream_t fp = NULL;
136   size_t dummy;
137
138   (void)ctrl;
139
140   /* Read just the prefix of the header.  */
141   err = read_keyblob_prefix (filename, &fp, &dummy);
142   if (!err)
143     es_fclose (fp);
144   return err;
145 }
146
147
148 /*
149  * Read the keyblob at FILENAME.  The caller should have acquired a
150  * lockfile and checked that the file exists.
151  */
152 gpg_error_t
153 g13_keyblob_read (const char *filename,
154                   void **r_enckeyblob, size_t *r_enckeybloblen)
155 {
156   gpg_error_t err;
157   estream_t fp = NULL;
158   size_t headerlen = 0;
159   size_t msglen;
160   void *msg = NULL;
161
162   *r_enckeyblob = NULL;
163   *r_enckeybloblen = 0;
164
165   err = read_keyblob_prefix (filename, &fp, &headerlen);
166   if (err)
167     goto leave;
168
169   if (opt.verbose)
170     log_info ("header length of '%s' is %zu\n", filename, headerlen);
171
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;
179   if (!msglen)
180     {
181       err = gpg_error (GPG_ERR_NO_DATA);
182       goto leave;
183     }
184   msg = xtrymalloc (msglen);
185   if (!msglen)
186     {
187       err = gpg_error_from_syserror ();
188       goto leave;
189     }
190   if (es_fread (msg, msglen, 1, fp) != 1)
191     {
192       err = gpg_error_from_syserror ();
193       log_error ("error reading keyblob of '%s': %s\n",
194                  filename, gpg_strerror (err));
195       goto leave;
196     }
197
198   *r_enckeyblob = msg;
199   msg = NULL;
200   *r_enckeybloblen = msglen;
201
202  leave:
203   xfree (msg);
204   es_fclose (fp);
205
206   return err;
207 }