summaryrefslogtreecommitdiff
path: root/src/libstrongswan/credentials/keys/public_key.c
blob: d6f211a34579a029cdfeab1c95873a31ad79152d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (C) 2015 Tobias Brunner
 * Copyright (C) 2007 Martin Willi
 * Copyright (C) 2014-2015 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <asn1/oid.h>

#include "public_key.h"

ENUM(key_type_names, KEY_ANY, KEY_BLISS,
	"ANY",
	"RSA",
	"ECDSA",
	"DSA",
	"BLISS"
);

ENUM(signature_scheme_names, SIGN_UNKNOWN, SIGN_BLISS_WITH_SHA3_512,
	"UNKNOWN",
	"RSA_EMSA_PKCS1_NULL",
	"RSA_EMSA_PKCS1_MD5",
	"RSA_EMSA_PKCS1_SHA1",
	"RSA_EMSA_PKCS1_SHA224",
	"RSA_EMSA_PKCS1_SHA256",
	"RSA_EMSA_PKCS1_SHA384",
	"RSA_EMSA_PKCS1_SHA512",
	"ECDSA_WITH_SHA1_DER",
	"ECDSA_WITH_SHA256_DER",
	"ECDSA_WITH_SHA384_DER",
	"ECDSA_WITH_SHA512_DER",
	"ECDSA_WITH_NULL",
	"ECDSA-256",
	"ECDSA-384",
	"ECDSA-521",
	"BLISS_WITH_SHA2_256",
	"BLISS_WITH_SHA2_384",
	"BLISS_WITH_SHA2_512",
	"BLISS_WITH_SHA3_256",
	"BLISS_WITH_SHA3_384",
	"BLISS_WITH_SHA3_512",
);

ENUM(encryption_scheme_names, ENCRYPT_UNKNOWN, ENCRYPT_RSA_OAEP_SHA512,
	"ENCRYPT_UNKNOWN",
	"ENCRYPT_RSA_PKCS1",
	"ENCRYPT_RSA_OAEP_SHA1",
	"ENCRYPT_RSA_OAEP_SHA224",
	"ENCRYPT_RSA_OAEP_SHA256",
	"ENCRYPT_RSA_OAEP_SHA384",
	"ENCRYPT_RSA_OAEP_SHA512",
);

/**
 * See header.
 */
bool public_key_equals(public_key_t *this, public_key_t *other)
{
	cred_encoding_type_t type;
	chunk_t a, b;

	if (this == other)
	{
		return TRUE;
	}

	for (type = 0; type < CRED_ENCODING_MAX; type++)
	{
		if (this->get_fingerprint(this, type, &a) &&
			other->get_fingerprint(other, type, &b))
		{
			return chunk_equals(a, b);
		}
	}
	return FALSE;
}

/**
 * See header.
 */
bool public_key_has_fingerprint(public_key_t *public, chunk_t fingerprint)
{
	cred_encoding_type_t type;
	chunk_t current;

	for (type = 0; type < KEYID_MAX; type++)
	{
		if (public->get_fingerprint(public, type, &current) &&
			chunk_equals(current, fingerprint))
		{
			return TRUE;
		}
	}
	return FALSE;
}

/*
 * Defined in header.
 */
