blob: a4f2c0c2c7222ecd31ca18459567b23b1dfacb09 [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 Bakker45a2c8d2013-10-28 12:57:08 +010050#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
51static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
52{ \
53 const TYPE_T *p = LIST; \
54 const oid_descriptor_t *cur = (const oid_descriptor_t *) p; \
55 if( p == NULL || oid == NULL ) return( NULL ); \
56 while( cur->asn1 != NULL ) { \
57 if( cur->asn1_len == oid->len && \
58 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
59 return( p ); \
60 } \
61 p++; \
62 cur = (const oid_descriptor_t *) p; \
63 } \
64 return( NULL ); \
65}
Paul Bakkerbd51ad52013-06-28 16:51:52 +020066
67/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020068 * Macro to generate a function for retrieving a single attribute from the
69 * descriptor of an oid_descriptor_t wrapper.
70 */
71#define FN_OID_GET_DESCRIPTOR_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->descriptor.ATTR1; \
77 return( 0 ); \
78}
79
80/*
81 * Macro to generate a function for retrieving a single attribute from an
82 * oid_descriptor_t wrapper.
83 */
84#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
85int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
86{ \
87 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
88 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
89 *ATTR1 = data->ATTR1; \
90 return( 0 ); \
91}
92
93/*
94 * Macro to generate a function for retrieving two attributes from an
95 * oid_descriptor_t wrapper.
96 */
97#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
98 ATTR2_TYPE, ATTR2) \
99int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
100{ \
101 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
102 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
103 *ATTR1 = data->ATTR1; \
104 *ATTR2 = data->ATTR2; \
105 return( 0 ); \
106}
107
108/*
Paul Bakkerce6ae232013-06-28 18:05:35 +0200109 * Macro to generate a function for retrieving the OID based on a single
110 * attribute from a oid_descriptor_t wrapper.
111 */
112#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200113int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200114{ \
115 const TYPE_T *cur = LIST; \
116 while( cur->descriptor.asn1 != NULL ) { \
117 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200118 *oid = cur->descriptor.asn1; \
119 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200120 return( 0 ); \
121 } \
122 cur++; \
123 } \
124 return( POLARSSL_ERR_OID_NOT_FOUND ); \
125}
126
127/*
128 * Macro to generate a function for retrieving the OID based on two
129 * attributes from a oid_descriptor_t wrapper.
130 */
131#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
132 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200133int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
134 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200135{ \
136 const TYPE_T *cur = LIST; \
137 while( cur->descriptor.asn1 != NULL ) { \
138 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200139 *oid = cur->descriptor.asn1; \
140 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200141 return( 0 ); \
142 } \
143 cur++; \
144 } \
145 return( POLARSSL_ERR_OID_NOT_FOUND ); \
146}
147
148/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200149 * For X520 attribute types
150 */
151typedef struct {
152 oid_descriptor_t descriptor;
153 const char *short_name;
154} oid_x520_attr_t;
155
156static const oid_x520_attr_t oid_x520_attr_type[] =
157{
158 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200159 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200160 "CN",
161 },
162 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200163 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200164 "C",
165 },
166 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200167 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200168 "L",
169 },
170 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200171 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200172 "ST",
173 },
174 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200175 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200176 "O",
177 },
178 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200179 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200180 "OU",
181 },
182 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200183 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200184 "emailAddress",
185 },
186 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200187 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200188 NULL,
189 }
190};
191
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200192FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
193FN_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 +0200194
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200196/*
197 * For X509 extensions
198 */
199typedef struct {
200 oid_descriptor_t descriptor;
201 int ext_type;
202} oid_x509_ext_t;
203
204static const oid_x509_ext_t oid_x509_ext[] =
205{
206 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200207 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200208 EXT_BASIC_CONSTRAINTS,
209 },
210 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200211 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200212 EXT_KEY_USAGE,
213 },
214 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200215 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200216 EXT_EXTENDED_KEY_USAGE,
217 },
218 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200219 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200220 EXT_SUBJECT_ALT_NAME,
221 },
222 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200223 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200224 EXT_NS_CERT_TYPE,
225 },
226 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200227 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200228 0,
229 },
230};
231
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200232FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
233FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200234
Paul Bakkerc70b9822013-04-07 22:00:46 +0200235static const oid_descriptor_t oid_ext_key_usage[] =
236{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200237 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
238 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
239 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
240 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
241 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
242 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
243 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200244};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200245
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200246FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
247FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200249
Paul Bakker47fce022013-06-28 17:34:34 +0200250#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200251/*
252 * For SignatureAlgorithmIdentifier
253 */
254typedef struct {
255 oid_descriptor_t descriptor;
256 md_type_t md_alg;
257 pk_type_t pk_alg;
258} oid_sig_alg_t;
259
260static const oid_sig_alg_t oid_sig_alg[] =
261{
262 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200263 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200264 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
265 },
266 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200267 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200268 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
269 },
270 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200271 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200272 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
273 },
274 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200275 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200276 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
277 },
278 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200279 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200280 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
281 },
282 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200283 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200284 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
285 },
286 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200287 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200288 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
289 },
290 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200291 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200292 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
293 },
294 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200295 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200296 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
297 },
298 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200299 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200300 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
301 },
302 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200303 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200304 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
305 },
306 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200307 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200308 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
309 },
310 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200311 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200312 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
313 },
314 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200315 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200316 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
317 },
318 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200319 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200320 0, 0,
321 },
322};
323
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200324FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
325FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
326FN_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 +0200327FN_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 +0200328#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200329
Paul Bakkerc70b9822013-04-07 22:00:46 +0200330/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200331 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200332 */
333typedef struct {
334 oid_descriptor_t descriptor;
335 pk_type_t pk_alg;
336} oid_pk_alg_t;
337
338static const oid_pk_alg_t oid_pk_alg[] =
339{
340 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200341 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200342 POLARSSL_PK_RSA,
343 },
344 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200345 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200346 POLARSSL_PK_ECKEY,
347 },
348 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200349 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200350 POLARSSL_PK_ECKEY_DH,
351 },
352 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200353 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200354 0,
355 },
356};
357
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200358FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
359FN_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 +0200360FN_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 +0200361
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200362#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200363/*
364 * For namedCurve (RFC 5480)
365 */
366typedef struct {
367 oid_descriptor_t descriptor;
368 ecp_group_id grp_id;
369} oid_ecp_grp_t;
370
371static const oid_ecp_grp_t oid_ecp_grp[] =
372{
373 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200374 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200375 POLARSSL_ECP_DP_SECP192R1,
376 },
377 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200378 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200379 POLARSSL_ECP_DP_SECP224R1,
380 },
381 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200382 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200383 POLARSSL_ECP_DP_SECP256R1,
384 },
385 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200386 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200387 POLARSSL_ECP_DP_SECP384R1,
388 },
389 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200390 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200391 POLARSSL_ECP_DP_SECP521R1,
392 },
393 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200394 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
395 POLARSSL_ECP_DP_BP256R1,
396 },
397 {
398 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
399 POLARSSL_ECP_DP_BP384R1,
400 },
401 {
402 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
403 POLARSSL_ECP_DP_BP512R1,
404 },
405 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200406 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200407 0,
408 },
409};
410
411FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
412FN_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 +0200413FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
414#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200415
Paul Bakker47fce022013-06-28 17:34:34 +0200416#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200417/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200418 * For PKCS#5 PBES2 encryption algorithm
419 */
420typedef struct {
421 oid_descriptor_t descriptor;
422 cipher_type_t cipher_alg;
423} oid_cipher_alg_t;
424
425static const oid_cipher_alg_t oid_cipher_alg[] =
426{
427 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200428 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200429 POLARSSL_CIPHER_DES_CBC,
430 },
431 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200432 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200433 POLARSSL_CIPHER_DES_EDE3_CBC,
434 },
435 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200436 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200437 0,
438 },
439};
440
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200441FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
442FN_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 +0200443#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200444
Paul Bakker47fce022013-06-28 17:34:34 +0200445#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200446/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200447 * For digestAlgorithm
448 */
449typedef struct {
450 oid_descriptor_t descriptor;
451 md_type_t md_alg;
452} oid_md_alg_t;
453
454static const oid_md_alg_t oid_md_alg[] =
455{
456 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200457 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200458 POLARSSL_MD_MD2,
459 },
460 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200461 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200462 POLARSSL_MD_MD4,
463 },
464 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200465 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200466 POLARSSL_MD_MD5,
467 },
468 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200469 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200470 POLARSSL_MD_SHA1,
471 },
472 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200473 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200474 POLARSSL_MD_SHA1,
475 },
476 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200477 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200478 POLARSSL_MD_SHA224,
479 },
480 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200481 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200482 POLARSSL_MD_SHA256,
483 },
484 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200485 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200486 POLARSSL_MD_SHA384,
487 },
488 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200489 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200490 POLARSSL_MD_SHA512,
491 },
492 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200493 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200494 0,
495 },
496};
497
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200498FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
499FN_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 +0200500FN_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 +0200501#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200502
Paul Bakker47fce022013-06-28 17:34:34 +0200503#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200504/*
505 * For PKCS#12 PBEs
506 */
507typedef struct {
508 oid_descriptor_t descriptor;
509 md_type_t md_alg;
510 cipher_type_t cipher_alg;
511} oid_pkcs12_pbe_alg_t;
512
513static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
514{
515 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200516 { 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 +0200517 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
518 },
519 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200520 { 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 +0200521 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
522 },
523 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200524 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200525 0, 0,
526 },
527};
528
529FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
530FN_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 +0200531#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200532
Paul Bakkerc70b9822013-04-07 22:00:46 +0200533#if defined _MSC_VER && !defined snprintf
534#include <stdarg.h>
535
536#if !defined vsnprintf
537#define vsnprintf _vsnprintf
538#endif // vsnprintf
539
540/*
541 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
542 * Result value is not size of buffer needed, but -1 if no fit is possible.
543 *
544 * This fuction tries to 'fix' this by at least suggesting enlarging the
545 * size by 20.
546 */
547static int compat_snprintf(char *str, size_t size, const char *format, ...)
548{
549 va_list ap;
550 int res = -1;
551
552 va_start( ap, format );
553
554 res = vsnprintf( str, size, format, ap );
555
556 va_end( ap );
557
558 // No quick fix possible
559 if ( res < 0 )
560 return( (int) size + 20 );
561
562 return res;
563}
564
565#define snprintf compat_snprintf
566#endif
567
568#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
569
570#define SAFE_SNPRINTF() \
571{ \
572 if( ret == -1 ) \
573 return( -1 ); \
574 \
575 if ( (unsigned int) ret > n ) { \
576 p[n - 1] = '\0'; \
577 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
578 } \
579 \
580 n -= (unsigned int) ret; \
581 p += (unsigned int) ret; \
582}
583
584/* Return the x.y.z.... style numeric string for the given OID */
585int oid_get_numeric_string( char *buf, size_t size,
586 const asn1_buf *oid )
587{
588 int ret;
589 size_t i, n;
590 unsigned int value;
591 char *p;
592
593 p = buf;
594 n = size;
595
596 /* First byte contains first two dots */
597 if( oid->len > 0 )
598 {
599 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
600 SAFE_SNPRINTF();
601 }
602
Paul Bakkerc70b9822013-04-07 22:00:46 +0200603 value = 0;
604 for( i = 1; i < oid->len; i++ )
605 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200606 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200607 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200608 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
609
Paul Bakkerc70b9822013-04-07 22:00:46 +0200610 value <<= 7;
611 value += oid->p[i] & 0x7F;
612
613 if( !( oid->p[i] & 0x80 ) )
614 {
615 /* Last byte */
616 ret = snprintf( p, n, ".%d", value );
617 SAFE_SNPRINTF();
618 value = 0;
619 }
620 }
621
622 return( (int) ( size - n ) );
623}
624
Paul Bakkerc70b9822013-04-07 22:00:46 +0200625#endif /* POLARSSL_OID_C */