Mercurial > dropbear
annotate src/pk/rsa/rsa_import.c @ 390:d8e44bef7917 libtomcrypt-dropbear
Replace // with /* */
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 11 Jan 2007 04:28:09 +0000 |
parents | d5faf4814ddb |
children |
rev | line source |
---|---|
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 * |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 * LibTomCrypt is a library that provides various cryptographic |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * algorithms in a highly modular and flexible manner. |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 * |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 * The library is free for all purposes without any express |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 * guarantee it works. |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 * |
380 | 9 * Tom St Denis, [email protected], http://libtomcrypt.com |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 #include "tomcrypt.h" |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 /** |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 @file rsa_import.c |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 Import a PKCS RSA key, Tom St Denis |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 #ifdef MRSA |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 /** |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 Import an RSAPublicKey or RSAPrivateKey [two-prime only, only support >= 1024-bit keys, defined in PKCS #1 v2.1] |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 @param in The packet to import from |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 @param inlen It's length (octets) |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 @param key [out] Destination for newly imported key |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
25 @return CRYPT_OK if successful, upon error allocated memory is freed |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key) |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
28 { |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 int err; |
380 | 30 void *zero; |
31 unsigned char *tmpbuf; | |
32 unsigned long t, x, y, z, tmpoid[16]; | |
33 ltc_asn1_list ssl_pubkey_hashoid[2]; | |
34 ltc_asn1_list ssl_pubkey[2]; | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 |
380 | 36 LTC_ARGCHK(in != NULL); |
37 LTC_ARGCHK(key != NULL); | |
38 LTC_ARGCHK(ltc_mp.name != NULL); | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
40 /* init key */ |
380 | 41 if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, |
42 &key->dP, &key->qP, &key->p, &key->q, NULL)) != CRYPT_OK) { | |
43 return err; | |
44 } | |
45 | |
46 /* see if the OpenSSL DER format RSA public key will work */ | |
47 tmpbuf = XCALLOC(1, MAX_RSA_SIZE*8); | |
48 if (tmpbuf == NULL) { | |
49 err = CRYPT_MEM; | |
50 goto LBL_ERR; | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 } |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 |
380 | 53 /* this includes the internal hash ID and optional params (NULL in this case) */ |
54 LTC_SET_ASN1(ssl_pubkey_hashoid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0])); | |
55 LTC_SET_ASN1(ssl_pubkey_hashoid, 1, LTC_ASN1_NULL, NULL, 0); | |
56 | |
57 /* the actual format of the SSL DER key is odd, it stores a RSAPublicKey in a **BIT** string ... so we have to extract it | |
58 then proceed to convert bit to octet | |
59 */ | |
60 LTC_SET_ASN1(ssl_pubkey, 0, LTC_ASN1_SEQUENCE, &ssl_pubkey_hashoid, 2); | |
61 LTC_SET_ASN1(ssl_pubkey, 1, LTC_ASN1_BIT_STRING, tmpbuf, MAX_RSA_SIZE*8); | |
62 | |
63 if (der_decode_sequence(in, inlen, | |
64 ssl_pubkey, 2UL) == CRYPT_OK) { | |
65 | |
66 /* ok now we have to reassemble the BIT STRING to an OCTET STRING. Thanks OpenSSL... */ | |
67 for (t = y = z = x = 0; x < ssl_pubkey[1].size; x++) { | |
68 y = (y << 1) | tmpbuf[x]; | |
69 if (++z == 8) { | |
70 tmpbuf[t++] = (unsigned char)y; | |
71 y = 0; | |
72 z = 0; | |
73 } | |
74 } | |
75 | |
76 /* now it should be SEQUENCE { INTEGER, INTEGER } */ | |
77 if ((err = der_decode_sequence_multi(tmpbuf, t, | |
78 LTC_ASN1_INTEGER, 1UL, key->N, | |
79 LTC_ASN1_INTEGER, 1UL, key->e, | |
80 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { | |
81 XFREE(tmpbuf); | |
82 goto LBL_ERR; | |
83 } | |
84 XFREE(tmpbuf); | |
85 key->type = PK_PUBLIC; | |
86 return CRYPT_OK; | |
87 } | |
88 XFREE(tmpbuf); | |
89 | |
90 /* not SSL public key, try to match against PKCS #1 standards */ | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
91 if ((err = der_decode_sequence_multi(in, inlen, |
380 | 92 LTC_ASN1_INTEGER, 1UL, key->N, |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
93 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
94 goto LBL_ERR; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
95 } |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
96 |
380 | 97 if (mp_cmp_d(key->N, 0) == LTC_MP_EQ) { |
98 if ((err = mp_init(&zero)) != CRYPT_OK) { | |
99 goto LBL_ERR; | |
100 } | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
101 /* it's a private key */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
102 if ((err = der_decode_sequence_multi(in, inlen, |
380 | 103 LTC_ASN1_INTEGER, 1UL, zero, |
104 LTC_ASN1_INTEGER, 1UL, key->N, | |
105 LTC_ASN1_INTEGER, 1UL, key->e, | |
106 LTC_ASN1_INTEGER, 1UL, key->d, | |
107 LTC_ASN1_INTEGER, 1UL, key->p, | |
108 LTC_ASN1_INTEGER, 1UL, key->q, | |
109 LTC_ASN1_INTEGER, 1UL, key->dP, | |
110 LTC_ASN1_INTEGER, 1UL, key->dQ, | |
111 LTC_ASN1_INTEGER, 1UL, key->qP, | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
112 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { |
380 | 113 mp_clear(zero); |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
114 goto LBL_ERR; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
115 } |
380 | 116 mp_clear(zero); |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
117 key->type = PK_PRIVATE; |
380 | 118 } else if (mp_cmp_d(key->N, 1) == LTC_MP_EQ) { |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
119 /* we don't support multi-prime RSA */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
120 err = CRYPT_PK_INVALID_TYPE; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
121 goto LBL_ERR; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
122 } else { |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
123 /* it's a public key and we lack e */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
124 if ((err = der_decode_sequence_multi(in, inlen, |
380 | 125 LTC_ASN1_INTEGER, 1UL, key->N, |
126 LTC_ASN1_INTEGER, 1UL, key->e, | |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
127 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
128 goto LBL_ERR; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
129 } |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
130 key->type = PK_PUBLIC; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
131 } |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
132 return CRYPT_OK; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
133 LBL_ERR: |
380 | 134 mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL); |
280
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
135 return err; |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
136 } |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
137 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
138 #endif /* MRSA */ |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
139 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
140 |
59400faa4b44
Re-import libtomcrypt 1.05 for cleaner propagating.
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
141 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_import.c,v $ */ |
380 | 142 /* $Revision: 1.21 $ */ |
143 /* $Date: 2006/12/04 22:23:27 $ */ |