oid.c 26.1 KB
Newer Older
1 2 3 4 5
/**
 * \file oid.c
 *
 * \brief Object Identifier (OID) database
 *
6
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7
 *  SPDX-License-Identifier: Apache-2.0
8
 *
9 10 11
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
12
 *
13
 *  http://www.apache.org/licenses/LICENSE-2.0
14
 *
15 16 17 18 19
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
20
 *
21
 *  This file is part of mbed TLS (https://tls.mbed.org)
22 23
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
24
#if !defined(MBEDTLS_CONFIG_FILE)
25
#include "mbedtls/config.h"
26
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
27
#include MBEDTLS_CONFIG_FILE
28
#endif
29

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
30
#if defined(MBEDTLS_OID_C)
31

32 33
#include "mbedtls/oid.h"
#include "mbedtls/rsa.h"
34

35 36 37
#include <stdio.h>
#include <string.h>

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
38
#if defined(MBEDTLS_PLATFORM_C)
39
#include "mbedtls/platform.h"
40
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
41
#define mbedtls_snprintf snprintf
42 43
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
44
#if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
45
#include "mbedtls/x509.h"
46 47
#endif

48 49 50
/*
 * Macro to automatically add the size of #define'd OIDs
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
51
#define ADD_LEN(s)      s, MBEDTLS_OID_SIZE(s)
52

53 54 55 56
/*
 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
 * the other functions)
 */
57
#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST )                        \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
58
static const TYPE_T * oid_ ## NAME ## _from_asn1( const mbedtls_asn1_buf *oid )     \
59 60
{                                                                           \
    const TYPE_T *p = LIST;                                                 \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
61
    const mbedtls_oid_descriptor_t *cur = (const mbedtls_oid_descriptor_t *) p;             \
62 63 64 65 66 67 68
    if( p == NULL || oid == NULL ) return( NULL );                          \
    while( cur->asn1 != NULL ) {                                            \
        if( cur->asn1_len == oid->len &&                                    \
            memcmp( cur->asn1, oid->p, oid->len ) == 0 ) {                  \
            return( p );                                                    \
        }                                                                   \
        p++;                                                                \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
69
        cur = (const mbedtls_oid_descriptor_t *) p;                                 \
70 71 72
    }                                                                       \
    return( NULL );                                                         \
}
73

74 75
/*
 * Macro to generate a function for retrieving a single attribute from the
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
76
 * descriptor of an mbedtls_oid_descriptor_t wrapper.
77 78
 */
#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
79
int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 )                  \
80 81
{                                                                       \
    const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid );        \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
82
    if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND );            \
83 84 85 86 87 88
    *ATTR1 = data->descriptor.ATTR1;                                    \
    return( 0 );                                                        \
}

/*
 * Macro to generate a function for retrieving a single attribute from an
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
89
 * mbedtls_oid_descriptor_t wrapper.
90 91
 */
#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
92
int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 )                  \
93 94
{                                                                       \
    const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid );        \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
95
    if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND );            \
96 97 98 99 100 101
    *ATTR1 = data->ATTR1;                                               \
    return( 0 );                                                        \
}

/*
 * Macro to generate a function for retrieving two attributes from an
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
102
 * mbedtls_oid_descriptor_t wrapper.
103 104 105
 */
#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1,     \
                         ATTR2_TYPE, ATTR2)                                 \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
106
int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 )  \
107 108
{                                                                           \
    const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid );            \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
109
    if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND );                \
110 111 112 113 114
    *ATTR1 = data->ATTR1;                                                   \
    *ATTR2 = data->ATTR2;                                                   \
    return( 0 );                                                            \
}

115 116
/*
 * Macro to generate a function for retrieving the OID based on a single
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
117
 * attribute from a mbedtls_oid_descriptor_t wrapper.
118 119
 */
