cfb8: Fix decrypt path
[gd/nettle] / rsa2openpgp.c
1 /* rsa2openpgp.c
2
3    Converting rsa keys to OpenPGP format.
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 <string.h>
39 #include <time.h>
40
41 #include "rsa.h"
42
43 #include "buffer.h"
44 #include "pgp.h"
45
46
47 /* According to RFC 2440, a public key consists of the following packets:
48  *
49  * Public key packet
50  *
51  * Zero or more revocation signatures
52  *
53  * One or more User ID packets
54  *
55  * After each User ID packet, zero or more signature packets
56  *
57  * Zero or more Subkey packets
58  *
59  * After each Subkey packet, one signature packet, optionally a
60  * revocation.
61  *
62  * Currently, we generate a public key packet, a single user id, and a
63  * signature. */
64
65 int
66 rsa_keypair_to_openpgp(struct nettle_buffer *buffer,
67                        const struct rsa_public_key *pub,
68                        const struct rsa_private_key *priv,
69                        /* A single user id. NUL-terminated utf8. */
70                        const char *userid)
71 {
72   time_t now = time(NULL);
73
74   unsigned key_start;
75   unsigned userid_start;
76   
77   struct sha1_ctx key_hash;
78   struct sha1_ctx signature_hash;
79   uint8_t fingerprint[SHA1_DIGEST_SIZE];
80   
81   key_start = buffer->size;
82   
83   if (!pgp_put_public_rsa_key(buffer, pub, now))
84     return 0;
85
86   /* userid packet */
87   userid_start = buffer->size;
88   if (!pgp_put_userid(buffer, strlen(userid), (const uint8_t *) userid))
89     return 0;
90
91   /* FIXME: We hash the key first, and then the user id. Is this right? */
92   sha1_init(&key_hash);
93   sha1_update(&key_hash,
94               userid_start - key_start,
95               buffer->contents + key_start);
96
97   signature_hash = key_hash;
98   sha1_digest(&key_hash, sizeof(fingerprint), fingerprint);
99
100   sha1_update(&signature_hash,
101               buffer->size - userid_start,
102               buffer->contents + userid_start);
103   
104   return pgp_put_rsa_sha1_signature(buffer,
105                                     priv,
106                                     fingerprint + SHA1_DIGEST_SIZE - 8,
107                                     PGP_SIGN_CERTIFICATION,
108                                     &signature_hash);
109 }