1 /* create.c - Create a new crypto container
2 * Copyright (C) 2009 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/>.
39 /* Create a new blob with all the session keys and other meta
40 information which are to be stored encrypted in the crypto
41 container header. On success the malloced blob is stored at R_BLOB
42 and its length at R_BLOBLEN. On error en error ocde is returned
43 and (R_BLOB,R_BLOBLEN) are set to (NULL,0).
45 The format of this blob is a sequence of tag-length-value tuples.
46 All tuples have this format:
48 2 byte TAG Big endian unsigned integer (0..65535)
49 described by the KEYBLOB_TAG_ constants.
50 2 byte LENGTH Big endian unsigned integer (0..65535)
51 giving the length of the value.
52 length bytes VALUE The value described by the tag.
54 The first tag in a keyblob must be a BLOBVERSION. The other tags
55 depend on the type of the container as described by the CONTTYPE
56 tag. See keyblob.h for details. */
58 create_new_keyblob (ctrl_t ctrl
, int is_detached
,
59 void **r_blob
, size_t *r_bloblen
)
62 unsigned char twobyte
[2];
68 init_membuf_secure (&mb
, 512);
70 append_tuple (&mb
, KEYBLOB_TAG_BLOBVERSION
, "\x01", 1);
72 twobyte
[0] = (ctrl
->conttype
>> 8);
73 twobyte
[1] = (ctrl
->conttype
);
74 append_tuple (&mb
, KEYBLOB_TAG_CONTTYPE
, twobyte
, 2);
76 append_tuple (&mb
, KEYBLOB_TAG_DETACHED
, NULL
, 0);
78 err
= be_create_new_keys (ctrl
->conttype
, &mb
);
82 /* Just for testing. */
83 append_tuple (&mb
, KEYBLOB_TAG_FILLER
, "filler", 6);
85 *r_blob
= get_membuf (&mb
, r_bloblen
);
88 err
= gpg_error_from_syserror ();
92 log_debug ("used keyblob size is %zu\n", *r_bloblen
);
95 xfree (get_membuf (&mb
, NULL
));
101 /* Encrypt the keyblob (KEYBLOB,KEYBLOBLEN) and store the result at
102 (R_ENCBLOB, R_ENCBLOBLEN). Returns 0 on success or an error code.
103 On error R_EKYBLOB is set to NULL. Depending on the keys set in
104 CTRL the result is a single OpenPGP binary message, a single
105 special OpenPGP packet encapsulating a CMS message or a
106 concatenation of both with the CMS packet being the last. */
108 encrypt_keyblob (ctrl_t ctrl
, void *keyblob
, size_t keybloblen
,
109 void **r_encblob
, size_t *r_encbloblen
)
113 /* FIXME: For now we only implement OpenPGP. */
114 err
= gpg_encrypt_blob (ctrl
, keyblob
, keybloblen
,
115 r_encblob
, r_encbloblen
);
121 /* Write a new file under the name FILENAME with the keyblob and an
122 appropriate header. This fucntion is called with a lock file in
123 place and after checking that the filename does not exists. */
125 write_keyblob (const char *filename
,
126 const void *keyblob
, size_t keybloblen
)
130 unsigned char packet
[32];
131 size_t headerlen
, paddinglen
;
133 fp
= es_fopen (filename
, "wbx");
136 err
= gpg_error_from_syserror ();
137 log_error ("error creating new container `%s': %s\n",
138 filename
, gpg_strerror (err
));
142 /* Allow for an least 8 times larger keyblob to accommodate for
143 future key changes. Round it up to 4096 byte. */
144 headerlen
= ((32 + 8 * keybloblen
+ 16) + 4095) / 4096 * 4096;
145 paddinglen
= headerlen
- 32 - keybloblen
;
146 assert (paddinglen
>= 16);
148 packet
[0] = (0xc0|61); /* CTB for the private packet type 0x61. */
149 packet
[1] = 0xff; /* 5 byte length packet, value 20. */
154 memcpy (packet
+6, "GnuPG/G13", 10); /* Packet subtype. */
155 packet
[16] = 1; /* G13 packet format version. */
156 packet
[17] = 0; /* Reserved. */
157 packet
[18] = 0; /* Reserved. */
158 packet
[19] = 0; /* OS Flag. */
159 packet
[20] = (headerlen
>> 24); /* Total length of header. */
160 packet
[21] = (headerlen
>> 16);
161 packet
[22] = (headerlen
>> 8);
162 packet
[23] = (headerlen
);
163 packet
[24] = 1; /* Number of header copies. */
164 packet
[25] = 0; /* Number of header copies at the end. */
165 packet
[26] = 0; /* Reserved. */
166 packet
[27] = 0; /* Reserved. */
167 packet
[28] = 0; /* Reserved. */
168 packet
[29] = 0; /* Reserved. */
169 packet
[30] = 0; /* Reserved. */
170 packet
[31] = 0; /* Reserved. */
172 if (es_fwrite (packet
, 32, 1, fp
) != 1)
175 if (es_fwrite (keyblob
, keybloblen
, 1, fp
) != 1)
178 /* Write the padding. */
179 packet
[0] = (0xc0|61); /* CTB for Private packet type 0x61. */
180 packet
[1] = 0xff; /* 5 byte length packet, value 20. */
181 packet
[2] = (paddinglen
-6) >> 24;
182 packet
[3] = (paddinglen
-6) >> 16;
183 packet
[4] = (paddinglen
-6) >> 8;
184 packet
[5] = (paddinglen
-6);
185 memcpy (packet
+6, "GnuPG/PAD", 10); /* Packet subtype. */
186 if (es_fwrite (packet
, 16, 1, fp
) != 1)
188 memset (packet
, 0, 32);
189 for (paddinglen
-=16; paddinglen
>= 32; paddinglen
-= 32)
190 if (es_fwrite (packet
, 32, 1, fp
) != 1)
193 if (es_fwrite (packet
, paddinglen
, 1, fp
) != 1)
198 err
= gpg_error_from_syserror ();
199 log_error ("error closing `%s': %s\n",
200 filename
, gpg_strerror (err
));
209 err
= gpg_error_from_syserror ();
210 log_error ("error writing header to `%s': %s\n",
211 filename
, gpg_strerror (err
));
219 /* Create a new container under the name FILENAME and intialize it
220 using the current settings. If the file already exists an error is
223 g13_create_container (ctrl_t ctrl
, const char *filename
)
227 void *keyblob
= NULL
;
229 void *enckeyblob
= NULL
;
230 size_t enckeybloblen
;
231 char *detachedname
= NULL
;
233 tupledesc_t tuples
= NULL
;
235 /* A quick check to see that no container with that name already
237 if (!access (filename
, F_OK
))
238 return gpg_error (GPG_ERR_EEXIST
);
240 /* Take a lock and proceed with the creation. If there is a lock we
241 immediately return an error because for creation it does not make
243 lock
= create_dotlock (filename
);
245 return gpg_error_from_syserror ();
246 if (make_dotlock (lock
, 0))
248 err
= gpg_error_from_syserror ();
254 /* Check again that the file does not exist. */
258 if (!stat (filename
, &sb
))
260 err
= gpg_error (GPG_ERR_EEXIST
);
264 /* And a possible detached file or directory may not exist either. */
265 err
= be_get_detached_name (ctrl
->conttype
, filename
,
266 &detachedname
, &detachedisdir
);
273 if (!stat (detachedname
, &sb
))
275 err
= gpg_error (GPG_ERR_EEXIST
);
280 /* Create a new keyblob. */
281 err
= create_new_keyblob (ctrl
, !!detachedname
, &keyblob
, &keybloblen
);
285 /* Encrypt that keyblob. */
286 err
= encrypt_keyblob (ctrl
, keyblob
, keybloblen
,
287 &enckeyblob
, &enckeybloblen
);
291 /* Put a copy of the keyblob into a tuple structure. */
292 err
= create_tupledesc (&tuples
, keyblob
, keybloblen
);
296 /* if (opt.verbose) */
297 /* dump_keyblob (tuples); */
299 /* Write out the header, the encrypted keyblob and some padding. */
300 err
= write_keyblob (filename
, enckeyblob
, enckeybloblen
);
304 /* Create and append the container. FIXME: We should pass the
305 estream object in addition to the filename, so that the backend
306 can append the container to the g13 file. */
307 err
= be_create_container (ctrl
, ctrl
->conttype
, filename
, -1, tuples
);
311 destroy_tupledesc (tuples
);
312 xfree (detachedname
);
315 destroy_dotlock (lock
);