#define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1)   \
120
int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen )             \
121 122 123 124
{                                                                           \
    const TYPE_T *cur = LIST;                                               \
    while( cur->descriptor.asn1 != NULL ) {                                 \
        if( cur->ATTR1 == ATTR1 ) {                                         \
125 126
            *oid = cur->descriptor.asn1;                                    \
            *olen = cur->descriptor.asn1_len;                               \
127 128 129 130
            return( 0 );                                                    \
        }                                                                   \
        cur++;                                                              \
    }                                                                       \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
131
    return( MBEDTLS_ERR_OID_NOT_FOUND );                                   \
132 133 134 135
}

/*
 * Macro to generate a function for retrieving the OID based on two
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
136
 * attributes from a mbedtls_oid_descriptor_t wrapper.
137 138 139
 */
#define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1,   \
                                ATTR2_TYPE, ATTR2)                          \
140 141
int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid ,         \
             size_t *olen )                                                 \
142 143 144 145
{                                                                           \
    const TYPE_T *cur = LIST;                                               \
    while( cur->descriptor.asn1 != NULL ) {                                 \
        if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) {                  \
146 147
            *oid = cur->descriptor.asn1;                                    \
            *olen = cur->descriptor.asn1_len;                               \
148 149 150 151
            return( 0 );                                                    \
        }                                                                   \
        cur++;                                                              \
    }                                                                       \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
152
    return( MBEDTLS_ERR_OID_NOT_FOUND );                                   \
153 154
}

