blob: 9b9df43ba084e91bb5832e90a8312c6ed7a94e01 [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é-Gonnard085ab042015-01-23 11:06:27 +00008 * This file is part of mbed TLS (https://www.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
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker2292d1f2013-09-15 17:06:49 +020037#include "polarssl/x509.h"
38#endif
39
Paul Bakkered27a042013-04-18 22:46:23 +020040#include <stdio.h>
41
Paul Bakkerdd1150e2013-06-28 17:20:22 +020042/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020043 * Macro to automatically add the size of #define'd OIDs
44 */
45#define ADD_LEN(s) s, OID_SIZE(s)
46
47/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020048 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
49 * the other functions)
50 */
Paul Bakker45a2c8d2013-10-28 12:57:08 +010051#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
52static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
53{ \
54 const TYPE_T *p = LIST; \
55 const oid_descriptor_t *cur = (const oid_descriptor_t *) p; \
56 if( p == NULL || oid == NULL ) return( NULL ); \
57 while( cur->asn1 != NULL ) { \
58 if( cur->asn1_len == oid->len && \
59 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
60 return( p ); \
61 } \
62 p++; \
63 cur = (const oid_descriptor_t *) p; \
64 } \
65 return( NULL ); \
66}
Paul Bakkerbd51ad52013-06-28 16:51:52 +020067
68/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020069 * Macro to generate a function for retrieving a single attribute from the
70 * descriptor of an oid_descriptor_t wrapper.
71 */
72#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
73int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
74{ \
75 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020076 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020077 *ATTR1 = data->descriptor.ATTR1; \
78 return( 0 ); \
79}
80
81/*
82 * Macro to generate a function for retrieving a single attribute from an
83 * oid_descriptor_t wrapper.
84 */
85#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
86int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
87{ \
88 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020089 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020090 *ATTR1 = data->ATTR1; \
91 return( 0 ); \
92}
93
94/*
95 * Macro to generate a function for retrieving two attributes from an
96 * oid_descriptor_t wrapper.
97 */
98#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
99 ATTR2_TYPE, ATTR2) \
100int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
101{ \
102 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +0200103 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200104 *ATTR1 = data->ATTR1; \
105 *ATTR2 = data->ATTR2; \
106 return( 0 ); \
107}
108
109/*
Paul Bakkerce6ae232013-06-28 18:05:35 +0200110 * Macro to generate a function for retrieving the OID based on a single
111 * attribute from a oid_descriptor_t wrapper.
112 */
113#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200114int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200115{ \
116 const TYPE_T *cur = LIST; \
117 while( cur->descriptor.asn1 != NULL ) { \
118 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200119 *oid = cur->descriptor.asn1; \
120 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200121 return( 0 ); \
122 } \
123 cur++; \
124 } \
125 return( POLARSSL_ERR_OID_NOT_FOUND ); \
126}
127
128/*
129 * Macro to generate a function for retrieving the OID based on two
130 * attributes from a oid_descriptor_t wrapper.
131 */
132#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
133 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200134int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
135 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200136{ \
137 const TYPE_T *cur = LIST; \
138 while( cur->descriptor.asn1 != NULL ) { \
139 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200140 *oid = cur->descriptor.asn1; \
141 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200142 return( 0 ); \
143 } \
144 cur++; \
145 } \
146 return( POLARSSL_ERR_OID_NOT_FOUND ); \
147}
148
149/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200150 * For X520 attribute types
151 */
152typedef struct {
153 oid_descriptor_t descriptor;
154 const char *short_name;
155} oid_x520_attr_t;
156
157static const oid_x520_attr_t oid_x520_attr_type[] =
158{
159 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200160 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200161 "CN",
162 },
163 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200164 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200165 "C",
166 },
167 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200168 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200169 "L",
170 },
171 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200172 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200173 "ST",
174 },
175 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200176 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200177 "O",
178 },
179 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200180 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200181 "OU",
182 },
183 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200184 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200185 "emailAddress",
186 },
187 {
Paul Bakker7b0be682013-10-29 14:24:37 +0100188 { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
189 "serialNumber",
190 },
191 {
192 { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
193 "postalAddress",
194 },
195 {
196 { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
197 "postalCode",
198 },
199 {
Paul Bakker63844402014-04-30 15:34:12 +0200200 { ADD_LEN( OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
201 "SN",
202 },
203 {
204 { ADD_LEN( OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
205 "GN",
206 },
207 {
208 { ADD_LEN( OID_AT_INITIALS ), "id-at-initials", "Initials" },
209 "initials",
210 },
211 {
212 { ADD_LEN( OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
213 "generationQualifier",
214 },
215 {
216 { ADD_LEN( OID_AT_TITLE ), "id-at-title", "Title" },
217 "title",
218 },
219 {
220 { ADD_LEN( OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
221 "dnQualifier",
222 },
223 {
224 { ADD_LEN( OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
225 "pseudonym",
226 },
227 {
228 { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
229 "DC",
230 },
231 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200232 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200233 NULL,
234 }
235};
236
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200237FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
238FN_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 +0200239
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200241/*
242 * For X509 extensions
243 */
244typedef struct {
245 oid_descriptor_t descriptor;
246 int ext_type;
247} oid_x509_ext_t;
248
249static const oid_x509_ext_t oid_x509_ext[] =
250{
251 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200252 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200253 EXT_BASIC_CONSTRAINTS,
254 },
255 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200256 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200257 EXT_KEY_USAGE,
258 },
259 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200260 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200261 EXT_EXTENDED_KEY_USAGE,
262 },
263 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200264 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200265 EXT_SUBJECT_ALT_NAME,
266 },
267 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200268 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200269 EXT_NS_CERT_TYPE,
270 },
271 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200272 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200273 0,
274 },
275};
276
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200277FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
278FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200279
Paul Bakkerc70b9822013-04-07 22:00:46 +0200280static const oid_descriptor_t oid_ext_key_usage[] =
281{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200282 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
283 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
284 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
285 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
286 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
287 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
288 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200289};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200290
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200291FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
292FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200294
Paul Bakker47fce022013-06-28 17:34:34 +0200295#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200296/*
297 * For SignatureAlgorithmIdentifier
298 */
299typedef struct {
300 oid_descriptor_t descriptor;
301 md_type_t md_alg;
302 pk_type_t pk_alg;
303} oid_sig_alg_t;
304
305static const oid_sig_alg_t oid_sig_alg[] =
306{
307 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200308 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200309 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
310 },
311 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200312 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200313 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
314 },
315 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200316 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200317 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
318 },
319 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200320 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200321 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
322 },
323 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200324 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200325 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
326 },
327 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200328 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200329 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
330 },
331 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200332 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200333 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
334 },
335 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200336 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200337 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
338 },
339 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200340 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200341 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
342 },
343 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200344 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200345 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
346 },
347 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200348 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200349 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
350 },
351 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200352 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200353 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
354 },
355 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200356 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200357 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
358 },
359 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200360 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200361 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
362 },
363 {
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100364 { ADD_LEN( OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" },
365 POLARSSL_MD_NONE, POLARSSL_PK_RSASSA_PSS,
366 },
367 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200368 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200369 0, 0,
370 },
371};
372
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200373FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
374FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
375FN_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 +0200376FN_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 +0200377#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200378
Paul Bakkerc70b9822013-04-07 22:00:46 +0200379/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200380 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200381 */
382typedef struct {
383 oid_descriptor_t descriptor;
384 pk_type_t pk_alg;
385} oid_pk_alg_t;
386
387static const oid_pk_alg_t oid_pk_alg[] =
388{
389 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200390 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200391 POLARSSL_PK_RSA,
392 },
393 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200394 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200395 POLARSSL_PK_ECKEY,
396 },
397 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200398 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200399 POLARSSL_PK_ECKEY_DH,
400 },
401 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200402 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200403 0,
404 },
405};
406
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200407FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
408FN_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 +0200409FN_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 +0200410
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200411#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200412/*
413 * For namedCurve (RFC 5480)
414 */
415typedef struct {
416 oid_descriptor_t descriptor;
417 ecp_group_id grp_id;
418} oid_ecp_grp_t;
419
420static const oid_ecp_grp_t oid_ecp_grp[] =
421{
422 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200423 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200424 POLARSSL_ECP_DP_SECP192R1,
425 },
426 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200427 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200428 POLARSSL_ECP_DP_SECP224R1,
429 },
430 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200431 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200432 POLARSSL_ECP_DP_SECP256R1,
433 },
434 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200435 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200436 POLARSSL_ECP_DP_SECP384R1,
437 },
438 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200439 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200440 POLARSSL_ECP_DP_SECP521R1,
441 },
442 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100443 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
444 POLARSSL_ECP_DP_SECP192K1,
445 },
446 {
447 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
448 POLARSSL_ECP_DP_SECP224K1,
449 },
450 {
451 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
452 POLARSSL_ECP_DP_SECP256K1,
453 },
454 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200455 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
456 POLARSSL_ECP_DP_BP256R1,
457 },
458 {
459 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
460 POLARSSL_ECP_DP_BP384R1,
461 },
462 {
463 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
464 POLARSSL_ECP_DP_BP512R1,
465 },
466 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200467 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200468 0,
469 },
470};
471
472FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
473FN_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 +0200474FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
475#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200476
Paul Bakker47fce022013-06-28 17:34:34 +0200477#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200478/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200479 * For PKCS#5 PBES2 encryption algorithm
480 */
481typedef struct {
482 oid_descriptor_t descriptor;
483 cipher_type_t cipher_alg;
484} oid_cipher_alg_t;
485
486static const oid_cipher_alg_t oid_cipher_alg[] =
487{
488 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200489 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200490 POLARSSL_CIPHER_DES_CBC,
491 },
492 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200493 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200494 POLARSSL_CIPHER_DES_EDE3_CBC,
495 },
496 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200497 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200498 0,
499 },
500};
501
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200502FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
503FN_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 +0200504#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200505
Paul Bakker47fce022013-06-28 17:34:34 +0200506#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200507/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200508 * For digestAlgorithm
509 */
510typedef struct {
511 oid_descriptor_t descriptor;
512 md_type_t md_alg;
513} oid_md_alg_t;
514
515static const oid_md_alg_t oid_md_alg[] =
516{
517 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200518 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200519 POLARSSL_MD_MD2,
520 },
521 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200522 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200523 POLARSSL_MD_MD4,
524 },
525 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200526 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200527 POLARSSL_MD_MD5,
528 },
529 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200530 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200531 POLARSSL_MD_SHA1,
532 },
533 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200534 { ADD_LEN( OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200535 POLARSSL_MD_SHA224,
536 },
537 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200538 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200539 POLARSSL_MD_SHA256,
540 },
541 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200542 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200543 POLARSSL_MD_SHA384,
544 },
545 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200546 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200547 POLARSSL_MD_SHA512,
548 },
549 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200550 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200551 0,
552 },
553};
554
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200555FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
556FN_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 +0200557FN_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 +0200558#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200559
Paul Bakker47fce022013-06-28 17:34:34 +0200560#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200561/*
562 * For PKCS#12 PBEs
563 */
564typedef struct {
565 oid_descriptor_t descriptor;
566 md_type_t md_alg;
567 cipher_type_t cipher_alg;
568} oid_pkcs12_pbe_alg_t;
569
570static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
571{
572 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200573 { 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 +0200574 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
575 },
576 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200577 { 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 +0200578 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
579 },
580 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200581 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200582 0, 0,
583 },
584};
585
586FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
587FN_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 +0200588#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200589
Paul Bakker6edcd412013-10-29 15:22:54 +0100590#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
591 !defined(EFI32)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200592#include <stdarg.h>
593
594#if !defined vsnprintf
595#define vsnprintf _vsnprintf
596#endif // vsnprintf
597
598/*
599 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
600 * Result value is not size of buffer needed, but -1 if no fit is possible.
601 *
602 * This fuction tries to 'fix' this by at least suggesting enlarging the
603 * size by 20.
604 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200605static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200606{
607 va_list ap;
608 int res = -1;
609
610 va_start( ap, format );
611
612 res = vsnprintf( str, size, format, ap );
613
614 va_end( ap );
615
616 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200617 if( res < 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200618 return( (int) size + 20 );
619
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200620 return( res );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200621}
622
623#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200624#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200625
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200626#define SAFE_SNPRINTF() \
627{ \
628 if( ret == -1 ) \
629 return( POLARSSL_ERR_OID_BUF_TOO_SMALL ); \
630 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200631 if( (unsigned int) ret >= n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200632 p[n - 1] = '\0'; \
633 return( POLARSSL_ERR_OID_BUF_TOO_SMALL ); \
634 } \
635 \
636 n -= (unsigned int) ret; \
637 p += (unsigned int) ret; \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200638}
639
640/* Return the x.y.z.... style numeric string for the given OID */
641int oid_get_numeric_string( char *buf, size_t size,
642 const asn1_buf *oid )
643{
644 int ret;
645 size_t i, n;
646 unsigned int value;
647 char *p;
648
649 p = buf;
650 n = size;
651
652 /* First byte contains first two dots */
653 if( oid->len > 0 )
654 {
655 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
656 SAFE_SNPRINTF();
657 }
658
Paul Bakkerc70b9822013-04-07 22:00:46 +0200659 value = 0;
660 for( i = 1; i < oid->len; i++ )
661 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200662 /* Prevent overflow in value. */
Paul Bakker66d5d072014-06-17 16:39:18 +0200663 if( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100664 return( POLARSSL_ERR_OID_BUF_TOO_SMALL );
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200665
Paul Bakkerc70b9822013-04-07 22:00:46 +0200666 value <<= 7;
667 value += oid->p[i] & 0x7F;
668
669 if( !( oid->p[i] & 0x80 ) )
670 {
671 /* Last byte */
672 ret = snprintf( p, n, ".%d", value );
673 SAFE_SNPRINTF();
674 value = 0;
675 }
676 }
677
678 return( (int) ( size - n ) );
679}
680
Paul Bakkerc70b9822013-04-07 22:00:46 +0200681#endif /* POLARSSL_OID_C */