Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / rsa2openpgp.c
blob4c62f495132ec58fcc0f59c9ba3e47d4e73e212b
1 /* rsa2openpgp.c
3 * Converting rsa keys to OpenPGP format.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001, 2002 Niels Möller
9 *
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <string.h>
31 #include <time.h>
33 #include "rsa.h"
35 #include "buffer.h"
36 #include "pgp.h"
39 /* According to RFC 2440, a public key consists of the following packets:
41 * Public key packet
43 * Zero or more revocation signatures
45 * One or more User ID packets
47 * After each User ID packet, zero or more signature packets
49 * Zero or more Subkey packets
51 * After each Subkey packet, one signature packet, optionally a
52 * revocation.
54 * Currently, we generate a public key packet, a single user id, and a
55 * signature. */
57 int
58 rsa_keypair_to_openpgp(struct nettle_buffer *buffer,
59 const struct rsa_public_key *pub,
60 const struct rsa_private_key *priv,
61 /* A single user id. NUL-terminated utf8. */
62 const char *userid)
64 time_t now = time(NULL);
66 unsigned key_start;
67 unsigned userid_start;
69 struct sha1_ctx key_hash;
70 struct sha1_ctx signature_hash;
71 uint8_t fingerprint[SHA1_DIGEST_SIZE];
73 key_start = buffer->size;
75 if (!pgp_put_public_rsa_key(buffer, pub, now))
76 return 0;
78 /* userid packet */
79 userid_start = buffer->size;
80 if (!pgp_put_userid(buffer, strlen(userid), userid))
81 return 0;
83 /* FIXME: We hash the key first, and then the user id. Is this right? */
84 sha1_init(&key_hash);
85 sha1_update(&key_hash,
86 userid_start - key_start,
87 buffer->contents + key_start);
89 signature_hash = key_hash;
90 sha1_digest(&key_hash, sizeof(fingerprint), fingerprint);
92 sha1_update(&signature_hash,
93 buffer->size - userid_start,
94 buffer->contents + userid_start);
96 return pgp_put_rsa_sha1_signature(buffer,
97 priv,
98 fingerprint + SHA1_DIGEST_SIZE - 8,
99 PGP_SIGN_CERTIFICATION,
100 &signature_hash);