signature_scheme_t signature_scheme_from_oid(int oid)
{
	switch (oid)
	{
		case OID_MD5_WITH_RSA:
		case OID_MD5:
			return SIGN_RSA_EMSA_PKCS1_MD5;
		case OID_SHA1_WITH_RSA:
		case OID_SHA1:
			return SIGN_RSA_EMSA_PKCS1_SHA1;
		case OID_SHA224_WITH_RSA:
		case OID_SHA224:
			return SIGN_RSA_EMSA_PKCS1_SHA224;
		case OID_SHA256_WITH_RSA:
		case OID_SHA256:
			return SIGN_RSA_EMSA_PKCS1_SHA256;
		case OID_SHA384_WITH_RSA:
		case OID_SHA384:
			return SIGN_RSA_EMSA_PKCS1_SHA384;
		case OID_SHA512_WITH_RSA:
		case OID_SHA512:
			return SIGN_RSA_EMSA_PKCS1_SHA512;
		case OID_ECDSA_WITH_SHA1:
		case OID_EC_PUBLICKEY:
			return SIGN_ECDSA_WITH_SHA1_DER;
		case OID_ECDSA_WITH_SHA256:
			return SIGN_ECDSA_WITH_SHA256_DER;
		case OID_ECDSA_WITH_SHA384:
			return SIGN_ECDSA_WITH_SHA384_DER;
		case OID_ECDSA_WITH_SHA512:
			return SIGN_ECDSA_WITH_SHA512_DER;
		case OID_BLISS_PUBLICKEY:
		case OID_BLISS_WITH_SHA2_512:
			return SIGN_BLISS_WITH_SHA2_512;
		case OID_BLISS_WITH_SHA2_384:
			return SIGN_BLISS_WITH_SHA2_384;
		case OID_BLISS_WITH_SHA2_256:
			return SIGN_BLISS_WITH_SHA2_256;
		case OID_BLISS_WITH_SHA3_512:
			return SIGN_BLISS_WITH_SHA3_512;
		case OID_BLISS_WITH_SHA3_384:
			return SIGN_BLISS_WITH_SHA3_384;
		case OID_BLISS_WITH_SHA3_256:
			return SIGN_BLISS_WITH_SHA3_256;
	}
	return SIGN_UNKNOWN;
}

/*
 * Defined in header.
 */
int signature_scheme_to_oid(signature_scheme_t scheme)
{
	switch (scheme)
	{
		case SIGN_UNKNOWN:
		case SIGN_RSA_EMSA_PKCS1_NULL:
		case SIGN_ECDSA_WITH_NULL:
		case SIGN_ECDSA_256:
		case SIGN_ECDSA_384:
		case SIGN_ECDSA_521:
			break;
		case SIGN_RSA_EMSA_PKCS1_MD5:
			return OID_MD5_WITH_RSA;
		case SIGN_RSA_EMSA_PKCS1_SHA1:
			return OID_SHA1_WITH_RSA;
		case SIGN_RSA_EMSA_PKCS1_SHA224:
			return OID_SHA224_WITH_RSA;
		case SIGN_RSA_EMSA_PKCS1_SHA256:
			return OID_SHA256_WITH_RSA;
		case SIGN_RSA_EMSA_PKCS1_SHA384:
			return OID_SHA384_WITH_RSA;
		case SIGN_RSA_EMSA_PKCS1_SHA512:
			return OID_SHA512_WITH_RSA;
		case SIGN_ECDSA_WITH_SHA1_DER:
			return OID_ECDSA_WITH_SHA1;
		case SIGN_ECDSA_WITH_SHA256_DER:
			return OID_ECDSA_WITH_SHA256;
		case SIGN_ECDSA_WITH_SHA384_DER:
			return OID_ECDSA_WITH_SHA384;
		case SIGN_ECDSA_WITH_SHA512_DER:
			return OID_ECDSA_WITH_SHA512;
		case SIGN_BLISS_WITH_SHA2_256:
			return OID_BLISS_WITH_SHA2_256;
		case SIGN_BLISS_WITH_SHA2_384:
			return OID_BLISS_WITH_SHA2_384;
		case SIGN_BLISS_WITH_SHA2_512:
			return OID_BLISS_WITH_SHA2_512;
		case SIGN_BLISS_WITH_SHA3_256:
			return OID_BLISS_WITH_SHA3_256;
		case SIGN_BLISS_WITH_SHA3_384:
			return OID_BLISS_WITH_SHA3_384;
		case SIGN_BLISS_WITH_SHA3_512:
			return OID_BLISS_WITH_SHA3_512;
	}
	return OID_UNKNOWN;
}

