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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4 /*
     5  * dumpcert.c
     6  *
     7  * dump certificate sample application
     8  *
     9  */
    11 #include <stdio.h>
    13 #include "pkix.h"
    14 #include "testutil.h"
    15 #include "prlong.h"
    16 #include "plstr.h"
    17 #include "prthread.h"
    18 #include "plarena.h"
    19 #include "seccomon.h"
    20 #include "secdert.h"
    21 #include "secasn1t.h"
    22 #include "certt.h"
    24 static void *plContext = NULL;
    26 static 
    27 void printUsage(void){
    28         (void) printf("\nUSAGE:\tdumpcert <certFile>\n");
    29         (void) printf("\tParses a certificate located at <certFile> "
    30                 "and displays it.\n");
    31 }
    33 static 
    34 void printFailure(char *msg){
    35         (void) printf("FAILURE: %s\n", msg);
    36 }
    38 static PKIX_PL_Cert *
    39 createCert(char *inFileName)
    40 {
    41         PKIX_PL_ByteArray *byteArray = NULL;
    42         PKIX_PL_Cert *cert = NULL;
    43         PKIX_Error *error = NULL;
    44         PRFileDesc *inFile = NULL;
    45         SECItem certDER;
    46         void *buf = NULL;
    47         PKIX_UInt32 len;
    48         SECStatus rv = SECFailure;
    50         certDER.data = NULL;
    52         inFile = PR_Open(inFileName, PR_RDONLY, 0);
    54         if (!inFile){
    55                 printFailure("Unable to open cert file");
    56                 goto cleanup;
    57         } else {
    58                 rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE, PR_FALSE);
    59                 if (!rv){
    60                         buf = (void *)certDER.data;
    61                         len = certDER.len;
    63                         error = PKIX_PL_ByteArray_Create
    64                                 (buf, len, &byteArray, plContext);
    66                         if (error){
    67                                 printFailure("PKIX_PL_ByteArray_Create failed");
    68                                 goto cleanup;
    69                         }
    71                         error = PKIX_PL_Cert_Create
    72                                 (byteArray, &cert, plContext);
    74                         if (error){
    75                                 printFailure("PKIX_PL_Cert_Create failed");
    76                                 goto cleanup;
    77                         }
    78                 } else {
    79                         printFailure("Unable to read DER from cert file");
    80                         goto cleanup;
    81                 }
    82         }
    84 cleanup:
    86         if (inFile){
    87                 PR_Close(inFile);
    88         }
    90         if (rv == SECSuccess){
    91                 SECITEM_FreeItem(&certDER, PR_FALSE);
    92         }
    94         if (byteArray){
    95                 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
    96         }
    98         return (cert);
    99 }
   101 int dumpcert(int argc, char *argv[])
   102 {
   104         PKIX_PL_String *string = NULL;
   105         PKIX_PL_Cert *cert = NULL;
   106         PKIX_Error *error = NULL;
   107         char *ascii = NULL;
   108         PKIX_UInt32 length = 0;
   109         PKIX_UInt32 j = 0;
   110 	PKIX_Boolean useArenas = PKIX_FALSE;
   111         PKIX_UInt32 actualMinorVersion;
   113         PKIX_TEST_STD_VARS();
   115         if (argc == 1){
   116                 printUsage();
   117                 return (0);
   118         }
   120         useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
   122         PKIX_Initialize
   123                 (PKIX_TRUE, /* nssInitNeeded */
   124                 useArenas,
   125                 PKIX_MAJOR_VERSION,
   126                 PKIX_MINOR_VERSION,
   127                 PKIX_MINOR_VERSION,
   128                 &actualMinorVersion,
   129                 &plContext);
   131         cert = createCert(argv[1+j]);
   133         if (cert){
   135                 error = PKIX_PL_Object_ToString
   136                         ((PKIX_PL_Object *)cert, &string, plContext);
   138                 if (error){
   139                         printFailure("Unable to get string representation "
   140                                     "of cert");
   141                         goto cleanup;
   142                 }
   144                 error = PKIX_PL_String_GetEncoded
   145                         (string,
   146                         PKIX_ESCASCII,
   147                         (void **)&ascii,
   148                         &length,
   149                         plContext);
   151                 if (error || !ascii){
   152                         printFailure("Unable to get ASCII encoding of string");
   153                         goto cleanup;
   154                 }
   156                 (void) printf("OUTPUT:\n%s\n", ascii);
   158         } else {
   159                 printFailure("Unable to create certificate");
   160                 goto cleanup;
   161         }
   163 cleanup:
   165         if (cert){
   166                 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(cert), plContext);
   167         }
   169         if (string){
   170                 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
   171         }
   173         if (ascii){
   174                 PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
   175         }
   177         PKIX_Shutdown(plContext);
   179         PKIX_TEST_RETURN();
   181         endTests("DUMPCERT");
   183         return (0);
   184 }

mercurial