155
#if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
156 157 158 159
/*
 * For X520 attribute types
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
160
    mbedtls_oid_descriptor_t    descriptor;
161 162 163 164 165 166
    const char          *short_name;
} oid_x520_attr_t;

static const oid_x520_attr_t oid_x520_attr_type[] =
{
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
167
        { ADD_LEN( MBEDTLS_OID_AT_CN ),          "id-at-commonName",               "Common Name" },
168 169 170
        "CN",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
171
        { ADD_LEN( MBEDTLS_OID_AT_COUNTRY ),     "id-at-countryName",              "Country" },
172 173 174
        "C",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
175
        { ADD_LEN( MBEDTLS_OID_AT_LOCALITY ),    "id-at-locality",                 "Locality" },
176 177 178
        "L",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
179
        { ADD_LEN( MBEDTLS_OID_AT_STATE ),       "id-at-state",                    "State" },
180 181 182
        "ST",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
183
        { ADD_LEN( MBEDTLS_OID_AT_ORGANIZATION ),"id-at-organizationName",         "Organization" },
184 185 186
        "O",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
187
        { ADD_LEN( MBEDTLS_OID_AT_ORG_UNIT ),    "id-at-organizationalUnitName",   "Org Unit" },
188 189 190
        "OU",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
191
        { ADD_LEN( MBEDTLS_OID_PKCS9_EMAIL ),    "emailAddress",                   "E-mail address" },
192 193
        "emailAddress",
    },
194
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
195
        { ADD_LEN( MBEDTLS_OID_AT_SERIAL_NUMBER ),"id-at-serialNumber",            "Serial number" },
196 197 198
        "serialNumber",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
199
        { ADD_LEN( MBEDTLS_OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress",          "Postal address" },
200 201 202
        "postalAddress",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
203
        { ADD_LEN( MBEDTLS_OID_AT_POSTAL_CODE ), "id-at-postalCode",               "Postal code" },
204 205
        "postalCode",
    },
206
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
207
        { ADD_LEN( MBEDTLS_OID_AT_SUR_NAME ),    "id-at-surName",                  "Surname" },
208 209 210
        "SN",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
211
        { ADD_LEN( MBEDTLS_OID_AT_GIVEN_NAME ),  "id-at-givenName",                "Given name" },
212 213 214
        "GN",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
215
        { ADD_LEN( MBEDTLS_OID_AT_INITIALS ),    "id-at-initials",                 "Initials" },
216 217 218
        "initials",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
219
        { ADD_LEN( MBEDTLS_OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
220 221 222
        "generationQualifier",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
223
        { ADD_LEN( MBEDTLS_OID_AT_TITLE ),       "id-at-title",                    "Title" },
224 225 226
        "title",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
227
        { ADD_LEN( MBEDTLS_OID_AT_DN_QUALIFIER ),"id-at-dnQualifier",              "Distinguished Name qualifier" },
228 229 230
        "dnQualifier",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
231
        { ADD_LEN( MBEDTLS_OID_AT_PSEUDONYM ),   "id-at-pseudonym",                "Pseudonym" },
232 233 234
        "pseudonym",
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
235
        { ADD_LEN( MBEDTLS_OID_DOMAIN_COMPONENT ), "id-domainComponent",           "Domain component" },
236 237
        "DC",
    },
238
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
239
        { ADD_LEN( MBEDTLS_OID_AT_UNIQUE_IDENTIFIER ), "id-at-uniqueIdentifier",    "Unique Identifier" },
240 241
        "uniqueIdentifier",
    },
242
    {
243
        { NULL, 0, NULL, NULL },
244 245 246 247
        NULL,
    }
};

248 249
FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type)
FN_OID_GET_ATTR1(mbedtls_oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name)
250

251 252 253 254
/*
 * For X509 extensions
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
255
    mbedtls_oid_descriptor_t    descriptor;
256 257 258 259 260 261
    int                 ext_type;
} oid_x509_ext_t;

static const oid_x509_ext_t oid_x509_ext[] =
{
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
262
        { ADD_LEN( MBEDTLS_OID_BASIC_CONSTRAINTS ),    "id-ce-basicConstraints",   "Basic Constraints" },
263
        MBEDTLS_X509_EXT_BASIC_CONSTRAINTS,
264 265
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
266 267
        { ADD_LEN( MBEDTLS_OID_KEY_USAGE ),            "id-ce-keyUsage",           "Key Usage" },
        MBEDTLS_X509_EXT_KEY_USAGE,
268 269
    },
    {
270
        { ADD_LEN( MBEDTLS_OID_EXTENDED_KEY_USAGE ),   "id-ce-extKeyUsage",        "Extended Key Usage" },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
271
        MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE,
272 273
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
274
        { ADD_LEN( MBEDTLS_OID_SUBJECT_ALT_NAME ),     "id-ce-subjectAltName",     "Subject Alt Name" },
275
        MBEDTLS_X509_EXT_SUBJECT_ALT_NAME,
276 277
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
278 279
        { ADD_LEN( MBEDTLS_OID_NS_CERT_TYPE ),         "id-netscape-certtype",     "Netscape Certificate Type" },
        MBEDTLS_X509_EXT_NS_CERT_TYPE,
280 281
    },
    {
282
        { NULL, 0, NULL, NULL },
283 284 285 286
        0,
    },
};

287 288
FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext)
FN_OID_GET_ATTR1(mbedtls_oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type)
289

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
290
static const mbedtls_oid_descriptor_t oid_ext_key_usage[] =
291
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
292 293 294 295 296 297
    { ADD_LEN( MBEDTLS_OID_SERVER_AUTH ),      "id-kp-serverAuth",      "TLS Web Server Authentication" },
    { ADD_LEN( MBEDTLS_OID_CLIENT_AUTH ),      "id-kp-clientAuth",      "TLS Web Client Authentication" },
    { ADD_LEN( MBEDTLS_OID_CODE_SIGNING ),     "id-kp-codeSigning",     "Code Signing" },
    { ADD_LEN( MBEDTLS_OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
    { ADD_LEN( MBEDTLS_OID_TIME_STAMPING ),    "id-kp-timeStamping",    "Time Stamping" },
    { ADD_LEN( MBEDTLS_OID_OCSP_SIGNING ),     "id-kp-OCSPSigning",     "OCSP Signing" },
298
    { NULL, 0, NULL, NULL },
299
};
300

301 302
FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage)
FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage, mbedtls_oid_descriptor_t, ext_key_usage, const char *, description)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
303
#endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
304

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
305
#if defined(MBEDTLS_MD_C)
306 307 308 309
/*
 * For SignatureAlgorithmIdentifier
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
310 311 312
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_md_type_t           md_alg;
    mbedtls_pk_type_t           pk_alg;
313 314 315 316
} oid_sig_alg_t;

static const oid_sig_alg_t oid_sig_alg[] =
{
317 318
#if defined(MBEDTLS_RSA_C)
#if defined(MBEDTLS_MD2_C)
319
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
320 321
        { ADD_LEN( MBEDTLS_OID_PKCS1_MD2 ),        "md2WithRSAEncryption",     "RSA with MD2" },
        MBEDTLS_MD_MD2,      MBEDTLS_PK_RSA,
322
    },
323 324
#endif /* MBEDTLS_MD2_C */
#if defined(MBEDTLS_MD4_C)
325
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
326 327
        { ADD_LEN( MBEDTLS_OID_PKCS1_MD4 ),        "md4WithRSAEncryption",     "RSA with MD4" },
        MBEDTLS_MD_MD4,      MBEDTLS_PK_RSA,
