blob: 81b313e338e87c48bdd20c0b56b833ea491dc1db [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);
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200371FN_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 +0200372
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200373#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200374/*
375 * For namedCurve (RFC 5480)
376 */
377typedef struct {
378 oid_descriptor_t descriptor;
379 ecp_group_id grp_id;
380} oid_ecp_grp_t;
381
382static const oid_ecp_grp_t oid_ecp_grp[] =
383{
384 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200385 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200386 POLARSSL_ECP_DP_SECP192R1,
387 },
388 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200389 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200390 POLARSSL_ECP_DP_SECP224R1,
391 },
392 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200393 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200394 POLARSSL_ECP_DP_SECP256R1,
395 },
396 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200397 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200398 POLARSSL_ECP_DP_SECP384R1,
399 },
400 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200401 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200402 POLARSSL_ECP_DP_SECP521R1,
403 },
404 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200405 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200406 0,
407 },
408};
409
410FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
411FN_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 +0200412FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
413#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200414
Paul Bakker47fce022013-06-28 17:34:34 +0200415#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200416/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200417 * For PKCS#5 PBES2 encryption algorithm
418 */
419typedef struct {
420 oid_descriptor_t descriptor;
421 cipher_type_t cipher_alg;
422} oid_cipher_alg_t;
423
424static const oid_cipher_alg_t oid_cipher_alg[] =
425{
426 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200427 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200428 POLARSSL_CIPHER_DES_CBC,
429 },
430 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200431 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200432 POLARSSL_CIPHER_DES_EDE3_CBC,
433 },
434 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200435 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200436 0,
437 },
438};
439
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200440FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
441FN_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 +0200442#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200443
Paul Bakker47fce022013-06-28 17:34:34 +0200444#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200445/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200446 * For digestAlgorithm
447 */
448typedef struct {
449 oid_descriptor_t descriptor;
450 md_type_t md_alg;
451} oid_md_alg_t;
452
453static const oid_md_alg_t oid_md_alg[] =
454{
455 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200456 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200457 POLARSSL_MD_MD2,
458 },
459 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200460 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200461 POLARSSL_MD_MD4,
462 },
463 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200464 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200465 POLARSSL_MD_MD5,
466 },
467 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200468 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200469 POLARSSL_MD_SHA1,
470 },
471 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200472 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200473 POLARSSL_MD_SHA1,
474 },
475 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200476 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200477 POLARSSL_MD_SHA224,
478 },
479 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200480 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200481 POLARSSL_MD_SHA256,
482 },
483 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200484 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200485 POLARSSL_MD_SHA384,
486 },
487 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200488 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200489 POLARSSL_MD_SHA512,
490 },
491 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200492 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200493 0,
494 },
495};
496
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200497FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
498FN_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 +0200499FN_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 +0200500#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200501
Paul Bakker47fce022013-06-28 17:34:34 +0200502#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200503/*
504 * For PKCS#12 PBEs
505 */
506typedef struct {
507 oid_descriptor_t descriptor;
508 md_type_t md_alg;
509 cipher_type_t cipher_alg;
510} oid_pkcs12_pbe_alg_t;
511
512static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
513{
514 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200515 { 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 +0200516 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
517 },
518 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200519 { 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 +0200520 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
521 },
522 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200523 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200524 0, 0,
525 },
526};
527
528FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
529FN_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 +0200530#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200531
Paul Bakkerc70b9822013-04-07 22:00:46 +0200532#if defined _MSC_VER && !defined snprintf
533#include <stdarg.h>
534
535#if !defined vsnprintf
536#define vsnprintf _vsnprintf
537#endif // vsnprintf
538
539/*
540 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
541 * Result value is not size of buffer needed, but -1 if no fit is possible.
542 *
543 * This fuction tries to 'fix' this by at least suggesting enlarging the
544 * size by 20.
545 */
546static int compat_snprintf(char *str, size_t size, const char *format, ...)
547{
548 va_list ap;
549 int res = -1;
550
551 va_start( ap, format );
552
553 res = vsnprintf( str, size, format, ap );
554
555 va_end( ap );
556
557 // No quick fix possible
558 if ( res < 0 )
559 return( (int) size + 20 );
560
561 return res;
562}
563
564#define snprintf compat_snprintf
565#endif
566
567#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
568
569#define SAFE_SNPRINTF() \
570{ \
571 if( ret == -1 ) \
572 return( -1 ); \
573 \
574 if ( (unsigned int) ret > n ) { \
575 p[n - 1] = '\0'; \
576 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
577 } \
578 \
579 n -= (unsigned int) ret; \
580 p += (unsigned int) ret; \
581}
582
583/* Return the x.y.z.... style numeric string for the given OID */
584int oid_get_numeric_string( char *buf, size_t size,
585 const asn1_buf *oid )
586{
587 int ret;
588 size_t i, n;
589 unsigned int value;
590 char *p;
591
592 p = buf;
593 n = size;
594
595 /* First byte contains first two dots */
596 if( oid->len > 0 )
597 {
598 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
599 SAFE_SNPRINTF();
600 }
601
Paul Bakkerc70b9822013-04-07 22:00:46 +0200602 value = 0;
603 for( i = 1; i < oid->len; i++ )
604 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200605 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200606 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200607 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
608
Paul Bakkerc70b9822013-04-07 22:00:46 +0200609 value <<= 7;
610 value += oid->p[i] & 0x7F;
611
612 if( !( oid->p[i] & 0x80 ) )
613 {
614 /* Last byte */
615 ret = snprintf( p, n, ".%d", value );
616 SAFE_SNPRINTF();
617 value = 0;
618 }
619 }
620
621 return( (int) ( size - n ) );
622}
623
Paul Bakkerc70b9822013-04-07 22:00:46 +0200624#endif /* POLARSSL_OID_C */