blob: 2a61193c4494888a813d2dfc503cbdf8e2d5003a [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
28#include "polarssl/config.h"
29
30#if defined(POLARSSL_OID_C)
31
32#include "polarssl/oid.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "polarssl/rsa.h"
34
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker2292d1f2013-09-15 17:06:49 +020036#include "polarssl/x509.h"
37#endif
38
Paul Bakkered27a042013-04-18 22:46:23 +020039#include <stdio.h>
40
Paul Bakkerdd1150e2013-06-28 17:20:22 +020041/*
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +020042 * Macro to automatically add the size of #define'd OIDs
43 */
44#define ADD_LEN(s) s, OID_SIZE(s)
45
46/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020047 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
48 * the other functions)
49 */
Paul Bakker45a2c8d2013-10-28 12:57:08 +010050#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
51static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
52{ \
53 const TYPE_T *p = LIST; \
54 const oid_descriptor_t *cur = (const oid_descriptor_t *) p; \
55 if( p == NULL || oid == NULL ) return( NULL ); \
56 while( cur->asn1 != NULL ) { \
57 if( cur->asn1_len == oid->len && \
58 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
59 return( p ); \
60 } \
61 p++; \
62 cur = (const oid_descriptor_t *) p; \
63 } \
64 return( NULL ); \
65}
Paul Bakkerbd51ad52013-06-28 16:51:52 +020066
67/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020068 * Macro to generate a function for retrieving a single attribute from the
69 * descriptor of an oid_descriptor_t wrapper.
70 */
71#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
72int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
73{ \
74 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
75 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
76 *ATTR1 = data->descriptor.ATTR1; \
77 return( 0 ); \
78}
79
80/*
81 * Macro to generate a function for retrieving a single attribute from an
82 * oid_descriptor_t wrapper.
83 */
84#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
85int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
86{ \
87 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
88 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
89 *ATTR1 = data->ATTR1; \
90 return( 0 ); \
91}
92
93/*
94 * Macro to generate a function for retrieving two attributes from an
95 * oid_descriptor_t wrapper.
96 */
97#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
98 ATTR2_TYPE, ATTR2) \
99int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
100{ \
101 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
102 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
103 *ATTR1 = data->ATTR1; \
104 *ATTR2 = data->ATTR2; \
105 return( 0 ); \
106}
107
108/*
Paul Bakkerce6ae232013-06-28 18:05:35 +0200109 * Macro to generate a function for retrieving the OID based on a single
110 * attribute from a oid_descriptor_t wrapper.
111 */
112#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200113int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200114{ \
115 const TYPE_T *cur = LIST; \
116 while( cur->descriptor.asn1 != NULL ) { \
117 if( cur->ATTR1 == ATTR1 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200118 *oid = cur->descriptor.asn1; \
119 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200120 return( 0 ); \
121 } \
122 cur++; \
123 } \
124 return( POLARSSL_ERR_OID_NOT_FOUND ); \
125}
126
127/*
128 * Macro to generate a function for retrieving the OID based on two
129 * attributes from a oid_descriptor_t wrapper.
130 */
131#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
132 ATTR2_TYPE, ATTR2) \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200133int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
134 size_t *olen ) \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200135{ \
136 const TYPE_T *cur = LIST; \
137 while( cur->descriptor.asn1 != NULL ) { \
138 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
Paul Bakker1c3853b2013-09-10 11:43:44 +0200139 *oid = cur->descriptor.asn1; \
140 *olen = cur->descriptor.asn1_len; \
Paul Bakkerce6ae232013-06-28 18:05:35 +0200141 return( 0 ); \
142 } \
143 cur++; \
144 } \
145 return( POLARSSL_ERR_OID_NOT_FOUND ); \
146}
147
148/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200149 * For X520 attribute types
150 */
151typedef struct {
152 oid_descriptor_t descriptor;
153 const char *short_name;
154} oid_x520_attr_t;
155
156static const oid_x520_attr_t oid_x520_attr_type[] =
157{
158 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200159 { ADD_LEN( OID_AT_CN ), "id-at-commonName", "Common Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200160 "CN",
161 },
162 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200163 { ADD_LEN( OID_AT_COUNTRY ), "id-at-countryName", "Country" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200164 "C",
165 },
166 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200167 { ADD_LEN( OID_AT_LOCALITY ), "id-at-locality", "Locality" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200168 "L",
169 },
170 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200171 { ADD_LEN( OID_AT_STATE ), "id-at-state", "State" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200172 "ST",
173 },
174 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200175 { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200176 "O",
177 },
178 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200179 { ADD_LEN( OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200180 "OU",
181 },
182 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200183 { ADD_LEN( OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200184 "emailAddress",
185 },
186 {
Paul Bakker7b0be682013-10-29 14:24:37 +0100187 { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
188 "serialNumber",
189 },
190 {
191 { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
192 "postalAddress",
193 },
194 {
195 { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
196 "postalCode",
197 },
198 {
Paul Bakker63844402014-04-30 15:34:12 +0200199 { ADD_LEN( OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
200 "SN",
201 },
202 {
203 { ADD_LEN( OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
204 "GN",
205 },
206 {
207 { ADD_LEN( OID_AT_INITIALS ), "id-at-initials", "Initials" },
208 "initials",
209 },
210 {
211 { ADD_LEN( OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
212 "generationQualifier",
213 },
214 {
215 { ADD_LEN( OID_AT_TITLE ), "id-at-title", "Title" },
216 "title",
217 },
218 {
219 { ADD_LEN( OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
220 "dnQualifier",
221 },
222 {
223 { ADD_LEN( OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
224 "pseudonym",
225 },
226 {
227 { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
228 "DC",
229 },
230 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200231 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200232 NULL,
233 }
234};
235
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200236FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
237FN_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 +0200238
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200240/*
241 * For X509 extensions
242 */
243typedef struct {
244 oid_descriptor_t descriptor;
245 int ext_type;
246} oid_x509_ext_t;
247
248static const oid_x509_ext_t oid_x509_ext[] =
249{
250 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200251 { ADD_LEN( OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200252 EXT_BASIC_CONSTRAINTS,
253 },
254 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200255 { ADD_LEN( OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200256 EXT_KEY_USAGE,
257 },
258 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200259 { ADD_LEN( OID_EXTENDED_KEY_USAGE ), "id-ce-keyUsage", "Extended Key Usage" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200260 EXT_EXTENDED_KEY_USAGE,
261 },
262 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200263 { ADD_LEN( OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200264 EXT_SUBJECT_ALT_NAME,
265 },
266 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200267 { ADD_LEN( OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200268 EXT_NS_CERT_TYPE,
269 },
270 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200271 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200272 0,
273 },
274};
275
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200276FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
277FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200278
Paul Bakkerc70b9822013-04-07 22:00:46 +0200279static const oid_descriptor_t oid_ext_key_usage[] =
280{
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200281 { ADD_LEN( OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
282 { ADD_LEN( OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
283 { ADD_LEN( OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
284 { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
285 { ADD_LEN( OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
286 { ADD_LEN( OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
287 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200288};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200289
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200290FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
291FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200293
Paul Bakker47fce022013-06-28 17:34:34 +0200294#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200295/*
296 * For SignatureAlgorithmIdentifier
297 */
298typedef struct {
299 oid_descriptor_t descriptor;
300 md_type_t md_alg;
301 pk_type_t pk_alg;
302} oid_sig_alg_t;
303
304static const oid_sig_alg_t oid_sig_alg[] =
305{
306 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200307 { ADD_LEN( OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200308 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
309 },
310 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200311 { ADD_LEN( OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200312 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
313 },
314 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200315 { ADD_LEN( OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200316 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
317 },
318 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200319 { ADD_LEN( OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200320 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
321 },
322 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200323 { ADD_LEN( OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200324 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
325 },
326 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200327 { ADD_LEN( OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200328 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
329 },
330 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200331 { ADD_LEN( OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200332 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
333 },
334 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200335 { ADD_LEN( OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200336 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
337 },
338 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200339 { ADD_LEN( OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200340 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
341 },
342 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200343 { ADD_LEN( OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200344 POLARSSL_MD_SHA1, POLARSSL_PK_ECDSA,
345 },
346 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200347 { ADD_LEN( OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200348 POLARSSL_MD_SHA224, POLARSSL_PK_ECDSA,
349 },
350 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200351 { ADD_LEN( OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200352 POLARSSL_MD_SHA256, POLARSSL_PK_ECDSA,
353 },
354 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200355 { ADD_LEN( OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200356 POLARSSL_MD_SHA384, POLARSSL_PK_ECDSA,
357 },
358 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200359 { ADD_LEN( OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +0200360 POLARSSL_MD_SHA512, POLARSSL_PK_ECDSA,
361 },
362 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200363 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200364 0, 0,
365 },
366};
367
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200368FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
369FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
370FN_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 +0200371FN_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 +0200372#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200373
Paul Bakkerc70b9822013-04-07 22:00:46 +0200374/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200375 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200376 */
377typedef struct {
378 oid_descriptor_t descriptor;
379 pk_type_t pk_alg;
380} oid_pk_alg_t;
381
382static const oid_pk_alg_t oid_pk_alg[] =
383{
384 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200385 { ADD_LEN( OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200386 POLARSSL_PK_RSA,
387 },
388 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200389 { ADD_LEN( OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200390 POLARSSL_PK_ECKEY,
391 },
392 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200393 { ADD_LEN( OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200394 POLARSSL_PK_ECKEY_DH,
395 },
396 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200397 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200398 0,
399 },
400};
401
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200402FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
403FN_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 +0200404FN_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 +0200405
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200406#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200407/*
408 * For namedCurve (RFC 5480)
409 */
410typedef struct {
411 oid_descriptor_t descriptor;
412 ecp_group_id grp_id;
413} oid_ecp_grp_t;
414
415static const oid_ecp_grp_t oid_ecp_grp[] =
416{
417 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200418 { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200419 POLARSSL_ECP_DP_SECP192R1,
420 },
421 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200422 { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200423 POLARSSL_ECP_DP_SECP224R1,
424 },
425 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200426 { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200427 POLARSSL_ECP_DP_SECP256R1,
428 },
429 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200430 { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200431 POLARSSL_ECP_DP_SECP384R1,
432 },
433 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200434 { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200435 POLARSSL_ECP_DP_SECP521R1,
436 },
437 {
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100438 { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
439 POLARSSL_ECP_DP_SECP192K1,
440 },
441 {
442 { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
443 POLARSSL_ECP_DP_SECP224K1,
444 },
445 {
446 { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
447 POLARSSL_ECP_DP_SECP256K1,
448 },
449 {
Manuel Pégourié-Gonnard48ac3db2013-10-10 15:11:33 +0200450 { ADD_LEN( OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
451 POLARSSL_ECP_DP_BP256R1,
452 },
453 {
454 { ADD_LEN( OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
455 POLARSSL_ECP_DP_BP384R1,
456 },
457 {
458 { ADD_LEN( OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
459 POLARSSL_ECP_DP_BP512R1,
460 },
461 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200462 { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200463 0,
464 },
465};
466
467FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
468FN_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 +0200469FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
470#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200471
Paul Bakker47fce022013-06-28 17:34:34 +0200472#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200473/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200474 * For PKCS#5 PBES2 encryption algorithm
475 */
476typedef struct {
477 oid_descriptor_t descriptor;
478 cipher_type_t cipher_alg;
479} oid_cipher_alg_t;
480
481static const oid_cipher_alg_t oid_cipher_alg[] =
482{
483 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200484 { ADD_LEN( OID_DES_CBC ), "desCBC", "DES-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200485 POLARSSL_CIPHER_DES_CBC,
486 },
487 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200488 { ADD_LEN( OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200489 POLARSSL_CIPHER_DES_EDE3_CBC,
490 },
491 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200492 { NULL, 0, NULL, NULL },
Paul Bakker9b5e8852013-06-28 16:12:50 +0200493 0,
494 },
495};
496
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200497FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
498FN_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 +0200499#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200500
Paul Bakker47fce022013-06-28 17:34:34 +0200501#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200502/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200503 * For digestAlgorithm
504 */
505typedef struct {
506 oid_descriptor_t descriptor;
507 md_type_t md_alg;
508} oid_md_alg_t;
509
510static const oid_md_alg_t oid_md_alg[] =
511{
512 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200513 { ADD_LEN( OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200514 POLARSSL_MD_MD2,
515 },
516 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200517 { ADD_LEN( OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200518 POLARSSL_MD_MD4,
519 },
520 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200521 { ADD_LEN( OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200522 POLARSSL_MD_MD5,
523 },
524 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200525 { ADD_LEN( OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200526 POLARSSL_MD_SHA1,
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_SHA224 ), "id-sha224", "SHA-224" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200534 POLARSSL_MD_SHA224,
535 },
536 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200537 { ADD_LEN( OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200538 POLARSSL_MD_SHA256,
539 },
540 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200541 { ADD_LEN( OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200542 POLARSSL_MD_SHA384,
543 },
544 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200545 { ADD_LEN( OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200546 POLARSSL_MD_SHA512,
547 },
548 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200549 { NULL, 0, NULL, NULL },
Paul Bakkerc70b9822013-04-07 22:00:46 +0200550 0,
551 },
552};
553
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200554FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
555FN_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 +0200556FN_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 +0200557#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200558
Paul Bakker47fce022013-06-28 17:34:34 +0200559#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200560/*
561 * For PKCS#12 PBEs
562 */
563typedef struct {
564 oid_descriptor_t descriptor;
565 md_type_t md_alg;
566 cipher_type_t cipher_alg;
567} oid_pkcs12_pbe_alg_t;
568
569static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
570{
571 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200572 { 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 +0200573 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
574 },
575 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200576 { 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 +0200577 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
578 },
579 {
Manuel Pégourié-Gonnard298aae42013-08-15 14:22:17 +0200580 { NULL, 0, NULL, NULL },
Paul Bakker7749a222013-06-28 17:28:20 +0200581 0, 0,
582 },
583};
584
585FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
586FN_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 +0200587#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200588
Paul Bakker6edcd412013-10-29 15:22:54 +0100589#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
590 !defined(EFI32)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200591#include <stdarg.h>
592
593#if !defined vsnprintf
594#define vsnprintf _vsnprintf
595#endif // vsnprintf
596
597/*
598 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
599 * Result value is not size of buffer needed, but -1 if no fit is possible.
600 *
601 * This fuction tries to 'fix' this by at least suggesting enlarging the
602 * size by 20.
603 */
604static int compat_snprintf(char *str, size_t size, const char *format, ...)
605{
606 va_list ap;
607 int res = -1;
608
609 va_start( ap, format );
610
611 res = vsnprintf( str, size, format, ap );
612
613 va_end( ap );
614
615 // No quick fix possible
616 if ( res < 0 )
617 return( (int) size + 20 );
618
619 return res;
620}
621
622#define snprintf compat_snprintf
623#endif
624
Paul Bakkerc70b9822013-04-07 22:00:46 +0200625#define SAFE_SNPRINTF() \
626{ \
627 if( ret == -1 ) \
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100628 return POLARSSL_ERR_OID_BUF_TOO_SMALL; \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200629 \
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100630 if ( (unsigned int) ret >= n ) { \
Paul Bakkerc70b9822013-04-07 22:00:46 +0200631 p[n - 1] = '\0'; \
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 } \
634 \
635 n -= (unsigned int) ret; \
636 p += (unsigned int) ret; \
637}
638
639/* Return the x.y.z.... style numeric string for the given OID */
640int oid_get_numeric_string( char *buf, size_t size,
641 const asn1_buf *oid )
642{
643 int ret;
644 size_t i, n;
645 unsigned int value;
646 char *p;
647
648 p = buf;
649 n = size;
650
651 /* First byte contains first two dots */
652 if( oid->len > 0 )
653 {
654 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
655 SAFE_SNPRINTF();
656 }
657
Paul Bakkerc70b9822013-04-07 22:00:46 +0200658 value = 0;
659 for( i = 1; i < oid->len; i++ )
660 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200661 /* Prevent overflow in value. */
Manuel Pégourié-Gonnard14d85642013-07-15 11:01:14 +0200662 if ( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100663 return( POLARSSL_ERR_OID_BUF_TOO_SMALL );
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200664
Paul Bakkerc70b9822013-04-07 22:00:46 +0200665 value <<= 7;
666 value += oid->p[i] & 0x7F;
667
668 if( !( oid->p[i] & 0x80 ) )
669 {
670 /* Last byte */
671 ret = snprintf( p, n, ".%d", value );
672 SAFE_SNPRINTF();
673 value = 0;
674 }
675 }
676
677 return( (int) ( size - n ) );
678}
679
Paul Bakkerc70b9822013-04-07 22:00:46 +0200680#endif /* POLARSSL_OID_C */