blob: f943c6d34835896e9f401acf977dac80d5b00933 [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 {
Paul Bakker7b0be682013-10-29 14:24:37 +0100187 { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
188 "serialNumber",
189 },
190 {
191 { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
192 "postalAddress",
193 },
194 {
195 { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
196 "postalCode",
197 },
198 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200199 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200200 NULL,
201 }
202};
203
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200204FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
205FN_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 +0200206
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200208/*
209 * For X509 extensions
210 */
211typedef struct {
212 oid_descriptor_t descriptor;
213 int ext_type;
214} oid_x509_ext_t;
215
216static const oid_x509_ext_t oid_x509_ext[] =
217{
218 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200219 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200220 EXT_BASIC_CONSTRAINTS,
221 },
222 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200223 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200224 EXT_KEY_USAGE,
225 },
226 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200227 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200228 EXT_EXTENDED_KEY_USAGE,
229 },
230 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200231 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200232 EXT_SUBJECT_ALT_NAME,
233 },
234 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200235 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200236 EXT_NS_CERT_TYPE,
237 },
238 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200239 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200240 0,
241 },
242};
243
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200244FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
245FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200246
Paul Bakkerc70b9822013-04-07 22:00:46 +0200247static const oid_descriptor_t oid_ext_key_usage[] =
248{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200249 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
250 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
251 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
252 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
253 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
254 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
255 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200256};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200257
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200258FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
259FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200261
Paul Bakker47fce022013-06-28 17:34:34 +0200262#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200263/*
264 * For SignatureAlgorithmIdentifier
265 */
266typedef struct {
267 oid_descriptor_t descriptor;
268 md_type_t md_alg;
269 pk_type_t pk_alg;
270} oid_sig_alg_t;
271
272static const oid_sig_alg_t oid_sig_alg[] =
273{
274 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200275 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200276 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
277 },
278 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200279 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200280 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
281 },
282 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200283 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200284 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
285 },
286 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200287 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200288 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
289 },
290 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200291 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200292 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
293 },
294 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200295 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200296 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
297 },
298 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200299 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200300 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
301 },
302 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200303 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200304 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
305 },
306 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200307 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200308 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
309 },
310 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200311 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200312 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
313 },
314 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200315 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200316 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
317 },
318 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200319 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200320 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
321 },
322 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200323 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200324 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
325 },
326 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200327 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200328 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
329 },
330 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200331 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200332 0, 0,
333 },
334};
335
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200336FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
337FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
338FN_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 +0200339FN_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 +0200340#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200341
Paul Bakkerc70b9822013-04-07 22:00:46 +0200342/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200343 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200344 */
345typedef struct {
346 oid_descriptor_t descriptor;
347 pk_type_t pk_alg;
348} oid_pk_alg_t;
349
350static const oid_pk_alg_t oid_pk_alg[] =
351{
352 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200353 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200354 POLARSSL_PK_RSA,
355 },
356 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200357 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200358 POLARSSL_PK_ECKEY,
359 },
360 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200361 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200362 POLARSSL_PK_ECKEY_DH,
363 },
364 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200365 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200366 0,
367 },
368};
369
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200370FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
371FN_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 +0200372FN_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 +0200373
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200374#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200375/*
376 * For namedCurve (RFC 5480)
377 */
378typedef struct {
379 oid_descriptor_t descriptor;
380 ecp_group_id grp_id;
381} oid_ecp_grp_t;
382
383static const oid_ecp_grp_t oid_ecp_grp[] =
384{
385 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200386 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200387 POLARSSL_ECP_DP_SECP192R1,
388 },
389 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200390 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200391 POLARSSL_ECP_DP_SECP224R1,
392 },
393 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200394 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200395 POLARSSL_ECP_DP_SECP256R1,
396 },
397 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200398 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200399 POLARSSL_ECP_DP_SECP384R1,
400 },
401 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200402 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200403 POLARSSL_ECP_DP_SECP521R1,
404 },
405 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100406 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
407 POLARSSL_ECP_DP_SECP192K1,
408 },
409 {
410 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
411 POLARSSL_ECP_DP_SECP224K1,
412 },
413 {
414 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
415 POLARSSL_ECP_DP_SECP256K1,
416 },
417 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200418 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
419 POLARSSL_ECP_DP_BP256R1,
420 },
421 {
422 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
423 POLARSSL_ECP_DP_BP384R1,
424 },
425 {
426 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
427 POLARSSL_ECP_DP_BP512R1,
428 },
429 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200430 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200431 0,
432 },
433};
434
435FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
436FN_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 +0200437FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
438#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200439
Paul Bakker47fce022013-06-28 17:34:34 +0200440#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200441/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200442 * For PKCS#5 PBES2 encryption algorithm
443 */
444typedef struct {
445 oid_descriptor_t descriptor;
446 cipher_type_t cipher_alg;
447} oid_cipher_alg_t;
448
449static const oid_cipher_alg_t oid_cipher_alg[] =
450{
451 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200452 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200453 POLARSSL_CIPHER_DES_CBC,
454 },
455 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200456 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200457 POLARSSL_CIPHER_DES_EDE3_CBC,
458 },
459 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200460 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200461 0,
462 },
463};
464
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200465FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
466FN_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 +0200467#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200468
Paul Bakker47fce022013-06-28 17:34:34 +0200469#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200470/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200471 * For digestAlgorithm
472 */
473typedef struct {
474 oid_descriptor_t descriptor;
475 md_type_t md_alg;
476} oid_md_alg_t;
477
478static const oid_md_alg_t oid_md_alg[] =
479{
480 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200481 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200482 POLARSSL_MD_MD2,
483 },
484 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200485 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200486 POLARSSL_MD_MD4,
487 },
488 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200489 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200490 POLARSSL_MD_MD5,
491 },
492 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200493 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200494 POLARSSL_MD_SHA1,
495 },
496 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200497 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200498 POLARSSL_MD_SHA1,
499 },
500 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200501 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200502 POLARSSL_MD_SHA224,
503 },
504 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200505 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200506 POLARSSL_MD_SHA256,
507 },
508 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200509 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200510 POLARSSL_MD_SHA384,
511 },
512 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200513 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200514 POLARSSL_MD_SHA512,
515 },
516 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200517 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200518 0,
519 },
520};
521
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200522FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
523FN_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 +0200524FN_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 +0200525#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200526
Paul Bakker47fce022013-06-28 17:34:34 +0200527#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200528/*
529 * For PKCS#12 PBEs
530 */
531typedef struct {
532 oid_descriptor_t descriptor;
533 md_type_t md_alg;
534 cipher_type_t cipher_alg;
535} oid_pkcs12_pbe_alg_t;
536
537static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
538{
539 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200540 { 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 +0200541 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
542 },
543 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200544 { 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 +0200545 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
546 },
547 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200548 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200549 0, 0,
550 },
551};
552
553FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
554FN_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 +0200555#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200556
Paul Bakker6edcd412013-10-29 15:22:54 +0100557#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
558 !defined(EFI32)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200559#include <stdarg.h>
560
561#if !defined vsnprintf
562#define vsnprintf _vsnprintf
563#endif // vsnprintf
564
565/*
566 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
567 * Result value is not size of buffer needed, but -1 if no fit is possible.
568 *
569 * This fuction tries to 'fix' this by at least suggesting enlarging the
570 * size by 20.
571 */
572static int compat_snprintf(char *str, size_t size, const char *format, ...)
573{
574 va_list ap;
575 int res = -1;
576
577 va_start( ap, format );
578
579 res = vsnprintf( str, size, format, ap );
580
581 va_end( ap );
582
583 // No quick fix possible
584 if ( res < 0 )
585 return( (int) size + 20 );
586
587 return res;
588}
589
590#define snprintf compat_snprintf
591#endif
592
593#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
594
595#define SAFE_SNPRINTF() \
596{ \
597 if( ret == -1 ) \
598 return( -1 ); \
599 \
600 if ( (unsigned int) ret > n ) { \
601 p[n - 1] = '\0'; \
602 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
603 } \
604 \
605 n -= (unsigned int) ret; \
606 p += (unsigned int) ret; \
607}
608
609/* Return the x.y.z.... style numeric string for the given OID */
610int oid_get_numeric_string( char *buf, size_t size,
611 const asn1_buf *oid )
612{
613 int ret;
614 size_t i, n;
615 unsigned int value;
616 char *p;
617
618 p = buf;
619 n = size;
620
621 /* First byte contains first two dots */
622 if( oid->len > 0 )
623 {
624 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
625 SAFE_SNPRINTF();
626 }
627
Paul Bakkerc70b9822013-04-07 22:00:46 +0200628 value = 0;
629 for( i = 1; i < oid->len; i++ )
630 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200631 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200632 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200633 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
634
Paul Bakkerc70b9822013-04-07 22:00:46 +0200635 value <<= 7;
636 value += oid->p[i] & 0x7F;
637
638 if( !( oid->p[i] & 0x80 ) )
639 {
640 /* Last byte */
641 ret = snprintf( p, n, ".%d", value );
642 SAFE_SNPRINTF();
643 value = 0;
644 }
645 }
646
647 return( (int) ( size - n ) );
648}
649
Paul Bakkerc70b9822013-04-07 22:00:46 +0200650#endif /* POLARSSL_OID_C */