/**
 * Map for signature schemes to the key type and maximum key size allowed.
 * We only cover schemes with hash algorithms supported by IKEv2 signature
 * authentication.
 */
static struct {
	signature_scheme_t scheme;
	key_type_t type;
	int max_keysize;
} scheme_map[] = {
	{ SIGN_RSA_EMSA_PKCS1_SHA256, KEY_RSA,   3072 },
	{ SIGN_RSA_EMSA_PKCS1_SHA384, KEY_RSA,   7680 },
	{ SIGN_RSA_EMSA_PKCS1_SHA512, KEY_RSA,   0 },
	{ SIGN_ECDSA_WITH_SHA256_DER, KEY_ECDSA, 256 },
	{ SIGN_ECDSA_WITH_SHA384_DER, KEY_ECDSA, 384 },
	{ SIGN_ECDSA_WITH_SHA512_DER, KEY_ECDSA, 0 },
	{ SIGN_BLISS_WITH_SHA2_256,   KEY_BLISS, 128 },
	{ SIGN_BLISS_WITH_SHA2_384,   KEY_BLISS, 192 },
	{ SIGN_BLISS_WITH_SHA2_512,   KEY_BLISS, 0 }
};

/**
 * Private data for signature scheme enumerator
 */
typedef struct  {
	enumerator_t public;
	int index;
	key_type_t type;
	int size;
} private_enumerator_t;

METHOD(enumerator_t, signature_schemes_enumerate, bool,
	private_enumerator_t *this, signature_scheme_t *scheme)
{
	while (++this->index < countof(scheme_map))
	{
		if (this->type == scheme_map[this->index].type &&
		   (this->size <= scheme_map[this->index].max_keysize ||
			!scheme_map[this->index].max_keysize))
		{
			*scheme = scheme_map[this->index].scheme;
			return TRUE;
		}
	}
	return FALSE;
}

/*
 * Defined in header.
 */
enumerator_t *signature_schemes_for_key(key_type_t type, int size)
{
	private_enumerator_t *this;

	INIT(this,
		.public = {
			.enumerate = (void*)_signature_schemes_enumerate,
			.destroy = (void*)free,
		},
		.index = -1,
		.type = type,
		.size = size,
	);

	return &this->public;
}

/*
 * Defined in header.
 */
key_type_t key_type_from_signature_scheme(signature_scheme_t scheme)
{
	switch (scheme)
	{
		case SIGN_UNKNOWN:
			break;
		case SIGN_RSA_EMSA_PKCS1_NULL:
		case SIGN_RSA_EMSA_PKCS1_MD5:
		case SIGN_RSA_EMSA_PKCS1_SHA1:
		case SIGN_RSA_EMSA_PKCS1_SHA224:
		case SIGN_RSA_EMSA_PKCS1_SHA256:
		case SIGN_RSA_EMSA_PKCS1_SHA384:
		case SIGN_RSA_EMSA_PKCS1_SHA512:
			return KEY_RSA;
		case SIGN_ECDSA_WITH_SHA1_DER:
		case SIGN_ECDSA_WITH_SHA256_DER:
		case SIGN_ECDSA_WITH_SHA384_DER:
		case SIGN_ECDSA_WITH_SHA512_DER:
		case SIGN_ECDSA_WITH_NULL:
		case SIGN_ECDSA_256:
		case SIGN_ECDSA_384:
		case SIGN_ECDSA_521:
			return KEY_ECDSA;
		case SIGN_BLISS_WITH_SHA2_256:
		case SIGN_BLISS_WITH_SHA2_384:
		case SIGN_BLISS_WITH_SHA2_512:
		case SIGN_BLISS_WITH_SHA3_256:
		case SIGN_BLISS_WITH_SHA3_384:
		case SIGN_BLISS_WITH_SHA3_512:
			return KEY_BLISS;
	}
	return KEY_ANY;
}