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