RSA BSAFE Micro Edition Suite

Streamlined security for mobile and embedded devices

Search  Print

ocsp_resp_print.c

/* $Id: ocsp_resp_print.c,v 1.1.2.5 2005/11/11 05:09:17 patrick Exp $ */
/*
 * Copyright (C) 1998-2005 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.
 *
 *
 */

/*
 * @file ocsp_resp_print.c
 * This sample demonstrates how to print information about an OCSP response.
 *
 * For example, to:
 *
 * Print information about an OCSP response:
 *   ocsp_resp_print -resp response.data
 *
 * where: response.bin = The OCSP response file (binary).
 */

#include "r_prod.h"
#include "ocsp_com.h"

/* Usage help message. */
static char *ocsp_resp_print_usage[] =
{
    "usage: ocsp_resp_print [options]\n",
    "where options are:\n",
    " -resp file             - The file containing the OCSP response message\n",
    "\n",
    "For example, to:\n",
    "  Print information about an OCSP response:\n",
    "    ocsp_resp_print -resp response.bin\n",
    "\n",
    "where: response.bin    = The OCSP response file (binary).\n"
    "\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.
 *           See @ref R_ERROR_IDS for valid values.
 */
int main(int argc, char **argv)
{
    int               ret       = R_ERROR_NONE;
    R_LIB_CTX        *lib_ctx   = NULL;
    R_OCSP_CTX       *ocsp_ctx  = NULL;
    R_OCSP_RESP      *resp      = NULL;
    BIO              *bio_out   = NULL;
    char             *resp_file = NULL;
    unsigned char    *resp_data = NULL;
    unsigned int      resp_len;
    unsigned int      num_bytes;
    int               print_usage = 0;
    char            **pp;

    /*
     * Create a 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;
    }

    /* Skip the program name */
    argc--;
    argv++;

    /*
     * Parse the command line parameters
     */
    while (argc >= 1)
    {
        /* The OCSP response message file. */
        if (Strcmp(*argv, "-resp") == 0)
        {
            if (--argc < 1)
            {
                print_usage = 1;
                goto end;
            }
            resp_file = *(++argv);
        }
        /* Display the usage information. */
        else if (Strcmp(*argv, "-help") == 0)
        {
            print_usage = 1;
            goto end;
        }
        /* Unknown option. */
        else
        {
            BIO_printf(bio_out, "Unknown option %s\n", *argv);
            print_usage = 1;
            goto end;
        }
        argc--;
        argv++;
    }

    /* Check the parameters provide all the data needed to be able to perform
     * verification operations.
     */
    if (resp_file == NULL)
    {
        BIO_printf(bio_out, "Input file required\n");
        print_usage = 1;
        goto end;
    }

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

    /*************************************************************************
     * Step 2. Create a new OCSP context.
     * Create an R_OCSP_CTX with which to create the OCSP response object.
     *************************************************************************/
    if ((ret = R_OCSP_CTX_new(lib_ctx, R_RES_FLAG_DEF, &ocsp_ctx)) !=
        R_ERROR_NONE)
    {
        BIO_printf(bio_out, "R_OCSP_CTX_new failure\n");
        goto end;
    }

    /*************************************************************************
     * Step 3. Create an OCSP response object from the response file.
     * Load the data from the file and then use the binary data to create an
     * OCSP response object with the OCSP context.
     *************************************************************************/

    /*
     * Read the data from the binary OCSP response message file into the
     * buffer.
     */
    if ((ret = data_from_file(bio_out, resp_file, &resp_data, &resp_len)) !=
        R_ERROR_NONE)
    {
        goto end;
    }

     /*
      * Create an OCSP response object from the binary data.
      */
    if ((ret = R_OCSP_RESP_from_binary(ocsp_ctx, R_FLAG_SHARE_DATA,
        resp_len, resp_data, &num_bytes, &resp)) != R_ERROR_NONE)
    {
        BIO_printf(bio_out, "R_OCSP_RESP_from_binary failure\n");
        goto end;
    }

    /*************************************************************************
     * Step 4. Print out the OCSP response information.
     *************************************************************************/
        if ((ret = R_OCSP_RESP_write(resp, bio_out, R_FORMAT_TEXT,
        NULL)) != R_ERROR_NONE)
    {
        BIO_printf(bio_out, "R_OCSP_RESP_write failure\n");
        goto end;
    }


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

    /*************************************************************************
     * Step 5. 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 (resp_data != NULL)
    {
        Free(resp_data);
    }
    if (resp != NULL)
    {
        R_OCSP_RESP_free(resp);
    }
    if (ocsp_ctx != NULL)
    {
        R_OCSP_CTX_free(ocsp_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