blob: 24018d65de081662c91a2f14358bb29b16cb713c [file] [log] [blame]
Paul Bakkerc70b9822013-04-07 22:00:46 +02001/**
2 * \file oid.c
3 *
4 * \brief Object Identifier (OID) database
5 *
Paul Bakker9af723c2014-05-01 13:03:14 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerc70b9822013-04-07 22:00:46 +02007 *
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerc70b9822013-04-07 22:00:46 +020029#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
31#include POLARSSL_CONFIG_FILE
32#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +020033
34#if defined(POLARSSL_OID_C)
35
36#include "polarssl/oid.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020037#include "polarssl/rsa.h"
38
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker2292d1f2013-09-15 17:06:49 +020040#include "polarssl/x509.h"
41#endif
42
Paul Bakkered27a042013-04-18 22:46:23 +020043#include <stdio.h>
44
Paul Bakkerdd1150e2013-06-28 17:20:22 +020045/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020046 * Macro to automatically add the size of #define'd OIDs
47 */
48#define ADD_LEN(s) s, OID_SIZE(s)
49
50/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020051 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
52 * the other functions)
53 */
Paul Bakker45a2c8d2013-10-28 12:57:08 +010054#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
55static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
56{ \
57 const TYPE_T *p = LIST; \
58 const oid_descriptor_t *cur = (const oid_descriptor_t *) p; \
59 if( p == NULL || oid == NULL ) return( NULL ); \
60 while( cur->asn1 != NULL ) { \
61 if( cur->asn1_len == oid->len && \
62 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
63 return( p ); \
64 } \
65 p++; \
66 cur = (const oid_descriptor_t *) p; \
67 } \
68 return( NULL ); \
69}
Paul Bakkerbd51ad52013-06-28 16:51:52 +020070
71/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020072 * Macro to generate a function for retrieving a single attribute from the
73 * descriptor of an oid_descriptor_t wrapper.
74 */
75#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
76int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
77{ \
78 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020079 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020080 *ATTR1 = data->descriptor.ATTR1; \
81 return( 0 ); \
82}
83
84/*
85 * Macro to generate a function for retrieving a single attribute from an
86 * oid_descriptor_t wrapper.
87 */
88#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
89int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
90{ \
91 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020092 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020093 *ATTR1 = data->ATTR1; \
94 return( 0 ); \
95}
96
97/*
98 * Macro to generate a function for retrieving two attributes from an
99 * oid_descriptor_t wrapper.
100 */
101#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
102 ATTR2_TYPE, ATTR2) \
103int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
104{ \
105 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +0200106 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200107 *ATTR1 = data->ATTR1; \
108 *ATTR2 = data->ATTR2; \
109 return( 0 ); \
110}
111
112/*
Paul Bakkerce6ae232013-06-28 18:05:35 +0200113 * Macro to generate a function for retrieving the OID based on a single
114 * attribute from a oid_descriptor_t wrapper.
115 */
116#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200117int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, 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 ) { \
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/*
132 * Macro to generate a function for retrieving the OID based on two
133 * attributes from a oid_descriptor_t wrapper.
134 */
135#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
136 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200137int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
138 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200139{ \
140 const TYPE_T *cur = LIST; \
141 while( cur->descriptor.asn1 != NULL ) { \
142 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200143 *oid = cur->descriptor.asn1; \
144 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200145 return( 0 ); \
146 } \
147 cur++; \
148 } \
149 return( POLARSSL_ERR_OID_NOT_FOUND ); \
150}
151
152/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200153 * For X520 attribute types
154 */
155typedef struct {
156 oid_descriptor_t descriptor;
157 const char *short_name;
158} oid_x520_attr_t;
159
160static const oid_x520_attr_t oid_x520_attr_type[] =
161{
162 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200163 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200164 "CN",
165 },
166 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200167 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200168 "C",
169 },
170 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200171 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200172 "L",
173 },
174 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200175 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200176 "ST",
177 },
178 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200179 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200180 "O",
181 },
182 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200183 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200184 "OU",
185 },
186 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200187 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200188 "emailAddress",
189 },
190 {
Paul Bakker7b0be682013-10-29 14:24:37 +0100191 { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
192 "serialNumber",
193 },
194 {
195 { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
196 "postalAddress",
197 },
198 {
199 { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
200 "postalCode",
201 },
202 {
Paul Bakker63844402014-04-30 15:34:12 +0200203 { ADD_LEN( OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
204 "SN",
205 },
206 {
207 { ADD_LEN( OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
208 "GN",
209 },
210 {
211 { ADD_LEN( OID_AT_INITIALS ), "id-at-initials", "Initials" },
212 "initials",
213 },
214 {
215 { ADD_LEN( OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
216 "generationQualifier",
217 },
218 {
219 { ADD_LEN( OID_AT_TITLE ), "id-at-title", "Title" },
220 "title",
221 },
222 {
223 { ADD_LEN( OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
224 "dnQualifier",
225 },
226 {
227 { ADD_LEN( OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
228 "pseudonym",
229 },
230 {
231 { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
232 "DC",
233 },
234 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200235 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200236 NULL,
237 }
238};
239
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200240FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
241FN_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 +0200242
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200244/*
245 * For X509 extensions
246 */
247typedef struct {
248 oid_descriptor_t descriptor;
249 int ext_type;
250} oid_x509_ext_t;
251
252static const oid_x509_ext_t oid_x509_ext[] =
253{
254 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200255 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200256 EXT_BASIC_CONSTRAINTS,
257 },
258 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200259 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200260 EXT_KEY_USAGE,
261 },
262 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200263 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200264 EXT_EXTENDED_KEY_USAGE,
265 },
266 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200267 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200268 EXT_SUBJECT_ALT_NAME,
269 },
270 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200271 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200272 EXT_NS_CERT_TYPE,
273 },
274 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200275 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200276 0,
277 },
278};
279
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200280FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
281FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200282
Paul Bakkerc70b9822013-04-07 22:00:46 +0200283static const oid_descriptor_t oid_ext_key_usage[] =
284{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200285 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
286 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
287 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
288 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
289 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
290 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
291 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200292};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200293
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200294FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
295FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200297
Paul Bakker47fce022013-06-28 17:34:34 +0200298#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200299/*
300 * For SignatureAlgorithmIdentifier
301 */
302typedef struct {
303 oid_descriptor_t descriptor;
304 md_type_t md_alg;
305 pk_type_t pk_alg;
306} oid_sig_alg_t;
307
308static const oid_sig_alg_t oid_sig_alg[] =
309{
310 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200311 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200312 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
313 },
314 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200315 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200316 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
317 },
318 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200319 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200320 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
321 },
322 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200323 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200324 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
325 },
326 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200327 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200328 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
329 },
330 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200331 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200332 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
333 },
334 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200335 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200336 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
337 },
338 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200339 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200340 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
341 },
342 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200343 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200344 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
345 },
346 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200347 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200348 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
349 },
350 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200351 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200352 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
353 },
354 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200355 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200356 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
357 },
358 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200359 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200360 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
361 },
362 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200363 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200364 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
365 },
366 {
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100367 { ADD_LEN( OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" },
368 POLARSSL_MD_NONE, POLARSSL_PK_RSASSA_PSS,
369 },
370 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200371 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200372 0, 0,
373 },
374};
375
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200376FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
377FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
378FN_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 +0200379FN_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 +0200380#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200381
Paul Bakkerc70b9822013-04-07 22:00:46 +0200382/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200383 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200384 */
385typedef struct {
386 oid_descriptor_t descriptor;
387 pk_type_t pk_alg;
388} oid_pk_alg_t;
389
390static const oid_pk_alg_t oid_pk_alg[] =
391{
392 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200393 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200394 POLARSSL_PK_RSA,
395 },
396 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200397 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200398 POLARSSL_PK_ECKEY,
399 },
400 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200401 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200402 POLARSSL_PK_ECKEY_DH,
403 },
404 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200405 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200406 0,
407 },
408};
409
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200410FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
411FN_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 +0200412FN_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 +0200413
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200414#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200415/*
416 * For namedCurve (RFC 5480)
417 */
418typedef struct {
419 oid_descriptor_t descriptor;
420 ecp_group_id grp_id;
421} oid_ecp_grp_t;
422
423static const oid_ecp_grp_t oid_ecp_grp[] =
424{
425 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200426 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200427 POLARSSL_ECP_DP_SECP192R1,
428 },
429 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200430 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200431 POLARSSL_ECP_DP_SECP224R1,
432 },
433 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200434 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200435 POLARSSL_ECP_DP_SECP256R1,
436 },
437 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200438 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200439 POLARSSL_ECP_DP_SECP384R1,
440 },
441 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200442 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200443 POLARSSL_ECP_DP_SECP521R1,
444 },
445 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100446 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
447 POLARSSL_ECP_DP_SECP192K1,
448 },
449 {
450 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
451 POLARSSL_ECP_DP_SECP224K1,
452 },
453 {
454 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
455 POLARSSL_ECP_DP_SECP256K1,
456 },
457 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200458 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
459 POLARSSL_ECP_DP_BP256R1,
460 },
461 {
462 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
463 POLARSSL_ECP_DP_BP384R1,
464 },
465 {
466 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
467 POLARSSL_ECP_DP_BP512R1,
468 },
469 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200470 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200471 0,
472 },
473};
474
475FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
476FN_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 +0200477FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
478#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200479
Paul Bakker47fce022013-06-28 17:34:34 +0200480#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200481/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200482 * For PKCS#5 PBES2 encryption algorithm
483 */
484typedef struct {
485 oid_descriptor_t descriptor;
486 cipher_type_t cipher_alg;
487} oid_cipher_alg_t;
488
489static const oid_cipher_alg_t oid_cipher_alg[] =
490{
491 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200492 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200493 POLARSSL_CIPHER_DES_CBC,
494 },
495 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200496 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200497 POLARSSL_CIPHER_DES_EDE3_CBC,
498 },
499 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200500 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200501 0,
502 },
503};
504
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200505FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
506FN_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 +0200507#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200508
Paul Bakker47fce022013-06-28 17:34:34 +0200509#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200510/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200511 * For digestAlgorithm
512 */
513typedef struct {
514 oid_descriptor_t descriptor;
515 md_type_t md_alg;
516} oid_md_alg_t;
517
518static const oid_md_alg_t oid_md_alg[] =
519{
520 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200521 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200522 POLARSSL_MD_MD2,
523 },
524 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200525 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200526 POLARSSL_MD_MD4,
527 },
528 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200529 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200530 POLARSSL_MD_MD5,
531 },
532 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200533 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200534 POLARSSL_MD_SHA1,
535 },
536 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200537 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200538 POLARSSL_MD_SHA1,
539 },
540 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200541 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200542 POLARSSL_MD_SHA224,
543 },
544 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200545 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200546 POLARSSL_MD_SHA256,
547 },
548 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200549 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200550 POLARSSL_MD_SHA384,
551 },
552 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200553 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200554 POLARSSL_MD_SHA512,
555 },
556 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200557 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200558 0,
559 },
560};
561
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200562FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
563FN_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 +0200564FN_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 +0200565#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200566
Paul Bakker47fce022013-06-28 17:34:34 +0200567#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200568/*
569 * For PKCS#12 PBEs
570 */
571typedef struct {
572 oid_descriptor_t descriptor;
573 md_type_t md_alg;
574 cipher_type_t cipher_alg;
575} oid_pkcs12_pbe_alg_t;
576
577static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
578{
579 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200580 { 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 +0200581 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
582 },
583 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200584 { 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 +0200585 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
586 },
587 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200588 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200589 0, 0,
590 },
591};
592
593FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
594FN_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 +0200595#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200596
Paul Bakker6edcd412013-10-29 15:22:54 +0100597#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
598 !defined(EFI32)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200599#include <stdarg.h>
600
601#if !defined vsnprintf
602#define vsnprintf _vsnprintf
603#endif // vsnprintf
604
605/*
606 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
607 * Result value is not size of buffer needed, but -1 if no fit is possible.
608 *
609 * This fuction tries to 'fix' this by at least suggesting enlarging the
610 * size by 20.
611 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200612static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200613{
614 va_list ap;
615 int res = -1;
616
617 va_start( ap, format );
618
619 res = vsnprintf( str, size, format, ap );
620
621 va_end( ap );
622
623 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200624 if( res < 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200625 return( (int) size + 20 );
626
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200627 return( res );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200628}
629
630#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200631#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200632
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633#define SAFE_SNPRINTF() \
634{ \
635 if( ret == -1 ) \
636 return( POLARSSL_ERR_OID_BUF_TOO_SMALL ); \
637 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200638 if( (unsigned int) ret >= n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200639 p[n - 1] = '\0'; \
640 return( POLARSSL_ERR_OID_BUF_TOO_SMALL ); \
641 } \
642 \
643 n -= (unsigned int) ret; \
644 p += (unsigned int) ret; \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200645}
646
647/* Return the x.y.z.... style numeric string for the given OID */
648int oid_get_numeric_string( char *buf, size_t size,
649 const asn1_buf *oid )
650{
651 int ret;
652 size_t i, n;
653 unsigned int value;
654 char *p;
655
656 p = buf;
657 n = size;
658
659 /* First byte contains first two dots */
660 if( oid->len > 0 )
661 {
662 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
663 SAFE_SNPRINTF();
664 }
665
Paul Bakkerc70b9822013-04-07 22:00:46 +0200666 value = 0;
667 for( i = 1; i < oid->len; i++ )
668 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200669 /* Prevent overflow in value. */
Paul Bakker66d5d072014-06-17 16:39:18 +0200670 if( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100671 return( POLARSSL_ERR_OID_BUF_TOO_SMALL );
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200672
Paul Bakkerc70b9822013-04-07 22:00:46 +0200673 value <<= 7;
674 value += oid->p[i] & 0x7F;
675
676 if( !( oid->p[i] & 0x80 ) )
677 {
678 /* Last byte */
679 ret = snprintf( p, n, ".%d", value );
680 SAFE_SNPRINTF();
681 value = 0;
682 }
683 }
684
685 return( (int) ( size - n ) );
686}
687
Paul Bakkerc70b9822013-04-07 22:00:46 +0200688#endif /* POLARSSL_OID_C */