RSA BSAFE Micro Edition Suite

Streamlined security for mobile and embedded devices

Search  Print

reslist_set.c

/* $Id: reslist_set.c,v 1.6 2005/04/26 06:06:08 dvantwes Exp $ */
/*
 * Copyright (C) 1998-2003 RSA Security Inc.
 *
 * This file shall only be used to demonstrate how to interface to an
 * RSA Security Inc. licensed development product.
 *
 * You have a royalty-free right to use, reproduce and distribute this
 * demonstration file, provided that you agree that RSA Security Inc.
 * has no warranty, implied or otherwise, or liability for this
 * demonstration file (including any modified version).  This software
 * is provided "as is" without warranties or representations of any
 * kind. RSA Security disclaims all conditions and warranties, statutory
 * and otherwise, both express and implied, with respect to the software,
 * its quality and performance, including but not limited to, all
 * implied warranties of merchantability, fitness for a particular
 * purpose, title and noninfringement of third party rights. Without
 * limiting the foregoing, RSA Security does not warrant that the
 * software is error-free or that errors in the product will be
 * corrected. You agree that RSA Security shall not be liable for any
 * direct, indirect, incidental, special, consequential, punitive or
 * other damages whatsoever resulting from your use of this software
 * or any modified version.
 *
 *
 */


#include "r_prod.h"


/*
 * Macro constants
 */

/* defines for resource list types */
#define DEFAULT_RESLIST 1
#define CERTCME_RESLIST 2
#define SSLCME_RESLIST 3
#define TINY_RESLIST 4

static char* mod_id_strings[] ={
  " R_RES_MOD_ID_RES_LIST",
  " R_RES_MOD_ID_LIBRARY",
  " R_RES_MOD_ID_MEM",
  " R_RES_MOD_ID_LOCK",
  " R_RES_MOD_ID_ERR",
  " R_RES_MOD_ID_TIME",
  " R_RES_MOD_ID_CRYPTO",
  " R_RES_MOD_ID_WTLSC_CRYPTO",
  " R_RES_MOD_ID_CACHE",
  " R_RES_MOD_ID_SKEY",
  " R_RES_MOD_ID_CERT",
  " R_RES_MOD_ID_COMP",
  " R_RES_MOD_ID_KGEN",
  " R_RES_MOD_ID_CGEN",
  " R_RES_MOD_ID_VERSION",
  " R_RES_MOD_ID_WTLS",
  " R_RES_MOD_ID_PKEY",
  " R_RES_MOD_ID_PEM",
  " R_RES_MOD_ID_FEATURE",
  " R_RES_MOD_ID_DEBUG",
  " R_RES_MOD_ID_PASSWD",
  " R_RES_MOD_ID_SIO",
  " R_RES_MOD_ID_VERIFY",
  " R_RES_MOD_ID_RAND",
  " R_RES_MOD_ID_BUFFER",
  " R_RES_MOD_ID_WMLS",
  " R_RES_MOD_ID_CERT_STORE",
  " R_RES_MOD_ID_CM",
  " R_RES_MOD_ID_SSL",
  " R_RES_MOD_ID_CERT_REQ",
  " R_RES_MOD_ID_CRL",
  " R_RES_MOD_ID_BIO",
  " R_RES_MOD_ID_FIPS",
  " R_RES_MOD_ID_HW",
  " R_RES_MOD_ID_EVP",
  " R_RES_MOD_ID_HW_SEARCH",
  " R_RES_MOD_ID_HW_CERT",
  " R_RES_MOD_ID_HW_LOGIN",
  " R_RES_MOD_ID_HW_LOGIN_CB",
  " R_RES_MOD_ID_HW_SESSION_POOL",
  " R_RES_MOD_ID_SPE",
  " R_RES_MOD_ID_COLLECTION",
  " R_RES_MOD_ID_ITERATOR",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN",
  " R_RES_MOD_ID_UNKNOWN"
};

