blob: 3cca1fa47a97e214641fd648709e98520c8f7e62 [file] [log] [blame]
Paul Bakkerc70b9822013-04-07 22:00:46 +02001/**
2 * \file oid.c
3 *
4 * \brief Object Identifier (OID) database
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerc70b9822013-04-07 22:00:46 +02007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerc70b9822013-04-07 22:00:46 +02009 *
Paul Bakkerc70b9822013-04-07 22:00:46 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerc70b9822013-04-07 22:00:46 +020026#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
28#include POLARSSL_CONFIG_FILE
29#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +020030
31#if defined(POLARSSL_OID_C)
32
33#include "polarssl/oid.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020034#include "polarssl/rsa.h"
35
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <stdio.h>
37#include <string.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 Bakkerdd1150e2013-06-28 17:20:22 +020043/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020044 * Macro to automatically add the size of #define'd OIDs
45 */
46#define ADD_LEN(s) s, OID_SIZE(s)
47
48/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020049 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
50 * the other functions)
51 */
Paul Bakker45a2c8d2013-10-28 12:57:08 +010052#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
53static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
54{ \
55 const TYPE_T *p = LIST; \
56 const oid_descriptor_t *cur = (const oid_descriptor_t *) p; \
57 if( p == NULL || oid == NULL ) return( NULL ); \
58 while( cur->asn1 != NULL ) { \
59 if( cur->asn1_len == oid->len && \
60 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
61 return( p ); \
62 } \
63 p++; \
64 cur = (const oid_descriptor_t *) p; \
65 } \
66 return( NULL ); \
67}
Paul Bakkerbd51ad52013-06-28 16:51:52 +020068
69/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020070 * Macro to generate a function for retrieving a single attribute from the
71 * descriptor of an oid_descriptor_t wrapper.
72 */
73#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
74int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
75{ \
76 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020077 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020078 *ATTR1 = data->descriptor.ATTR1; \
79 return( 0 ); \
80}
81
82/*
83 * Macro to generate a function for retrieving a single attribute from an
84 * oid_descriptor_t wrapper.
85 */
86#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
87int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
88{ \
89 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020090 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020091 *ATTR1 = data->ATTR1; \
92 return( 0 ); \
93}
94
95/*
96 * Macro to generate a function for retrieving two attributes from an
97 * oid_descriptor_t wrapper.
98 */
99#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
100 ATTR2_TYPE, ATTR2) \
101int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
102{ \
103 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +0200104 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200105 *ATTR1 = data->ATTR1; \
106 *ATTR2 = data->ATTR2; \
107 return( 0 ); \
108}
109
110/*
Paul Bakkerce6ae232013-06-28 18:05:35 +0200111 * Macro to generate a function for retrieving the OID based on a single
112 * attribute from a oid_descriptor_t wrapper.
113 */
114#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200115int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200116{ \
117 const TYPE_T *cur = LIST; \
118 while( cur->descriptor.asn1 != NULL ) { \
119 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200120 *oid = cur->descriptor.asn1; \
121 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200122 return( 0 ); \
123 } \
124 cur++; \
125 } \
126 return( POLARSSL_ERR_OID_NOT_FOUND ); \
127}
128
129/*
130 * Macro to generate a function for retrieving the OID based on two
131 * attributes from a oid_descriptor_t wrapper.
132 */
133#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
134 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200135int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
136 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200137{ \
138 const TYPE_T *cur = LIST; \
139 while( cur->descriptor.asn1 != NULL ) { \
140 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200141 *oid = cur->descriptor.asn1; \
142 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200143 return( 0 ); \
144 } \
145 cur++; \
146 } \
147 return( POLARSSL_ERR_OID_NOT_FOUND ); \
148}
149
150/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200151 * For X520 attribute types
152 */
153typedef struct {
154 oid_descriptor_t descriptor;
155 const char *short_name;
156} oid_x520_attr_t;
157
158static const oid_x520_attr_t oid_x520_attr_type[] =
159{
160 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200161 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200162 "CN",
163 },
164 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200165 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200166 "C",
167 },
168 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200169 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200170 "L",
171 },
172 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200173 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200174 "ST",
175 },
176 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200177 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200178 "O",
179 },
180 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200181 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200182 "OU",
183 },
184 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200185 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200186 "emailAddress",
187 },
188 {
Paul Bakker7b0be682013-10-29 14:24:37 +0100189 { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
190 "serialNumber",
191 },
192 {
193 { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
194 "postalAddress",
195 },
196 {
197 { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
198 "postalCode",
199 },
200 {
Paul Bakker63844402014-04-30 15:34:12 +0200201 { ADD_LEN( OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
202 "SN",
203 },
204 {
205 { ADD_LEN( OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
206 "GN",
207 },
208 {
209 { ADD_LEN( OID_AT_INITIALS ), "id-at-initials", "Initials" },
210 "initials",
211 },
212 {
213 { ADD_LEN( OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
214 "generationQualifier",
215 },
216 {
217 { ADD_LEN( OID_AT_TITLE ), "id-at-title", "Title" },
218 "title",
219 },
220 {
221 { ADD_LEN( OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
222 "dnQualifier",
223 },
224 {
225 { ADD_LEN( OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
226 "pseudonym",
227 },
228 {
229 { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
230 "DC",
231 },
232 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200233 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200234 NULL,
235 }
236};
237
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200238FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
239FN_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 +0200240
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200242/*
243 * For X509 extensions
244 */
245typedef struct {
246 oid_descriptor_t descriptor;
247 int ext_type;
248} oid_x509_ext_t;
249
250static const oid_x509_ext_t oid_x509_ext[] =
251{
252 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200253 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200254 EXT_BASIC_CONSTRAINTS,
255 },
256 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200257 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200258 EXT_KEY_USAGE,
259 },
260 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200261 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200262 EXT_EXTENDED_KEY_USAGE,
263 },
264 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200265 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200266 EXT_SUBJECT_ALT_NAME,
267 },
268 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200269 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200270 EXT_NS_CERT_TYPE,
271 },
272 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200273 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200274 0,
275 },
276};
277
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200278FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
279FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200280
Paul Bakkerc70b9822013-04-07 22:00:46 +0200281static const oid_descriptor_t oid_ext_key_usage[] =
282{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200283 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
284 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
285 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
286 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
287 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
288 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
289 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200290};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200291
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200292FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
293FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200295
Paul Bakker47fce022013-06-28 17:34:34 +0200296#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200297/*
298 * For SignatureAlgorithmIdentifier
299 */
300typedef struct {
301 oid_descriptor_t descriptor;
302 md_type_t md_alg;
303 pk_type_t pk_alg;
304} oid_sig_alg_t;
305
306static const oid_sig_alg_t oid_sig_alg[] =
307{
308 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200309 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200310 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
311 },
312 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200313 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200314 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
315 },
316 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200317 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200318 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
319 },
320 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200321 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200322 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
323 },
324 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200325 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200326 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
327 },
328 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200329 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200330 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
331 },
332 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200333 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200334 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
335 },
336 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200337 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200338 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
339 },
340 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200341 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200342 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
343 },
344 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200345 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200346 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
347 },
348 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200349 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200350 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
351 },
352 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200353 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200354 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
355 },
356 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200357 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200358 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
359 },
360 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200361 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200362 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
363 },
364 {
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100365 { ADD_LEN( OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" },
366 POLARSSL_MD_NONE, POLARSSL_PK_RSASSA_PSS,
367 },
368 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200369 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200370 0, 0,
371 },
372};
373
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200374FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
375FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
376FN_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 +0200377FN_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 +0200378#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200379
Paul Bakkerc70b9822013-04-07 22:00:46 +0200380/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200381 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200382 */
383typedef struct {
384 oid_descriptor_t descriptor;
385 pk_type_t pk_alg;
386} oid_pk_alg_t;
387
388static const oid_pk_alg_t oid_pk_alg[] =
389{
390 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200391 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200392 POLARSSL_PK_RSA,
393 },
394 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200395 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200396 POLARSSL_PK_ECKEY,
397 },
398 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200399 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200400 POLARSSL_PK_ECKEY_DH,
401 },
402 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200403 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200404 0,
405 },
406};
407
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200408FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
409FN_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 +0200410FN_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 +0200411
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200412#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200413/*
414 * For namedCurve (RFC 5480)
415 */
416typedef struct {
417 oid_descriptor_t descriptor;
418 ecp_group_id grp_id;
419} oid_ecp_grp_t;
420
421static const oid_ecp_grp_t oid_ecp_grp[] =
422{
423 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200424 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200425 POLARSSL_ECP_DP_SECP192R1,
426 },
427 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200428 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200429 POLARSSL_ECP_DP_SECP224R1,
430 },
431 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200432 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200433 POLARSSL_ECP_DP_SECP256R1,
434 },
435 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200436 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200437 POLARSSL_ECP_DP_SECP384R1,
438 },
439 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200440 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200441 POLARSSL_ECP_DP_SECP521R1,
442 },
443 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100444 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
445 POLARSSL_ECP_DP_SECP192K1,
446 },
447 {
448 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
449 POLARSSL_ECP_DP_SECP224K1,
450 },
451 {
452 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
453 POLARSSL_ECP_DP_SECP256K1,
454 },
455 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200456 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
457 POLARSSL_ECP_DP_BP256R1,
458 },
459 {
460 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
461 POLARSSL_ECP_DP_BP384R1,
462 },
463 {
464 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
465 POLARSSL_ECP_DP_BP512R1,
466 },
467 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200468 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200469 0,
470 },
471};
472
473FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
474FN_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 +0200475FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
476#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200477
Paul Bakker47fce022013-06-28 17:34:34 +0200478#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200479/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200480 * For PKCS#5 PBES2 encryption algorithm
481 */
482typedef struct {
483 oid_descriptor_t descriptor;
484 cipher_type_t cipher_alg;
485} oid_cipher_alg_t;
486
487static const oid_cipher_alg_t oid_cipher_alg[] =
488{
489 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200490 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200491 POLARSSL_CIPHER_DES_CBC,
492 },
493 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200494 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200495 POLARSSL_CIPHER_DES_EDE3_CBC,
496 },
497 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200498 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200499 0,
500 },
501};
502
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200503FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
504FN_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 +0200505#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200506
Paul Bakker47fce022013-06-28 17:34:34 +0200507#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200508/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200509 * For digestAlgorithm
510 */
511typedef struct {
512 oid_descriptor_t descriptor;
513 md_type_t md_alg;
514} oid_md_alg_t;
515
516static const oid_md_alg_t oid_md_alg[] =
517{
518 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200519 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200520 POLARSSL_MD_MD2,
521 },
522 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200523 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200524 POLARSSL_MD_MD4,
525 },
526 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200527 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200528 POLARSSL_MD_MD5,
529 },
530 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200531 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200532 POLARSSL_MD_SHA1,
533 },
534 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200535 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200536 POLARSSL_MD_SHA224,
537 },
538 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200539 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200540 POLARSSL_MD_SHA256,
541 },
542 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200543 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200544 POLARSSL_MD_SHA384,
545 },
546 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200547 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200548 POLARSSL_MD_SHA512,
549 },
550 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200551 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200552 0,
553 },
554};
555
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200556FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
557FN_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 +0200558FN_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 +0200559#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200560
Paul Bakker47fce022013-06-28 17:34:34 +0200561#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200562/*
563 * For PKCS#12 PBEs
564 */
565typedef struct {
566 oid_descriptor_t descriptor;
567 md_type_t md_alg;
568 cipher_type_t cipher_alg;
569} oid_pkcs12_pbe_alg_t;
570
571static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
572{
573 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200574 { 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 +0200575 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
576 },
577 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200578 { 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 +0200579 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
580 },
581 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200582 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200583 0, 0,
584 },
585};
586
587FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
588FN_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 +0200589#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200590
Paul Bakker6edcd412013-10-29 15:22:54 +0100591#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
592 !defined(EFI32)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200593#include <stdarg.h>
594
595#if !defined vsnprintf
596#define vsnprintf _vsnprintf
597#endif // vsnprintf
598
599/*
600 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
601 * Result value is not size of buffer needed, but -1 if no fit is possible.
602 *
603 * This fuction tries to 'fix' this by at least suggesting enlarging the
604 * size by 20.
605 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200606static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200607{
608 va_list ap;
609 int res = -1;
610
611 va_start( ap, format );
612
613 res = vsnprintf( str, size, format, ap );
614
615 va_end( ap );
616
617 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200618 if( res < 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200619 return( (int) size + 20 );
620
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200621 return( res );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200622}
623
624#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200625#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200626
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200627#define SAFE_SNPRINTF() \
628{ \
629 if( ret == -1 ) \
630 return( POLARSSL_ERR_OID_BUF_TOO_SMALL ); \
631 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200632 if( (unsigned int) ret >= n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633 p[n - 1] = '\0'; \
634 return( POLARSSL_ERR_OID_BUF_TOO_SMALL ); \
635 } \
636 \
637 n -= (unsigned int) ret; \
638 p += (unsigned int) ret; \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200639}
640
641/* Return the x.y.z.... style numeric string for the given OID */
642int oid_get_numeric_string( char *buf, size_t size,
643 const asn1_buf *oid )
644{
645 int ret;
646 size_t i, n;
647 unsigned int value;
648 char *p;
649
650 p = buf;
651 n = size;
652
653 /* First byte contains first two dots */
654 if( oid->len > 0 )
655 {
656 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
657 SAFE_SNPRINTF();
658 }
659
Paul Bakkerc70b9822013-04-07 22:00:46 +0200660 value = 0;
661 for( i = 1; i < oid->len; i++ )
662 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200663 /* Prevent overflow in value. */
Paul Bakker66d5d072014-06-17 16:39:18 +0200664 if( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100665 return( POLARSSL_ERR_OID_BUF_TOO_SMALL );
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200666
Paul Bakkerc70b9822013-04-07 22:00:46 +0200667 value <<= 7;
668 value += oid->p[i] & 0x7F;
669
670 if( !( oid->p[i] & 0x80 ) )
671 {
672 /* Last byte */
673 ret = snprintf( p, n, ".%d", value );
674 SAFE_SNPRINTF();
675 value = 0;
676 }
677 }
678
679 return( (int) ( size - n ) );
680}
681
Paul Bakkerc70b9822013-04-07 22:00:46 +0200682#endif /* POLARSSL_OID_C */