Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | /* |
michael@0 | 5 | * buildChain.c |
michael@0 | 6 | * |
michael@0 | 7 | * Tests Cert Chain Building |
michael@0 | 8 | * |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include <stdio.h> |
michael@0 | 12 | #include <string.h> |
michael@0 | 13 | #include <stddef.h> |
michael@0 | 14 | |
michael@0 | 15 | #include "pkix_pl_generalname.h" |
michael@0 | 16 | #include "pkix_pl_cert.h" |
michael@0 | 17 | #include "pkix.h" |
michael@0 | 18 | #include "testutil.h" |
michael@0 | 19 | #include "prlong.h" |
michael@0 | 20 | #include "plstr.h" |
michael@0 | 21 | #include "prthread.h" |
michael@0 | 22 | #include "nspr.h" |
michael@0 | 23 | #include "prtypes.h" |
michael@0 | 24 | #include "prtime.h" |
michael@0 | 25 | #include "pk11func.h" |
michael@0 | 26 | #include "secasn1.h" |
michael@0 | 27 | #include "cert.h" |
michael@0 | 28 | #include "cryptohi.h" |
michael@0 | 29 | #include "secoid.h" |
michael@0 | 30 | #include "certdb.h" |
michael@0 | 31 | #include "secitem.h" |
michael@0 | 32 | #include "keythi.h" |
michael@0 | 33 | #include "nss.h" |
michael@0 | 34 | |
michael@0 | 35 | static void *plContext = NULL; |
michael@0 | 36 | |
michael@0 | 37 | static |
michael@0 | 38 | void printUsage(void){ |
michael@0 | 39 | (void) printf("\nUSAGE:\tbuildChain " |
michael@0 | 40 | "<trustedCert> <targetCert> <certStoreDirectory>\n\n"); |
michael@0 | 41 | (void) printf |
michael@0 | 42 | ("Builds a chain of certificates between " |
michael@0 | 43 | "<trustedCert> and <targetCert>\n" |
michael@0 | 44 | "using the certs and CRLs in <certStoreDirectory>.\n"); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | static PKIX_PL_Cert * |
michael@0 | 48 | createCert(char *inFileName) |
michael@0 | 49 | { |
michael@0 | 50 | PKIX_PL_ByteArray *byteArray = NULL; |
michael@0 | 51 | void *buf = NULL; |
michael@0 | 52 | PRFileDesc *inFile = NULL; |
michael@0 | 53 | PKIX_UInt32 len; |
michael@0 | 54 | SECItem certDER; |
michael@0 | 55 | SECStatus rv; |
michael@0 | 56 | /* default: NULL cert (failure case) */ |
michael@0 | 57 | PKIX_PL_Cert *cert = NULL; |
michael@0 | 58 | |
michael@0 | 59 | PKIX_TEST_STD_VARS(); |
michael@0 | 60 | |
michael@0 | 61 | certDER.data = NULL; |
michael@0 | 62 | |
michael@0 | 63 | inFile = PR_Open(inFileName, PR_RDONLY, 0); |
michael@0 | 64 | |
michael@0 | 65 | if (!inFile){ |
michael@0 | 66 | pkixTestErrorMsg = "Unable to open cert file"; |
michael@0 | 67 | goto cleanup; |
michael@0 | 68 | } else { |
michael@0 | 69 | rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE, PR_FALSE); |
michael@0 | 70 | if (!rv){ |
michael@0 | 71 | buf = (void *)certDER.data; |
michael@0 | 72 | len = certDER.len; |
michael@0 | 73 | |
michael@0 | 74 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_Create |
michael@0 | 75 | (buf, len, &byteArray, plContext)); |
michael@0 | 76 | |
michael@0 | 77 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_Create |
michael@0 | 78 | (byteArray, &cert, plContext)); |
michael@0 | 79 | |
michael@0 | 80 | SECITEM_FreeItem(&certDER, PR_FALSE); |
michael@0 | 81 | } else { |
michael@0 | 82 | pkixTestErrorMsg = "Unable to read DER from cert file"; |
michael@0 | 83 | goto cleanup; |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | cleanup: |
michael@0 | 88 | |
michael@0 | 89 | if (inFile){ |
michael@0 | 90 | PR_Close(inFile); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | if (PKIX_TEST_ERROR_RECEIVED){ |
michael@0 | 94 | SECITEM_FreeItem(&certDER, PR_FALSE); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | PKIX_TEST_DECREF_AC(byteArray); |
michael@0 | 98 | |
michael@0 | 99 | PKIX_TEST_RETURN(); |
michael@0 | 100 | |
michael@0 | 101 | return (cert); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | int build_chain(int argc, char *argv[]) |
michael@0 | 105 | { |
michael@0 | 106 | PKIX_BuildResult *buildResult = NULL; |
michael@0 | 107 | PKIX_ComCertSelParams *certSelParams = NULL; |
michael@0 | 108 | PKIX_CertSelector *certSelector = NULL; |
michael@0 | 109 | PKIX_TrustAnchor *anchor = NULL; |
michael@0 | 110 | PKIX_List *anchors = NULL; |
michael@0 | 111 | PKIX_List *certs = NULL; |
michael@0 | 112 | PKIX_PL_Cert *cert = NULL; |
michael@0 | 113 | PKIX_ProcessingParams *procParams = NULL; |
michael@0 | 114 | char *trustedCertFile = NULL; |
michael@0 | 115 | char *targetCertFile = NULL; |
michael@0 | 116 | char *storeDirAscii = NULL; |
michael@0 | 117 | PKIX_PL_String *storeDirString = NULL; |
michael@0 | 118 | PKIX_PL_Cert *trustedCert = NULL; |
michael@0 | 119 | PKIX_PL_Cert *targetCert = NULL; |
michael@0 | 120 | PKIX_UInt32 actualMinorVersion, numCerts, i; |
michael@0 | 121 | PKIX_UInt32 j = 0; |
michael@0 | 122 | PKIX_CertStore *certStore = NULL; |
michael@0 | 123 | PKIX_List *certStores = NULL; |
michael@0 | 124 | char * asciiResult = NULL; |
michael@0 | 125 | PKIX_Boolean useArenas = PKIX_FALSE; |
michael@0 | 126 | void *buildState = NULL; /* needed by pkix_build for non-blocking I/O */ |
michael@0 | 127 | void *nbioContext = NULL; |
michael@0 | 128 | |
michael@0 | 129 | PKIX_TEST_STD_VARS(); |
michael@0 | 130 | |
michael@0 | 131 | if (argc < 4){ |
michael@0 | 132 | printUsage(); |
michael@0 | 133 | return (0); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | useArenas = PKIX_TEST_ARENAS_ARG(argv[1]); |
michael@0 | 137 | |
michael@0 | 138 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize |
michael@0 | 139 | (PKIX_TRUE, /* nssInitNeeded */ |
michael@0 | 140 | useArenas, |
michael@0 | 141 | PKIX_MAJOR_VERSION, |
michael@0 | 142 | PKIX_MINOR_VERSION, |
michael@0 | 143 | PKIX_MINOR_VERSION, |
michael@0 | 144 | &actualMinorVersion, |
michael@0 | 145 | &plContext)); |
michael@0 | 146 | |
michael@0 | 147 | /* create processing params with list of trust anchors */ |
michael@0 | 148 | trustedCertFile = argv[j+1]; |
michael@0 | 149 | trustedCert = createCert(trustedCertFile); |
michael@0 | 150 | |
michael@0 | 151 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_CreateWithCert |
michael@0 | 152 | (trustedCert, &anchor, plContext)); |
michael@0 | 153 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&anchors, plContext)); |
michael@0 | 154 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem |
michael@0 | 155 | (anchors, (PKIX_PL_Object *)anchor, plContext)); |
michael@0 | 156 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_Create |
michael@0 | 157 | (anchors, &procParams, plContext)); |
michael@0 | 158 | |
michael@0 | 159 | |
michael@0 | 160 | /* create CertSelector with target certificate in params */ |
michael@0 | 161 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 162 | (PKIX_ComCertSelParams_Create(&certSelParams, plContext)); |
michael@0 | 163 | |
michael@0 | 164 | targetCertFile = argv[j+2]; |
michael@0 | 165 | targetCert = createCert(targetCertFile); |
michael@0 | 166 | |
michael@0 | 167 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 168 | (PKIX_ComCertSelParams_SetCertificate |
michael@0 | 169 | (certSelParams, targetCert, plContext)); |
michael@0 | 170 | |
michael@0 | 171 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 172 | (PKIX_CertSelector_Create(NULL, NULL, &certSelector, plContext)); |
michael@0 | 173 | |
michael@0 | 174 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_SetCommonCertSelectorParams |
michael@0 | 175 | (certSelector, certSelParams, plContext)); |
michael@0 | 176 | |
michael@0 | 177 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 178 | (PKIX_ProcessingParams_SetTargetCertConstraints |
michael@0 | 179 | (procParams, certSelector, plContext)); |
michael@0 | 180 | |
michael@0 | 181 | /* create CertStores */ |
michael@0 | 182 | |
michael@0 | 183 | storeDirAscii = argv[j+3]; |
michael@0 | 184 | |
michael@0 | 185 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create |
michael@0 | 186 | (PKIX_ESCASCII, storeDirAscii, 0, &storeDirString, plContext)); |
michael@0 | 187 | |
michael@0 | 188 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_CollectionCertStore_Create |
michael@0 | 189 | (storeDirString, &certStore, plContext)); |
michael@0 | 190 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&certStores, plContext)); |
michael@0 | 191 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem |
michael@0 | 192 | (certStores, (PKIX_PL_Object *)certStore, plContext)); |
michael@0 | 193 | |
michael@0 | 194 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetCertStores |
michael@0 | 195 | (procParams, certStores, plContext)); |
michael@0 | 196 | |
michael@0 | 197 | /* build cert chain using processing params and return buildResult */ |
michael@0 | 198 | |
michael@0 | 199 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_BuildChain |
michael@0 | 200 | (procParams, |
michael@0 | 201 | &nbioContext, |
michael@0 | 202 | &buildState, |
michael@0 | 203 | &buildResult, |
michael@0 | 204 | NULL, |
michael@0 | 205 | plContext)); |
michael@0 | 206 | |
michael@0 | 207 | /* |
michael@0 | 208 | * As long as we use only CertStores with blocking I/O, we can omit |
michael@0 | 209 | * checking for completion with nbioContext. |
michael@0 | 210 | */ |
michael@0 | 211 | |
michael@0 | 212 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 213 | (PKIX_BuildResult_GetCertChain(buildResult, &certs, plContext)); |
michael@0 | 214 | |
michael@0 | 215 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 216 | (PKIX_List_GetLength(certs, &numCerts, plContext)); |
michael@0 | 217 | |
michael@0 | 218 | printf("\n"); |
michael@0 | 219 | |
michael@0 | 220 | for (i = 0; i < numCerts; i++){ |
michael@0 | 221 | PKIX_TEST_EXPECT_NO_ERROR |
michael@0 | 222 | (PKIX_List_GetItem |
michael@0 | 223 | (certs, i, (PKIX_PL_Object**)&cert, plContext)); |
michael@0 | 224 | |
michael@0 | 225 | asciiResult = PKIX_Cert2ASCII(cert); |
michael@0 | 226 | |
michael@0 | 227 | printf("CERT[%d]:\n%s\n", i, asciiResult); |
michael@0 | 228 | |
michael@0 | 229 | PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(asciiResult, plContext)); |
michael@0 | 230 | asciiResult = NULL; |
michael@0 | 231 | |
michael@0 | 232 | PKIX_TEST_DECREF_BC(cert); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | cleanup: |
michael@0 | 236 | |
michael@0 | 237 | if (PKIX_TEST_ERROR_RECEIVED){ |
michael@0 | 238 | (void) printf("FAILED TO BUILD CHAIN\n"); |
michael@0 | 239 | } else { |
michael@0 | 240 | (void) printf("SUCCESSFULLY BUILT CHAIN\n"); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | PKIX_PL_Free(asciiResult, plContext); |
michael@0 | 244 | |
michael@0 | 245 | PKIX_TEST_DECREF_AC(certs); |
michael@0 | 246 | PKIX_TEST_DECREF_AC(cert); |
michael@0 | 247 | PKIX_TEST_DECREF_AC(certStore); |
michael@0 | 248 | PKIX_TEST_DECREF_AC(certStores); |
michael@0 | 249 | PKIX_TEST_DECREF_AC(storeDirString); |
michael@0 | 250 | PKIX_TEST_DECREF_AC(trustedCert); |
michael@0 | 251 | PKIX_TEST_DECREF_AC(targetCert); |
michael@0 | 252 | PKIX_TEST_DECREF_AC(anchor); |
michael@0 | 253 | PKIX_TEST_DECREF_AC(anchors); |
michael@0 | 254 | PKIX_TEST_DECREF_AC(procParams); |
michael@0 | 255 | PKIX_TEST_DECREF_AC(certSelParams); |
michael@0 | 256 | PKIX_TEST_DECREF_AC(certSelector); |
michael@0 | 257 | PKIX_TEST_DECREF_AC(buildResult); |
michael@0 | 258 | |
michael@0 | 259 | PKIX_TEST_RETURN(); |
michael@0 | 260 | |
michael@0 | 261 | PKIX_Shutdown(plContext); |
michael@0 | 262 | |
michael@0 | 263 | return (0); |
michael@0 | 264 | |
michael@0 | 265 | } |