/* Implementation ID defines */
static char* impl_id_strings[] ={
  " R_RES_IMPL_ID_DEF",
  " R_RES_IMPL_ID_ALL",
  " R_RES_IMPL_ID_WTLSC",
  " R_RES_IMPL_ID_SSLC",
  " R_RES_IMPL_ID_WMLSC",
  " R_RES_IMPL_ID_CIPHER_ALG_FUNC",
  " R_RES_IMPL_ID_PRINT_FUNC",
  " R_RES_IMPL_ID_VFY_DETAILS_TIME_FUNC",
  " R_RES_IMPL_ID_VFY_DETAILS_FUNC",
  " R_RES_IMPL_ID_CREATE_STATE_FUNC",
  " R_RES_IMPL_ID_VERIFY_STATE_FUNC",
  " R_RES_IMPL_ID_PROTOCOL",
  " R_RES_IMPL_ID_PROTOCOL_METHOD",
  " R_RES_IMPL_ID_CIPHER_MAP",
  " R_RES_IMPL_ID_CIPHER_FUNC",
  " R_RES_IMPL_ID_CRYPTO_METHOD",
  " R_RES_IMPL_ID_VFY_EXTENSIONS",
  " R_RES_IMPL_ID_VFY_CRL_FUNC",
  " R_RES_IMPL_ID_HDW",
  " R_RES_IMPL_ID_FIPS140",
  " R_RES_IMPL_ID_STD",
  " R_RES_IMPL_ID_DUP_FUNC",
  " R_RES_IMPL_ID_HW_CONFIG_FUNC",
  " R_RES_IMPL_ID_UNKNOWN",
  " R_RES_IMPL_ID_UNKNOWN",
  " R_RES_IMPL_ID_UNKNOWN",
  " R_RES_IMPL_ID_UNKNOWN",
  " R_RES_IMPL_ID_UNKNOWN",
  " R_RES_IMPL_ID_UNKNOWN",
  " R_RES_IMPL_ID_UNKNOWN"
};

/* Sub ID defines */
static char* sub_id_strings[] ={
  " R_RES_SUB_ID_DEF",
  " R_RES_SUB_ID_ALL",
  " R_RES_SUB_ID_ERROR_STRINGS",
  " R_RES_SUB_ID_REASON_STRINGS",
  " R_RES_SUB_ID_DETAIL_STRINGS",
  " R_RES_SUB_ID_FUNCTION_STRINGS",
  " R_RES_SUB_ID_MAP",
  " R_RES_SUB_ID_SSL2_SERVER",
  " R_RES_SUB_ID_SSL2_CLIENT",
  " R_RES_SUB_ID_SSL2_BOTH",
  " R_RES_SUB_ID_SSL3_SERVER",
  " R_RES_SUB_ID_SSL3_CLIENT",
  " R_RES_SUB_ID_SSL3_BOTH",
  " R_RES_SUB_ID_TLS1_SERVER",
  " R_RES_SUB_ID_TLS1_CLIENT",
  " R_RES_SUB_ID_TLS1_BOTH",
  " R_RES_SUB_ID_SSL23_SERVER",
  " R_RES_SUB_ID_SSL23_CLIENT",
  " R_RES_SUB_ID_SSL23_BOTH",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN",
  " R_RES_SUB_ID_UNKNOWN"
};

