blob: dd4cbf0370532a5e4017f20d71ebadf86c9fa275 [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 Bakkered27a042013-04-18 22:46:23 +020035#include <stdio.h>
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +020036#include <limits.h>
Paul Bakkered27a042013-04-18 22:46:23 +020037
Paul Bakkerdd1150e2013-06-28 17:20:22 +020038/*
39 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
40 * the other functions)
41 */
Paul Bakkerbd51ad52013-06-28 16:51:52 +020042#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
43static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
44{ return (const TYPE_T *) oid_descriptor_from_buf(LIST, sizeof(TYPE_T), oid->p, oid->len ); }
45
46/*
Paul Bakkerdd1150e2013-06-28 17:20:22 +020047 * Macro to generate a function for retrieving a single attribute from the
48 * descriptor of an oid_descriptor_t wrapper.
49 */
50#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
51int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
52{ \
53 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
54 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
55 *ATTR1 = data->descriptor.ATTR1; \
56 return( 0 ); \
57}
58
59/*
60 * Macro to generate a function for retrieving a single attribute from an
61 * oid_descriptor_t wrapper.
62 */
63#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
64int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
65{ \
66 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
67 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
68 *ATTR1 = data->ATTR1; \
69 return( 0 ); \
70}
71
72/*
73 * Macro to generate a function for retrieving two attributes from an
74 * oid_descriptor_t wrapper.
75 */
76#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
77 ATTR2_TYPE, ATTR2) \
78int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
79{ \
80 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
81 if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
82 *ATTR1 = data->ATTR1; \
83 *ATTR2 = data->ATTR2; \
84 return( 0 ); \
85}
86
87/*
Paul Bakkerce6ae232013-06-28 18:05:35 +020088 * Macro to generate a function for retrieving the OID based on a single
89 * attribute from a oid_descriptor_t wrapper.
90 */
91#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
92int FN_NAME( ATTR1_TYPE ATTR1, const char **oid_str ) \
93{ \
94 const TYPE_T *cur = LIST; \
95 while( cur->descriptor.asn1 != NULL ) { \
96 if( cur->ATTR1 == ATTR1 ) { \
97 *oid_str = cur->descriptor.asn1; \
98 return( 0 ); \
99 } \
100 cur++; \
101 } \
102 return( POLARSSL_ERR_OID_NOT_FOUND ); \
103}
104
105/*
106 * Macro to generate a function for retrieving the OID based on two
107 * attributes from a oid_descriptor_t wrapper.
108 */
109#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
110 ATTR2_TYPE, ATTR2) \
111int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid_str ) \
112{ \
113 const TYPE_T *cur = LIST; \
114 while( cur->descriptor.asn1 != NULL ) { \
115 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
116 *oid_str = cur->descriptor.asn1; \
117 return( 0 ); \
118 } \
119 cur++; \
120 } \
121 return( POLARSSL_ERR_OID_NOT_FOUND ); \
122}
123
124/*
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200125 * Core generic function
126 */
127static const oid_descriptor_t *oid_descriptor_from_buf( const void *struct_set,
128 size_t struct_size, const unsigned char *oid, size_t len )
129{
130 const unsigned char *p = (const unsigned char *) struct_set;
131 const oid_descriptor_t *cur;
132
133 if( struct_set == NULL || oid == NULL )
134 return( NULL );
135
136 cur = (const oid_descriptor_t *) p;
137 while( cur->asn1 != NULL )
138 {
139 if( strlen( cur->asn1 ) == len &&
140 memcmp( cur->asn1, oid, len ) == 0 )
141 {
142 return( cur );
143 }
144
145 p += struct_size;
146 cur = (const oid_descriptor_t *) p;
147 }
148
149 return( NULL );
150}
151
Paul Bakkerc70b9822013-04-07 22:00:46 +0200152/*
153 * 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 {
163 { OID_AT_CN, "id-at-commonName", "Common Name" },
164 "CN",
165 },
166 {
167 { OID_AT_COUNTRY, "id-at-countryName", "Country" },
168 "C",
169 },
170 {
171 { OID_AT_LOCALITY, "id-at-locality", "Locality" },
172 "L",
173 },
174 {
175 { OID_AT_STATE, "id-at-state", "State" },
176 "ST",
177 },
178 {
179 { OID_AT_ORGANIZATION,"id-at-organizationName", "Organization" },
180 "O",
181 },
182 {
183 { OID_AT_ORG_UNIT, "id-at-organizationalUnitName", "Org Unit" },
184 "OU",
185 },
186 {
187 { OID_PKCS9_EMAIL, "emailAddress", "E-mail address" },
188 "emailAddress",
189 },
190 {
191 { NULL, NULL, NULL },
192 NULL,
193 }
194};
195
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200196FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
197FN_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 +0200198
Paul Bakkered27a042013-04-18 22:46:23 +0200199#if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200200/*
201 * For X509 extensions
202 */
203typedef struct {
204 oid_descriptor_t descriptor;
205 int ext_type;
206} oid_x509_ext_t;
207
208static const oid_x509_ext_t oid_x509_ext[] =
209{
210 {
211 { OID_BASIC_CONSTRAINTS, "id-ce-basicConstraints", "Basic Constraints" },
212 EXT_BASIC_CONSTRAINTS,
213 },
214 {
215 { OID_KEY_USAGE, "id-ce-keyUsage", "Key Usage" },
216 EXT_KEY_USAGE,
217 },
218 {
219 { OID_EXTENDED_KEY_USAGE, "id-ce-keyUsage", "Extended Key Usage" },
220 EXT_EXTENDED_KEY_USAGE,
221 },
222 {
223 { OID_SUBJECT_ALT_NAME, "id-ce-subjectAltName", "Subject Alt Name" },
224 EXT_SUBJECT_ALT_NAME,
225 },
226 {
227 { OID_NS_CERT_TYPE, "id-netscape-certtype", "Netscape Certificate Type" },
228 EXT_NS_CERT_TYPE,
229 },
230 {
231 { NULL, NULL, NULL },
232 0,
233 },
234};
235
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200236FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
237FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200238
Paul Bakkerc70b9822013-04-07 22:00:46 +0200239static const oid_descriptor_t oid_ext_key_usage[] =
240{
241 { OID_SERVER_AUTH, "id-kp-serverAuth", "TLS Web Server Authentication" },
242 { OID_CLIENT_AUTH, "id-kp-clientAuth", "TLS Web Client Authentication" },
243 { OID_CODE_SIGNING, "id-kp-codeSigning", "Code Signing" },
244 { OID_EMAIL_PROTECTION, "id-kp-emailProtection", "E-mail Protection" },
245 { OID_TIME_STAMPING, "id-kp-timeStamping", "Time Stamping" },
246 { OID_OCSP_SIGNING, "id-kp-OCSPSigning", "OCSP Signing" },
247 { NULL, NULL, NULL },
248};
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200249
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200250FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
251FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
Paul Bakkered27a042013-04-18 22:46:23 +0200252#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200253
Paul Bakker47fce022013-06-28 17:34:34 +0200254#if defined(POLARSSL_MD_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200255/*
256 * For SignatureAlgorithmIdentifier
257 */
258typedef struct {
259 oid_descriptor_t descriptor;
260 md_type_t md_alg;
261 pk_type_t pk_alg;
262} oid_sig_alg_t;
263
264static const oid_sig_alg_t oid_sig_alg[] =
265{
266 {
267 { OID_PKCS1_MD2, "md2WithRSAEncryption", "RSA with MD2" },
268 POLARSSL_MD_MD2, POLARSSL_PK_RSA,
269 },
270 {
271 { OID_PKCS1_MD4, "md4WithRSAEncryption", "RSA with MD4" },
272 POLARSSL_MD_MD4, POLARSSL_PK_RSA,
273 },
274 {
275 { OID_PKCS1_MD5, "md5WithRSAEncryption", "RSA with MD5" },
276 POLARSSL_MD_MD5, POLARSSL_PK_RSA,
277 },
278 {
279 { OID_PKCS1_SHA1, "sha-1WithRSAEncryption", "RSA with SHA1" },
280 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
281 },
282 {
283 { OID_PKCS1_SHA224, "sha224WithRSAEncryption", "RSA with SHA-224" },
284 POLARSSL_MD_SHA224, POLARSSL_PK_RSA,
285 },
286 {
287 { OID_PKCS1_SHA256, "sha256WithRSAEncryption", "RSA with SHA-256" },
288 POLARSSL_MD_SHA256, POLARSSL_PK_RSA,
289 },
290 {
291 { OID_PKCS1_SHA384, "sha384WithRSAEncryption", "RSA with SHA-384" },
292 POLARSSL_MD_SHA384, POLARSSL_PK_RSA,
293 },
294 {
295 { OID_PKCS1_SHA512, "sha512WithRSAEncryption", "RSA with SHA-512" },
296 POLARSSL_MD_SHA512, POLARSSL_PK_RSA,
297 },
298 {
299 { OID_RSA_SHA_OBS, "sha-1WithRSAEncryption", "RSA with SHA1" },
300 POLARSSL_MD_SHA1, POLARSSL_PK_RSA,
301 },
302 {
303 { NULL, NULL, NULL },
304 0, 0,
305 },
306};
307
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200308FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
309FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
310FN_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 +0200311FN_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 +0200312#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200313
Paul Bakkerc70b9822013-04-07 22:00:46 +0200314/*
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200315 * For PublicKeyInfo (PKCS1, RFC 5480)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200316 */
317typedef struct {
318 oid_descriptor_t descriptor;
319 pk_type_t pk_alg;
320} oid_pk_alg_t;
321
322static const oid_pk_alg_t oid_pk_alg[] =
323{
324 {
325 { OID_PKCS1_RSA, "rsaEncryption", "RSA" },
326 POLARSSL_PK_RSA,
327 },
328 {
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +0200329 { OID_EC_ALG_UNRESTRICTED, "id-ecPublicKey", "Generic EC key" },
330 POLARSSL_PK_ECKEY,
331 },
332 {
333 { OID_EC_ALG_ECDH, "id-ecDH", "EC key for ECDH" },
334 POLARSSL_PK_ECKEY_DH,
335 },
336 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200337 { NULL, NULL, NULL },
338 0,
339 },
340};
341
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200342FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
343FN_OID_GET_ATTR1(oid_get_pk_alg, oid_pk_alg_t, pk_alg, pk_type_t, pk_alg);
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200344
Manuel Pégourié-Gonnardf0b30d02013-07-01 17:34:57 +0200345/*
346 * For namedCurve (RFC 5480)
347 */
348typedef struct {
349 oid_descriptor_t descriptor;
350 ecp_group_id grp_id;
351} oid_ecp_grp_t;
352
353static const oid_ecp_grp_t oid_ecp_grp[] =
354{
355 {
356 { OID_EC_GRP_SECP192R1, "secp192r1", "secp192r1" },
357 POLARSSL_ECP_DP_SECP192R1,
358 },
359 {
360 { OID_EC_GRP_SECP224R1, "secp224r1", "secp224r1" },
361 POLARSSL_ECP_DP_SECP224R1,
362 },
363 {
364 { OID_EC_GRP_SECP256R1, "secp256r1", "secp256r1" },
365 POLARSSL_ECP_DP_SECP256R1,
366 },
367 {
368 { OID_EC_GRP_SECP384R1, "secp384r1", "secp384r1" },
369 POLARSSL_ECP_DP_SECP384R1,
370 },
371 {
372 { OID_EC_GRP_SECP521R1, "secp521r1", "secp521r1" },
373 POLARSSL_ECP_DP_SECP521R1,
374 },
375 {
376 { NULL, NULL, NULL },
377 0,
378 },
379};
380
381FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
382FN_OID_GET_ATTR1(oid_get_ec_grp, oid_ecp_grp_t, grp_id, ecp_group_id, grp_id);
383
Paul Bakker47fce022013-06-28 17:34:34 +0200384#if defined(POLARSSL_CIPHER_C)
Paul Bakkerc70b9822013-04-07 22:00:46 +0200385/*
Paul Bakker9b5e8852013-06-28 16:12:50 +0200386 * For PKCS#5 PBES2 encryption algorithm
387 */
388typedef struct {
389 oid_descriptor_t descriptor;
390 cipher_type_t cipher_alg;
391} oid_cipher_alg_t;
392
393static const oid_cipher_alg_t oid_cipher_alg[] =
394{
395 {
396 { OID_DES_CBC, "desCBC", "DES-CBC" },
397 POLARSSL_CIPHER_DES_CBC,
398 },
399 {
400 { OID_DES_EDE3_CBC, "des-ede3-cbc", "DES-EDE3-CBC" },
401 POLARSSL_CIPHER_DES_EDE3_CBC,
402 },
403 {
404 { NULL, NULL, NULL },
405 0,
406 },
407};
408
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200409FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
410FN_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 +0200411#endif /* POLARSSL_CIPHER_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200412
Paul Bakker47fce022013-06-28 17:34:34 +0200413#if defined(POLARSSL_MD_C)
Paul Bakker9b5e8852013-06-28 16:12:50 +0200414/*
Paul Bakkerc70b9822013-04-07 22:00:46 +0200415 * For digestAlgorithm
416 */
417typedef struct {
418 oid_descriptor_t descriptor;
419 md_type_t md_alg;
420} oid_md_alg_t;
421
422static const oid_md_alg_t oid_md_alg[] =
423{
424 {
425 { OID_DIGEST_ALG_MD2, "id-md2", "MD2" },
426 POLARSSL_MD_MD2,
427 },
428 {
429 { OID_DIGEST_ALG_MD4, "id-md4", "MD4" },
430 POLARSSL_MD_MD4,
431 },
432 {
433 { OID_DIGEST_ALG_MD5, "id-md5", "MD5" },
434 POLARSSL_MD_MD5,
435 },
436 {
437 { OID_DIGEST_ALG_SHA1, "id-sha1", "SHA-1" },
438 POLARSSL_MD_SHA1,
439 },
440 {
441 { OID_DIGEST_ALG_SHA1, "id-sha1", "SHA-1" },
442 POLARSSL_MD_SHA1,
443 },
444 {
445 { OID_DIGEST_ALG_SHA224, "id-sha224", "SHA-224" },
446 POLARSSL_MD_SHA224,
447 },
448 {
449 { OID_DIGEST_ALG_SHA256, "id-sha256", "SHA-256" },
450 POLARSSL_MD_SHA256,
451 },
452 {
453 { OID_DIGEST_ALG_SHA384, "id-sha384", "SHA-384" },
454 POLARSSL_MD_SHA384,
455 },
456 {
457 { OID_DIGEST_ALG_SHA512, "id-sha512", "SHA-512" },
458 POLARSSL_MD_SHA512,
459 },
460 {
461 { NULL, NULL, NULL },
462 0,
463 },
464};
465
Paul Bakkerdd1150e2013-06-28 17:20:22 +0200466FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
467FN_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 +0200468FN_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 +0200469#endif /* POLARSSL_MD_C */
Paul Bakkerbd51ad52013-06-28 16:51:52 +0200470
Paul Bakker47fce022013-06-28 17:34:34 +0200471#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +0200472/*
473 * For PKCS#12 PBEs
474 */
475typedef struct {
476 oid_descriptor_t descriptor;
477 md_type_t md_alg;
478 cipher_type_t cipher_alg;
479} oid_pkcs12_pbe_alg_t;
480
481static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
482{
483 {
484 { OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
485 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE3_CBC,
486 },
487 {
488 { OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
489 POLARSSL_MD_SHA1, POLARSSL_CIPHER_DES_EDE_CBC,
490 },
491 {
492 { NULL, NULL, NULL },
493 0, 0,
494 },
495};
496
497FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
498FN_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 +0200499#endif /* POLARSSL_PKCS12_C */
Paul Bakker7749a222013-06-28 17:28:20 +0200500
Paul Bakkerc70b9822013-04-07 22:00:46 +0200501#if defined _MSC_VER && !defined snprintf
502#include <stdarg.h>
503
504#if !defined vsnprintf
505#define vsnprintf _vsnprintf
506#endif // vsnprintf
507
508/*
509 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
510 * Result value is not size of buffer needed, but -1 if no fit is possible.
511 *
512 * This fuction tries to 'fix' this by at least suggesting enlarging the
513 * size by 20.
514 */
515static int compat_snprintf(char *str, size_t size, const char *format, ...)
516{
517 va_list ap;
518 int res = -1;
519
520 va_start( ap, format );
521
522 res = vsnprintf( str, size, format, ap );
523
524 va_end( ap );
525
526 // No quick fix possible
527 if ( res < 0 )
528 return( (int) size + 20 );
529
530 return res;
531}
532
533#define snprintf compat_snprintf
534#endif
535
536#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
537
538#define SAFE_SNPRINTF() \
539{ \
540 if( ret == -1 ) \
541 return( -1 ); \
542 \
543 if ( (unsigned int) ret > n ) { \
544 p[n - 1] = '\0'; \
545 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
546 } \
547 \
548 n -= (unsigned int) ret; \
549 p += (unsigned int) ret; \
550}
551
552/* Return the x.y.z.... style numeric string for the given OID */
553int oid_get_numeric_string( char *buf, size_t size,
554 const asn1_buf *oid )
555{
556 int ret;
557 size_t i, n;
558 unsigned int value;
559 char *p;
560
561 p = buf;
562 n = size;
563
564 /* First byte contains first two dots */
565 if( oid->len > 0 )
566 {
567 ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
568 SAFE_SNPRINTF();
569 }
570
Paul Bakkerc70b9822013-04-07 22:00:46 +0200571 value = 0;
572 for( i = 1; i < oid->len; i++ )
573 {
Manuel Pégourié-Gonnarddffba8f2013-07-01 17:33:31 +0200574 /* Prevent overflow in value. */
575 if (value > (UINT_MAX >> 7) )
576 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
577
Paul Bakkerc70b9822013-04-07 22:00:46 +0200578 value <<= 7;
579 value += oid->p[i] & 0x7F;
580
581 if( !( oid->p[i] & 0x80 ) )
582 {
583 /* Last byte */
584 ret = snprintf( p, n, ".%d", value );
585 SAFE_SNPRINTF();
586 value = 0;
587 }
588 }
589
590 return( (int) ( size - n ) );
591}
592
Paul Bakkerc70b9822013-04-07 22:00:46 +0200593#endif /* POLARSSL_OID_C */