983d5a23a1743d0ce722b4bee9cb3e36ea973d40
[platform/upstream/nettle.git] / pgp-encode.c
1 /* pgp-encode.c
2
3    PGP related functions.
4
5    Copyright (C) 2001, 2002 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "pgp.h"
43
44 #include "base64.h"
45 #include "buffer.h"
46 #include "macros.h"
47 #include "rsa.h"
48
49 int
50 pgp_put_uint32(struct nettle_buffer *buffer, uint32_t i)
51 {
52   uint8_t *p = nettle_buffer_space(buffer, 4);
53   if (!p)
54     return 0;
55   
56   WRITE_UINT32(p, i);
57   return 1;
58 }
59
60 int
61 pgp_put_uint16(struct nettle_buffer *buffer, unsigned i)
62 {
63   uint8_t *p = nettle_buffer_space(buffer, 2);
64   if (!p)
65     return 0;
66   
67   WRITE_UINT16(p, i);
68   return 1;
69 }
70
71 int
72 pgp_put_mpi(struct nettle_buffer *buffer, const mpz_t x)
73 {
74   unsigned bits = mpz_sizeinbase(x, 2);
75   unsigned octets = (bits + 7) / 8;
76
77   uint8_t *p;
78
79   /* FIXME: What's the correct representation of zero? */
80   if (!pgp_put_uint16(buffer, bits))
81     return 0;
82   
83   p = nettle_buffer_space(buffer, octets);
84
85   if (!p)
86     return 0;
87   
88   nettle_mpz_get_str_256(octets, p, x);
89
90   return 1;
91 }
92
93 int
94 pgp_put_string(struct nettle_buffer *buffer,
95                unsigned length,
96                const uint8_t *s)
97 {
98   return nettle_buffer_write(buffer, length, s);
99 }
100
101 #if 0
102 static unsigned
103 length_field(unsigned length)
104 {
105   if (length < PGP_LENGTH_TWO_OCTET)
106     return 1;
107   else if (length < PGP_LENGTH_FOUR_OCTETS)
108     return 2;
109   else return 4;
110 }
111 #endif
112
113 /*   bodyLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192
114  *   ==> bodyLen - 192 + 192 << 8 = (1st_octet << 8) + (2nd_octet) 
115  */
116
117 #define LENGTH_TWO_OFFSET (192 * 255)
118
119 int
120 pgp_put_length(struct nettle_buffer *buffer,
121                unsigned length)
122 {
123   if (length < PGP_LENGTH_TWO_OCTETS)
124     return NETTLE_BUFFER_PUTC(buffer, length);
125
126   else if (length < PGP_LENGTH_FOUR_OCTETS)
127     return pgp_put_uint16(buffer, length + LENGTH_TWO_OFFSET);
128   else
129     return NETTLE_BUFFER_PUTC(buffer, 0xff) && pgp_put_uint32(buffer, length);
130 }
131
132 /* Uses the "new" packet format */
133 int
134 pgp_put_header(struct nettle_buffer *buffer,
135                unsigned tag, unsigned length)
136 {
137   assert(tag < 0x40);
138
139   return (NETTLE_BUFFER_PUTC(buffer, 0xC0 | tag)
140           && pgp_put_length(buffer, length));  
141 }
142
143 /* FIXME: Should we abort or return error if the length and the field
144  * size don't match? */
145 void
146 pgp_put_header_length(struct nettle_buffer *buffer,
147                       /* start of the header */
148                       unsigned start,
149                       unsigned field_size)
150 {
151   unsigned length;
152   switch (field_size)
153     {
154     case 1:
155       length = buffer->size - (start + 2);
156       assert(length < PGP_LENGTH_TWO_OCTETS);
157       buffer->contents[start + 1] = length;
158       break;
159     case 2:
160       length = buffer->size - (start + 3);
161       assert(length < PGP_LENGTH_FOUR_OCTETS
162              && length >= PGP_LENGTH_TWO_OCTETS);
163       WRITE_UINT16(buffer->contents + start + 1, length + LENGTH_TWO_OFFSET);
164       break;
165     case 4:
166       length = buffer->size - (start + 5);
167       WRITE_UINT32(buffer->contents + start + 2, length);
168       break;
169     default:
170       abort();
171     }
172 }
173
174 int
175 pgp_put_userid(struct nettle_buffer *buffer,
176                unsigned length,
177                const uint8_t *name)
178 {
179   return (pgp_put_header(buffer, PGP_TAG_USERID, length)
180           && pgp_put_string(buffer, length, name));
181 }
182
183 unsigned
184 pgp_sub_packet_start(struct nettle_buffer *buffer)
185 {
186   return nettle_buffer_space(buffer, 2) ? buffer->size : 0;
187 }
188
189 int
190 pgp_put_sub_packet(struct nettle_buffer *buffer,
191                    unsigned type,
192                    unsigned length,
193                    const uint8_t *data)
194 {
195   return (pgp_put_length(buffer, length + 1)
196           && NETTLE_BUFFER_PUTC(buffer, type)
197           && pgp_put_string(buffer, length, data));
198 }
199
200 void
201 pgp_sub_packet_end(struct nettle_buffer *buffer, unsigned start)
202 {
203   unsigned length;
204   
205   assert(start >= 2);
206   assert(start <= buffer->size);
207
208   length = buffer->size - start;
209   WRITE_UINT32(buffer->contents + start - 2, length);
210 }
211
212 int
213 pgp_put_public_rsa_key(struct nettle_buffer *buffer,
214                        const struct rsa_public_key *pub,
215                        time_t timestamp)
216 {
217   /* Public key packet, version 4 */
218   unsigned start;
219   unsigned length;
220
221   /* Size of packet is 16 + the size of e and n */
222   length = (4 * 4
223           + nettle_mpz_sizeinbase_256_u(pub->n)
224           + nettle_mpz_sizeinbase_256_u(pub->e));
225
226   if (!pgp_put_header(buffer, PGP_TAG_PUBLIC_KEY, length))
227     return 0;
228
229   start = buffer->size;
230   
231   if (! (pgp_put_header(buffer, PGP_TAG_PUBLIC_KEY,
232                         /* Assume that we need two octets */
233                         PGP_LENGTH_TWO_OCTETS)
234          && pgp_put_uint32(buffer, 4)        /* Version */  
235          && pgp_put_uint32(buffer, timestamp)/* Time stamp */
236          && pgp_put_uint32(buffer, PGP_RSA)  /* Algorithm */
237          && pgp_put_mpi(buffer, pub->n)
238          && pgp_put_mpi(buffer, pub->e)) )
239     return 0;
240
241   assert(buffer->size == start + length);
242
243   return 1;
244 }
245
246 int
247 pgp_put_rsa_sha1_signature(struct nettle_buffer *buffer,
248                            const struct rsa_private_key *key,
249                            const uint8_t *keyid,
250                            unsigned type,
251                            struct sha1_ctx *hash)
252 {
253   unsigned signature_start = buffer->size;
254   unsigned hash_end;
255   unsigned sub_packet_start;
256   uint8_t trailer[6];
257   mpz_t s;
258   
259   /* Signature packet. The packet could reasonably be both smaller and
260    * larger than 192, so for simplicity we use the 4 octet header
261    * form. */
262
263   if (! (pgp_put_header(buffer, PGP_TAG_SIGNATURE, PGP_LENGTH_FOUR_OCTETS)
264          && NETTLE_BUFFER_PUTC(buffer, 4)  /* Version */
265          && NETTLE_BUFFER_PUTC(buffer, type)
266          /* Could also be PGP_RSA_SIGN */
267          && NETTLE_BUFFER_PUTC(buffer, PGP_RSA)
268          && NETTLE_BUFFER_PUTC(buffer, PGP_SHA1)
269          && pgp_put_uint16(buffer, 0)))  /* Hashed subpacket length */
270     return 0;
271
272   hash_end = buffer->size;
273
274   sha1_update(hash,
275               hash_end - signature_start,
276               buffer->contents + signature_start);
277
278   trailer[0] = 4; trailer[1] = 0xff;
279   WRITE_UINT32(trailer + 2, buffer->size - signature_start);
280
281   sha1_update(hash, sizeof(trailer), trailer);
282
283   {
284     struct sha1_ctx hcopy = *hash;
285     uint8_t *p = nettle_buffer_space(buffer, 2);
286     if (!p)
287       return 0;
288     
289     sha1_digest(&hcopy, 2, p);
290   }
291
292   /* One "sub-packet" field with the issuer keyid */
293   sub_packet_start = pgp_sub_packet_start(buffer);
294   if (!sub_packet_start)
295     return 0;
296
297   if (pgp_put_sub_packet(buffer, PGP_SUBPACKET_ISSUER_KEY_ID, 8, keyid))
298     {
299       pgp_sub_packet_end(buffer, sub_packet_start);
300       return 0;
301     }
302     
303   mpz_init(s);
304   if (!(rsa_sha1_sign(key, hash, s)
305         && pgp_put_mpi(buffer, s)))
306     {
307       mpz_clear(s);
308       return 0;
309     }
310
311   mpz_clear(s);
312   pgp_put_header_length(buffer, signature_start, 4);
313
314   return 1;
315 }
316
317 #define CRC24_INIT 0x0b704ceL
318 #define CRC24_POLY 0x1864cfbL
319
320 uint32_t
321 pgp_crc24(unsigned length, const uint8_t *data)
322 {
323   uint32_t crc = CRC24_INIT;
324
325   unsigned i;
326   for (i = 0; i<length; i++)
327     {
328       unsigned j;
329       crc ^= ((unsigned) (data[i]) << 16);
330       for (j = 0; j<8; j++)
331         {
332           crc <<= 1;
333           if (crc & 0x1000000)
334             crc ^= CRC24_POLY;
335         }
336     }
337   assert(crc < 0x1000000);
338   return crc;
339 }
340
341
342 #define WRITE(buffer, s) (nettle_buffer_write(buffer, strlen((s)), (s)))
343
344 /* 15 base 64 groups data per line */
345 #define BINARY_PER_LINE 45
346 #define TEXT_PER_LINE BASE64_ENCODE_LENGTH(BINARY_PER_LINE)
347
348 int
349 pgp_armor(struct nettle_buffer *buffer,
350           const char *tag,
351           unsigned length,
352           const uint8_t *data)
353 {
354   struct base64_encode_ctx ctx;
355   
356   unsigned crc = pgp_crc24(length, data);
357
358   base64_encode_init(&ctx);
359   
360   if (! (WRITE(buffer, "BEGIN PGP ")
361          && WRITE(buffer, tag)
362          && WRITE(buffer, "\nComment: Nettle\n\n")))
363     return 0;
364
365   for (;
366        length >= BINARY_PER_LINE;
367        length -= BINARY_PER_LINE, data += BINARY_PER_LINE)
368     {
369       unsigned done;
370       uint8_t *p
371         = nettle_buffer_space(buffer, TEXT_PER_LINE);
372       
373       if (!p)
374         return 0;
375
376       done = base64_encode_update(&ctx, p, BINARY_PER_LINE, data);
377       assert(done <= TEXT_PER_LINE);
378
379       /* FIXME: Create some official way to do this */
380       buffer->size -= (TEXT_PER_LINE - done);
381       
382       if (!NETTLE_BUFFER_PUTC(buffer, '\n'))
383         return 0;
384     }
385
386   if (length)
387     {
388       unsigned text_size = BASE64_ENCODE_LENGTH(length)
389         + BASE64_ENCODE_FINAL_LENGTH;
390       unsigned done;
391       
392       uint8_t *p
393         = nettle_buffer_space(buffer, text_size);
394       if (!p)
395         return 0;
396
397       done = base64_encode_update(&ctx, p, length, data);
398       done += base64_encode_final(&ctx, p + done);
399
400       /* FIXME: Create some official way to do this */
401       buffer->size -= (text_size - done);
402       
403       if (!NETTLE_BUFFER_PUTC(buffer, '\n'))
404         return 0;
405     }
406   /* Checksum */
407   if (!NETTLE_BUFFER_PUTC(buffer, '='))
408     return 0;
409
410   {
411     uint8_t *p = nettle_buffer_space(buffer, 4);
412     if (!p)
413       return 0;
414     base64_encode_group(p, crc);
415   }
416   
417   return (WRITE(buffer, "\nBEGIN PGP ")
418           && WRITE(buffer, tag)
419           && NETTLE_BUFFER_PUTC(buffer, '\n'));
420 }