int main(int argc, char *argv[])
{
    int ret = R_ERROR_FAILED;                        /* Program return code */
    R_LIB_CTX *lib_ctx = NULL;                           /* Library context */
    int badop = 0;         /* Indicates if a command line argument is wrong */
    FILE *out;                                         /* Standard out file */
    FILE *err;                                       /* Standard error file */
    int               reslist_type = 0;

    BIO              *bio_out = NULL;
    BIO              *bio_err = NULL;
    R_RES_LIST       *resource_list = NULL;
    R_RES_LIST       *myResourceList = NULL;
    R_RES_ITEM       *myResourceItem = NULL;
    int            num_modids=(int)(sizeof(mod_id_strings)/sizeof(char*));
    int            num_implids=(int)(sizeof(impl_id_strings)/sizeof(char*));
    int            num_subids=(int)(sizeof(sub_id_strings)/sizeof(char*));

    /*
     * Create BIOs to stderr and stdout. BIOs are the Basic Input/Output
     * mechanism provided by RSA and are recommended for all input and output
     * from applications.
     */
    bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    if ((bio_err == NULL) || (bio_out == NULL))
    {
        ret = R_ERROR_ALLOC_FAILURE;
        goto end;
    }

    BIO_set_flags(bio_err, BIO_FLAGS_FLUSH_ON_WRITE);
    BIO_set_flags(bio_out, BIO_FLAGS_FLUSH_ON_WRITE);

    /* Standard out and error file pointer assignments */
    out = stdout;
    err = stderr;

    /* Ignore the program name in the command line arguments */
    argc--;
    argv++;

    if (argc == 0)
    {
        fprintf(err, "No options\n\n");
        fflush(err);
        badop = 1;
    }

    /* Process the program options */
    while (argc >= 1)
    {
        if (Strcmp(*argv, "-default") == 0)
        {
            reslist_type = 1;
        }
        else if (Strcmp(*argv, "-certcme") == 0)
        {
            reslist_type = 2;
        }
        else if (Strcmp(*argv, "-sslcme") == 0)
        {
            reslist_type = 3;
        }
        else if (Strcmp(*argv, "-tiny") == 0)
        {
            reslist_type = 4;
        }
        else
        {
            /*
             * All other options (include -h, -?, -help, etc.) are
             * unknown are will result in a usage message and program exit
             */
            fprintf(err, "Unknown option %s\n", *argv);
            fflush(err);
            badop = 1;
            break;
        }

        /* Decrement the argument counter and move to the next argument */
        argc--;
        argv++;
    }

    /* Deal with problems with the command line arguments */
    if (badop)
    {
        /* Print a usage message */
        fprintf(err, "usage: reslist_test [options]\n");
        fprintf(err, " -default\n");
        fprintf(err, " -sslcme\n");
        fprintf(err, " -certcme\n");
        fprintf(err, " -tiny\n");
        fflush(err);

        /* Exit the program */
        goto end;
    }

    /* Choose the actual resource list to use */
    switch(reslist_type)
    {
    case 1:
        resource_list = PRODUCT_DEFAULT_RESOURCE_LIST();
        break;
    case 2:
        resource_list = PRODUCT_CERTCME_RESOURCE_LIST();
        break;
    case 3:
        resource_list = PRODUCT_SSLCME_RESOURCE_LIST();
        break;
    case 4:
        resource_list = PRODUCT_TINY_RESOURCE_LIST();
        break;
    }

    /* Initialize the MES library using selected resources */
    if (PRODUCT_LIBRARY_NEW(resource_list, R_RES_FLAG_DEF, &lib_ctx) !=
        R_ERROR_NONE)
    {
        fprintf(err, "Unable to create library context\n");
        fflush(err);
        goto end;
    }


    /* Get the resource list that was used to initialize the library */
    ret = R_LIB_CTX_get_info( lib_ctx, R_LIB_CTX_INFO_ID_RESOURCE_LIST,
                              &myResourceList );
    if (ret != R_ERROR_NONE)
    {
        BIO_printf(bio_out, "Unable to get the resource list from lib_ctx\n");
        goto end;
    }

    if (myResourceList == NULL)
    {
        BIO_printf(bio_out, "resource list from lib_ctx is null\n");
        return(R_ERROR_NULL_ARG);
    }

    /* Iterate through the resource list and print the modules present */
    do {

        if (myResourceList->mod_id == R_RES_MOD_ID_RES_LIST)
        {
            myResourceItem = NULL;
        }
        else
        {
            myResourceItem = myResourceList++;
            /* Analyze the resource item */
            BIO_printf(bio_out,
                       "resource list mod id: %d impl id: %d sub id: %d\n",
                       myResourceItem->mod_id,
                       myResourceItem->impl_id,
                       myResourceItem->sub_id);

            if( myResourceItem->mod_id!=R_RES_MOD_ID_BIO)
            {
              BIO_printf(bio_out, "\t%s %s %s\n",
                              myResourceItem->mod_id<=num_modids?
                         mod_id_strings[myResourceItem->mod_id]:"Not Defined",
                              myResourceItem->impl_id<=num_implids?
                         impl_id_strings[myResourceItem->impl_id]:"Not Defined",
                              myResourceItem->sub_id<=num_subids?
                         sub_id_strings[myResourceItem->sub_id]:"Not Defined");
            }
            else
            {
              /*
               * The R_RES_MOD_ID_BIO has ORed impl_ids and it is a bit
               * complex to try to work out a string representation.
               * It would be very useful to do this at some point as this
               * sample can be used to debug some resource list issues and
               * for general learning.
               */
              BIO_printf(bio_out,
                         "\t%s - No impl_id or sub_id strings available\n",
                         mod_id_strings[myResourceItem->mod_id]);
            }

        }
        BIO_printf(bio_out, "\n\n");
    } while( myResourceItem != NULL );

    ret = R_ERROR_NONE;

end:
    if (lib_ctx != NULL)
    {
        PRODUCT_LIBRARY_FREE(lib_ctx);
    }

    if (bio_out != NULL)
    {
        BIO_free(bio_out);
    }
    if (bio_err != NULL)
    {
        BIO_free(bio_err);
    }

    return(R_ERROR_EXIT_CODE(ret));
}

Copyright (c) 1999-2005 RSA Security Inc. All rights reserved. 072-001001-2100-001-000 - 2.1