blob: 2b5293596d716008f73403173c42d855d3e9014b [file] [log] [blame]
Paul Bakkerc70b9822013-04-07 22:00:46 +02001/**
2 * \file oid.c
3 *
4 * \brief Object Identifier (OID) database
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27
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 ); \
79 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
80 *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 ); \
92 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
93 *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 ); \
106 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
107 *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é-Gonnard298aae42013-08-15 14:22:17 +0200367 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200368 0, 0,
369 },
370};
371
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200372FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
373FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
374FN_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 +0200375FN_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 +0200376#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200377
Paul Bakkerc70b9822013-04-07 22:00:46 +0200378/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200379 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200380 */
381typedef struct {
382 oid_descriptor_t descriptor;
383 pk_type_t pk_alg;
384} oid_pk_alg_t;
385
386static const oid_pk_alg_t oid_pk_alg[] =
387{
388 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200389 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200390 POLARSSL_PK_RSA,
391 },
392 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200393 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200394 POLARSSL_PK_ECKEY,
395 },
396 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200397 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200398 POLARSSL_PK_ECKEY_DH,
399 },
400 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200401 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200402 0,
403 },
404};
405
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200406FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
407FN_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 +0200408FN_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 +0200409
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200410#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200411/*
412 * For namedCurve (RFC 5480)
413 */
414typedef struct {
415 oid_descriptor_t descriptor;
416 ecp_group_id grp_id;
417} oid_ecp_grp_t;
418
419static const oid_ecp_grp_t oid_ecp_grp[] =
420{
421 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200422 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200423 POLARSSL_ECP_DP_SECP192R1,
424 },
425 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200426 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200427 POLARSSL_ECP_DP_SECP224R1,
428 },
429 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200430 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200431 POLARSSL_ECP_DP_SECP256R1,
432 },
433 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200434 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200435 POLARSSL_ECP_DP_SECP384R1,
436 },
437 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200438 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200439 POLARSSL_ECP_DP_SECP521R1,
440 },
441 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100442 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
443 POLARSSL_ECP_DP_SECP192K1,
444 },
445 {
446 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
447 POLARSSL_ECP_DP_SECP224K1,
448 },
449 {
450 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
451 POLARSSL_ECP_DP_SECP256K1,
452 },
453 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200454 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
455 POLARSSL_ECP_DP_BP256R1,
456 },
457 {
458 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
459 POLARSSL_ECP_DP_BP384R1,
460 },
461 {
462 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
463 POLARSSL_ECP_DP_BP512R1,
464 },
465 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200466 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200467 0,
468 },
469};
470
471FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
472FN_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 +0200473FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
474#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200475
Paul Bakker47fce022013-06-28 17:34:34 +0200476#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200477/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200478 * For PKCS#5 PBES2 encryption algorithm
479 */
480typedef struct {
481 oid_descriptor_t descriptor;
482 cipher_type_t cipher_alg;
483} oid_cipher_alg_t;
484
485static const oid_cipher_alg_t oid_cipher_alg[] =
486{
487 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200488 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200489 POLARSSL_CIPHER_DES_CBC,
490 },
491 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200492 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200493 POLARSSL_CIPHER_DES_EDE3_CBC,
494 },
495 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200496 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200497 0,
498 },
499};
500
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200501FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
502FN_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 +0200503#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200504
Paul Bakker47fce022013-06-28 17:34:34 +0200505#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200506/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200507 * For digestAlgorithm
508 */
509typedef struct {
510 oid_descriptor_t descriptor;
511 md_type_t md_alg;
512} oid_md_alg_t;
513
514static const oid_md_alg_t oid_md_alg[] =
515{
516 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200517 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200518 POLARSSL_MD_MD2,
519 },
520 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200521 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200522 POLARSSL_MD_MD4,
523 },
524 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200525 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200526 POLARSSL_MD_MD5,
527 },
528 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200529 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200530 POLARSSL_MD_SHA1,
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_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200538 POLARSSL_MD_SHA224,
539 },
540 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200541 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200542 POLARSSL_MD_SHA256,
543 },
544 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200545 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200546 POLARSSL_MD_SHA384,
547 },
548 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200549 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200550 POLARSSL_MD_SHA512,
551 },
552 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200553 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200554 0,
555 },
556};
557
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200558FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
559FN_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 +0200560FN_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 +0200561#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200562
Paul Bakker47fce022013-06-28 17:34:34 +0200563#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200564/*
565 * For PKCS#12 PBEs
566 */
567typedef struct {
568 oid_descriptor_t descriptor;
569 md_type_t md_alg;
570 cipher_type_t cipher_alg;
571} oid_pkcs12_pbe_alg_t;
572
573static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
574{
575 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200576 { 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 +0200577 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
578 },
579 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200580 { 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 +0200581 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
582 },
583 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200584 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200585 0, 0,
586 },
587};
588
589FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
590FN_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 +0200591#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200592
Paul Bakker6edcd412013-10-29 15:22:54 +0100593#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
594 !defined(EFI32)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200595#include <stdarg.h>
596
597#if !defined vsnprintf
598#define vsnprintf _vsnprintf
599#endif // vsnprintf
600
601/*
602 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
603 * Result value is not size of buffer needed, but -1 if no fit is possible.
604 *
605 * This fuction tries to 'fix' this by at least suggesting enlarging the
606 * size by 20.
607 */
608static int compat_snprintf(char *str, size_t size, const char *format, ...)
609{
610 va_list ap;
611 int res = -1;
612
613 va_start( ap, format );
614
615 res = vsnprintf( str, size, format, ap );
616
617 va_end( ap );
618
619 // No quick fix possible
620 if ( res < 0 )
621 return( (int) size + 20 );
622
623 return res;
624}
625
626#define snprintf compat_snprintf
627#endif
628
Paul Bakkerc70b9822013-04-07 22:00:46 +0200629#define SAFE_SNPRINTF() \
630{ \
631 if( ret == -1 ) \
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100632 return POLARSSL_ERR_OID_BUF_TOO_SMALL; \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200633 \
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100634 if ( (unsigned int) ret >= n ) { \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200635 p[n - 1] = '\0'; \
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100636 return POLARSSL_ERR_OID_BUF_TOO_SMALL; \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200637 } \
638 \
639 n -= (unsigned int) ret; \
640 p += (unsigned int) ret; \
641}
642
643/* Return the x.y.z.... style numeric string for the given OID */
644int oid_get_numeric_string( char *buf, size_t size,
645 const asn1_buf *oid )
646{
647 int ret;
648 size_t i, n;
649 unsigned int value;
650 char *p;
651
652 p = buf;
653 n = size;
654
655 /* First byte contains first two dots */
656 if( oid->len > 0 )
657 {
658 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
659 SAFE_SNPRINTF();
660 }
661
Paul Bakkerc70b9822013-04-07 22:00:46 +0200662 value = 0;
663 for( i = 1; i < oid->len; i++ )
664 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200665 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200666 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100667 return( POLARSSL_ERR_OID_BUF_TOO_SMALL );
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200668
Paul Bakkerc70b9822013-04-07 22:00:46 +0200669 value <<= 7;
670 value += oid->p[i] & 0x7F;
671
672 if( !( oid->p[i] & 0x80 ) )
673 {
674 /* Last byte */
675 ret = snprintf( p, n, ".%d", value );
676 SAFE_SNPRINTF();
677 value = 0;
678 }
679 }
680
681 return( (int) ( size - n ) );
682}
683
Paul Bakkerc70b9822013-04-07 22:00:46 +0200684#endif /* POLARSSL_OID_C */