blob: c9cfe4842a6234c22a3e969e17cee9801f3bf593 [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 Bakker7c6b2c32013-09-16 13:49:26 +020035#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker2292d1f2013-09-15 17:06:49 +020036#include "polarssl/x509.h"
37#endif
38
Paul Bakkered27a042013-04-18 22:46:23 +020039#include <stdio.h>
40
Paul Bakkerdd1150e2013-06-28 17:20:22 +020041/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020042 * Macro to automatically add the size of #define'd OIDs
43 */
44#define ADD_LEN(s) s, OID_SIZE(s)
45
46/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020047 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
48 * the other functions)
49 */
Paul Bakkerbd51ad52013-06-28 16:51:52 +020050#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
51static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
52{ return (const TYPE_T *) oid_descriptor_from_buf(LIST, sizeof(TYPE_T), oid->p, oid->len ); }
53
54/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020055 * Macro to generate a function for retrieving a single attribute from the
56 * descriptor of an oid_descriptor_t wrapper.
57 */
58#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
59int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
60{ \
61 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
62 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
63 *ATTR1 = data->descriptor.ATTR1; \
64 return( 0 ); \
65}
66
67/*
68 * Macro to generate a function for retrieving a single attribute from an
69 * oid_descriptor_t wrapper.
70 */
71#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
72int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
73{ \
74 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
75 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
76 *ATTR1 = data->ATTR1; \
77 return( 0 ); \
78}
79
80/*
81 * Macro to generate a function for retrieving two attributes from an
82 * oid_descriptor_t wrapper.
83 */
84#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
85 ATTR2_TYPE, ATTR2) \
86int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
87{ \
88 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
89 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
90 *ATTR1 = data->ATTR1; \
91 *ATTR2 = data->ATTR2; \
92 return( 0 ); \
93}
94
95/*
Paul Bakkerce6ae232013-06-28 18:05:35 +020096 * Macro to generate a function for retrieving the OID based on a single
97 * attribute from a oid_descriptor_t wrapper.
98 */
99#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200100int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200101{ \
102 const TYPE_T *cur = LIST; \
103 while( cur->descriptor.asn1 != NULL ) { \
104 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200105 *oid = cur->descriptor.asn1; \
106 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200107 return( 0 ); \
108 } \
109 cur++; \
110 } \
111 return( POLARSSL_ERR_OID_NOT_FOUND ); \
112}
113
114/*
115 * Macro to generate a function for retrieving the OID based on two
116 * attributes from a oid_descriptor_t wrapper.
117 */
118#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
119 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200120int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
121 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200122{ \
123 const TYPE_T *cur = LIST; \
124 while( cur->descriptor.asn1 != NULL ) { \
125 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200126 *oid = cur->descriptor.asn1; \
127 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200128 return( 0 ); \
129 } \
130 cur++; \
131 } \
132 return( POLARSSL_ERR_OID_NOT_FOUND ); \
133}
134
135/*
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200136 * Core generic function
137 */
138static const oid_descriptor_t *oid_descriptor_from_buf( const void *struct_set,
139 size_t struct_size, const unsigned char *oid, size_t len )
140{
141 const unsigned char *p = (const unsigned char *) struct_set;
142 const oid_descriptor_t *cur;
143
144 if( struct_set == NULL || oid == NULL )
145 return( NULL );
146
147 cur = (const oid_descriptor_t *) p;
148 while( cur->asn1 != NULL )
149 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200150 if( cur->asn1_len == len &&
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200151 memcmp( cur->asn1, oid, len ) == 0 )
152 {
153 return( cur );
154 }
155
156 p += struct_size;
157 cur = (const oid_descriptor_t *) p;
158 }
159
160 return( NULL );
161}
162
Paul Bakkerc70b9822013-04-07 22:00:46 +0200163/*
164 * For X520 attribute types
165 */
166typedef struct {
167 oid_descriptor_t descriptor;
168 const char *short_name;
169} oid_x520_attr_t;
170
171static const oid_x520_attr_t oid_x520_attr_type[] =
172{
173 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200174 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200175 "CN",
176 },
177 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200178 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200179 "C",
180 },
181 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200182 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200183 "L",
184 },
185 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200186 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200187 "ST",
188 },
189 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200190 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200191 "O",
192 },
193 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200194 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200195 "OU",
196 },
197 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200198 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200199 "emailAddress",
200 },
201 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200202 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200203 NULL,
204 }
205};
206
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200207FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
208FN_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 +0200209
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200211/*
212 * For X509 extensions
213 */
214typedef struct {
215 oid_descriptor_t descriptor;
216 int ext_type;
217} oid_x509_ext_t;
218
219static const oid_x509_ext_t oid_x509_ext[] =
220{
221 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200222 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200223 EXT_BASIC_CONSTRAINTS,
224 },
225 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200226 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200227 EXT_KEY_USAGE,
228 },
229 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200230 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200231 EXT_EXTENDED_KEY_USAGE,
232 },
233 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200234 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200235 EXT_SUBJECT_ALT_NAME,
236 },
237 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200238 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200239 EXT_NS_CERT_TYPE,
240 },
241 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200242 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200243 0,
244 },
245};
246
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200247FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
248FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200249
Paul Bakkerc70b9822013-04-07 22:00:46 +0200250static const oid_descriptor_t oid_ext_key_usage[] =
251{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200252 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
253 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
254 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
255 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
256 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
257 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
258 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200259};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200260
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200261FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
262FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200264
Paul Bakker47fce022013-06-28 17:34:34 +0200265#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200266/*
267 * For SignatureAlgorithmIdentifier
268 */
269typedef struct {
270 oid_descriptor_t descriptor;
271 md_type_t md_alg;
272 pk_type_t pk_alg;
273} oid_sig_alg_t;
274
275static const oid_sig_alg_t oid_sig_alg[] =
276{
277 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200278 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200279 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
280 },
281 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200282 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200283 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
284 },
285 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200286 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200287 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
288 },
289 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200290 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200291 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
292 },
293 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200294 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200295 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
296 },
297 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200298 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200299 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
300 },
301 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200302 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200303 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
304 },
305 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200306 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200307 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
308 },
309 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200310 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200311 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
312 },
313 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200314 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200315 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
316 },
317 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200318 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200319 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
320 },
321 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200322 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200323 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
324 },
325 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200326 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200327 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
328 },
329 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200330 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200331 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
332 },
333 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200334 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200335 0, 0,
336 },
337};
338
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200339FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
340FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
341FN_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 +0200342FN_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 +0200343#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200344
Paul Bakkerc70b9822013-04-07 22:00:46 +0200345/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200346 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200347 */
348typedef struct {
349 oid_descriptor_t descriptor;
350 pk_type_t pk_alg;
351} oid_pk_alg_t;
352
353static const oid_pk_alg_t oid_pk_alg[] =
354{
355 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200356 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200357 POLARSSL_PK_RSA,
358 },
359 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200360 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200361 POLARSSL_PK_ECKEY,
362 },
363 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200364 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200365 POLARSSL_PK_ECKEY_DH,
366 },
367 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200368 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200369 0,
370 },
371};
372
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200373FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
374FN_OID_GET_ATTR1(oid_get_pk_alg, oid_pk_alg_t, pk_alg, pk_type_t, pk_alg);
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200375FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_pk_alg, oid_pk_alg_t, oid_pk_alg, pk_type_t, pk_alg);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200376
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200377#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200378/*
379 * For namedCurve (RFC 5480)
380 */
381typedef struct {
382 oid_descriptor_t descriptor;
383 ecp_group_id grp_id;
384} oid_ecp_grp_t;
385
386static const oid_ecp_grp_t oid_ecp_grp[] =
387{
388 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200389 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200390 POLARSSL_ECP_DP_SECP192R1,
391 },
392 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200393 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200394 POLARSSL_ECP_DP_SECP224R1,
395 },
396 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200397 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200398 POLARSSL_ECP_DP_SECP256R1,
399 },
400 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200401 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200402 POLARSSL_ECP_DP_SECP384R1,
403 },
404 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200405 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200406 POLARSSL_ECP_DP_SECP521R1,
407 },
408 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200409 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
410 POLARSSL_ECP_DP_BP256R1,
411 },
412 {
413 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
414 POLARSSL_ECP_DP_BP384R1,
415 },
416 {
417 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
418 POLARSSL_ECP_DP_BP512R1,
419 },
420 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200421 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200422 0,
423 },
424};
425
426FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
427FN_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 +0200428FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
429#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200430
Paul Bakker47fce022013-06-28 17:34:34 +0200431#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200432/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200433 * For PKCS#5 PBES2 encryption algorithm
434 */
435typedef struct {
436 oid_descriptor_t descriptor;
437 cipher_type_t cipher_alg;
438} oid_cipher_alg_t;
439
440static const oid_cipher_alg_t oid_cipher_alg[] =
441{
442 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200443 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200444 POLARSSL_CIPHER_DES_CBC,
445 },
446 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200447 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200448 POLARSSL_CIPHER_DES_EDE3_CBC,
449 },
450 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200451 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200452 0,
453 },
454};
455
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200456FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
457FN_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 +0200458#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200459
Paul Bakker47fce022013-06-28 17:34:34 +0200460#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200461/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200462 * For digestAlgorithm
463 */
464typedef struct {
465 oid_descriptor_t descriptor;
466 md_type_t md_alg;
467} oid_md_alg_t;
468
469static const oid_md_alg_t oid_md_alg[] =
470{
471 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200472 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200473 POLARSSL_MD_MD2,
474 },
475 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200476 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200477 POLARSSL_MD_MD4,
478 },
479 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200480 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200481 POLARSSL_MD_MD5,
482 },
483 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200484 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200485 POLARSSL_MD_SHA1,
486 },
487 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200488 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200489 POLARSSL_MD_SHA1,
490 },
491 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200492 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200493 POLARSSL_MD_SHA224,
494 },
495 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200496 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200497 POLARSSL_MD_SHA256,
498 },
499 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200500 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200501 POLARSSL_MD_SHA384,
502 },
503 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200504 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200505 POLARSSL_MD_SHA512,
506 },
507 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200508 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200509 0,
510 },
511};
512
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200513FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
514FN_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 +0200515FN_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 +0200516#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200517
Paul Bakker47fce022013-06-28 17:34:34 +0200518#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200519/*
520 * For PKCS#12 PBEs
521 */
522typedef struct {
523 oid_descriptor_t descriptor;
524 md_type_t md_alg;
525 cipher_type_t cipher_alg;
526} oid_pkcs12_pbe_alg_t;
527
528static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
529{
530 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200531 { 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 +0200532 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
533 },
534 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200535 { 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 +0200536 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
537 },
538 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200539 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200540 0, 0,
541 },
542};
543
544FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
545FN_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 +0200546#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200547
Paul Bakkerc70b9822013-04-07 22:00:46 +0200548#if defined _MSC_VER && !defined snprintf
549#include <stdarg.h>
550
551#if !defined vsnprintf
552#define vsnprintf _vsnprintf
553#endif // vsnprintf
554
555/*
556 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
557 * Result value is not size of buffer needed, but -1 if no fit is possible.
558 *
559 * This fuction tries to 'fix' this by at least suggesting enlarging the
560 * size by 20.
561 */
562static int compat_snprintf(char *str, size_t size, const char *format, ...)
563{
564 va_list ap;
565 int res = -1;
566
567 va_start( ap, format );
568
569 res = vsnprintf( str, size, format, ap );
570
571 va_end( ap );
572
573 // No quick fix possible
574 if ( res < 0 )
575 return( (int) size + 20 );
576
577 return res;
578}
579
580#define snprintf compat_snprintf
581#endif
582
583#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
584
585#define SAFE_SNPRINTF() \
586{ \
587 if( ret == -1 ) \
588 return( -1 ); \
589 \
590 if ( (unsigned int) ret > n ) { \
591 p[n - 1] = '\0'; \
592 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
593 } \
594 \
595 n -= (unsigned int) ret; \
596 p += (unsigned int) ret; \
597}
598
599/* Return the x.y.z.... style numeric string for the given OID */
600int oid_get_numeric_string( char *buf, size_t size,
601 const asn1_buf *oid )
602{
603 int ret;
604 size_t i, n;
605 unsigned int value;
606 char *p;
607
608 p = buf;
609 n = size;
610
611 /* First byte contains first two dots */
612 if( oid->len > 0 )
613 {
614 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
615 SAFE_SNPRINTF();
616 }
617
Paul Bakkerc70b9822013-04-07 22:00:46 +0200618 value = 0;
619 for( i = 1; i < oid->len; i++ )
620 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200621 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200622 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200623 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
624
Paul Bakkerc70b9822013-04-07 22:00:46 +0200625 value <<= 7;
626 value += oid->p[i] & 0x7F;
627
628 if( !( oid->p[i] & 0x80 ) )
629 {
630 /* Last byte */
631 ret = snprintf( p, n, ".%d", value );
632 SAFE_SNPRINTF();
633 value = 0;
634 }
635 }
636
637 return( (int) ( size - n ) );
638}
639
Paul Bakkerc70b9822013-04-07 22:00:46 +0200640#endif /* POLARSSL_OID_C */