3 Converting rsa keys to OpenPGP format.
5 Copyright (C) 2001, 2002 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
47 /* According to RFC 2440, a public key consists of the following packets:
51 * Zero or more revocation signatures
53 * One or more User ID packets
55 * After each User ID packet, zero or more signature packets
57 * Zero or more Subkey packets
59 * After each Subkey packet, one signature packet, optionally a
62 * Currently, we generate a public key packet, a single user id, and a
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. */
72 time_t now = time(NULL);
75 unsigned userid_start;
77 struct sha1_ctx key_hash;
78 struct sha1_ctx signature_hash;
79 uint8_t fingerprint[SHA1_DIGEST_SIZE];
81 key_start = buffer->size;
83 if (!pgp_put_public_rsa_key(buffer, pub, now))
87 userid_start = buffer->size;
88 if (!pgp_put_userid(buffer, strlen(userid), (const uint8_t *) userid))
91 /* FIXME: We hash the key first, and then the user id. Is this right? */
93 sha1_update(&key_hash,
94 userid_start - key_start,
95 buffer->contents + key_start);
97 signature_hash = key_hash;
98 sha1_digest(&key_hash, sizeof(fingerprint), fingerprint);
100 sha1_update(&signature_hash,
101 buffer->size - userid_start,
102 buffer->contents + userid_start);
104 return pgp_put_rsa_sha1_signature(buffer,
106 fingerprint + SHA1_DIGEST_SIZE - 8,
107 PGP_SIGN_CERTIFICATION,