blob: 1aaf8e8873e56c869f8f90e36ac556c1d54722f4 [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/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020038 * Macro to automatically add the size of #define'd OIDs
39 */
40#define ADD_LEN(s) s, OID_SIZE(s)
41
42/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020043 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
44 * the other functions)
45 */
Paul Bakkerbd51ad52013-06-28 16:51:52 +020046#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
47static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
48{ return (const TYPE_T *) oid_descriptor_from_buf(LIST, sizeof(TYPE_T), oid->p, oid->len ); }
49
50/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020051 * Macro to generate a function for retrieving a single attribute from the
52 * descriptor of an oid_descriptor_t wrapper.
53 */
54#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
55int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
56{ \
57 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
58 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
59 *ATTR1 = data->descriptor.ATTR1; \
60 return( 0 ); \
61}
62
63/*
64 * Macro to generate a function for retrieving a single attribute from an
65 * oid_descriptor_t wrapper.
66 */
67#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
68int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
69{ \
70 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
71 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
72 *ATTR1 = data->ATTR1; \
73 return( 0 ); \
74}
75
76/*
77 * Macro to generate a function for retrieving two attributes from an
78 * oid_descriptor_t wrapper.
79 */
80#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
81 ATTR2_TYPE, ATTR2) \
82int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
83{ \
84 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
85 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
86 *ATTR1 = data->ATTR1; \
87 *ATTR2 = data->ATTR2; \
88 return( 0 ); \
89}
90
91/*
Paul Bakkerce6ae232013-06-28 18:05:35 +020092 * Macro to generate a function for retrieving the OID based on a single
93 * attribute from a oid_descriptor_t wrapper.
94 */
95#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +020096int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +020097{ \
98 const TYPE_T *cur = LIST; \
99 while( cur->descriptor.asn1 != NULL ) { \
100 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200101 *oid = cur->descriptor.asn1; \
102 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200103 return( 0 ); \
104 } \
105 cur++; \
106 } \
107 return( POLARSSL_ERR_OID_NOT_FOUND ); \
108}
109
110/*
111 * Macro to generate a function for retrieving the OID based on two
112 * attributes from a oid_descriptor_t wrapper.
113 */
114#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
115 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200116int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
117 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200118{ \
119 const TYPE_T *cur = LIST; \
120 while( cur->descriptor.asn1 != NULL ) { \
121 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200122 *oid = cur->descriptor.asn1; \
123 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200124 return( 0 ); \
125 } \
126 cur++; \
127 } \
128 return( POLARSSL_ERR_OID_NOT_FOUND ); \
129}
130
131/*
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200132 * Core generic function
133 */
134static const oid_descriptor_t *oid_descriptor_from_buf( const void *struct_set,
135 size_t struct_size, const unsigned char *oid, size_t len )
136{
137 const unsigned char *p = (const unsigned char *) struct_set;
138 const oid_descriptor_t *cur;
139
140 if( struct_set == NULL || oid == NULL )
141 return( NULL );
142
143 cur = (const oid_descriptor_t *) p;
144 while( cur->asn1 != NULL )
145 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200146 if( cur->asn1_len == len &&
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200147 memcmp( cur->asn1, oid, len ) == 0 )
148 {
149 return( cur );
150 }
151
152 p += struct_size;
153 cur = (const oid_descriptor_t *) p;
154 }
155
156 return( NULL );
157}
158
Paul Bakkerc70b9822013-04-07 22:00:46 +0200159/*
160 * For X520 attribute types
161 */
162typedef struct {
163 oid_descriptor_t descriptor;
164 const char *short_name;
165} oid_x520_attr_t;
166
167static const oid_x520_attr_t oid_x520_attr_type[] =
168{
169 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200170 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200171 "CN",
172 },
173 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200174 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200175 "C",
176 },
177 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200178 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200179 "L",
180 },
181 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200182 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200183 "ST",
184 },
185 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200186 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200187 "O",
188 },
189 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200190 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200191 "OU",
192 },
193 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200194 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200195 "emailAddress",
196 },
197 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200198 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200199 NULL,
200 }
201};
202
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200203FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
204FN_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 +0200205
Paul Bakkered27a042013-04-18 22:46:23 +0200206#if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200207/*
208 * For X509 extensions
209 */
210typedef struct {
211 oid_descriptor_t descriptor;
212 int ext_type;
213} oid_x509_ext_t;
214
215static const oid_x509_ext_t oid_x509_ext[] =
216{
217 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200218 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200219 EXT_BASIC_CONSTRAINTS,
220 },
221 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200222 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200223 EXT_KEY_USAGE,
224 },
225 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200226 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200227 EXT_EXTENDED_KEY_USAGE,
228 },
229 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200230 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200231 EXT_SUBJECT_ALT_NAME,
232 },
233 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200234 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200235 EXT_NS_CERT_TYPE,
236 },
237 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200238 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200239 0,
240 },
241};
242
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200243FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
244FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200245
Paul Bakkerc70b9822013-04-07 22:00:46 +0200246static const oid_descriptor_t oid_ext_key_usage[] =
247{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200248 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
249 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
250 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
251 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
252 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
253 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
254 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200255};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200256
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200257FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
258FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakkered27a042013-04-18 22:46:23 +0200259#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200260
Paul Bakker47fce022013-06-28 17:34:34 +0200261#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200262/*
263 * For SignatureAlgorithmIdentifier
264 */
265typedef struct {
266 oid_descriptor_t descriptor;
267 md_type_t md_alg;
268 pk_type_t pk_alg;
269} oid_sig_alg_t;
270
271static const oid_sig_alg_t oid_sig_alg[] =
272{
273 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200274 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200275 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
276 },
277 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200278 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200279 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
280 },
281 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200282 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200283 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
284 },
285 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200286 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200287 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
288 },
289 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200290 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200291 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
292 },
293 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200294 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200295 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
296 },
297 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200298 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200299 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
300 },
301 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200302 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200303 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
304 },
305 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200306 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200307 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
308 },
309 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200310 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200311 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
312 },
313 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200314 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200315 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
316 },
317 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200318 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200319 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
320 },
321 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200322 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200323 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
324 },
325 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200326 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200327 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
328 },
329 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200330 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200331 0, 0,
332 },
333};
334
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200335FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
336FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
337FN_OID_GET_ATTR2(oid_get_sig_alg, oid_sig_alg_t, sig_alg, md_type_t, md_alg, pk_type_t, pk_alg);
Paul Bakkerce6ae232013-06-28 18:05:35 +0200338FN_OID_GET_OID_BY_ATTR2(oid_get_oid_by_sig_alg, oid_sig_alg_t, oid_sig_alg, pk_type_t, pk_alg, md_type_t, md_alg);
Paul Bakker47fce022013-06-28 17:34:34 +0200339#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200340
Paul Bakkerc70b9822013-04-07 22:00:46 +0200341/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200342 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200343 */
344typedef struct {
345 oid_descriptor_t descriptor;
346 pk_type_t pk_alg;
347} oid_pk_alg_t;
348
349static const oid_pk_alg_t oid_pk_alg[] =
350{
351 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200352 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200353 POLARSSL_PK_RSA,
354 },
355 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200356 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200357 POLARSSL_PK_ECKEY,
358 },
359 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200360 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200361 POLARSSL_PK_ECKEY_DH,
362 },
363 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200364 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200365 0,
366 },
367};
368
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200369FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
370FN_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 +0200371
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200372#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200373/*
374 * For namedCurve (RFC 5480)
375 */
376typedef struct {
377 oid_descriptor_t descriptor;
378 ecp_group_id grp_id;
379} oid_ecp_grp_t;
380
381static const oid_ecp_grp_t oid_ecp_grp[] =
382{
383 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200384 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200385 POLARSSL_ECP_DP_SECP192R1,
386 },
387 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200388 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200389 POLARSSL_ECP_DP_SECP224R1,
390 },
391 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200392 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200393 POLARSSL_ECP_DP_SECP256R1,
394 },
395 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200396 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200397 POLARSSL_ECP_DP_SECP384R1,
398 },
399 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200400 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200401 POLARSSL_ECP_DP_SECP521R1,
402 },
403 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200404 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200405 0,
406 },
407};
408
409FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
410FN_OID_GET_ATTR1(oid_get_ec_grp, oid_ecp_grp_t, grp_id, ecp_group_id, grp_id);
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200411FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
412#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200413
Paul Bakker47fce022013-06-28 17:34:34 +0200414#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200415/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200416 * For PKCS#5 PBES2 encryption algorithm
417 */
418typedef struct {
419 oid_descriptor_t descriptor;
420 cipher_type_t cipher_alg;
421} oid_cipher_alg_t;
422
423static const oid_cipher_alg_t oid_cipher_alg[] =
424{
425 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200426 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200427 POLARSSL_CIPHER_DES_CBC,
428 },
429 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200430 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200431 POLARSSL_CIPHER_DES_EDE3_CBC,
432 },
433 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200434 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200435 0,
436 },
437};
438
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200439FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
440FN_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 +0200441#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200442
Paul Bakker47fce022013-06-28 17:34:34 +0200443#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200444/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200445 * For digestAlgorithm
446 */
447typedef struct {
448 oid_descriptor_t descriptor;
449 md_type_t md_alg;
450} oid_md_alg_t;
451
452static const oid_md_alg_t oid_md_alg[] =
453{
454 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200455 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200456 POLARSSL_MD_MD2,
457 },
458 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200459 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200460 POLARSSL_MD_MD4,
461 },
462 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200463 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200464 POLARSSL_MD_MD5,
465 },
466 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200467 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200468 POLARSSL_MD_SHA1,
469 },
470 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200471 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200472 POLARSSL_MD_SHA1,
473 },
474 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200475 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200476 POLARSSL_MD_SHA224,
477 },
478 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200479 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200480 POLARSSL_MD_SHA256,
481 },
482 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200483 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200484 POLARSSL_MD_SHA384,
485 },
486 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200487 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200488 POLARSSL_MD_SHA512,
489 },
490 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200491 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200492 0,
493 },
494};
495
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200496FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
497FN_OID_GET_ATTR1(oid_get_md_alg, oid_md_alg_t, md_alg, md_type_t, md_alg);
Paul Bakkerce6ae232013-06-28 18:05:35 +0200498FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_md, oid_md_alg_t, oid_md_alg, md_type_t, md_alg);
Paul Bakker47fce022013-06-28 17:34:34 +0200499#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200500
Paul Bakker47fce022013-06-28 17:34:34 +0200501#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200502/*
503 * For PKCS#12 PBEs
504 */
505typedef struct {
506 oid_descriptor_t descriptor;
507 md_type_t md_alg;
508 cipher_type_t cipher_alg;
509} oid_pkcs12_pbe_alg_t;
510
511static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
512{
513 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200514 { ADD_LEN( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC ), "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
Paul Bakker7749a222013-06-28 17:28:20 +0200515 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
516 },
517 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200518 { ADD_LEN( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC ), "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
Paul Bakker7749a222013-06-28 17:28:20 +0200519 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
520 },
521 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200522 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200523 0, 0,
524 },
525};
526
527FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
528FN_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 +0200529#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200530
Paul Bakkerc70b9822013-04-07 22:00:46 +0200531#if defined _MSC_VER && !defined snprintf
532#include <stdarg.h>
533
534#if !defined vsnprintf
535#define vsnprintf _vsnprintf
536#endif // vsnprintf
537
538/*
539 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
540 * Result value is not size of buffer needed, but -1 if no fit is possible.
541 *
542 * This fuction tries to 'fix' this by at least suggesting enlarging the
543 * size by 20.
544 */
545static int compat_snprintf(char *str, size_t size, const char *format, ...)
546{
547 va_list ap;
548 int res = -1;
549
550 va_start( ap, format );
551
552 res = vsnprintf( str, size, format, ap );
553
554 va_end( ap );
555
556 // No quick fix possible
557 if ( res < 0 )
558 return( (int) size + 20 );
559
560 return res;
561}
562
563#define snprintf compat_snprintf
564#endif
565
566#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
567
568#define SAFE_SNPRINTF() \
569{ \
570 if( ret == -1 ) \
571 return( -1 ); \
572 \
573 if ( (unsigned int) ret > n ) { \
574 p[n - 1] = '\0'; \
575 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
576 } \
577 \
578 n -= (unsigned int) ret; \
579 p += (unsigned int) ret; \
580}
581
582/* Return the x.y.z.... style numeric string for the given OID */
583int oid_get_numeric_string( char *buf, size_t size,
584 const asn1_buf *oid )
585{
586 int ret;
587 size_t i, n;
588 unsigned int value;
589 char *p;
590
591 p = buf;
592 n = size;
593
594 /* First byte contains first two dots */
595 if( oid->len > 0 )
596 {
597 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
598 SAFE_SNPRINTF();
599 }
600
Paul Bakkerc70b9822013-04-07 22:00:46 +0200601 value = 0;
602 for( i = 1; i < oid->len; i++ )
603 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200604 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200605 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200606 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
607
Paul Bakkerc70b9822013-04-07 22:00:46 +0200608 value <<= 7;
609 value += oid->p[i] & 0x7F;
610
611 if( !( oid->p[i] & 0x80 ) )
612 {
613 /* Last byte */
614 ret = snprintf( p, n, ".%d", value );
615 SAFE_SNPRINTF();
616 value = 0;
617 }
618 }
619
620 return( (int) ( size - n ) );
621}
622
Paul Bakkerc70b9822013-04-07 22:00:46 +0200623#endif /* POLARSSL_OID_C */