Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
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 | * libpkixBuildThreads.c |
michael@0 | 6 | * |
michael@0 | 7 | * libpkix Builder Performance Evaluation application (multi-threaded) |
michael@0 | 8 | * |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include <stdio.h> |
michael@0 | 12 | #include <string.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "secutil.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "nspr.h" |
michael@0 | 17 | #include "prtypes.h" |
michael@0 | 18 | #include "prtime.h" |
michael@0 | 19 | #include "prlong.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "pk11func.h" |
michael@0 | 22 | #include "secasn1.h" |
michael@0 | 23 | #include "cert.h" |
michael@0 | 24 | #include "cryptohi.h" |
michael@0 | 25 | #include "secoid.h" |
michael@0 | 26 | #include "certdb.h" |
michael@0 | 27 | #include "nss.h" |
michael@0 | 28 | |
michael@0 | 29 | #include "pkix.h" |
michael@0 | 30 | #include "pkix_tools.h" |
michael@0 | 31 | #include "pkix_pl_cert.h" |
michael@0 | 32 | |
michael@0 | 33 | #include "testutil.h" |
michael@0 | 34 | #include "testutil_nss.h" |
michael@0 | 35 | |
michael@0 | 36 | static void *plContext = NULL; |
michael@0 | 37 | |
michael@0 | 38 | #undef pkixTempResult |
michael@0 | 39 | #define PERF_DECREF(obj) \ |
michael@0 | 40 | { \ |
michael@0 | 41 | PKIX_Error *pkixTempResult = NULL; \ |
michael@0 | 42 | if (obj){ \ |
michael@0 | 43 | pkixTempResult = PKIX_PL_Object_DecRef \ |
michael@0 | 44 | ((PKIX_PL_Object *)(obj), plContext); \ |
michael@0 | 45 | obj = NULL; \ |
michael@0 | 46 | } \ |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | static void finish(char* message, int code); |
michael@0 | 50 | |
michael@0 | 51 | typedef struct ThreadDataStr tData; |
michael@0 | 52 | |
michael@0 | 53 | struct ThreadDataStr { |
michael@0 | 54 | CERTCertificate* anchor; |
michael@0 | 55 | char* eecertName; |
michael@0 | 56 | PRIntervalTime duration; |
michael@0 | 57 | CERTCertDBHandle *handle; |
michael@0 | 58 | PRUint32 iterations; |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | #define PKIX_LOGGER_ON 1 |
michael@0 | 62 | |
michael@0 | 63 | #ifdef PKIX_LOGGER_ON |
michael@0 | 64 | |
michael@0 | 65 | char *logLevels[] = { |
michael@0 | 66 | "None", |
michael@0 | 67 | "Fatal Error", |
michael@0 | 68 | "Error", |
michael@0 | 69 | "Warning", |
michael@0 | 70 | "Debug", |
michael@0 | 71 | "Trace" |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | static PKIX_Error *loggerCallback( |
michael@0 | 75 | PKIX_Logger *logger, |
michael@0 | 76 | PKIX_PL_String *message, |
michael@0 | 77 | PKIX_UInt32 logLevel, |
michael@0 | 78 | PKIX_ERRORCLASS logComponent, |
michael@0 | 79 | void *plContext) |
michael@0 | 80 | { |
michael@0 | 81 | char *msg = NULL; |
michael@0 | 82 | static int callCount = 0; |
michael@0 | 83 | |
michael@0 | 84 | msg = PKIX_String2ASCII(message, plContext); |
michael@0 | 85 | printf("Logging %s (%s): %s\n", |
michael@0 | 86 | logLevels[logLevel], |
michael@0 | 87 | PKIX_ERRORCLASSNAMES[logComponent], |
michael@0 | 88 | msg); |
michael@0 | 89 | PR_Free((void *)msg); |
michael@0 | 90 | |
michael@0 | 91 | return(NULL); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | #endif /* PKIX_LOGGER_ON */ |
michael@0 | 95 | |
michael@0 | 96 | static void ThreadEntry(void* data) |
michael@0 | 97 | { |
michael@0 | 98 | tData* tdata = (tData*) data; |
michael@0 | 99 | PRIntervalTime duration = tdata->duration; |
michael@0 | 100 | PRIntervalTime start = PR_IntervalNow(); |
michael@0 | 101 | |
michael@0 | 102 | PKIX_List *anchors = NULL; |
michael@0 | 103 | PKIX_ProcessingParams *procParams = NULL; |
michael@0 | 104 | PKIX_BuildResult *buildResult = NULL; |
michael@0 | 105 | CERTCertificate* nsseecert; |
michael@0 | 106 | PKIX_PL_Cert *eeCert = NULL; |
michael@0 | 107 | PKIX_CertStore *certStore = NULL; |
michael@0 | 108 | PKIX_List *certStores = NULL; |
michael@0 | 109 | PKIX_ComCertSelParams *certSelParams = NULL; |
michael@0 | 110 | PKIX_CertSelector *certSelector = NULL; |
michael@0 | 111 | PKIX_PL_Date *nowDate = NULL; |
michael@0 | 112 | void *state = NULL; /* only relevant with non-blocking I/O */ |
michael@0 | 113 | void *nbioContext = NULL; /* only relevant with non-blocking I/O */ |
michael@0 | 114 | |
michael@0 | 115 | PR_ASSERT(duration); |
michael@0 | 116 | if (!duration){ |
michael@0 | 117 | return; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | do { |
michael@0 | 121 | |
michael@0 | 122 | /* libpkix code */ |
michael@0 | 123 | |
michael@0 | 124 | /* keep more update time, testing cache */ |
michael@0 | 125 | PKIX_PL_Date_Create_UTCTime(NULL, &nowDate, plContext); |
michael@0 | 126 | |
michael@0 | 127 | /* CertUsage is 0x10 and no NSS arena */ |
michael@0 | 128 | /* We haven't determined how we obtain the value of wincx */ |
michael@0 | 129 | |
michael@0 | 130 | nsseecert = CERT_FindCertByNicknameOrEmailAddr(tdata->handle, |
michael@0 | 131 | tdata->eecertName); |
michael@0 | 132 | if (!nsseecert) finish("Unable to find eecert.\n", 1); |
michael@0 | 133 | |
michael@0 | 134 | pkix_pl_Cert_CreateWithNSSCert |
michael@0 | 135 | (nsseecert, &eeCert, plContext); |
michael@0 | 136 | |
michael@0 | 137 | PKIX_List_Create(&anchors, plContext); |
michael@0 | 138 | |
michael@0 | 139 | /* |
michael@0 | 140 | * This code is retired. |
michael@0 | 141 | * pkix_pl_Cert_CreateWithNSSCert |
michael@0 | 142 | * (tdata->anchor, &anchorCert, NULL); |
michael@0 | 143 | * PKIX_TrustAnchor_CreateWithCert(anchorCert, &anchor, NULL); |
michael@0 | 144 | * PKIX_List_AppendItem(anchors, (PKIX_PL_Object *)anchor, NULL); |
michael@0 | 145 | */ |
michael@0 | 146 | |
michael@0 | 147 | PKIX_ProcessingParams_Create(anchors, &procParams, plContext); |
michael@0 | 148 | |
michael@0 | 149 | PKIX_ProcessingParams_SetRevocationEnabled |
michael@0 | 150 | (procParams, PKIX_TRUE, plContext); |
michael@0 | 151 | |
michael@0 | 152 | PKIX_ProcessingParams_SetDate |
michael@0 | 153 | (procParams, nowDate, plContext); |
michael@0 | 154 | |
michael@0 | 155 | /* create CertSelector with target certificate in params */ |
michael@0 | 156 | |
michael@0 | 157 | PKIX_ComCertSelParams_Create(&certSelParams, plContext); |
michael@0 | 158 | |
michael@0 | 159 | PKIX_ComCertSelParams_SetCertificate |
michael@0 | 160 | (certSelParams, eeCert, plContext); |
michael@0 | 161 | |
michael@0 | 162 | PKIX_CertSelector_Create |
michael@0 | 163 | (NULL, NULL, &certSelector, plContext); |
michael@0 | 164 | |
michael@0 | 165 | PKIX_CertSelector_SetCommonCertSelectorParams |
michael@0 | 166 | (certSelector, certSelParams, plContext); |
michael@0 | 167 | |
michael@0 | 168 | PKIX_ProcessingParams_SetTargetCertConstraints |
michael@0 | 169 | (procParams, certSelector, plContext); |
michael@0 | 170 | |
michael@0 | 171 | PKIX_PL_Pk11CertStore_Create(&certStore, plContext); |
michael@0 | 172 | |
michael@0 | 173 | PKIX_List_Create(&certStores, plContext); |
michael@0 | 174 | PKIX_List_AppendItem |
michael@0 | 175 | (certStores, (PKIX_PL_Object *)certStore, plContext); |
michael@0 | 176 | PKIX_ProcessingParams_SetCertStores |
michael@0 | 177 | (procParams, certStores, plContext); |
michael@0 | 178 | |
michael@0 | 179 | PKIX_BuildChain |
michael@0 | 180 | (procParams, |
michael@0 | 181 | &nbioContext, |
michael@0 | 182 | &state, |
michael@0 | 183 | &buildResult, |
michael@0 | 184 | NULL, |
michael@0 | 185 | plContext); |
michael@0 | 186 | |
michael@0 | 187 | /* |
michael@0 | 188 | * As long as we use only CertStores with blocking I/O, we |
michael@0 | 189 | * know we must be done at this point. |
michael@0 | 190 | */ |
michael@0 | 191 | |
michael@0 | 192 | if (!buildResult){ |
michael@0 | 193 | (void) fprintf(stderr, "libpkix BuildChain failed.\n"); |
michael@0 | 194 | PORT_Assert(0); |
michael@0 | 195 | return; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | tdata->iterations ++; |
michael@0 | 199 | |
michael@0 | 200 | PERF_DECREF(nowDate); |
michael@0 | 201 | PERF_DECREF(anchors); |
michael@0 | 202 | PERF_DECREF(procParams); |
michael@0 | 203 | PERF_DECREF(buildResult); |
michael@0 | 204 | PERF_DECREF(certStore); |
michael@0 | 205 | PERF_DECREF(certStores); |
michael@0 | 206 | PERF_DECREF(certSelParams); |
michael@0 | 207 | PERF_DECREF(certSelector); |
michael@0 | 208 | PERF_DECREF(eeCert); |
michael@0 | 209 | |
michael@0 | 210 | } while ((PR_IntervalNow() - start) < duration); |
michael@0 | 211 | |
michael@0 | 212 | |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | static void |
michael@0 | 216 | Test( |
michael@0 | 217 | CERTCertificate* anchor, |
michael@0 | 218 | char* eecertName, |
michael@0 | 219 | PRIntervalTime duration, |
michael@0 | 220 | CERTCertDBHandle *handle, |
michael@0 | 221 | PRUint32 threads) |
michael@0 | 222 | { |
michael@0 | 223 | tData data; |
michael@0 | 224 | tData** alldata; |
michael@0 | 225 | PRIntervalTime starttime, endtime, elapsed; |
michael@0 | 226 | PRUint32 msecs; |
michael@0 | 227 | float total = 0; |
michael@0 | 228 | PRThread** pthreads = NULL; |
michael@0 | 229 | PRUint32 i = 0; |
michael@0 | 230 | |
michael@0 | 231 | data.duration = duration; |
michael@0 | 232 | data.anchor = anchor; |
michael@0 | 233 | data.eecertName = eecertName; |
michael@0 | 234 | data.handle = handle; |
michael@0 | 235 | |
michael@0 | 236 | data.iterations = 0; |
michael@0 | 237 | |
michael@0 | 238 | starttime = PR_IntervalNow(); |
michael@0 | 239 | pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*)); |
michael@0 | 240 | alldata = (tData**)PR_Malloc(threads*sizeof (tData*)); |
michael@0 | 241 | for (i = 0; i < threads; i++){ |
michael@0 | 242 | alldata[i] = (tData*)PR_Malloc(sizeof (tData)); |
michael@0 | 243 | *alldata[i] = data; |
michael@0 | 244 | pthreads[i] = |
michael@0 | 245 | PR_CreateThread(PR_USER_THREAD, |
michael@0 | 246 | ThreadEntry, |
michael@0 | 247 | (void*) alldata[i], |
michael@0 | 248 | PR_PRIORITY_NORMAL, |
michael@0 | 249 | PR_GLOBAL_THREAD, |
michael@0 | 250 | PR_JOINABLE_THREAD, |
michael@0 | 251 | 0); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | for (i = 0; i < threads; i++) { |
michael@0 | 255 | tData* args = alldata[i]; |
michael@0 | 256 | PR_JoinThread(pthreads[i]); |
michael@0 | 257 | total += args->iterations; |
michael@0 | 258 | PR_Free((void*)args); |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | PR_Free((void*) pthreads); |
michael@0 | 262 | PR_Free((void*) alldata); |
michael@0 | 263 | endtime = PR_IntervalNow(); |
michael@0 | 264 | |
michael@0 | 265 | endtime = PR_IntervalNow(); |
michael@0 | 266 | elapsed = endtime - starttime; |
michael@0 | 267 | msecs = PR_IntervalToMilliseconds(elapsed); |
michael@0 | 268 | total /= msecs; |
michael@0 | 269 | total *= 1000; |
michael@0 | 270 | (void) fprintf(stdout, "%f operations per second.\n", total); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | |
michael@0 | 274 | static void finish(char* message, int code) |
michael@0 | 275 | { |
michael@0 | 276 | (void) printf(message); |
michael@0 | 277 | exit(code); |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | static void usage(char* progname) |
michael@0 | 281 | { |
michael@0 | 282 | (void) printf("Usage : %s <-d certStoreDirectory> <duration> <threads> " |
michael@0 | 283 | "<anchorNickname> <eecertNickname>\n\n", progname); |
michael@0 | 284 | finish("", 0); |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | int |
michael@0 | 288 | libpkix_buildthreads(int argc, char** argv) |
michael@0 | 289 | { |
michael@0 | 290 | CERTCertDBHandle *handle = NULL; |
michael@0 | 291 | CERTCertificate* eecert = NULL; |
michael@0 | 292 | PRIntervalTime duration = PR_SecondsToInterval(1); |
michael@0 | 293 | PRUint32 threads = 1; |
michael@0 | 294 | PKIX_UInt32 actualMinorVersion; |
michael@0 | 295 | PKIX_UInt32 j = 0; |
michael@0 | 296 | PKIX_Logger *logger = NULL; |
michael@0 | 297 | void *wincx = NULL; |
michael@0 | 298 | |
michael@0 | 299 | /* if (argc != 5) -- when TrustAnchor used to be on command line */ |
michael@0 | 300 | if (argc != 4) |
michael@0 | 301 | { |
michael@0 | 302 | usage(argv[0]); |
michael@0 | 303 | } |
michael@0 | 304 | if (atoi(argv[1]) > 0) |
michael@0 | 305 | { |
michael@0 | 306 | duration = PR_SecondsToInterval(atoi(argv[1])); |
michael@0 | 307 | } |
michael@0 | 308 | if (atoi(argv[2]) > 0) |
michael@0 | 309 | { |
michael@0 | 310 | threads = atoi(argv[2]); |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | PKIX_PL_NssContext_Create(certificateUsageEmailSigner, PKIX_FALSE, |
michael@0 | 314 | NULL, &plContext); |
michael@0 | 315 | |
michael@0 | 316 | handle = CERT_GetDefaultCertDB(); |
michael@0 | 317 | PR_ASSERT(handle); |
michael@0 | 318 | |
michael@0 | 319 | #ifdef PKIX_LOGGER_ON |
michael@0 | 320 | |
michael@0 | 321 | /* set logger to log trace and up */ |
michael@0 | 322 | PKIX_SetLoggers(NULL, plContext); |
michael@0 | 323 | PKIX_Logger_Create(loggerCallback, NULL, &logger, plContext); |
michael@0 | 324 | PKIX_Logger_SetMaxLoggingLevel |
michael@0 | 325 | (logger, PKIX_LOGGER_LEVEL_WARNING, plContext); |
michael@0 | 326 | PKIX_AddLogger(logger, plContext); |
michael@0 | 327 | |
michael@0 | 328 | #endif /* PKIX_LOGGER_ON */ |
michael@0 | 329 | |
michael@0 | 330 | /* |
michael@0 | 331 | * This code is retired |
michael@0 | 332 | * anchor = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]); |
michael@0 | 333 | * if (!anchor) finish("Unable to find anchor.\n", 1); |
michael@0 | 334 | * |
michael@0 | 335 | * eecert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[4]); |
michael@0 | 336 | |
michael@0 | 337 | * if (!eecert) finish("Unable to find eecert.\n", 1); |
michael@0 | 338 | * |
michael@0 | 339 | * Test(anchor, eecert, duration, threads); |
michael@0 | 340 | */ |
michael@0 | 341 | |
michael@0 | 342 | Test(NULL, argv[3], duration, handle, threads); |
michael@0 | 343 | |
michael@0 | 344 | PERF_DECREF(logger); |
michael@0 | 345 | |
michael@0 | 346 | PKIX_Shutdown(plContext); |
michael@0 | 347 | |
michael@0 | 348 | return (0); |
michael@0 | 349 | } |