security/nss/cmd/libpkix/sample_apps/dumpcert.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/dumpcert.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     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 + * dumpcert.c
     1.9 + *
    1.10 + * dump certificate 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:\tdumpcert <certFile>\n");
    1.32 +        (void) printf("\tParses a certificate located at <certFile> "
    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_Cert *
    1.42 +createCert(char *inFileName)
    1.43 +{
    1.44 +        PKIX_PL_ByteArray *byteArray = NULL;
    1.45 +        PKIX_PL_Cert *cert = NULL;
    1.46 +        PKIX_Error *error = NULL;
    1.47 +        PRFileDesc *inFile = NULL;
    1.48 +        SECItem certDER;
    1.49 +        void *buf = NULL;
    1.50 +        PKIX_UInt32 len;
    1.51 +        SECStatus rv = SECFailure;
    1.52 +
    1.53 +        certDER.data = NULL;
    1.54 +
    1.55 +        inFile = PR_Open(inFileName, PR_RDONLY, 0);
    1.56 +
    1.57 +        if (!inFile){
    1.58 +                printFailure("Unable to open cert file");
    1.59 +                goto cleanup;
    1.60 +        } else {
    1.61 +                rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE, PR_FALSE);
    1.62 +                if (!rv){
    1.63 +                        buf = (void *)certDER.data;
    1.64 +                        len = certDER.len;
    1.65 +
    1.66 +                        error = PKIX_PL_ByteArray_Create
    1.67 +                                (buf, len, &byteArray, plContext);
    1.68 +
    1.69 +                        if (error){
    1.70 +                                printFailure("PKIX_PL_ByteArray_Create failed");
    1.71 +                                goto cleanup;
    1.72 +                        }
    1.73 +
    1.74 +                        error = PKIX_PL_Cert_Create
    1.75 +                                (byteArray, &cert, plContext);
    1.76 +
    1.77 +                        if (error){
    1.78 +                                printFailure("PKIX_PL_Cert_Create failed");
    1.79 +                                goto cleanup;
    1.80 +                        }
    1.81 +                } else {
    1.82 +                        printFailure("Unable to read DER from cert file");
    1.83 +                        goto cleanup;
    1.84 +                }
    1.85 +        }
    1.86 +
    1.87 +cleanup:
    1.88 +
    1.89 +        if (inFile){
    1.90 +                PR_Close(inFile);
    1.91 +        }
    1.92 +
    1.93 +        if (rv == SECSuccess){
    1.94 +                SECITEM_FreeItem(&certDER, PR_FALSE);
    1.95 +        }
    1.96 +
    1.97 +        if (byteArray){
    1.98 +                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
    1.99 +        }
   1.100 +
   1.101 +        return (cert);
   1.102 +}
   1.103 +
   1.104 +int dumpcert(int argc, char *argv[])
   1.105 +{
   1.106 +
   1.107 +        PKIX_PL_String *string = NULL;
   1.108 +        PKIX_PL_Cert *cert = NULL;
   1.109 +        PKIX_Error *error = NULL;
   1.110 +        char *ascii = NULL;
   1.111 +        PKIX_UInt32 length = 0;
   1.112 +        PKIX_UInt32 j = 0;
   1.113 +	PKIX_Boolean useArenas = PKIX_FALSE;
   1.114 +        PKIX_UInt32 actualMinorVersion;
   1.115 +
   1.116 +        PKIX_TEST_STD_VARS();
   1.117 +
   1.118 +        if (argc == 1){
   1.119 +                printUsage();
   1.120 +                return (0);
   1.121 +        }
   1.122 +
   1.123 +        useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
   1.124 +
   1.125 +        PKIX_Initialize
   1.126 +                (PKIX_TRUE, /* nssInitNeeded */
   1.127 +                useArenas,
   1.128 +                PKIX_MAJOR_VERSION,
   1.129 +                PKIX_MINOR_VERSION,
   1.130 +                PKIX_MINOR_VERSION,
   1.131 +                &actualMinorVersion,
   1.132 +                &plContext);
   1.133 +
   1.134 +        cert = createCert(argv[1+j]);
   1.135 +
   1.136 +        if (cert){
   1.137 +
   1.138 +                error = PKIX_PL_Object_ToString
   1.139 +                        ((PKIX_PL_Object *)cert, &string, plContext);
   1.140 +
   1.141 +                if (error){
   1.142 +                        printFailure("Unable to get string representation "
   1.143 +                                    "of cert");
   1.144 +                        goto cleanup;
   1.145 +                }
   1.146 +
   1.147 +                error = PKIX_PL_String_GetEncoded
   1.148 +                        (string,
   1.149 +                        PKIX_ESCASCII,
   1.150 +                        (void **)&ascii,
   1.151 +                        &length,
   1.152 +                        plContext);
   1.153 +
   1.154 +                if (error || !ascii){
   1.155 +                        printFailure("Unable to get ASCII encoding of string");
   1.156 +                        goto cleanup;
   1.157 +                }
   1.158 +
   1.159 +                (void) printf("OUTPUT:\n%s\n", ascii);
   1.160 +
   1.161 +        } else {
   1.162 +                printFailure("Unable to create certificate");
   1.163 +                goto cleanup;
   1.164 +        }
   1.165 +
   1.166 +cleanup:
   1.167 +
   1.168 +        if (cert){
   1.169 +                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(cert), plContext);
   1.170 +        }
   1.171 +
   1.172 +        if (string){
   1.173 +                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
   1.174 +        }
   1.175 +
   1.176 +        if (ascii){
   1.177 +                PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
   1.178 +        }
   1.179 +
   1.180 +        PKIX_Shutdown(plContext);
   1.181 +
   1.182 +        PKIX_TEST_RETURN();
   1.183 +
   1.184 +        endTests("DUMPCERT");
   1.185 +
   1.186 +        return (0);
   1.187 +}

mercurial