blob: ad45a3d45d7787064ec06537675c21c1160344b3 [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é-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.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
Rich Evans8f3a9432015-01-30 10:54:04 +000039#if defined(POLARSSL_PLATFORM_C)
40#include "polarssl/platform.h"
41#else
42#define polarssl_snprintf snprintf
43#endif
44
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker2292d1f2013-09-15 17:06:49 +020046#include "polarssl/x509.h"
47#endif
48
Paul Bakkerdd1150e2013-06-28 17:20:22 +020049/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020050 * Macro to automatically add the size of #define'd OIDs
51 */
52#define ADD_LEN(s) s, OID_SIZE(s)
53
54/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020055 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
56 * the other functions)
57 */
Paul Bakker45a2c8d2013-10-28 12:57:08 +010058#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
59static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
60{ \
61 const TYPE_T *p = LIST; \
62 const oid_descriptor_t *cur = (const oid_descriptor_t *) p; \
63 if( p == NULL || oid == NULL ) return( NULL ); \
64 while( cur->asn1 != NULL ) { \
65 if( cur->asn1_len == oid->len && \
66 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
67 return( p ); \
68 } \
69 p++; \
70 cur = (const oid_descriptor_t *) p; \
71 } \
72 return( NULL ); \
73}
Paul Bakkerbd51ad52013-06-28 16:51:52 +020074
75/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020076 * Macro to generate a function for retrieving a single attribute from the
77 * descriptor of an oid_descriptor_t wrapper.
78 */
79#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
80int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
81{ \
82 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020083 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020084 *ATTR1 = data->descriptor.ATTR1; \
85 return( 0 ); \
86}
87
88/*
89 * Macro to generate a function for retrieving a single attribute from an
90 * oid_descriptor_t wrapper.
91 */
92#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
93int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
94{ \
95 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +020096 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +020097 *ATTR1 = data->ATTR1; \
98 return( 0 ); \
99}
100
101/*
102 * Macro to generate a function for retrieving two attributes from an
103 * oid_descriptor_t wrapper.
104 */
105#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
106 ATTR2_TYPE, ATTR2) \
107int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
108{ \
109 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
Paul Bakker66d5d072014-06-17 16:39:18 +0200110 if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND ); \
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200111 *ATTR1 = data->ATTR1; \
112 *ATTR2 = data->ATTR2; \
113 return( 0 ); \
114}
115
116/*
Paul Bakkerce6ae232013-06-28 18:05:35 +0200117 * Macro to generate a function for retrieving the OID based on a single
118 * attribute from a oid_descriptor_t wrapper.
119 */
120#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200121int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200122{ \
123 const TYPE_T *cur = LIST; \
124 while( cur->descriptor.asn1 != NULL ) { \
125 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200126 *oid = cur->descriptor.asn1; \
127 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200128 return( 0 ); \
129 } \
130 cur++; \
131 } \
132 return( POLARSSL_ERR_OID_NOT_FOUND ); \
133}
134
135/*
136 * Macro to generate a function for retrieving the OID based on two
137 * attributes from a oid_descriptor_t wrapper.
138 */
139#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
140 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200141int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
142 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200143{ \
144 const TYPE_T *cur = LIST; \
145 while( cur->descriptor.asn1 != NULL ) { \
146 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200147 *oid = cur->descriptor.asn1; \
148 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200149 return( 0 ); \
150 } \
151 cur++; \
152 } \
153 return( POLARSSL_ERR_OID_NOT_FOUND ); \
154}
155
156/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200157 * For X520 attribute types
158 */
159typedef struct {
160 oid_descriptor_t descriptor;
161 const char *short_name;
162} oid_x520_attr_t;
163
164static const oid_x520_attr_t oid_x520_attr_type[] =
165{
166 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200167 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200168 "CN",
169 },
170 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200171 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200172 "C",
173 },
174 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200175 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200176 "L",
177 },
178 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200179 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200180 "ST",
181 },
182 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200183 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200184 "O",
185 },
186 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200187 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200188 "OU",
189 },
190 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200191 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200192 "emailAddress",
193 },
194 {
Paul Bakker7b0be682013-10-29 14:24:37 +0100195 { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
196 "serialNumber",
197 },
198 {
199 { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
200 "postalAddress",
201 },
202 {
203 { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
204 "postalCode",
205 },
206 {
Paul Bakker63844402014-04-30 15:34:12 +0200207 { ADD_LEN( OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
208 "SN",
209 },
210 {
211 { ADD_LEN( OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
212 "GN",
213 },
214 {
215 { ADD_LEN( OID_AT_INITIALS ), "id-at-initials", "Initials" },
216 "initials",
217 },
218 {
219 { ADD_LEN( OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
220 "generationQualifier",
221 },
222 {
223 { ADD_LEN( OID_AT_TITLE ), "id-at-title", "Title" },
224 "title",
225 },
226 {
227 { ADD_LEN( OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
228 "dnQualifier",
229 },
230 {
231 { ADD_LEN( OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
232 "pseudonym",
233 },
234 {
235 { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
236 "DC",
237 },
238 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200239 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200240 NULL,
241 }
242};
243
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200244FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
245FN_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 +0200246
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200248/*
249 * For X509 extensions
250 */
251typedef struct {
252 oid_descriptor_t descriptor;
253 int ext_type;
254} oid_x509_ext_t;
255
256static const oid_x509_ext_t oid_x509_ext[] =
257{
258 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200259 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200260 EXT_BASIC_CONSTRAINTS,
261 },
262 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200263 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200264 EXT_KEY_USAGE,
265 },
266 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200267 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200268 EXT_EXTENDED_KEY_USAGE,
269 },
270 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200271 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200272 EXT_SUBJECT_ALT_NAME,
273 },
274 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200275 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200276 EXT_NS_CERT_TYPE,
277 },
278 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200279 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200280 0,
281 },
282};
283
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200284FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
285FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200286
Paul Bakkerc70b9822013-04-07 22:00:46 +0200287static const oid_descriptor_t oid_ext_key_usage[] =
288{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200289 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
290 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
291 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
292 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
293 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
294 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
295 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200296};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200297
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200298FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
299FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200301
Paul Bakker47fce022013-06-28 17:34:34 +0200302#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200303/*
304 * For SignatureAlgorithmIdentifier
305 */
306typedef struct {
307 oid_descriptor_t descriptor;
308 md_type_t md_alg;
309 pk_type_t pk_alg;
310} oid_sig_alg_t;
311
312static const oid_sig_alg_t oid_sig_alg[] =
313{
314 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200315 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200316 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
317 },
318 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200319 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200320 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
321 },
322 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200323 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200324 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
325 },
326 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200327 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200328 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
329 },
330 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200331 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200332 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
333 },
334 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200335 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200336 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
337 },
338 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200339 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200340 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
341 },
342 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200343 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200344 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
345 },
346 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200347 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200348 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
349 },
350 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200351 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200352 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
353 },
354 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200355 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200356 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
357 },
358 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200359 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200360 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
361 },
362 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200363 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200364 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
365 },
366 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200367 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200368 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
369 },
370 {
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100371 { ADD_LEN( OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" },
372 POLARSSL_MD_NONE, POLARSSL_PK_RSASSA_PSS,
373 },
374 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200375 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100376 POLARSSL_MD_NONE, POLARSSL_PK_NONE,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200377 },
378};
379
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200380FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
381FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
382FN_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 +0200383FN_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 +0200384#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200385
Paul Bakkerc70b9822013-04-07 22:00:46 +0200386/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200387 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200388 */
389typedef struct {
390 oid_descriptor_t descriptor;
391 pk_type_t pk_alg;
392} oid_pk_alg_t;
393
394static const oid_pk_alg_t oid_pk_alg[] =
395{
396 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200397 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200398 POLARSSL_PK_RSA,
399 },
400 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200401 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200402 POLARSSL_PK_ECKEY,
403 },
404 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200405 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200406 POLARSSL_PK_ECKEY_DH,
407 },
408 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200409 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100410 POLARSSL_PK_NONE,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200411 },
412};
413
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200414FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
415FN_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 +0200416FN_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 +0200417
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200418#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200419/*
420 * For namedCurve (RFC 5480)
421 */
422typedef struct {
423 oid_descriptor_t descriptor;
424 ecp_group_id grp_id;
425} oid_ecp_grp_t;
426
427static const oid_ecp_grp_t oid_ecp_grp[] =
428{
429 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200430 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200431 POLARSSL_ECP_DP_SECP192R1,
432 },
433 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200434 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200435 POLARSSL_ECP_DP_SECP224R1,
436 },
437 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200438 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200439 POLARSSL_ECP_DP_SECP256R1,
440 },
441 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200442 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200443 POLARSSL_ECP_DP_SECP384R1,
444 },
445 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200446 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200447 POLARSSL_ECP_DP_SECP521R1,
448 },
449 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100450 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
451 POLARSSL_ECP_DP_SECP192K1,
452 },
453 {
454 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
455 POLARSSL_ECP_DP_SECP224K1,
456 },
457 {
458 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
459 POLARSSL_ECP_DP_SECP256K1,
460 },
461 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200462 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
463 POLARSSL_ECP_DP_BP256R1,
464 },
465 {
466 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
467 POLARSSL_ECP_DP_BP384R1,
468 },
469 {
470 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
471 POLARSSL_ECP_DP_BP512R1,
472 },
473 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200474 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100475 POLARSSL_ECP_DP_NONE,
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200476 },
477};
478
479FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
480FN_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 +0200481FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
482#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200483
Paul Bakker47fce022013-06-28 17:34:34 +0200484#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200485/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200486 * For PKCS#5 PBES2 encryption algorithm
487 */
488typedef struct {
489 oid_descriptor_t descriptor;
490 cipher_type_t cipher_alg;
491} oid_cipher_alg_t;
492
493static const oid_cipher_alg_t oid_cipher_alg[] =
494{
495 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200496 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200497 POLARSSL_CIPHER_DES_CBC,
498 },
499 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200500 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200501 POLARSSL_CIPHER_DES_EDE3_CBC,
502 },
503 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200504 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100505 POLARSSL_CIPHER_NONE,
Paul Bakker9b5e8852013-06-28 16:12:50 +0200506 },
507};
508
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200509FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
510FN_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 +0200511#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200512
Paul Bakker47fce022013-06-28 17:34:34 +0200513#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200514/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200515 * For digestAlgorithm
516 */
517typedef struct {
518 oid_descriptor_t descriptor;
519 md_type_t md_alg;
520} oid_md_alg_t;
521
522static const oid_md_alg_t oid_md_alg[] =
523{
524 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200525 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200526 POLARSSL_MD_MD2,
527 },
528 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200529 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200530 POLARSSL_MD_MD4,
531 },
532 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200533 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200534 POLARSSL_MD_MD5,
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 },
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100558 POLARSSL_MD_NONE,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200559 },
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 },
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100589 POLARSSL_MD_NONE, POLARSSL_CIPHER_NONE,
Paul Bakker7749a222013-06-28 17:28:20 +0200590 },
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 {
Rich Evans8f3a9432015-01-30 10:54:04 +0000662 ret = polarssl_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200663 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 */
Rich Evans8f3a9432015-01-30 10:54:04 +0000679 ret = polarssl_snprintf( p, n, ".%d", value );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200680 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 */