Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

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