blob: c5608c3a1f28ced08b07018950524bd739ac72ad [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) \
96int FN_NAME( ATTR1_TYPE ATTR1, const char **oid_str ) \
97{ \
98 const TYPE_T *cur = LIST; \
99 while( cur->descriptor.asn1 != NULL ) { \
100 if( cur->ATTR1 == ATTR1 ) { \
101 *oid_str = cur->descriptor.asn1; \
102 return( 0 ); \
103 } \
104 cur++; \
105 } \
106 return( POLARSSL_ERR_OID_NOT_FOUND ); \
107}
108
109/*
110 * Macro to generate a function for retrieving the OID based on two
111 * attributes from a oid_descriptor_t wrapper.
112 */
113#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
114 ATTR2_TYPE, ATTR2) \
115int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid_str ) \
116{ \
117 const TYPE_T *cur = LIST; \
118 while( cur->descriptor.asn1 != NULL ) { \
119 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
120 *oid_str = cur->descriptor.asn1; \
121 return( 0 ); \
122 } \
123 cur++; \
124 } \
125 return( POLARSSL_ERR_OID_NOT_FOUND ); \
126}
127
128/*
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200129 * Core generic function
130 */
131static const oid_descriptor_t *oid_descriptor_from_buf( const void *struct_set,
132 size_t struct_size, const unsigned char *oid, size_t len )
133{
134 const unsigned char *p = (const unsigned char *) struct_set;
135 const oid_descriptor_t *cur;
136
137 if( struct_set == NULL || oid == NULL )
138 return( NULL );
139
140 cur = (const oid_descriptor_t *) p;
141 while( cur->asn1 != NULL )
142 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200143 if( cur->asn1_len == len &&
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200144 memcmp( cur->asn1, oid, len ) == 0 )
145 {
146 return( cur );
147 }
148
149 p += struct_size;
150 cur = (const oid_descriptor_t *) p;
151 }
152
153 return( NULL );
154}
155
Paul Bakkerc70b9822013-04-07 22:00:46 +0200156/*
157 * For X520 attribute types
158 */
159typedef struct {
160 oid_descriptor_t descriptor;
161 const char *short_name;
162} oid_x520_attr_t;
163
164static const oid_x520_attr_t oid_x520_attr_type[] =
165{
166 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200167 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200168 "CN",
169 },
170 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200171 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200172 "C",
173 },
174 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200175 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200176 "L",
177 },
178 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200179 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200180 "ST",
181 },
182 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200183 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200184 "O",
185 },
186 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200187 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200188 "OU",
189 },
190 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200191 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200192 "emailAddress",
193 },
194 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200195 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200196 NULL,
197 }
198};
199
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200200FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
201FN_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 +0200202
Paul Bakkered27a042013-04-18 22:46:23 +0200203#if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200204/*
205 * For X509 extensions
206 */
207typedef struct {
208 oid_descriptor_t descriptor;
209 int ext_type;
210} oid_x509_ext_t;
211
212static const oid_x509_ext_t oid_x509_ext[] =
213{
214 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200215 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200216 EXT_BASIC_CONSTRAINTS,
217 },
218 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200219 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200220 EXT_KEY_USAGE,
221 },
222 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200223 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200224 EXT_EXTENDED_KEY_USAGE,
225 },
226 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200227 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200228 EXT_SUBJECT_ALT_NAME,
229 },
230 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200231 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200232 EXT_NS_CERT_TYPE,
233 },
234 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200235 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200236 0,
237 },
238};
239
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200240FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
241FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200242
Paul Bakkerc70b9822013-04-07 22:00:46 +0200243static const oid_descriptor_t oid_ext_key_usage[] =
244{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200245 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
246 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
247 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
248 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
249 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
250 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
251 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200252};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200253
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200254FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
255FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakkered27a042013-04-18 22:46:23 +0200256#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200257
Paul Bakker47fce022013-06-28 17:34:34 +0200258#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200259/*
260 * For SignatureAlgorithmIdentifier
261 */
262typedef struct {
263 oid_descriptor_t descriptor;
264 md_type_t md_alg;
265 pk_type_t pk_alg;
266} oid_sig_alg_t;
267
268static const oid_sig_alg_t oid_sig_alg[] =
269{
270 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200271 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200272 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
273 },
274 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200275 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200276 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
277 },
278 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200279 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200280 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
281 },
282 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200283 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200284 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
285 },
286 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200287 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200288 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
289 },
290 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200291 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200292 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
293 },
294 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200295 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200296 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
297 },
298 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200299 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200300 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
301 },
302 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200303 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200304 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
305 },
306 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200307 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200308 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
309 },
310 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200311 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200312 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
313 },
314 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200315 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200316 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
317 },
318 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200319 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200320 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
321 },
322 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200323 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200324 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
325 },
326 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200327 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200328 0, 0,
329 },
330};
331
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200332FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
333FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
334FN_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 +0200335FN_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 +0200336#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200337
Paul Bakkerc70b9822013-04-07 22:00:46 +0200338/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200339 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200340 */
341typedef struct {
342 oid_descriptor_t descriptor;
343 pk_type_t pk_alg;
344} oid_pk_alg_t;
345
346static const oid_pk_alg_t oid_pk_alg[] =
347{
348 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200349 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200350 POLARSSL_PK_RSA,
351 },
352 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200353 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200354 POLARSSL_PK_ECKEY,
355 },
356 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200357 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200358 POLARSSL_PK_ECKEY_DH,
359 },
360 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200361 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200362 0,
363 },
364};
365
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200366FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
367FN_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 +0200368
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200369/*
370 * For namedCurve (RFC 5480)
371 */
372typedef struct {
373 oid_descriptor_t descriptor;
374 ecp_group_id grp_id;
375} oid_ecp_grp_t;
376
377static const oid_ecp_grp_t oid_ecp_grp[] =
378{
379 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200380 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200381 POLARSSL_ECP_DP_SECP192R1,
382 },
383 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200384 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200385 POLARSSL_ECP_DP_SECP224R1,
386 },
387 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200388 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200389 POLARSSL_ECP_DP_SECP256R1,
390 },
391 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200392 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200393 POLARSSL_ECP_DP_SECP384R1,
394 },
395 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200396 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200397 POLARSSL_ECP_DP_SECP521R1,
398 },
399 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200400 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200401 0,
402 },
403};
404
405FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
406FN_OID_GET_ATTR1(oid_get_ec_grp, oid_ecp_grp_t, grp_id, ecp_group_id, grp_id);
407
Paul Bakker47fce022013-06-28 17:34:34 +0200408#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200409/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200410 * For PKCS#5 PBES2 encryption algorithm
411 */
412typedef struct {
413 oid_descriptor_t descriptor;
414 cipher_type_t cipher_alg;
415} oid_cipher_alg_t;
416
417static const oid_cipher_alg_t oid_cipher_alg[] =
418{
419 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200420 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200421 POLARSSL_CIPHER_DES_CBC,
422 },
423 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200424 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200425 POLARSSL_CIPHER_DES_EDE3_CBC,
426 },
427 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200428 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200429 0,
430 },
431};
432
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200433FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
434FN_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 +0200435#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200436
Paul Bakker47fce022013-06-28 17:34:34 +0200437#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200438/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200439 * For digestAlgorithm
440 */
441typedef struct {
442 oid_descriptor_t descriptor;
443 md_type_t md_alg;
444} oid_md_alg_t;
445
446static const oid_md_alg_t oid_md_alg[] =
447{
448 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200449 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200450 POLARSSL_MD_MD2,
451 },
452 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200453 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200454 POLARSSL_MD_MD4,
455 },
456 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200457 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200458 POLARSSL_MD_MD5,
459 },
460 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200461 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200462 POLARSSL_MD_SHA1,
463 },
464 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200465 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200466 POLARSSL_MD_SHA1,
467 },
468 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200469 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200470 POLARSSL_MD_SHA224,
471 },
472 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200473 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200474 POLARSSL_MD_SHA256,
475 },
476 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200477 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200478 POLARSSL_MD_SHA384,
479 },
480 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200481 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200482 POLARSSL_MD_SHA512,
483 },
484 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200485 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200486 0,
487 },
488};
489
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200490FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
491FN_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 +0200492FN_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 +0200493#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200494
Paul Bakker47fce022013-06-28 17:34:34 +0200495#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200496/*
497 * For PKCS#12 PBEs
498 */
499typedef struct {
500 oid_descriptor_t descriptor;
501 md_type_t md_alg;
502 cipher_type_t cipher_alg;
503} oid_pkcs12_pbe_alg_t;
504
505static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
506{
507 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200508 { 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 +0200509 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
510 },
511 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200512 { 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 +0200513 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
514 },
515 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200516 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200517 0, 0,
518 },
519};
520
521FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
522FN_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 +0200523#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200524
Paul Bakkerc70b9822013-04-07 22:00:46 +0200525#if defined _MSC_VER && !defined snprintf
526#include <stdarg.h>
527
528#if !defined vsnprintf
529#define vsnprintf _vsnprintf
530#endif // vsnprintf
531
532/*
533 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
534 * Result value is not size of buffer needed, but -1 if no fit is possible.
535 *
536 * This fuction tries to 'fix' this by at least suggesting enlarging the
537 * size by 20.
538 */
539static int compat_snprintf(char *str, size_t size, const char *format, ...)
540{
541 va_list ap;
542 int res = -1;
543
544 va_start( ap, format );
545
546 res = vsnprintf( str, size, format, ap );
547
548 va_end( ap );
549
550 // No quick fix possible
551 if ( res < 0 )
552 return( (int) size + 20 );
553
554 return res;
555}
556
557#define snprintf compat_snprintf
558#endif
559
560#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
561
562#define SAFE_SNPRINTF() \
563{ \
564 if( ret == -1 ) \
565 return( -1 ); \
566 \
567 if ( (unsigned int) ret > n ) { \
568 p[n - 1] = '\0'; \
569 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
570 } \
571 \
572 n -= (unsigned int) ret; \
573 p += (unsigned int) ret; \
574}
575
576/* Return the x.y.z.... style numeric string for the given OID */
577int oid_get_numeric_string( char *buf, size_t size,
578 const asn1_buf *oid )
579{
580 int ret;
581 size_t i, n;
582 unsigned int value;
583 char *p;
584
585 p = buf;
586 n = size;
587
588 /* First byte contains first two dots */
589 if( oid->len > 0 )
590 {
591 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
592 SAFE_SNPRINTF();
593 }
594
Paul Bakkerc70b9822013-04-07 22:00:46 +0200595 value = 0;
596 for( i = 1; i < oid->len; i++ )
597 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200598 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200599 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200600 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
601
Paul Bakkerc70b9822013-04-07 22:00:46 +0200602 value <<= 7;
603 value += oid->p[i] & 0x7F;
604
605 if( !( oid->p[i] & 0x80 ) )
606 {
607 /* Last byte */
608 ret = snprintf( p, n, ".%d", value );
609 SAFE_SNPRINTF();
610 value = 0;
611 }
612 }
613
614 return( (int) ( size - n ) );
615}
616
Paul Bakkerc70b9822013-04-07 22:00:46 +0200617#endif /* POLARSSL_OID_C */