blob: c4edcc58ba87084908b41be1e67e13def38c1432 [file] [log] [blame]
Paul Bakkerc70b9822013-04-07 22:00:46 +02001/**
2 * \file oid.c
3 *
4 * \brief Object Identifier (OID) database
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27
28#include "polarssl/config.h"
29
30#if defined(POLARSSL_OID_C)
31
32#include "polarssl/oid.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "polarssl/rsa.h"
34
Paul Bakkered27a042013-04-18 22:46:23 +020035#include <stdio.h>
36
Paul Bakkerdd1150e2013-06-28 17:20:22 +020037/*
38 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
39 * the other functions)
40 */
Paul Bakkerbd51ad52013-06-28 16:51:52 +020041#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
42static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
43{ return (const TYPE_T *) oid_descriptor_from_buf(LIST, sizeof(TYPE_T), oid->p, oid->len ); }
44
45/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020046 * Macro to generate a function for retrieving a single attribute from the
47 * descriptor of an oid_descriptor_t wrapper.
48 */
49#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
50int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
51{ \
52 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
53 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
54 *ATTR1 = data->descriptor.ATTR1; \
55 return( 0 ); \
56}
57
58/*
59 * Macro to generate a function for retrieving a single attribute from an
60 * oid_descriptor_t wrapper.
61 */
62#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
63int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
64{ \
65 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
66 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
67 *ATTR1 = data->ATTR1; \
68 return( 0 ); \
69}
70
71/*
72 * Macro to generate a function for retrieving two attributes from an
73 * oid_descriptor_t wrapper.
74 */
75#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
76 ATTR2_TYPE, ATTR2) \
77int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
78{ \
79 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
80 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
81 *ATTR1 = data->ATTR1; \
82 *ATTR2 = data->ATTR2; \
83 return( 0 ); \
84}
85
86/*
Paul Bakkerbd51ad52013-06-28 16:51:52 +020087 * Core generic function
88 */
89static const oid_descriptor_t *oid_descriptor_from_buf( const void *struct_set,
90 size_t struct_size, const unsigned char *oid, size_t len )
91{
92 const unsigned char *p = (const unsigned char *) struct_set;
93 const oid_descriptor_t *cur;
94
95 if( struct_set == NULL || oid == NULL )
96 return( NULL );
97
98 cur = (const oid_descriptor_t *) p;
99 while( cur->asn1 != NULL )
100 {
101 if( strlen( cur->asn1 ) == len &&
102 memcmp( cur->asn1, oid, len ) == 0 )
103 {
104 return( cur );
105 }
106
107 p += struct_size;
108 cur = (const oid_descriptor_t *) p;
109 }
110
111 return( NULL );
112}
113
Paul Bakkerc70b9822013-04-07 22:00:46 +0200114/*
115 * For X520 attribute types
116 */
117typedef struct {
118 oid_descriptor_t descriptor;
119 const char *short_name;
120} oid_x520_attr_t;
121
122static const oid_x520_attr_t oid_x520_attr_type[] =
123{
124 {
125 { OID_AT_CN, "id-at-commonName", "Common Name" },
126 "CN",
127 },
128 {
129 { OID_AT_COUNTRY, "id-at-countryName", "Country" },
130 "C",
131 },
132 {
133 { OID_AT_LOCALITY, "id-at-locality", "Locality" },
134 "L",
135 },
136 {
137 { OID_AT_STATE, "id-at-state", "State" },
138 "ST",
139 },
140 {
141 { OID_AT_ORGANIZATION,"id-at-organizationName", "Organization" },
142 "O",
143 },
144 {
145 { OID_AT_ORG_UNIT, "id-at-organizationalUnitName", "Org Unit" },
146 "OU",
147 },
148 {
149 { OID_PKCS9_EMAIL, "emailAddress", "E-mail address" },
150 "emailAddress",
151 },
152 {
153 { NULL, NULL, NULL },
154 NULL,
155 }
156};
157
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200158FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
159FN_OID_GET_ATTR1(oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200160
Paul Bakkered27a042013-04-18 22:46:23 +0200161#if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200162/*
163 * For X509 extensions
164 */
165typedef struct {
166 oid_descriptor_t descriptor;
167 int ext_type;
168} oid_x509_ext_t;
169
170static const oid_x509_ext_t oid_x509_ext[] =
171{
172 {
173 { OID_BASIC_CONSTRAINTS, "id-ce-basicConstraints", "Basic Constraints" },
174 EXT_BASIC_CONSTRAINTS,
175 },
176 {
177 { OID_KEY_USAGE, "id-ce-keyUsage", "Key Usage" },
178 EXT_KEY_USAGE,
179 },
180 {
181 { OID_EXTENDED_KEY_USAGE, "id-ce-keyUsage", "Extended Key Usage" },
182 EXT_EXTENDED_KEY_USAGE,
183 },
184 {
185 { OID_SUBJECT_ALT_NAME, "id-ce-subjectAltName", "Subject Alt Name" },
186 EXT_SUBJECT_ALT_NAME,
187 },
188 {
189 { OID_NS_CERT_TYPE, "id-netscape-certtype", "Netscape Certificate Type" },
190 EXT_NS_CERT_TYPE,
191 },
192 {
193 { NULL, NULL, NULL },
194 0,
195 },
196};
197
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200198FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
199FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200200
Paul Bakkerc70b9822013-04-07 22:00:46 +0200201static const oid_descriptor_t oid_ext_key_usage[] =
202{
203 { OID_SERVER_AUTH, "id-kp-serverAuth", "TLS Web Server Authentication" },
204 { OID_CLIENT_AUTH, "id-kp-clientAuth", "TLS Web Client Authentication" },
205 { OID_CODE_SIGNING, "id-kp-codeSigning", "Code Signing" },
206 { OID_EMAIL_PROTECTION, "id-kp-emailProtection", "E-mail Protection" },
207 { OID_TIME_STAMPING, "id-kp-timeStamping", "Time Stamping" },
208 { OID_OCSP_SIGNING, "id-kp-OCSPSigning", "OCSP Signing" },
209 { NULL, NULL, NULL },
210};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200211
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200212FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
213FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200214
Paul Bakkered27a042013-04-18 22:46:23 +0200215#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200216
Paul Bakker47fce022013-06-28 17:34:34 +0200217#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200218/*
219 * For SignatureAlgorithmIdentifier
220 */
221typedef struct {
222 oid_descriptor_t descriptor;
223 md_type_t md_alg;
224 pk_type_t pk_alg;
225} oid_sig_alg_t;
226
227static const oid_sig_alg_t oid_sig_alg[] =
228{
229 {
230 { OID_PKCS1_MD2, "md2WithRSAEncryption", "RSA with MD2" },
231 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
232 },
233 {
234 { OID_PKCS1_MD4, "md4WithRSAEncryption", "RSA with MD4" },
235 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
236 },
237 {
238 { OID_PKCS1_MD5, "md5WithRSAEncryption", "RSA with MD5" },
239 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
240 },
241 {
242 { OID_PKCS1_SHA1, "sha-1WithRSAEncryption", "RSA with SHA1" },
243 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
244 },
245 {
246 { OID_PKCS1_SHA224, "sha224WithRSAEncryption", "RSA with SHA-224" },
247 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
248 },
249 {
250 { OID_PKCS1_SHA256, "sha256WithRSAEncryption", "RSA with SHA-256" },
251 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
252 },
253 {
254 { OID_PKCS1_SHA384, "sha384WithRSAEncryption", "RSA with SHA-384" },
255 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
256 },
257 {
258 { OID_PKCS1_SHA512, "sha512WithRSAEncryption", "RSA with SHA-512" },
259 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
260 },
261 {
262 { OID_RSA_SHA_OBS, "sha-1WithRSAEncryption", "RSA with SHA1" },
263 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
264 },
265 {
266 { NULL, NULL, NULL },
267 0, 0,
268 },
269};
270
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200271FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
272FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
273FN_OID_GET_ATTR2(oid_get_sig_alg, oid_sig_alg_t, sig_alg, md_type_t, md_alg, pk_type_t, pk_alg);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200274
275int oid_get_oid_by_sig_alg( pk_type_t pk_alg, md_type_t md_alg,
276 const char **oid_str )
277{
278 const oid_sig_alg_t *cur = oid_sig_alg;
279
280 while( cur->descriptor.asn1 != NULL )
281 {
282 if( cur->pk_alg == pk_alg &&
283 cur->md_alg == md_alg )
284 {
285 *oid_str = cur->descriptor.asn1;
286 return( 0 );
287 }
288
289 cur++;
290 }
291
292 return( POLARSSL_ERR_OID_NOT_FOUND );
293}
Paul Bakker47fce022013-06-28 17:34:34 +0200294#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200295
Paul Bakkerc70b9822013-04-07 22:00:46 +0200296/*
297 * For PublicKeyInfo
298 */
299typedef struct {
300 oid_descriptor_t descriptor;
301 pk_type_t pk_alg;
302} oid_pk_alg_t;
303
304static const oid_pk_alg_t oid_pk_alg[] =
305{
306 {
307 { OID_PKCS1_RSA, "rsaEncryption", "RSA" },
308 POLARSSL_PK_RSA,
309 },
310 {
311 { NULL, NULL, NULL },
312 0,
313 },
314};
315
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200316FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
317FN_OID_GET_ATTR1(oid_get_pk_alg, oid_pk_alg_t, pk_alg, pk_type_t, pk_alg);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200318
Paul Bakker47fce022013-06-28 17:34:34 +0200319#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200320/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200321 * For PKCS#5 PBES2 encryption algorithm
322 */
323typedef struct {
324 oid_descriptor_t descriptor;
325 cipher_type_t cipher_alg;
326} oid_cipher_alg_t;
327
328static const oid_cipher_alg_t oid_cipher_alg[] =
329{
330 {
331 { OID_DES_CBC, "desCBC", "DES-CBC" },
332 POLARSSL_CIPHER_DES_CBC,
333 },
334 {
335 { OID_DES_EDE3_CBC, "des-ede3-cbc", "DES-EDE3-CBC" },
336 POLARSSL_CIPHER_DES_EDE3_CBC,
337 },
338 {
339 { NULL, NULL, NULL },
340 0,
341 },
342};
343
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200344FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
345FN_OID_GET_ATTR1(oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, cipher_type_t, cipher_alg);
Paul Bakker47fce022013-06-28 17:34:34 +0200346#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200347
Paul Bakker47fce022013-06-28 17:34:34 +0200348#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200349/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200350 * For digestAlgorithm
351 */
352typedef struct {
353 oid_descriptor_t descriptor;
354 md_type_t md_alg;
355} oid_md_alg_t;
356
357static const oid_md_alg_t oid_md_alg[] =
358{
359 {
360 { OID_DIGEST_ALG_MD2, "id-md2", "MD2" },
361 POLARSSL_MD_MD2,
362 },
363 {
364 { OID_DIGEST_ALG_MD4, "id-md4", "MD4" },
365 POLARSSL_MD_MD4,
366 },
367 {
368 { OID_DIGEST_ALG_MD5, "id-md5", "MD5" },
369 POLARSSL_MD_MD5,
370 },
371 {
372 { OID_DIGEST_ALG_SHA1, "id-sha1", "SHA-1" },
373 POLARSSL_MD_SHA1,
374 },
375 {
376 { OID_DIGEST_ALG_SHA1, "id-sha1", "SHA-1" },
377 POLARSSL_MD_SHA1,
378 },
379 {
380 { OID_DIGEST_ALG_SHA224, "id-sha224", "SHA-224" },
381 POLARSSL_MD_SHA224,
382 },
383 {
384 { OID_DIGEST_ALG_SHA256, "id-sha256", "SHA-256" },
385 POLARSSL_MD_SHA256,
386 },
387 {
388 { OID_DIGEST_ALG_SHA384, "id-sha384", "SHA-384" },
389 POLARSSL_MD_SHA384,
390 },
391 {
392 { OID_DIGEST_ALG_SHA512, "id-sha512", "SHA-512" },
393 POLARSSL_MD_SHA512,
394 },
395 {
396 { NULL, NULL, NULL },
397 0,
398 },
399};
400
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200401FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
402FN_OID_GET_ATTR1(oid_get_md_alg, oid_md_alg_t, md_alg, md_type_t, md_alg);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200403
404int oid_get_oid_by_md( md_type_t md_alg, const char **oid_str )
405{
406 const oid_md_alg_t *cur = oid_md_alg;
407
408 while( cur->descriptor.asn1 != NULL )
409 {
410 if( cur->md_alg == md_alg )
411 {
412 *oid_str = cur->descriptor.asn1;
413 return( 0 );
414 }
415
416 cur++;
417 }
418
419 return( POLARSSL_ERR_OID_NOT_FOUND );
420}
Paul Bakker47fce022013-06-28 17:34:34 +0200421#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200422
Paul Bakker47fce022013-06-28 17:34:34 +0200423#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200424/*
425 * For PKCS#12 PBEs
426 */
427typedef struct {
428 oid_descriptor_t descriptor;
429 md_type_t md_alg;
430 cipher_type_t cipher_alg;
431} oid_pkcs12_pbe_alg_t;
432
433static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
434{
435 {
436 { OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
437 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
438 },
439 {
440 { OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
441 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
442 },
443 {
444 { NULL, NULL, NULL },
445 0, 0,
446 },
447};
448
449FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
450FN_OID_GET_ATTR2(oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, md_type_t, md_alg, cipher_type_t, cipher_alg);
Paul Bakker47fce022013-06-28 17:34:34 +0200451#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200452
Paul Bakkerc70b9822013-04-07 22:00:46 +0200453#if defined _MSC_VER && !defined snprintf
454#include <stdarg.h>
455
456#if !defined vsnprintf
457#define vsnprintf _vsnprintf
458#endif // vsnprintf
459
460/*
461 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
462 * Result value is not size of buffer needed, but -1 if no fit is possible.
463 *
464 * This fuction tries to 'fix' this by at least suggesting enlarging the
465 * size by 20.
466 */
467static int compat_snprintf(char *str, size_t size, const char *format, ...)
468{
469 va_list ap;
470 int res = -1;
471
472 va_start( ap, format );
473
474 res = vsnprintf( str, size, format, ap );
475
476 va_end( ap );
477
478 // No quick fix possible
479 if ( res < 0 )
480 return( (int) size + 20 );
481
482 return res;
483}
484
485#define snprintf compat_snprintf
486#endif
487
488#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
489
490#define SAFE_SNPRINTF() \
491{ \
492 if( ret == -1 ) \
493 return( -1 ); \
494 \
495 if ( (unsigned int) ret > n ) { \
496 p[n - 1] = '\0'; \
497 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
498 } \
499 \
500 n -= (unsigned int) ret; \
501 p += (unsigned int) ret; \
502}
503
504/* Return the x.y.z.... style numeric string for the given OID */
505int oid_get_numeric_string( char *buf, size_t size,
506 const asn1_buf *oid )
507{
508 int ret;
509 size_t i, n;
510 unsigned int value;
511 char *p;
512
513 p = buf;
514 n = size;
515
516 /* First byte contains first two dots */
517 if( oid->len > 0 )
518 {
519 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
520 SAFE_SNPRINTF();
521 }
522
523 /* Prevent overflow in value. */
524 if( oid->len > sizeof(value) )
525 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
526
527 value = 0;
528 for( i = 1; i < oid->len; i++ )
529 {
530 value <<= 7;
531 value += oid->p[i] & 0x7F;
532
533 if( !( oid->p[i] & 0x80 ) )
534 {
535 /* Last byte */
536 ret = snprintf( p, n, ".%d", value );
537 SAFE_SNPRINTF();
538 value = 0;
539 }
540 }
541
542 return( (int) ( size - n ) );
543}
544
Paul Bakkerc70b9822013-04-07 22:00:46 +0200545#endif /* POLARSSL_OID_C */