328
    },
329 330
#endif /* MBEDTLS_MD4_C */
#if defined(MBEDTLS_MD5_C)
331
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
332 333
        { ADD_LEN( MBEDTLS_OID_PKCS1_MD5 ),        "md5WithRSAEncryption",     "RSA with MD5" },
        MBEDTLS_MD_MD5,      MBEDTLS_PK_RSA,
334
    },
335 336
#endif /* MBEDTLS_MD5_C */
#if defined(MBEDTLS_SHA1_C)
337
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
338 339
        { ADD_LEN( MBEDTLS_OID_PKCS1_SHA1 ),       "sha-1WithRSAEncryption",   "RSA with SHA1" },
        MBEDTLS_MD_SHA1,     MBEDTLS_PK_RSA,
340
    },
341 342
#endif /* MBEDTLS_SHA1_C */
#if defined(MBEDTLS_SHA256_C)
343
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
344 345
        { ADD_LEN( MBEDTLS_OID_PKCS1_SHA224 ),     "sha224WithRSAEncryption",  "RSA with SHA-224" },
        MBEDTLS_MD_SHA224,   MBEDTLS_PK_RSA,
346 347
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
348 349
        { ADD_LEN( MBEDTLS_OID_PKCS1_SHA256 ),     "sha256WithRSAEncryption",  "RSA with SHA-256" },
        MBEDTLS_MD_SHA256,   MBEDTLS_PK_RSA,
350
    },
351 352
#endif /* MBEDTLS_SHA256_C */
#if defined(MBEDTLS_SHA512_C)
353
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
354 355
        { ADD_LEN( MBEDTLS_OID_PKCS1_SHA384 ),     "sha384WithRSAEncryption",  "RSA with SHA-384" },
        MBEDTLS_MD_SHA384,   MBEDTLS_PK_RSA,
356 357
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
358 359
        { ADD_LEN( MBEDTLS_OID_PKCS1_SHA512 ),     "sha512WithRSAEncryption",  "RSA with SHA-512" },
        MBEDTLS_MD_SHA512,   MBEDTLS_PK_RSA,
360
    },
361 362
#endif /* MBEDTLS_SHA512_C */
#if defined(MBEDTLS_SHA1_C)
363
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
364 365
        { ADD_LEN( MBEDTLS_OID_RSA_SHA_OBS ),      "sha-1WithRSAEncryption",   "RSA with SHA1" },
        MBEDTLS_MD_SHA1,     MBEDTLS_PK_RSA,
366
    },
