RSA BSAFE Micro Edition Suite

Streamlined security for mobile and embedded devices

Search  Print

cm_type.c

/* $Id: cm_type.c,v 1.12 2005/07/27 01:10:33 itaylor 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.
 *
 *
 */

/*
 * This sample demonstrates identifying the content type identifier of a
 * supplied PKCS #7 message. The toolkit supports data, signed data and
 * enveloped data message types.
 *
 * For information detailing generating PKCS #7 signed messages,
 * see cm_sign.c.
 *
 * For example, to output the content type of an input cryptographic:
 * message, use:
 * 
 *   cm_type -cm_msg signed.data
 */

#include "r_prod.h"
#include "cm_com.h"

#define CM_SMPL_RESOURCE_LIST PRODUCT_DEFAULT_RESOURCE_LIST

/* Usage help message. */
static char *cm_type_usage[] =
{
    "usage: cm_type [options]\n",
    "where options are:\n",
    " -cm_msg file         - The file containing the cryptographic message\n",
    NULL
};

/*
 * Main sample program entry point
 *
 * @param argc  [In]  The number of arguments typed on the command line.
 * @param argv  [In]  The array of individual arguments from the command line.
 *
 * @returns  R_ERROR_NONE indicates success.
 */
int main(int argc, char **argv)
{
    R_LIB_CTX        *lib_ctx   = NULL;
    R_CM_CTX         *ctx       = NULL;
    R_CM             *obj       = NULL;
    BIO              *bio_out   = NULL;
    R_ITEM            data      = { 0, NULL };
    char             *cm_file;
    unsigned char    *cm_data   = NULL;
    unsigned char    type[R_CM_TYPE_MAX_STR_LENGTH];
    unsigned int      num_used;
    unsigned int      cm_len;
    int               ret       = R_ERROR_NONE;
    R_CM_TYPE         data_type;

    /* Set the defaults */
    cm_file   = NULL;

    /*
     * Create BIO to stdout
     * BIOs are the Basic Input/Output mechanism provided by RSA and are
     * recommended for all input and output from applications.
     */
    if ((bio_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL)
    {
        ret = R_ERROR_ALLOC_FAILURE;
        goto end;
    }

    /*
     * Parse the command line parameters
     */
    /* Skip the program name */
    argc--;
    argv++;

    /* Process all command line options */
    while (argc >= 1)
    {
        if (Strcmp(*argv, "-cm_msg") == 0)
        {
            if (--argc < 1)
            {
                goto bad;
            }
            cm_file = *(++argv);
        }
        else
        {
            BIO_printf(bio_out, "Unknown option %s\n", *argv);
            goto bad;
        }
        argc--;
        argv++;
    }

    /* Simple checks first */
    if (cm_file == NULL)
    {
        BIO_printf(bio_out, "Input file required\n");
        goto bad;
    }

    /* Display the help menu if an invalid command line option was entered */
    if (0)
    {
        char **pp;
bad:
        for (pp = cm_type_usage; (*pp != NULL); pp++)
        {
            BIO_printf(bio_out, *pp);
        }
        goto end;
    }

    /*
     * Create the library context.
     * Retrieve the default resource list and create a library context to
     * provide access to all configurable aspects of the library.
     */
    if ((ret = PRODUCT_LIBRARY_NEW(CM_SMPL_RESOURCE_LIST(),
        R_RES_FLAG_DEF, &lib_ctx)) != R_ERROR_NONE)
    {
        BIO_printf(bio_out, "Library new failure\n");
        goto end;
    }

    /*
     * Create a new R_CM context.
     */
    if ((ret = R_CM_CTX_new(lib_ctx, R_RES_FLAG_DEF, R_CM_TYPE_DEFAULT,
        &ctx)) != R_ERROR_NONE)
    {
        BIO_printf(bio_out, "R_CM_CTX_new failure\n");
        goto end;
    }

    /*
     * Read the data from cryptographic message file into the buffer.
     * The cryptographic message is in binary form and simply copied to a
     * buffer pointed to by cm_data.
     */
    if ((ret = data_from_file(bio_out, cm_file, &cm_data, &cm_len)) !=
        R_ERROR_NONE)
    {
        goto end;
    }

    /*
     * Load the cryptographic message buffer into a cryptographic
     * message object.
     */
    if ((ret = R_CM_from_binary(ctx, R_FLAG_SHARE_DATA, R_CM_TYPE_UNKNOWN,
        R_CM_ENCODING_FORMAT_WRAPPED, cm_len, cm_data, &num_used, &obj)) !=
        R_ERROR_NONE)
    {
        BIO_printf(bio_out, "From binary failure\n");
        goto end;
    }

    /*
     * Determine the content type of the cryptographic message.
     * The content type of the cryptographic message is used to determine
     * how to handle the attached data within the cryptographic message.
     */
    if ((ret = R_CM_get_info(obj, R_CM_INFO_TYPE, &data_type)) != R_ERROR_NONE)
    {
        BIO_printf(bio_out, "Retrieve content type failure\n");
        goto end;
    }
    else
    {
        if ((ret = R_CM_TYPE_to_string(data_type, sizeof(type),
            (char *)type)) != R_ERROR_NONE)
        {
            BIO_printf(bio_out, "Content type string conversion failure\n");
        }
        else
        {
            BIO_printf(bio_out, "Content Type: %s\n",type);
        }
    }

end:

    /*
     * Clean up.
     * Report errors if there is an output stream using both the error and the
     * string representation.
     * Destroy the dynamically allocated objects and return an exit code.
     */
    if ((ret != R_ERROR_NONE) && (bio_out != NULL))
    {
        BIO_printf(bio_out, "ERROR: (%d) %s\n", ret,
            R_LIB_CTX_get_error_string(lib_ctx, R_RES_MOD_ID_LIBRARY, ret));
    }

    if (cm_data != NULL)
    {
        Free(cm_data);
    }
    if (data.data != NULL)
    {
        Free(data.data);
    }
    if (obj != NULL)
    {
        R_CM_free(obj);
    }
    if (ctx != NULL)
    {
        R_CM_CTX_free(ctx);
    }
    if (lib_ctx != NULL)
    {
        PRODUCT_LIBRARY_FREE(lib_ctx);
    }
    if (bio_out != NULL)
    {
        BIO_free(bio_out);
    }

    return(R_ERROR_EXIT_CODE(ret));
}


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