security/nss/cmd/libpkix/sample_apps/dumpcrl.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/libpkix/sample_apps/dumpcrl.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +/*
     1.8 + * dumpcrl.c
     1.9 + *
    1.10 + * dump CRL sample application
    1.11 + *
    1.12 + */
    1.13 +
    1.14 +#include <stdio.h>
    1.15 +
    1.16 +#include "pkix.h"
    1.17 +#include "testutil.h"
    1.18 +#include "prlong.h"
    1.19 +#include "plstr.h"
    1.20 +#include "prthread.h"
    1.21 +#include "plarena.h"
    1.22 +#include "seccomon.h"
    1.23 +#include "secdert.h"
    1.24 +#include "secasn1t.h"
    1.25 +#include "certt.h"
    1.26 +
    1.27 +static void *plContext = NULL;
    1.28 +
    1.29 +static 
    1.30 +void printUsage(void){
    1.31 +        (void) printf("\nUSAGE:\tdumpcrl <crlFile>\n");
    1.32 +        (void) printf("\tParses a CRL located at <crlFile> "
    1.33 +                "and displays it.\n");
    1.34 +}
    1.35 +
    1.36 +static 
    1.37 +void printFailure(char *msg){
    1.38 +        (void) printf("FAILURE: %s\n", msg);
    1.39 +}
    1.40 +
    1.41 +static PKIX_PL_CRL *
    1.42 +createCRL(char *inFileName)
    1.43 +{
    1.44 +        PKIX_PL_ByteArray *byteArray = NULL;
    1.45 +        PKIX_PL_CRL *crl = NULL;
    1.46 +        PKIX_Error *error = NULL;
    1.47 +        PRFileDesc *inFile = NULL;
    1.48 +        SECItem crlDER;
    1.49 +        void *buf = NULL;
    1.50 +        PKIX_UInt32 len;
    1.51 +        SECStatus rv;
    1.52 +
    1.53 +        PKIX_TEST_STD_VARS();
    1.54 +
    1.55 +        crlDER.data = NULL;
    1.56 +
    1.57 +        inFile = PR_Open(inFileName, PR_RDONLY, 0);
    1.58 +
    1.59 +        if (!inFile){
    1.60 +                printFailure("Unable to open crl file");
    1.61 +                goto cleanup;
    1.62 +        } else {
    1.63 +                rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE, PR_FALSE);
    1.64 +                if (!rv){
    1.65 +                        buf = (void *)crlDER.data;
    1.66 +                        len = crlDER.len;
    1.67 +
    1.68 +                        error = PKIX_PL_ByteArray_Create
    1.69 +                                (buf, len, &byteArray, plContext);
    1.70 +
    1.71 +                        if (error){
    1.72 +                                printFailure("PKIX_PL_ByteArray_Create failed");
    1.73 +                                goto cleanup;
    1.74 +                        }
    1.75 +
    1.76 +                        error = PKIX_PL_CRL_Create(byteArray, &crl, plContext);
    1.77 +                        if (error){
    1.78 +                                printFailure("PKIX_PL_CRL_Create failed");
    1.79 +                                goto cleanup;
    1.80 +                        }
    1.81 +
    1.82 +                        SECITEM_FreeItem(&crlDER, PR_FALSE);
    1.83 +                } else {
    1.84 +                        printFailure("Unable to read DER from crl file");
    1.85 +                        goto cleanup;
    1.86 +                }
    1.87 +        }
    1.88 +
    1.89 +cleanup:
    1.90 +
    1.91 +        if (inFile){
    1.92 +                PR_Close(inFile);
    1.93 +        }
    1.94 +
    1.95 +        if (error){
    1.96 +                SECITEM_FreeItem(&crlDER, PR_FALSE);
    1.97 +        }
    1.98 +
    1.99 +        if (byteArray){
   1.100 +                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
   1.101 +        }
   1.102 +
   1.103 +        PKIX_TEST_RETURN();
   1.104 +
   1.105 +        return (crl);
   1.106 +}
   1.107 +
   1.108 +int dumpcrl(int argc, char *argv[])
   1.109 +{
   1.110 +
   1.111 +        PKIX_PL_String *string = NULL;
   1.112 +        PKIX_PL_CRL *crl = NULL;
   1.113 +        PKIX_Error *error = NULL;
   1.114 +        char *ascii = NULL;
   1.115 +        PKIX_UInt32 length;
   1.116 +        PKIX_UInt32 actualMinorVersion;
   1.117 +        PKIX_UInt32 j = 0;
   1.118 +        PKIX_Boolean useArenas = PKIX_FALSE;
   1.119 +
   1.120 +        PKIX_TEST_STD_VARS();
   1.121 +
   1.122 +        if (argc == 1){
   1.123 +                printUsage();
   1.124 +                return (0);
   1.125 +        }
   1.126 +
   1.127 +        useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
   1.128 +
   1.129 +        PKIX_Initialize
   1.130 +                (PKIX_TRUE, /* nssInitNeeded */
   1.131 +                useArenas,
   1.132 +                PKIX_MAJOR_VERSION,
   1.133 +                PKIX_MINOR_VERSION,
   1.134 +                PKIX_MINOR_VERSION,
   1.135 +                &actualMinorVersion,
   1.136 +                &plContext);
   1.137 +
   1.138 +        crl = createCRL(argv[j+1]);
   1.139 +
   1.140 +        if (crl){
   1.141 +
   1.142 +                error = PKIX_PL_Object_ToString
   1.143 +                        ((PKIX_PL_Object *)crl, &string, plContext);
   1.144 +
   1.145 +                if (error){
   1.146 +                        printFailure("Unable to get string representation "
   1.147 +                                    "of crl");
   1.148 +                        goto cleanup;
   1.149 +                }
   1.150 +
   1.151 +                error = PKIX_PL_String_GetEncoded
   1.152 +                        (string,
   1.153 +                        PKIX_ESCASCII,
   1.154 +                        (void **)&ascii,
   1.155 +                        &length,
   1.156 +                        plContext);
   1.157 +                if (error || !ascii){
   1.158 +                        printFailure("Unable to get ASCII encoding of string");
   1.159 +                        goto cleanup;
   1.160 +                }
   1.161 +
   1.162 +                (void) printf("OUTPUT:\n%s\n", ascii);
   1.163 +
   1.164 +        } else {
   1.165 +                printFailure("Unable to create CRL");
   1.166 +                goto cleanup;
   1.167 +        }
   1.168 +
   1.169 +cleanup:
   1.170 +
   1.171 +        if (crl){
   1.172 +                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(crl), plContext);
   1.173 +        }
   1.174 +
   1.175 +        if (string){
   1.176 +                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
   1.177 +        }
   1.178 +
   1.179 +        if (ascii){
   1.180 +                PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
   1.181 +        }
   1.182 +
   1.183 +        PKIX_Shutdown(plContext);
   1.184 +
   1.185 +        PKIX_TEST_RETURN();
   1.186 +
   1.187 +        endTests("DUMPCRL");
   1.188 +
   1.189 +        return (0);
   1.190 +}

mercurial