367 368 369 370
#endif /* MBEDTLS_SHA1_C */
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECDSA_C)
#if defined(MBEDTLS_SHA1_C)
371
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
372 373
        { ADD_LEN( MBEDTLS_OID_ECDSA_SHA1 ),       "ecdsa-with-SHA1",      "ECDSA with SHA1" },
        MBEDTLS_MD_SHA1,     MBEDTLS_PK_ECDSA,
374
    },
375 376
#endif /* MBEDTLS_SHA1_C */
#if defined(MBEDTLS_SHA256_C)
377
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
378 379
        { ADD_LEN( MBEDTLS_OID_ECDSA_SHA224 ),     "ecdsa-with-SHA224",    "ECDSA with SHA224" },
        MBEDTLS_MD_SHA224,   MBEDTLS_PK_ECDSA,
380 381
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
382 383
        { ADD_LEN( MBEDTLS_OID_ECDSA_SHA256 ),     "ecdsa-with-SHA256",    "ECDSA with SHA256" },
        MBEDTLS_MD_SHA256,   MBEDTLS_PK_ECDSA,
384
    },
385 386
#endif /* MBEDTLS_SHA256_C */
#if defined(MBEDTLS_SHA512_C)
387
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
388 389
        { ADD_LEN( MBEDTLS_OID_ECDSA_SHA384 ),     "ecdsa-with-SHA384",    "ECDSA with SHA384" },
        MBEDTLS_MD_SHA384,   MBEDTLS_PK_ECDSA,
390 391
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
392 393
        { ADD_LEN( MBEDTLS_OID_ECDSA_SHA512 ),     "ecdsa-with-SHA512",    "ECDSA with SHA512" },
        MBEDTLS_MD_SHA512,   MBEDTLS_PK_ECDSA,
394
    },
395 396 397
#endif /* MBEDTLS_SHA512_C */
#endif /* MBEDTLS_ECDSA_C */
#if defined(MBEDTLS_RSA_C)
398
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
399 400
        { ADD_LEN( MBEDTLS_OID_RSASSA_PSS ),        "RSASSA-PSS",           "RSASSA-PSS" },
        MBEDTLS_MD_NONE,     MBEDTLS_PK_RSASSA_PSS,
401
    },
402
#endif /* MBEDTLS_RSA_C */
403
    {
404
        { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
405
        MBEDTLS_MD_NONE, MBEDTLS_PK_NONE,
406 407 408
    },
};

409 410 411 412
FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg)
FN_OID_GET_DESCRIPTOR_ATTR1(mbedtls_oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description)
FN_OID_GET_ATTR2(mbedtls_oid_get_sig_alg, oid_sig_alg_t, sig_alg, mbedtls_md_type_t, md_alg, mbedtls_pk_type_t, pk_alg)
FN_OID_GET_OID_BY_ATTR2(mbedtls_oid_get_oid_by_sig_alg, oid_sig_alg_t, oid_sig_alg, mbedtls_pk_type_t, pk_alg, mbedtls_md_type_t, md_alg)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
413
#endif /* MBEDTLS_MD_C */
414

