mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers oid.c Source File

oid.c

Go to the documentation of this file.
00001 /**
00002  * \file oid.c
00003  *
00004  * \brief Object Identifier (OID) database
00005  *
00006  *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
00007  *
00008  *  This file is part of mbed TLS (https://tls.mbed.org)
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License along
00021  *  with this program; if not, write to the Free Software Foundation, Inc.,
00022  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00023  */
00024 
00025 #if !defined(POLARSSL_CONFIG_FILE)
00026 #include "polarssl/config.h"
00027 #else
00028 #include POLARSSL_CONFIG_FILE
00029 #endif
00030 
00031 #if defined(POLARSSL_OID_C)
00032 
00033 #include "polarssl/oid.h"
00034 #include "polarssl/rsa.h"
00035 
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 #if defined(POLARSSL_PLATFORM_C)
00040 #include "polarssl/platform.h"
00041 #else
00042 #define polarssl_snprintf snprintf
00043 #endif
00044 
00045 #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
00046 #include "polarssl/x509.h"
00047 #endif
00048 
00049 /*
00050  * Macro to automatically add the size of #define'd OIDs
00051  */
00052 #define ADD_LEN(s)      s, OID_SIZE(s)
00053 
00054 /*
00055  * Macro to generate an internal function for oid_XXX_from_asn1() (used by
00056  * the other functions)
00057  */
00058 #define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST )                        \
00059 static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid )     \
00060 {                                                                           \
00061     const TYPE_T *p = LIST;                                                 \
00062     const oid_descriptor_t *cur = (const oid_descriptor_t *) p;             \
00063     if( p == NULL || oid == NULL ) return( NULL );                          \
00064     while( cur->asn1 != NULL ) {                                            \
00065         if( cur->asn1_len == oid->len &&                                    \
00066             memcmp( cur->asn1, oid->p, oid->len ) == 0 ) {                  \
00067             return( p );                                                    \
00068         }                                                                   \
00069         p++;                                                                \
00070         cur = (const oid_descriptor_t *) p;                                 \
00071     }                                                                       \
00072     return( NULL );                                                         \
00073 }
00074 
00075 /*
00076  * Macro to generate a function for retrieving a single attribute from the
00077  * descriptor of an oid_descriptor_t wrapper.
00078  */
00079 #define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
00080 int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 )                  \
00081 {                                                                       \
00082     const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid );        \
00083     if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND );            \
00084     *ATTR1 = data->descriptor.ATTR1;                                    \
00085     return( 0 );                                                        \
00086 }
00087 
00088 /*
00089  * Macro to generate a function for retrieving a single attribute from an
00090  * oid_descriptor_t wrapper.
00091  */
00092 #define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
00093 int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 )                  \
00094 {                                                                       \
00095     const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid );        \
00096     if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND );            \
00097     *ATTR1 = data->ATTR1;                                               \
00098     return( 0 );                                                        \
00099 }
00100 
00101 /*
00102  * Macro to generate a function for retrieving two attributes from an
00103  * oid_descriptor_t wrapper.
00104  */
00105 #define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1,     \
00106                          ATTR2_TYPE, ATTR2)                                 \
00107 int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 )  \
00108 {                                                                           \
00109     const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid );            \
00110     if( data == NULL ) return( POLARSSL_ERR_OID_NOT_FOUND );                \
00111     *ATTR1 = data->ATTR1;                                                   \
00112     *ATTR2 = data->ATTR2;                                                   \
00113     return( 0 );                                                            \
00114 }
00115 
00116 /*
00117  * Macro to generate a function for retrieving the OID based on a single
00118  * attribute from a oid_descriptor_t wrapper.
00119  */
00120 #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1)   \
00121 int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen )             \
00122 {                                                                           \
00123     const TYPE_T *cur = LIST;                                               \
00124     while( cur->descriptor.asn1 != NULL ) {                                 \
00125         if( cur->ATTR1 == ATTR1 ) {                                         \
00126             *oid = cur->descriptor.asn1;                                    \
00127             *olen = cur->descriptor.asn1_len;                               \
00128             return( 0 );                                                    \
00129         }                                                                   \
00130         cur++;                                                              \
00131     }                                                                       \
00132     return( POLARSSL_ERR_OID_NOT_FOUND );                                   \
00133 }
00134 
00135 /*
00136  * Macro to generate a function for retrieving the OID based on two
00137  * attributes from a oid_descriptor_t wrapper.
00138  */
00139 #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1,   \
00140                                 ATTR2_TYPE, ATTR2)                          \
00141 int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid ,         \
00142              size_t *olen )                                                 \
00143 {                                                                           \
00144     const TYPE_T *cur = LIST;                                               \
00145     while( cur->descriptor.asn1 != NULL ) {                                 \
00146         if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) {                  \
00147             *oid = cur->descriptor.asn1;                                    \
00148             *olen = cur->descriptor.asn1_len;                               \
00149             return( 0 );                                                    \
00150         }                                                                   \
00151         cur++;                                                              \
00152     }                                                                       \
00153     return( POLARSSL_ERR_OID_NOT_FOUND );                                   \
00154 }
00155 
00156 /*
00157  * For X520 attribute types
00158  */
00159 typedef struct {
00160     oid_descriptor_t    descriptor;
00161     const char          *short_name;
00162 } oid_x520_attr_t;
00163 
00164 static const oid_x520_attr_t oid_x520_attr_type[] =
00165 {
00166     {
00167         { ADD_LEN( OID_AT_CN ),          "id-at-commonName",               "Common Name" },
00168         "CN",
00169     },
00170     {
00171         { ADD_LEN( OID_AT_COUNTRY ),     "id-at-countryName",              "Country" },
00172         "C",
00173     },
00174     {
00175         { ADD_LEN( OID_AT_LOCALITY ),    "id-at-locality",                 "Locality" },
00176         "L",
00177     },
00178     {
00179         { ADD_LEN( OID_AT_STATE ),       "id-at-state",                    "State" },
00180         "ST",
00181     },
00182     {
00183         { ADD_LEN( OID_AT_ORGANIZATION ),"id-at-organizationName",         "Organization" },
00184         "O",
00185     },
00186     {
00187         { ADD_LEN( OID_AT_ORG_UNIT ),    "id-at-organizationalUnitName",   "Org Unit" },
00188         "OU",
00189     },
00190     {
00191         { ADD_LEN( OID_PKCS9_EMAIL ),    "emailAddress",                   "E-mail address" },
00192         "emailAddress",
00193     },
00194     {
00195         { ADD_LEN( OID_AT_SERIAL_NUMBER ),"id-at-serialNumber",            "Serial number" },
00196         "serialNumber",
00197     },
00198     {
00199         { ADD_LEN( OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress",          "Postal address" },
00200         "postalAddress",
00201     },
00202     {
00203         { ADD_LEN( OID_AT_POSTAL_CODE ), "id-at-postalCode",               "Postal code" },
00204         "postalCode",
00205     },
00206     {
00207         { ADD_LEN( OID_AT_SUR_NAME ),    "id-at-surName",                  "Surname" },
00208         "SN",
00209     },
00210     {
00211         { ADD_LEN( OID_AT_GIVEN_NAME ),  "id-at-givenName",                "Given name" },
00212         "GN",
00213     },
00214     {
00215         { ADD_LEN( OID_AT_INITIALS ),    "id-at-initials",                 "Initials" },
00216         "initials",
00217     },
00218     {
00219         { ADD_LEN( OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
00220         "generationQualifier",
00221     },
00222     {
00223         { ADD_LEN( OID_AT_TITLE ),       "id-at-title",                    "Title" },
00224         "title",
00225     },
00226     {
00227         { ADD_LEN( OID_AT_DN_QUALIFIER ),"id-at-dnQualifier",              "Distinguished Name qualifier" },
00228         "dnQualifier",
00229     },
00230     {
00231         { ADD_LEN( OID_AT_PSEUDONYM ),   "id-at-pseudonym",                "Pseudonym" },
00232         "pseudonym",
00233     },
00234     {
00235         { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent",           "Domain component" },
00236         "DC",
00237     },
00238     {
00239         { ADD_LEN( OID_AT_UNIQUE_IDENTIFIER ), "id-at-uniqueIdentifier",    "Unique Identifier" },
00240         "uniqueIdentifier",
00241     },
00242     {
00243         { NULL, 0, NULL, NULL },
00244         NULL,
00245     }
00246 };
00247 
00248 FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
00249 FN_OID_GET_ATTR1(oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name);
00250 
00251 #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
00252 /*
00253  * For X509 extensions
00254  */
00255 typedef struct {
00256     oid_descriptor_t    descriptor;
00257     int                 ext_type;
00258 } oid_x509_ext_t;
00259 
00260 static const oid_x509_ext_t oid_x509_ext[] =
00261 {
00262     {
00263         { ADD_LEN( OID_BASIC_CONSTRAINTS ),    "id-ce-basicConstraints",   "Basic Constraints" },
00264         EXT_BASIC_CONSTRAINTS,
00265     },
00266     {
00267         { ADD_LEN( OID_KEY_USAGE ),            "id-ce-keyUsage",           "Key Usage" },
00268         EXT_KEY_USAGE,
00269     },
00270     {
00271         { ADD_LEN( OID_EXTENDED_KEY_USAGE ),   "id-ce-keyUsage",           "Extended Key Usage" },
00272         EXT_EXTENDED_KEY_USAGE,
00273     },
00274     {
00275         { ADD_LEN( OID_SUBJECT_ALT_NAME ),     "id-ce-subjectAltName",     "Subject Alt Name" },
00276         EXT_SUBJECT_ALT_NAME,
00277     },
00278     {
00279         { ADD_LEN( OID_NS_CERT_TYPE ),         "id-netscape-certtype",     "Netscape Certificate Type" },
00280         EXT_NS_CERT_TYPE,
00281     },
00282     {
00283         { NULL, 0, NULL, NULL },
00284         0,
00285     },
00286 };
00287 
00288 FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
00289 FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
00290 
00291 static const oid_descriptor_t oid_ext_key_usage[] =
00292 {
00293     { ADD_LEN( OID_SERVER_AUTH ),      "id-kp-serverAuth",      "TLS Web Server Authentication" },
00294     { ADD_LEN( OID_CLIENT_AUTH ),      "id-kp-clientAuth",      "TLS Web Client Authentication" },
00295     { ADD_LEN( OID_CODE_SIGNING ),     "id-kp-codeSigning",     "Code Signing" },
00296     { ADD_LEN( OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
00297     { ADD_LEN( OID_TIME_STAMPING ),    "id-kp-timeStamping",    "Time Stamping" },
00298     { ADD_LEN( OID_OCSP_SIGNING ),     "id-kp-OCSPSigning",     "OCSP Signing" },
00299     { NULL, 0, NULL, NULL },
00300 };
00301 
00302 FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
00303 FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
00304 #endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
00305 
00306 #if defined(POLARSSL_MD_C)
00307 /*
00308  * For SignatureAlgorithmIdentifier
00309  */
00310 typedef struct {
00311     oid_descriptor_t    descriptor;
00312     md_type_t           md_alg;
00313     pk_type_t           pk_alg;
00314 } oid_sig_alg_t;
00315 
00316 static const oid_sig_alg_t oid_sig_alg[] =
00317 {
00318     {
00319         { ADD_LEN( OID_PKCS1_MD2 ),        "md2WithRSAEncryption",     "RSA with MD2" },
00320         POLARSSL_MD_MD2,      POLARSSL_PK_RSA,
00321     },
00322     {
00323         { ADD_LEN( OID_PKCS1_MD4 ),        "md4WithRSAEncryption",     "RSA with MD4" },
00324         POLARSSL_MD_MD4,      POLARSSL_PK_RSA,
00325     },
00326     {
00327         { ADD_LEN( OID_PKCS1_MD5 ),        "md5WithRSAEncryption",     "RSA with MD5" },
00328         POLARSSL_MD_MD5,      POLARSSL_PK_RSA,
00329     },
00330     {
00331         { ADD_LEN( OID_PKCS1_SHA1 ),       "sha-1WithRSAEncryption",   "RSA with SHA1" },
00332         POLARSSL_MD_SHA1,     POLARSSL_PK_RSA,
00333     },
00334     {
00335         { ADD_LEN( OID_PKCS1_SHA224 ),     "sha224WithRSAEncryption",  "RSA with SHA-224" },
00336         POLARSSL_MD_SHA224,   POLARSSL_PK_RSA,
00337     },
00338     {
00339         { ADD_LEN( OID_PKCS1_SHA256 ),     "sha256WithRSAEncryption",  "RSA with SHA-256" },
00340         POLARSSL_MD_SHA256,   POLARSSL_PK_RSA,
00341     },
00342     {
00343         { ADD_LEN( OID_PKCS1_SHA384 ),     "sha384WithRSAEncryption",  "RSA with SHA-384" },
00344         POLARSSL_MD_SHA384,   POLARSSL_PK_RSA,
00345     },
00346     {
00347         { ADD_LEN( OID_PKCS1_SHA512 ),     "sha512WithRSAEncryption",  "RSA with SHA-512" },
00348         POLARSSL_MD_SHA512,   POLARSSL_PK_RSA,
00349     },
00350     {
00351         { ADD_LEN( OID_RSA_SHA_OBS ),      "sha-1WithRSAEncryption",   "RSA with SHA1" },
00352         POLARSSL_MD_SHA1,     POLARSSL_PK_RSA,
00353     },
00354     {
00355         { ADD_LEN( OID_ECDSA_SHA1 ),       "ecdsa-with-SHA1",      "ECDSA with SHA1" },
00356         POLARSSL_MD_SHA1,     POLARSSL_PK_ECDSA,
00357     },
00358     {
00359         { ADD_LEN( OID_ECDSA_SHA224 ),     "ecdsa-with-SHA224",    "ECDSA with SHA224" },
00360         POLARSSL_MD_SHA224,   POLARSSL_PK_ECDSA,
00361     },
00362     {
00363         { ADD_LEN( OID_ECDSA_SHA256 ),     "ecdsa-with-SHA256",    "ECDSA with SHA256" },
00364         POLARSSL_MD_SHA256,   POLARSSL_PK_ECDSA,
00365     },
00366     {
00367         { ADD_LEN( OID_ECDSA_SHA384 ),     "ecdsa-with-SHA384",    "ECDSA with SHA384" },
00368         POLARSSL_MD_SHA384,   POLARSSL_PK_ECDSA,
00369     },
00370     {
00371         { ADD_LEN( OID_ECDSA_SHA512 ),     "ecdsa-with-SHA512",    "ECDSA with SHA512" },
00372         POLARSSL_MD_SHA512,   POLARSSL_PK_ECDSA,
00373     },
00374     {
00375         { ADD_LEN( OID_RSASSA_PSS ),        "RSASSA-PSS",           "RSASSA-PSS" },
00376         POLARSSL_MD_NONE,     POLARSSL_PK_RSASSA_PSS,
00377     },
00378     {
00379         { NULL, 0, NULL, NULL },
00380         POLARSSL_MD_NONE, POLARSSL_PK_NONE,
00381     },
00382 };
00383 
00384 FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
00385 FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
00386 FN_OID_GET_ATTR2(oid_get_sig_alg, oid_sig_alg_t, sig_alg, md_type_t, md_alg, pk_type_t, pk_alg);
00387 FN_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);
00388 #endif /* POLARSSL_MD_C */
00389 
00390 /*
00391  * For PublicKeyInfo (PKCS1, RFC 5480)
00392  */
00393 typedef struct {
00394     oid_descriptor_t    descriptor;
00395     pk_type_t           pk_alg;
00396 } oid_pk_alg_t;
00397 
00398 static const oid_pk_alg_t oid_pk_alg[] =
00399 {
00400     {
00401         { ADD_LEN( OID_PKCS1_RSA ),      "rsaEncryption",   "RSA" },
00402         POLARSSL_PK_RSA,
00403     },
00404     {
00405         { ADD_LEN( OID_EC_ALG_UNRESTRICTED ),  "id-ecPublicKey",   "Generic EC key" },
00406         POLARSSL_PK_ECKEY,
00407     },
00408     {
00409         { ADD_LEN( OID_EC_ALG_ECDH ),          "id-ecDH",          "EC key for ECDH" },
00410         POLARSSL_PK_ECKEY_DH,
00411     },
00412     {
00413         { NULL, 0, NULL, NULL },
00414         POLARSSL_PK_NONE,
00415     },
00416 };
00417 
00418 FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
00419 FN_OID_GET_ATTR1(oid_get_pk_alg, oid_pk_alg_t, pk_alg, pk_type_t, pk_alg);
00420 FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_pk_alg, oid_pk_alg_t, oid_pk_alg, pk_type_t, pk_alg);
00421 
00422 #if defined(POLARSSL_ECP_C)
00423 /*
00424  * For namedCurve (RFC 5480)
00425  */
00426 typedef struct {
00427     oid_descriptor_t    descriptor;
00428     ecp_group_id        grp_id;
00429 } oid_ecp_grp_t;
00430 
00431 static const oid_ecp_grp_t oid_ecp_grp[] =
00432 {
00433     {
00434         { ADD_LEN( OID_EC_GRP_SECP192R1 ), "secp192r1",    "secp192r1" },
00435         POLARSSL_ECP_DP_SECP192R1 ,
00436     },
00437     {
00438         { ADD_LEN( OID_EC_GRP_SECP224R1 ), "secp224r1",    "secp224r1" },
00439         POLARSSL_ECP_DP_SECP224R1 ,
00440     },
00441     {
00442         { ADD_LEN( OID_EC_GRP_SECP256R1 ), "secp256r1",    "secp256r1" },
00443         POLARSSL_ECP_DP_SECP256R1 ,
00444     },
00445     {
00446         { ADD_LEN( OID_EC_GRP_SECP384R1 ), "secp384r1",    "secp384r1" },
00447         POLARSSL_ECP_DP_SECP384R1 ,
00448     },
00449     {
00450         { ADD_LEN( OID_EC_GRP_SECP521R1 ), "secp521r1",    "secp521r1" },
00451         POLARSSL_ECP_DP_SECP521R1 ,
00452     },
00453     {
00454         { ADD_LEN( OID_EC_GRP_SECP192K1 ), "secp192k1",    "secp192k1" },
00455         POLARSSL_ECP_DP_SECP192K1 ,
00456     },
00457     {
00458         { ADD_LEN( OID_EC_GRP_SECP224K1 ), "secp224k1",    "secp224k1" },
00459         POLARSSL_ECP_DP_SECP224K1 ,
00460     },
00461     {
00462         { ADD_LEN( OID_EC_GRP_SECP256K1 ), "secp256k1",    "secp256k1" },
00463         POLARSSL_ECP_DP_SECP256K1 ,
00464     },
00465     {
00466         { ADD_LEN( OID_EC_GRP_BP256R1 ),   "brainpoolP256r1","brainpool256r1" },
00467         POLARSSL_ECP_DP_BP256R1 ,
00468     },
00469     {
00470         { ADD_LEN( OID_EC_GRP_BP384R1 ),   "brainpoolP384r1","brainpool384r1" },
00471         POLARSSL_ECP_DP_BP384R1 ,
00472     },
00473     {
00474         { ADD_LEN( OID_EC_GRP_BP512R1 ),   "brainpoolP512r1","brainpool512r1" },
00475         POLARSSL_ECP_DP_BP512R1 ,
00476     },
00477     {
00478         { NULL, 0, NULL, NULL },
00479         POLARSSL_ECP_DP_NONE,
00480     },
00481 };
00482 
00483 FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp);
00484 FN_OID_GET_ATTR1(oid_get_ec_grp, oid_ecp_grp_t, grp_id, ecp_group_id, grp_id);
00485 FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, ecp_group_id, grp_id);
00486 #endif /* POLARSSL_ECP_C */
00487 
00488 #if defined(POLARSSL_CIPHER_C)
00489 /*
00490  * For PKCS#5 PBES2 encryption algorithm
00491  */
00492 typedef struct {
00493     oid_descriptor_t    descriptor;
00494     cipher_type_t       cipher_alg;
00495 } oid_cipher_alg_t;
00496 
00497 static const oid_cipher_alg_t oid_cipher_alg[] =
00498 {
00499     {
00500         { ADD_LEN( OID_DES_CBC ),              "desCBC",       "DES-CBC" },
00501         POLARSSL_CIPHER_DES_CBC,
00502     },
00503     {
00504         { ADD_LEN( OID_DES_EDE3_CBC ),         "des-ede3-cbc", "DES-EDE3-CBC" },
00505         POLARSSL_CIPHER_DES_EDE3_CBC,
00506     },
00507     {
00508         { NULL, 0, NULL, NULL },
00509         POLARSSL_CIPHER_NONE,
00510     },
00511 };
00512 
00513 FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
00514 FN_OID_GET_ATTR1(oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, cipher_type_t, cipher_alg);
00515 #endif /* POLARSSL_CIPHER_C */
00516 
00517 #if defined(POLARSSL_MD_C)
00518 /*
00519  * For digestAlgorithm
00520  */
00521 typedef struct {
00522     oid_descriptor_t    descriptor;
00523     md_type_t           md_alg;
00524 } oid_md_alg_t;
00525 
00526 static const oid_md_alg_t oid_md_alg[] =
00527 {
00528     {
00529         { ADD_LEN( OID_DIGEST_ALG_MD2 ),       "id-md2",       "MD2" },
00530         POLARSSL_MD_MD2,
00531     },
00532     {
00533         { ADD_LEN( OID_DIGEST_ALG_MD4 ),       "id-md4",       "MD4" },
00534         POLARSSL_MD_MD4,
00535     },
00536     {
00537         { ADD_LEN( OID_DIGEST_ALG_MD5 ),       "id-md5",       "MD5" },
00538         POLARSSL_MD_MD5,
00539     },
00540     {
00541         { ADD_LEN( OID_DIGEST_ALG_SHA1 ),      "id-sha1",      "SHA-1" },
00542         POLARSSL_MD_SHA1,
00543     },
00544     {
00545         { ADD_LEN( OID_DIGEST_ALG_SHA224 ),    "id-sha224",    "SHA-224" },
00546         POLARSSL_MD_SHA224,
00547     },
00548     {
00549         { ADD_LEN( OID_DIGEST_ALG_SHA256 ),    "id-sha256",    "SHA-256" },
00550         POLARSSL_MD_SHA256,
00551     },
00552     {
00553         { ADD_LEN( OID_DIGEST_ALG_SHA384 ),    "id-sha384",    "SHA-384" },
00554         POLARSSL_MD_SHA384,
00555     },
00556     {
00557         { ADD_LEN( OID_DIGEST_ALG_SHA512 ),    "id-sha512",    "SHA-512" },
00558         POLARSSL_MD_SHA512,
00559     },
00560     {
00561         { NULL, 0, NULL, NULL },
00562         POLARSSL_MD_NONE,
00563     },
00564 };
00565 
00566 FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
00567 FN_OID_GET_ATTR1(oid_get_md_alg, oid_md_alg_t, md_alg, md_type_t, md_alg);
00568 FN_OID_GET_OID_BY_ATTR1(oid_get_oid_by_md, oid_md_alg_t, oid_md_alg, md_type_t, md_alg);
00569 #endif /* POLARSSL_MD_C */
00570 
00571 #if defined(POLARSSL_PKCS12_C)
00572 /*
00573  * For PKCS#12 PBEs
00574  */
00575 typedef struct {
00576     oid_descriptor_t    descriptor;
00577     md_type_t           md_alg;
00578     cipher_type_t       cipher_alg;
00579 } oid_pkcs12_pbe_alg_t;
00580 
00581 static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
00582 {
00583     {
00584         { ADD_LEN( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC ), "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
00585         POLARSSL_MD_SHA1,      POLARSSL_CIPHER_DES_EDE3_CBC,
00586     },
00587     {
00588         { ADD_LEN( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC ), "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
00589         POLARSSL_MD_SHA1,      POLARSSL_CIPHER_DES_EDE_CBC,
00590     },
00591     {
00592         { NULL, 0, NULL, NULL },
00593         POLARSSL_MD_NONE, POLARSSL_CIPHER_NONE,
00594     },
00595 };
00596 
00597 FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg);
00598 FN_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);
00599 #endif /* POLARSSL_PKCS12_C */
00600 
00601 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
00602     !defined(EFI32)
00603 #include <stdarg.h>
00604 
00605 #if !defined vsnprintf
00606 #define vsnprintf _vsnprintf
00607 #endif // vsnprintf
00608 
00609 /*
00610  * Windows _snprintf and _vsnprintf are not compatible to linux versions.
00611  * Result value is not size of buffer needed, but -1 if no fit is possible.
00612  *
00613  * This fuction tries to 'fix' this by at least suggesting enlarging the
00614  * size by 20.
00615  */
00616 static int compat_snprintf( char *str, size_t size, const char *format, ... )
00617 {
00618     va_list ap;
00619     int res = -1;
00620 
00621     va_start( ap, format );
00622 
00623     res = vsnprintf( str, size, format, ap );
00624 
00625     va_end( ap );
00626 
00627     // No quick fix possible
00628     if( res < 0 )
00629         return( (int) size + 20 );
00630 
00631     return( res );
00632 }
00633 
00634 #define snprintf compat_snprintf
00635 #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
00636 
00637 #define SAFE_SNPRINTF()                             \
00638 {                                                   \
00639     if( ret == -1 )                                 \
00640         return( POLARSSL_ERR_OID_BUF_TOO_SMALL );   \
00641                                                     \
00642     if( (unsigned int) ret >= n ) {                 \
00643         p[n - 1] = '\0';                            \
00644         return( POLARSSL_ERR_OID_BUF_TOO_SMALL );   \
00645     }                                               \
00646                                                     \
00647     n -= (unsigned int) ret;                        \
00648     p += (unsigned int) ret;                        \
00649 }
00650 
00651 /* Return the x.y.z.... style numeric string for the given OID */
00652 int oid_get_numeric_string( char *buf, size_t size,
00653                             const asn1_buf *oid )
00654 {
00655     int ret;
00656     size_t i, n;
00657     unsigned int value;
00658     char *p;
00659 
00660     p = buf;
00661     n = size;
00662 
00663     /* First byte contains first two dots */
00664     if( oid->len > 0 )
00665     {
00666         ret = polarssl_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
00667         SAFE_SNPRINTF();
00668     }
00669 
00670     value = 0;
00671     for( i = 1; i < oid->len; i++ )
00672     {
00673         /* Prevent overflow in value. */
00674         if( ( ( value << 7 ) >> 7 ) != value )
00675             return( POLARSSL_ERR_OID_BUF_TOO_SMALL );
00676 
00677         value <<= 7;
00678         value += oid->p[i] & 0x7F;
00679 
00680         if( !( oid->p[i] & 0x80 ) )
00681         {
00682             /* Last byte */
00683             ret = polarssl_snprintf( p, n, ".%d", value );
00684             SAFE_SNPRINTF();
00685             value = 0;
00686         }
00687     }
00688 
00689     return( (int) ( size - n ) );
00690 }
00691 
00692 #endif /* POLARSSL_OID_C */
00693