summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/libpkix/sample_apps/dumpcert.c
blob: 6ff5f8377944119b999b43b1ba339df9eab61a6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * dumpcert.c
 *
 * dump certificate sample application
 *
 */

#include <stdio.h>

#include "pkix.h"
#include "testutil.h"
#include "prlong.h"
#include "plstr.h"
#include "prthread.h"
#include "plarena.h"
#include "seccomon.h"
#include "secdert.h"
#include "secasn1t.h"
#include "certt.h"

static void *plContext = NULL;

static void
printUsage(void)
{
    (void)printf("\nUSAGE:\tdumpcert <certFile>\n");
    (void)printf("\tParses a certificate located at <certFile> "
                 "and displays it.\n");
}

static void
printFailure(char *msg)
{
    (void)printf("FAILURE: %s\n", msg);
}

static PKIX_PL_Cert *
createCert(char *inFileName)
{
    PKIX_PL_ByteArray *byteArray = NULL;
    PKIX_PL_Cert *cert = NULL;
    PKIX_Error *error = NULL;
    PRFileDesc *inFile = NULL;
    SECItem certDER;
    void *buf = NULL;
    PKIX_UInt32 len;
    SECStatus rv = SECFailure;

    certDER.data = NULL;

    inFile = PR_Open(inFileName, PR_RDONLY, 0);

    if (!inFile) {
        printFailure("Unable to open cert file");
        goto cleanup;
    } else {
        rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE, PR_FALSE);
        if (!rv) {
            buf = (void *)certDER.data;
            len = certDER.len;

            error = PKIX_PL_ByteArray_Create(buf, len, &byteArray, plContext);

            if (error) {
                printFailure("PKIX_PL_ByteArray_Create failed");
                goto cleanup;
            }

            error = PKIX_PL_Cert_Create(byteArray, &cert, plContext);

            if (error) {
                printFailure("PKIX_PL_Cert_Create failed");
                goto cleanup;
            }
        } else {
            printFailure("Unable to read DER from cert file");
            goto cleanup;
        }
    }

cleanup:

    if (inFile) {
        PR_Close(inFile);
    }

    if (rv == SECSuccess) {
        SECITEM_FreeItem(&certDER, PR_FALSE);
    }

    if (byteArray) {
        PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
    }

    return (cert);
}

int
dumpcert(int argc, char *argv[])
{

    PKIX_PL_String *string = NULL;
    PKIX_PL_Cert *cert = NULL;
    PKIX_Error *error = NULL;
    char *ascii = NULL;
    PKIX_UInt32 length = 0;
    PKIX_UInt32 j = 0;
    PKIX_Boolean useArenas = PKIX_FALSE;
    PKIX_UInt32 actualMinorVersion;

    PKIX_TEST_STD_VARS();

    if (argc == 1) {
        printUsage();
        return (0);
    }

    useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);

    PKIX_Initialize(PKIX_TRUE, /* nssInitNeeded */
                    useArenas,
                    PKIX_MAJOR_VERSION,
                    PKIX_MINOR_VERSION,
                    PKIX_MINOR_VERSION,
                    &actualMinorVersion,
                    &plContext);

    cert = createCert(argv[1 + j]);

    if (cert) {

        error = PKIX_PL_Object_ToString((PKIX_PL_Object *)cert, &string, plContext);

        if (error) {
            printFailure("Unable to get string representation "
                         "of cert");
            goto cleanup;
        }

        error = PKIX_PL_String_GetEncoded(string,
                                          PKIX_ESCASCII,
                                          (void **)&ascii,
                                          &length,
                                          plContext);

        if (error || !ascii) {
            printFailure("Unable to get ASCII encoding of string");
            goto cleanup;
        }

        (void)printf("OUTPUT:\n%s\n", ascii);

    } else {
        printFailure("Unable to create certificate");
        goto cleanup;
    }

cleanup:

    if (cert) {
        PKIX_PL_Object_DecRef((PKIX_PL_Object *)(cert), plContext);
    }

    if (string) {
        PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
    }

    if (ascii) {
        PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
    }

    PKIX_Shutdown(plContext);

    PKIX_TEST_RETURN();

    endTests("DUMPCERT");

    return (0);
}