415
/*
416
 * For PublicKeyInfo (PKCS1, RFC 5480)
417 418
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
419 420
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_pk_type_t           pk_alg;
421 422 423 424 425
} oid_pk_alg_t;

static const oid_pk_alg_t oid_pk_alg[] =
{
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
426 427
        { ADD_LEN( MBEDTLS_OID_PKCS1_RSA ),      "rsaEncryption",   "RSA" },
        MBEDTLS_PK_RSA,
428
    },
429
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
430 431
        { ADD_LEN( MBEDTLS_OID_EC_ALG_UNRESTRICTED ),  "id-ecPublicKey",   "Generic EC key" },
        MBEDTLS_PK_ECKEY,
432 433
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
434 435
        { ADD_LEN( MBEDTLS_OID_EC_ALG_ECDH ),          "id-ecDH",          "EC key for ECDH" },
        MBEDTLS_PK_ECKEY_DH,
436
    },
437
    {
438
        { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
439
        MBEDTLS_PK_NONE,
440 441 442
    },
};

443 444 445
FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg)
FN_OID_GET_ATTR1(mbedtls_oid_get_pk_alg, oid_pk_alg_t, pk_alg, mbedtls_pk_type_t, pk_alg)
FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_pk_alg, oid_pk_alg_t, oid_pk_alg, mbedtls_pk_type_t, pk_alg)
446

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
447
#if defined(MBEDTLS_ECP_C)
448 449 450 451
/*
 * For namedCurve (RFC 5480)
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
452 453
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_ecp_group_id        grp_id;
454 455 456 457
} oid_ecp_grp_t;

static const oid_ecp_grp_t oid_ecp_grp[] =
{
458
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
459
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
460 461
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192R1 ), "secp192r1",    "secp192r1" },
        MBEDTLS_ECP_DP_SECP192R1,
462
    },
463 464
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
465
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
466 467
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224R1 ), "secp224r1",    "secp224r1" },
        MBEDTLS_ECP_DP_SECP224R1,
468
    },
469 470
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
471
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
472 473
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256R1 ), "secp256r1",    "secp256r1" },
        MBEDTLS_ECP_DP_SECP256R1,
474
    },
475 476
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
477
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
478 479
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP384R1 ), "secp384r1",    "secp384r1" },
        MBEDTLS_ECP_DP_SECP384R1,
480
    },
481 482
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
483
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
484 485
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP521R1 ), "secp521r1",    "secp521r1" },
        MBEDTLS_ECP_DP_SECP521R1,
486
    },
487 488
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
489
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
490 491
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192K1 ), "secp192k1",    "secp192k1" },
        MBEDTLS_ECP_DP_SECP192K1,
492
    },
493 494
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
495
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
496 497
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224K1 ), "secp224k1",    "secp224k1" },
        MBEDTLS_ECP_DP_SECP224K1,
498
    },
499 500
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
501
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
502 503
        { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256K1 ), "secp256k1",    "secp256k1" },
        MBEDTLS_ECP_DP_SECP256K1,
504
    },
505 506
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
507
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
508 509
        { ADD_LEN( MBEDTLS_OID_EC_GRP_BP256R1 ),   "brainpoolP256r1","brainpool256r1" },
        MBEDTLS_ECP_DP_BP256R1,
510
    },
511 512
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
513
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
514 515
        { ADD_LEN( MBEDTLS_OID_EC_GRP_BP384R1 ),   "brainpoolP384r1","brainpool384r1" },
        MBEDTLS_ECP_DP_BP384R1,
516
    },
517 518
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
519
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
520 521
        { ADD_LEN( MBEDTLS_OID_EC_GRP_BP512R1 ),   "brainpoolP512r1","brainpool512r1" },
        MBEDTLS_ECP_DP_BP512R1,
522
    },
523
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
524
    {
525
        { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
526
        MBEDTLS_ECP_DP_NONE,
527 528 529
    },
};

530 531 532
FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp)
FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp, oid_ecp_grp_t, grp_id, mbedtls_ecp_group_id, grp_id)
FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, mbedtls_ecp_group_id, grp_id)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
533
#endif /* MBEDTLS_ECP_C */
534

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
535
#if defined(MBEDTLS_CIPHER_C)
536 537 538 539
/*
 * For PKCS#5 PBES2 encryption algorithm
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
540 541
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_cipher_type_t       cipher_alg;
542 543 544 545 546
} oid_cipher_alg_t;

static const oid_cipher_alg_t oid_cipher_alg[] =
{
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
547 548
        { ADD_LEN( MBEDTLS_OID_DES_CBC ),              "desCBC",       "DES-CBC" },
        MBEDTLS_CIPHER_DES_CBC,
549 550
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
551 552
        { ADD_LEN( MBEDTLS_OID_DES_EDE3_CBC ),         "des-ede3-cbc", "DES-EDE3-CBC" },
        MBEDTLS_CIPHER_DES_EDE3_CBC,
553 554
    },
    {
555
        { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
556
        MBEDTLS_CIPHER_NONE,
557 558 559
    },
};

560 561
FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg)
FN_OID_GET_ATTR1(mbedtls_oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, mbedtls_cipher_type_t, cipher_alg)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
562
#endif /* MBEDTLS_CIPHER_C */
563

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
564
#if defined(MBEDTLS_MD_C)
565 566 567 568
/*
 * For digestAlgorithm
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
569 570
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_md_type_t           md_alg;
571 572 573 574
} oid_md_alg_t;

static const oid_md_alg_t oid_md_alg[] =
{
575
#if defined(MBEDTLS_MD2_C)
576
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
577 578
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD2 ),       "id-md2",       "MD2" },
        MBEDTLS_MD_MD2,
579
    },
580 581
#endif /* MBEDTLS_MD2_C */
#if defined(MBEDTLS_MD4_C)
582
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
583 584
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD4 ),       "id-md4",       "MD4" },
        MBEDTLS_MD_MD4,
