Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/rsa/rsa_import.c
4396 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
#include "tomcrypt.h"
10
11
/**
12
@file rsa_import.c
13
Import a PKCS RSA key, Tom St Denis
14
*/
15
16
#ifdef LTC_MRSA
17
18
/**
19
Import an RSAPublicKey or RSAPrivateKey [two-prime only, only support >= 1024-bit keys, defined in PKCS #1 v2.1]
20
@param in The packet to import from
21
@param inlen It's length (octets)
22
@param key [out] Destination for newly imported key
23
@return CRYPT_OK if successful, upon error allocated memory is freed
24
*/
25
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key)
26
{
27
int err;
28
void *zero;
29
unsigned char *tmpbuf=NULL;
30
unsigned long tmpbuf_len;
31
32
LTC_ARGCHK(in != NULL);
33
LTC_ARGCHK(key != NULL);
34
LTC_ARGCHK(ltc_mp.name != NULL);
35
36
/* init key */
37
if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ,
38
&key->dP, &key->qP, &key->p, &key->q, NULL)) != CRYPT_OK) {
39
return err;
40
}
41
42
/* see if the OpenSSL DER format RSA public key will work */
43
tmpbuf_len = inlen;
44
tmpbuf = XCALLOC(1, tmpbuf_len);
45
if (tmpbuf == NULL) {
46
err = CRYPT_MEM;
47
goto LBL_ERR;
48
}
49
50
err = der_decode_subject_public_key_info(in, inlen,
51
PKA_RSA, tmpbuf, &tmpbuf_len,
52
LTC_ASN1_NULL, NULL, 0);
53
54
if (err == CRYPT_OK) { /* SubjectPublicKeyInfo format */
55
56
/* now it should be SEQUENCE { INTEGER, INTEGER } */
57
if ((err = der_decode_sequence_multi(tmpbuf, tmpbuf_len,
58
LTC_ASN1_INTEGER, 1UL, key->N,
59
LTC_ASN1_INTEGER, 1UL, key->e,
60
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
61
goto LBL_ERR;
62
}
63
key->type = PK_PUBLIC;
64
err = CRYPT_OK;
65
goto LBL_FREE;
66
}
67
68
/* not SSL public key, try to match against PKCS #1 standards */
69
err = der_decode_sequence_multi(in, inlen, LTC_ASN1_INTEGER, 1UL, key->N,
70
LTC_ASN1_EOL, 0UL, NULL);
71
72
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
73
goto LBL_ERR;
74
}
75
76
if (mp_cmp_d(key->N, 0) == LTC_MP_EQ) {
77
if ((err = mp_init(&zero)) != CRYPT_OK) {
78
goto LBL_ERR;
79
}
80
/* it's a private key */
81
if ((err = der_decode_sequence_multi(in, inlen,
82
LTC_ASN1_INTEGER, 1UL, zero,
83
LTC_ASN1_INTEGER, 1UL, key->N,
84
LTC_ASN1_INTEGER, 1UL, key->e,
85
LTC_ASN1_INTEGER, 1UL, key->d,
86
LTC_ASN1_INTEGER, 1UL, key->p,
87
LTC_ASN1_INTEGER, 1UL, key->q,
88
LTC_ASN1_INTEGER, 1UL, key->dP,
89
LTC_ASN1_INTEGER, 1UL, key->dQ,
90
LTC_ASN1_INTEGER, 1UL, key->qP,
91
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
92
mp_clear(zero);
93
goto LBL_ERR;
94
}
95
mp_clear(zero);
96
key->type = PK_PRIVATE;
97
} else if (mp_cmp_d(key->N, 1) == LTC_MP_EQ) {
98
/* we don't support multi-prime RSA */
99
err = CRYPT_PK_INVALID_TYPE;
100
goto LBL_ERR;
101
} else {
102
/* it's a public key and we lack e */
103
if ((err = der_decode_sequence_multi(in, inlen,
104
LTC_ASN1_INTEGER, 1UL, key->N,
105
LTC_ASN1_INTEGER, 1UL, key->e,
106
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
107
goto LBL_ERR;
108
}
109
key->type = PK_PUBLIC;
110
}
111
err = CRYPT_OK;
112
goto LBL_FREE;
113
114
LBL_ERR:
115
mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL);
116
117
LBL_FREE:
118
if (tmpbuf != NULL)
119
XFREE(tmpbuf);
120
121
return err;
122
}
123
124
#endif /* LTC_MRSA */
125
126