585
    },
586 587
#endif /* MBEDTLS_MD4_C */
#if defined(MBEDTLS_MD5_C)
588
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
589 590
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD5 ),       "id-md5",       "MD5" },
        MBEDTLS_MD_MD5,
591
    },
592 593
#endif /* MBEDTLS_MD5_C */
#if defined(MBEDTLS_SHA1_C)
594
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
595 596
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA1 ),      "id-sha1",      "SHA-1" },
        MBEDTLS_MD_SHA1,
597
    },
598 599
#endif /* MBEDTLS_SHA1_C */
#if defined(MBEDTLS_SHA256_C)
600
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
601 602
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA224 ),    "id-sha224",    "SHA-224" },
        MBEDTLS_MD_SHA224,
603 604
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
605 606
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA256 ),    "id-sha256",    "SHA-256" },
        MBEDTLS_MD_SHA256,
607
    },
608 609
#endif /* MBEDTLS_SHA256_C */
#if defined(MBEDTLS_SHA512_C)
610
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
611 612
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA384 ),    "id-sha384",    "SHA-384" },
        MBEDTLS_MD_SHA384,
613 614
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
615 616
        { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA512 ),    "id-sha512",    "SHA-512" },
        MBEDTLS_MD_SHA512,
617
    },
618
#endif /* MBEDTLS_SHA512_C */
619
    {
620
        { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
621
        MBEDTLS_MD_NONE,
622 623 624
    },
};

625 626 627
FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg)
FN_OID_GET_ATTR1(mbedtls_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg)
FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_md, oid_md_alg_t, oid_md_alg, mbedtls_md_type_t, md_alg)
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

/*
 * For HMAC digestAlgorithm
 */
typedef struct {
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_md_type_t           md_hmac;
} oid_md_hmac_t;

static const oid_md_hmac_t oid_md_hmac[] =
{
#if defined(MBEDTLS_SHA1_C)
    {
        { ADD_LEN( MBEDTLS_OID_HMAC_SHA1 ),      "hmacSHA1",      "HMAC-SHA-1" },
        MBEDTLS_MD_SHA1,
    },
#endif /* MBEDTLS_SHA1_C */
#if defined(MBEDTLS_SHA256_C)
    {
        { ADD_LEN( MBEDTLS_OID_HMAC_SHA224 ),    "hmacSHA224",    "HMAC-SHA-224" },
        MBEDTLS_MD_SHA224,
    },
    {
        { ADD_LEN( MBEDTLS_OID_HMAC_SHA256 ),    "hmacSHA256",    "HMAC-SHA-256" },
        MBEDTLS_MD_SHA256,
    },
#endif /* MBEDTLS_SHA256_C */
#if defined(MBEDTLS_SHA512_C)
    {
        { ADD_LEN( MBEDTLS_OID_HMAC_SHA384 ),    "hmacSHA384",    "HMAC-SHA-384" },
        MBEDTLS_MD_SHA384,
    },
    {
        { ADD_LEN( MBEDTLS_OID_HMAC_SHA512 ),    "hmacSHA512",    "HMAC-SHA-512" },
        MBEDTLS_MD_SHA512,
    },
#endif /* MBEDTLS_SHA512_C */
    {
        { NULL, 0, NULL, NULL },
        MBEDTLS_MD_NONE,
    },
};

FN_OID_TYPED_FROM_ASN1(oid_md_hmac_t, md_hmac, oid_md_hmac)
FN_OID_GET_ATTR1(mbedtls_oid_get_md_hmac, oid_md_hmac_t, md_hmac, mbedtls_md_type_t, md_hmac)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
673
#endif /* MBEDTLS_MD_C */
674

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
675
#if defined(MBEDTLS_PKCS12_C)
676 677 678 679
/*
 * For PKCS#12 PBEs
 */
typedef struct {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
680 681 682
    mbedtls_oid_descriptor_t    descriptor;
    mbedtls_md_type_t           md_alg;
    mbedtls_cipher_type_t       cipher_alg;
683 684 685 686 687
} oid_pkcs12_pbe_alg_t;

static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
{
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
688 689
        { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC ), "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
        MBEDTLS_MD_SHA1,      MBEDTLS_CIPHER_DES_EDE3_CBC,
690 691
    },
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
692 693
        { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC ), "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
        MBEDTLS_MD_SHA1,      MBEDTLS_CIPHER_DES_EDE_CBC,
694 695
    },
    {
696
        { NULL, 0, NULL, NULL },
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
697
        MBEDTLS_MD_NONE, MBEDTLS_CIPHER_NONE,
698 699 700
    },
};

701 702
FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, mbedtls_md_type_t, md_alg, mbedtls_cipher_type_t, cipher_alg)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
703
#endif /* MBEDTLS_PKCS12_C */
704

705 706 707 708 709 710 711 712
#define OID_SAFE_SNPRINTF                               \
    do {                                                \
        if( ret < 0 || (size_t) ret >= n )              \
            return( MBEDTLS_ERR_OID_BUF_TOO_SMALL );    \
                                                        \
        n -= (size_t) ret;                              \
        p += (size_t) ret;                              \
    } while( 0 )
713 714

/* Return the x.y.z.... style numeric string for the given OID */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
715 716
int mbedtls_oid_get_numeric_string( char *buf, size_t size,
                            const mbedtls_asn1_buf *oid )
717 718 719 720 721 722 723 724 725 726 727 728
{
    int ret;
    size_t i, n;
    unsigned int value;
    char *p;

    p = buf;
    n = size;

    /* First byte contains first two dots */
    if( oid->len > 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
729
        ret = mbedtls_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
730
        OID_SAFE_SNPRINTF;
731 732 733 734 735
    }

    value = 0;
    for( i = 1; i < oid->len; i++ )
    {
736
        /* Prevent overflow in value. */
737
        if( ( ( value << 7 ) >> 7 ) != value )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
738
            return( MBEDTLS_ERR_OID_BUF_TOO_SMALL );
739

740 741 742 743 744 745
        value <<= 7;
        value += oid->p[i] & 0x7F;

        if( !( oid->p[i] & 0x80 ) )
        {
            /* Last byte */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
746
            ret = mbedtls_snprintf( p, n, ".%d", value );
747
            OID_SAFE_SNPRINTF;
748 749 750 751 752 753 754
            value = 0;
        }
    }

    return( (int) ( size - n ) );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
755
#endif /* MBEDTLS_OID_C */