~ubuntu-branches/ubuntu/intrepid/xulrunner-1.9/intrepid

« back to all changes in this revision

Viewing changes to mozilla/security/nss/cmd/vfyserv/vfyutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack, Alexander Sack, Fabien Tassin
  • Date: 2008-02-13 11:47:21 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080213114721-7om0mgzngvuk9czv
Tags: 1.9~b3+nobinonly-0ubuntu1
* release FIREFOX_3_0b3_RELEASE

[ Alexander Sack ]
* submit patch that ships xpcshell to bugzilla
  - rename debian/patches/ship_xpcshell.patch =>
           debian/patches/bz410617_att295212_ship_xpcshell.patch
  - update debian/patches/series
* fix tooltip in epiphany: previously displayed out of screen bounds
  (LP: #37507)
  - add debian/patches/bz233371_att297343_fix_outofscreen_embed_tooltip.patch
  - update debian/patches/series
* use default upstream gcc tweaks for improved performance - especially of the
  javascript engine
  - update debian/rules
* update global extension/plugin patch to apply cleanly against latest code
  - update debian/patches/bzXXX_gre_extension_plugin_support.patch
* fix pyxpcom build failure introduced by recent commit
  - add debian/patches/bzXXX_fix_pyxpcom_build_failure.patch
  - update debian/patches/series
* add distro independent global install locations for extensions,
  /usr/lib/mozilla/extensions and /usr/share/mozilla/extensions
  - update debian/xulrunner-1.9.dirs
* support embedded tarball layout when either there is a *.tar.bz2 in orig tarball
  or if DEBIAN_MOZCLIENT_EMBEDDED is not unset (you will need this to produce embedded
  tarballs during |get-orig-source|
  - update debian/rules
* bump minimum libnss3-dev build requirements to >= 3.12.0~1.9b3
  - update debian/control
* bump minimum libnspr4-dev build requirements to >= 4.7.0~1.9b3
  - update debian/control

[ Fabien Tassin ]
* Drop patches applied upstream
  - drop debian/patches/bz410617_att295212_ship_xpcshell.patch
  - drop debian/patches/bz404634_att294921.patch
  - drop debian/patches/bz386610_python2.5_ftbfs_amd64.patch
  - drop debian/patches/bz373918_att295042.patch
  - drop debian/patches/bz408062_unstable_pc.patch
  - drop debian/patches/bz384304_fix_recursive_symlinks.patch
  - update debian/patches/series
* Refresh diverged patches:
  - update debian/patches/bzXXX_pc_honour_system_nspr_nss.patch
  - update debian/patches/rename_venkman_addon.patch
  - update debian/patches/bz344818_cairo_xrender.patch
* Install links for all .so libs in the -dev package
  - update debian/patches/dont_install_so_in_dev.patch
* Bump gtk requirement to 2.12 as per Mozilla bug 412432
  - update debian/control
* Add #DEBHELPER# token to postinst/prerm scripts
  - update debian/xulrunner-1.9.{postinst,prerm}
* Install previously missed libdbusservice.so
  - update debian/xulrunner-1.9.install
* Update venkman patch to also rename locales
  - update debian/patches/rename_venkman_addon.patch
* Bump requirement for system cairo to >= 1.5.8 as we now need
  the newly added cairo_path_extents()
  - update debian/rules
* Include mozilla-devscripts file using -include so ifneq could be omitted
  - update debian/rules
* Fix missing .so symlinks regression
  - update debian/patches/dont_install_so_in_dev.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ***** BEGIN LICENSE BLOCK *****
2
 
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3
 
 *
4
 
 * The contents of this file are subject to the Mozilla Public License Version
5
 
 * 1.1 (the "License"); you may not use this file except in compliance with
6
 
 * the License. You may obtain a copy of the License at
7
 
 * http://www.mozilla.org/MPL/
8
 
 *
9
 
 * Software distributed under the License is distributed on an "AS IS" basis,
10
 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
 
 * for the specific language governing rights and limitations under the
12
 
 * License.
13
 
 *
14
 
 * The Original Code is the Netscape security libraries.
15
 
 *
16
 
 * The Initial Developer of the Original Code is
17
 
 * Netscape Communications Corporation.
18
 
 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19
 
 * the Initial Developer. All Rights Reserved.
20
 
 *
21
 
 * Contributor(s):
22
 
 *
23
 
 * Alternatively, the contents of this file may be used under the terms of
24
 
 * either the GNU General Public License Version 2 or later (the "GPL"), or
25
 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
 
 * in which case the provisions of the GPL or the LGPL are applicable instead
27
 
 * of those above. If you wish to allow use of your version of this file only
28
 
 * under the terms of either the GPL or the LGPL, and not to allow others to
29
 
 * use your version of this file under the terms of the MPL, indicate your
30
 
 * decision by deleting the provisions above and replace them with the notice
31
 
 * and other provisions required by the GPL or the LGPL. If you do not delete
32
 
 * the provisions above, a recipient may use your version of this file under
33
 
 * the terms of any one of the MPL, the GPL or the LGPL.
34
 
 *
35
 
 * ***** END LICENSE BLOCK ***** */
36
 
 
37
 
#include "vfyserv.h"
38
 
#include "secerr.h"
39
 
#include "sslerr.h"
40
 
#include "nspr.h"
41
 
#include "secutil.h"
42
 
 
43
 
 
44
 
extern PRBool dumpChain;
45
 
extern void dumpCertChain(CERTCertificate *, SECCertUsage);
46
 
 
47
 
/* Declare SSL cipher suites. */
48
 
 
49
 
int ssl2CipherSuites[] = {
50
 
    SSL_EN_RC4_128_WITH_MD5,              /* A */
51
 
    SSL_EN_RC4_128_EXPORT40_WITH_MD5,     /* B */
52
 
    SSL_EN_RC2_128_CBC_WITH_MD5,          /* C */
53
 
    SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5, /* D */
54
 
    SSL_EN_DES_64_CBC_WITH_MD5,           /* E */
55
 
    SSL_EN_DES_192_EDE3_CBC_WITH_MD5,     /* F */
56
 
    0
57
 
};
58
 
 
59
 
int ssl3CipherSuites[] = {
60
 
    -1, /* SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA* a */
61
 
    -1, /* SSL_FORTEZZA_DMS_WITH_RC4_128_SHA,    * b */
62
 
    SSL_RSA_WITH_RC4_128_MD5,                   /* c */
63
 
    SSL_RSA_WITH_3DES_EDE_CBC_SHA,              /* d */
64
 
    SSL_RSA_WITH_DES_CBC_SHA,                   /* e */
65
 
    SSL_RSA_EXPORT_WITH_RC4_40_MD5,             /* f */
66
 
    SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5,         /* g */
67
 
    -1, /* SSL_FORTEZZA_DMS_WITH_NULL_SHA,       * h */
68
 
    SSL_RSA_WITH_NULL_MD5,                      /* i */
69
 
    SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA,         /* j */
70
 
    SSL_RSA_FIPS_WITH_DES_CBC_SHA,              /* k */
71
 
    TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA,        /* l */
72
 
    TLS_RSA_EXPORT1024_WITH_RC4_56_SHA,         /* m */
73
 
    SSL_RSA_WITH_RC4_128_SHA,                   /* n */
74
 
    TLS_DHE_DSS_WITH_RC4_128_SHA,               /* o */
75
 
    SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA,          /* p */
76
 
    SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA,          /* q */
77
 
    SSL_DHE_RSA_WITH_DES_CBC_SHA,               /* r */
78
 
    SSL_DHE_DSS_WITH_DES_CBC_SHA,               /* s */
79
 
    TLS_DHE_DSS_WITH_AES_128_CBC_SHA,           /* t */
80
 
    TLS_DHE_RSA_WITH_AES_128_CBC_SHA,           /* u */
81
 
    TLS_RSA_WITH_AES_128_CBC_SHA,               /* v */
82
 
    TLS_DHE_DSS_WITH_AES_256_CBC_SHA,           /* w */
83
 
    TLS_DHE_RSA_WITH_AES_256_CBC_SHA,           /* x */
84
 
    TLS_RSA_WITH_AES_256_CBC_SHA,               /* y */
85
 
    SSL_RSA_WITH_NULL_SHA,                      /* z */
86
 
    0
87
 
};
88
 
 
89
 
/**************************************************************************
90
 
** 
91
 
** SSL callback routines.
92
 
**
93
 
**************************************************************************/
94
 
 
95
 
/* Function: char * myPasswd()
96
 
 * 
97
 
 * Purpose: This function is our custom password handler that is called by
98
 
 * SSL when retreiving private certs and keys from the database. Returns a
99
 
 * pointer to a string that with a password for the database. Password pointer
100
 
 * should point to dynamically allocated memory that will be freed later.
101
 
 */
102
 
char *
103
 
myPasswd(PK11SlotInfo *info, PRBool retry, void *arg)
104
 
{
105
 
    char * passwd = NULL;
106
 
 
107
 
    if ( (!retry) && arg ) {
108
 
        passwd = PORT_Strdup((char *)arg);
109
 
    }
110
 
    return passwd;
111
 
}
112
 
 
113
 
/* Function: SECStatus myAuthCertificate()
114
 
 *
115
 
 * Purpose: This function is our custom certificate authentication handler.
116
 
 * 
117
 
 * Note: This implementation is essentially the same as the default 
118
 
 *       SSL_AuthCertificate().
119
 
 */
120
 
SECStatus 
121
 
myAuthCertificate(void *arg, PRFileDesc *socket, 
122
 
                  PRBool checksig, PRBool isServer) 
123
 
{
124
 
 
125
 
    SECCertificateUsage certUsage;
126
 
    CERTCertificate *   cert;
127
 
    void *              pinArg;
128
 
    char *              hostName;
129
 
    SECStatus           secStatus;
130
 
 
131
 
    if (!arg || !socket) {
132
 
        errWarn("myAuthCertificate");
133
 
        return SECFailure;
134
 
    }
135
 
 
136
 
    /* Define how the cert is being used based upon the isServer flag. */
137
 
 
138
 
    certUsage = isServer ? certificateUsageSSLClient : certificateUsageSSLServer;
139
 
 
140
 
    cert = SSL_PeerCertificate(socket);
141
 
        
142
 
    pinArg = SSL_RevealPinArg(socket);
143
 
    
144
 
    if (dumpChain == PR_TRUE) {
145
 
        dumpCertChain(cert, certUsage);
146
 
    }
147
 
 
148
 
    secStatus = CERT_VerifyCertificateNow((CERTCertDBHandle *)arg,
149
 
                                   cert,
150
 
                                   checksig,
151
 
                                   certUsage,
152
 
                                   pinArg,
153
 
                                   NULL);
154
 
 
155
 
    /* If this is a server, we're finished. */
156
 
    if (isServer || secStatus != SECSuccess) {
157
 
        SECU_printCertProblems(stderr, (CERTCertDBHandle *)arg, cert, 
158
 
                          checksig, certUsage, pinArg, PR_FALSE);
159
 
        CERT_DestroyCertificate(cert);
160
 
        return secStatus;
161
 
    }
162
 
 
163
 
    /* Certificate is OK.  Since this is the client side of an SSL
164
 
     * connection, we need to verify that the name field in the cert
165
 
     * matches the desired hostname.  This is our defense against
166
 
     * man-in-the-middle attacks.
167
 
     */
168
 
 
169
 
    /* SSL_RevealURL returns a hostName, not an URL. */
170
 
    hostName = SSL_RevealURL(socket);
171
 
 
172
 
    if (hostName && hostName[0]) {
173
 
        secStatus = CERT_VerifyCertName(cert, hostName);
174
 
    } else {
175
 
        PR_SetError(SSL_ERROR_BAD_CERT_DOMAIN, 0);
176
 
        secStatus = SECFailure;
177
 
    }
178
 
 
179
 
    if (hostName)
180
 
        PR_Free(hostName);
181
 
 
182
 
    CERT_DestroyCertificate(cert);
183
 
    return secStatus;
184
 
}
185
 
 
186
 
/* Function: SECStatus myBadCertHandler()
187
 
 *
188
 
 * Purpose: This callback is called when the incoming certificate is not
189
 
 * valid. We define a certain set of parameters that still cause the
190
 
 * certificate to be "valid" for this session, and return SECSuccess to cause
191
 
 * the server to continue processing the request when any of these conditions
192
 
 * are met. Otherwise, SECFailure is return and the server rejects the 
193
 
 * request.
194
 
 */
195
 
SECStatus 
196
 
myBadCertHandler(void *arg, PRFileDesc *socket) 
197
 
{
198
 
 
199
 
    SECStatus   secStatus = SECFailure;
200
 
    PRErrorCode err;
201
 
 
202
 
    /* log invalid cert here */
203
 
 
204
 
    if (!arg) {
205
 
        return secStatus;
206
 
    }
207
 
 
208
 
    *(PRErrorCode *)arg = err = PORT_GetError();
209
 
 
210
 
    /* If any of the cases in the switch are met, then we will proceed   */
211
 
    /* with the processing of the request anyway. Otherwise, the default */     
212
 
    /* case will be reached and we will reject the request.              */
213
 
 
214
 
    switch (err) {
215
 
    case SEC_ERROR_INVALID_AVA:
216
 
    case SEC_ERROR_INVALID_TIME:
217
 
    case SEC_ERROR_BAD_SIGNATURE:
218
 
    case SEC_ERROR_EXPIRED_CERTIFICATE:
219
 
    case SEC_ERROR_UNKNOWN_ISSUER:
220
 
    case SEC_ERROR_UNTRUSTED_CERT:
221
 
    case SEC_ERROR_CERT_VALID:
222
 
    case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
223
 
    case SEC_ERROR_CRL_EXPIRED:
224
 
    case SEC_ERROR_CRL_BAD_SIGNATURE:
225
 
    case SEC_ERROR_EXTENSION_VALUE_INVALID:
226
 
    case SEC_ERROR_CA_CERT_INVALID:
227
 
    case SEC_ERROR_CERT_USAGES_INVALID:
228
 
    case SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION:
229
 
        secStatus = SECSuccess;
230
 
        break;
231
 
    default:
232
 
        secStatus = SECFailure;
233
 
        break;
234
 
    }
235
 
 
236
 
    fprintf(stderr, "Bad certificate: %d, %s\n", err, SECU_Strerror(err));
237
 
 
238
 
    return secStatus;
239
 
}
240
 
 
241
 
/* Function: SECStatus ownGetClientAuthData()
242
 
 *
243
 
 * Purpose: This callback is used by SSL to pull client certificate 
244
 
 * information upon server request.
245
 
 */
246
 
SECStatus 
247
 
myGetClientAuthData(void *arg,
248
 
                    PRFileDesc *socket,
249
 
                    struct CERTDistNamesStr *caNames,
250
 
                    struct CERTCertificateStr **pRetCert,
251
 
                    struct SECKEYPrivateKeyStr **pRetKey) 
252
 
{
253
 
 
254
 
    CERTCertificate *  cert;
255
 
    SECKEYPrivateKey * privKey;
256
 
    char *             chosenNickName = (char *)arg;
257
 
    void *             proto_win      = NULL;
258
 
    SECStatus          secStatus      = SECFailure;
259
 
 
260
 
    proto_win = SSL_RevealPinArg(socket);
261
 
 
262
 
    if (chosenNickName) {
263
 
        cert = PK11_FindCertFromNickname(chosenNickName, proto_win);
264
 
        if (cert) {
265
 
            privKey = PK11_FindKeyByAnyCert(cert, proto_win);
266
 
            if (privKey) {
267
 
                secStatus = SECSuccess;
268
 
            } else {
269
 
                CERT_DestroyCertificate(cert);
270
 
            }
271
 
        }
272
 
    } else { /* no nickname given, automatically find the right cert */
273
 
        CERTCertNicknames *names;
274
 
        int                i;
275
 
 
276
 
        names = CERT_GetCertNicknames(CERT_GetDefaultCertDB(), 
277
 
                                      SEC_CERT_NICKNAMES_USER, proto_win);
278
 
 
279
 
        if (names != NULL) {
280
 
            for(i = 0; i < names->numnicknames; i++ ) {
281
 
 
282
 
                cert = PK11_FindCertFromNickname(names->nicknames[i], 
283
 
                                                 proto_win);
284
 
                if (!cert) {
285
 
                    continue;
286
 
                }
287
 
 
288
 
                /* Only check unexpired certs */
289
 
                if (CERT_CheckCertValidTimes(cert, PR_Now(), PR_FALSE)
290
 
                      != secCertTimeValid ) {
291
 
                    CERT_DestroyCertificate(cert);
292
 
                    continue;
293
 
                }
294
 
 
295
 
                secStatus = NSS_CmpCertChainWCANames(cert, caNames);
296
 
                if (secStatus == SECSuccess) {
297
 
                    privKey = PK11_FindKeyByAnyCert(cert, proto_win);
298
 
                    if (privKey) {
299
 
                        break;
300
 
                    }
301
 
                    secStatus = SECFailure;
302
 
                }
303
 
                CERT_DestroyCertificate(cert);
304
 
            } /* for loop */
305
 
            CERT_FreeNicknames(names);
306
 
        }
307
 
    }
308
 
 
309
 
    if (secStatus == SECSuccess) {
310
 
        *pRetCert = cert;
311
 
        *pRetKey  = privKey;
312
 
    }
313
 
 
314
 
    return secStatus;
315
 
}
316
 
 
317
 
/* Function: SECStatus myHandshakeCallback()
318
 
 *
319
 
 * Purpose: Called by SSL to inform application that the handshake is
320
 
 * complete. This function is mostly used on the server side of an SSL
321
 
 * connection, although it is provided for a client as well.
322
 
 * Useful when a non-blocking SSL_ReHandshake or SSL_ResetHandshake 
323
 
 * is used to initiate a handshake.
324
 
 *
325
 
 * A typical scenario would be:
326
 
 *
327
 
 * 1. Server accepts an SSL connection from the client without client auth.
328
 
 * 2. Client sends a request.
329
 
 * 3. Server determines that to service request it needs to authenticate the
330
 
 * client and initiates another handshake requesting client auth.
331
 
 * 4. While handshake is in progress, server can do other work or spin waiting
332
 
 * for the handshake to complete.
333
 
 * 5. Server is notified that handshake has been successfully completed by
334
 
 * the custom handshake callback function and it can service the client's
335
 
 * request.
336
 
 *
337
 
 * Note: This function is not implemented in this sample, as we are using
338
 
 * blocking sockets.
339
 
 */
340
 
SECStatus 
341
 
myHandshakeCallback(PRFileDesc *socket, void *arg) 
342
 
{
343
 
    fprintf(stderr,"Handshake Complete: SERVER CONFIGURED CORRECTLY\n");
344
 
    return SECSuccess;
345
 
}
346
 
 
347
 
 
348
 
/**************************************************************************
349
 
** 
350
 
** Routines for disabling SSL ciphers.
351
 
**
352
 
**************************************************************************/
353
 
 
354
 
void
355
 
disableAllSSLCiphers(void)
356
 
{
357
 
    const PRUint16 *cipherSuites = SSL_ImplementedCiphers;
358
 
    int             i            = SSL_NumImplementedCiphers;
359
 
    SECStatus       rv;
360
 
 
361
 
    /* disable all the SSL3 cipher suites */
362
 
    while (--i >= 0) {
363
 
        PRUint16 suite = cipherSuites[i];
364
 
        rv = SSL_CipherPrefSetDefault(suite, PR_FALSE);
365
 
        if (rv != SECSuccess) {
366
 
            fprintf(stderr,
367
 
                "SSL_CipherPrefSetDefault didn't like value 0x%04x (i = %d)\n",
368
 
                   suite, i);
369
 
            errWarn("SSL_CipherPrefSetDefault");
370
 
            exit(2);
371
 
        }
372
 
    }
373
 
}
374
 
 
375
 
/**************************************************************************
376
 
** 
377
 
** Error and information routines.
378
 
**
379
 
**************************************************************************/
380
 
 
381
 
void
382
 
errWarn(char *function)
383
 
{
384
 
    PRErrorCode  errorNumber = PR_GetError();
385
 
    const char * errorString = SECU_Strerror(errorNumber);
386
 
 
387
 
    fprintf(stderr, "Error in function %s: %d\n - %s\n",
388
 
                    function, errorNumber, errorString);
389
 
}
390
 
 
391
 
void
392
 
exitErr(char *function)
393
 
{
394
 
    errWarn(function);
395
 
    /* Exit gracefully. */
396
 
    /* ignoring return value of NSS_Shutdown as code exits with 1 anyway*/
397
 
    (void) NSS_Shutdown();
398
 
    PR_Cleanup();
399
 
    exit(1);
400
 
}
401
 
 
402
 
void 
403
 
printSecurityInfo(FILE *outfile, PRFileDesc *fd)
404
 
{
405
 
    char * cp;  /* bulk cipher name */
406
 
    char * ip;  /* cert issuer DN */
407
 
    char * sp;  /* cert subject DN */
408
 
    int    op;  /* High, Low, Off */
409
 
    int    kp0; /* total key bits */
410
 
    int    kp1; /* secret key bits */
411
 
    int    result;
412
 
    SSL3Statistics * ssl3stats = SSL_GetStatistics();
413
 
 
414
 
    if (!outfile) {
415
 
        outfile = stdout;
416
 
    }
417
 
 
418
 
    result = SSL_SecurityStatus(fd, &op, &cp, &kp0, &kp1, &ip, &sp);
419
 
    if (result != SECSuccess)
420
 
        return;
421
 
    fprintf(outfile,
422
 
     "   bulk cipher %s, %d secret key bits, %d key bits, status: %d\n"
423
 
     "   subject DN:\n %s\n"
424
 
     "   issuer  DN:\n %s\n", cp, kp1, kp0, op, sp, ip);
425
 
    PR_Free(cp);
426
 
    PR_Free(ip);
427
 
    PR_Free(sp);
428
 
 
429
 
    fprintf(outfile,
430
 
      "   %ld cache hits; %ld cache misses, %ld cache not reusable\n",
431
 
            ssl3stats->hch_sid_cache_hits, ssl3stats->hch_sid_cache_misses,
432
 
    ssl3stats->hch_sid_cache_not_ok);
433
 
 
434
 
}
435
 
 
436
 
 
437
 
/**************************************************************************
438
 
** Begin thread management routines and data.
439
 
**************************************************************************/
440
 
 
441
 
void
442
 
thread_wrapper(void * arg)
443
 
{
444
 
    GlobalThreadMgr *threadMGR = (GlobalThreadMgr *)arg;
445
 
    perThread *slot = &threadMGR->threads[threadMGR->index];
446
 
 
447
 
    /* wait for parent to finish launching us before proceeding. */
448
 
    PR_Lock(threadMGR->threadLock);
449
 
    PR_Unlock(threadMGR->threadLock);
450
 
 
451
 
    slot->rv = (* slot->startFunc)(slot->a, slot->b);
452
 
 
453
 
    PR_Lock(threadMGR->threadLock);
454
 
    slot->running = rs_zombie;
455
 
 
456
 
    /* notify the thread exit handler. */
457
 
    PR_NotifyCondVar(threadMGR->threadEndQ);
458
 
 
459
 
    PR_Unlock(threadMGR->threadLock);
460
 
}
461
 
 
462
 
SECStatus
463
 
launch_thread(GlobalThreadMgr *threadMGR,
464
 
              startFn         *startFunc,
465
 
              void            *a,
466
 
              int              b)
467
 
{
468
 
    perThread *slot;
469
 
    int        i;
470
 
 
471
 
    if (!threadMGR->threadStartQ) {
472
 
        threadMGR->threadLock   = PR_NewLock();
473
 
        threadMGR->threadStartQ = PR_NewCondVar(threadMGR->threadLock);
474
 
        threadMGR->threadEndQ   = PR_NewCondVar(threadMGR->threadLock);
475
 
    }
476
 
    PR_Lock(threadMGR->threadLock);
477
 
    while (threadMGR->numRunning >= MAX_THREADS) {
478
 
        PR_WaitCondVar(threadMGR->threadStartQ, PR_INTERVAL_NO_TIMEOUT);
479
 
    }
480
 
    for (i = 0; i < threadMGR->numUsed; ++i) {
481
 
        slot = &threadMGR->threads[i];
482
 
        if (slot->running == rs_idle) 
483
 
            break;
484
 
    }
485
 
    if (i >= threadMGR->numUsed) {
486
 
        if (i >= MAX_THREADS) {
487
 
            /* something's really wrong here. */
488
 
            PORT_Assert(i < MAX_THREADS);
489
 
            PR_Unlock(threadMGR->threadLock);
490
 
            return SECFailure;
491
 
        }
492
 
        ++(threadMGR->numUsed);
493
 
        PORT_Assert(threadMGR->numUsed == i + 1);
494
 
        slot = &threadMGR->threads[i];
495
 
    }
496
 
 
497
 
    slot->a = a;
498
 
    slot->b = b;
499
 
    slot->startFunc = startFunc;
500
 
 
501
 
    threadMGR->index = i;
502
 
 
503
 
    slot->prThread = PR_CreateThread(PR_USER_THREAD,
504
 
                                     thread_wrapper, threadMGR,
505
 
                                     PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
506
 
                                     PR_JOINABLE_THREAD, 0);
507
 
 
508
 
    if (slot->prThread == NULL) {
509
 
        PR_Unlock(threadMGR->threadLock);
510
 
        printf("Failed to launch thread!\n");
511
 
        return SECFailure;
512
 
    } 
513
 
 
514
 
    slot->inUse   = 1;
515
 
    slot->running = 1;
516
 
    ++(threadMGR->numRunning);
517
 
    PR_Unlock(threadMGR->threadLock);
518
 
 
519
 
    return SECSuccess;
520
 
}
521
 
 
522
 
SECStatus 
523
 
reap_threads(GlobalThreadMgr *threadMGR)
524
 
{
525
 
    perThread * slot;
526
 
    int                 i;
527
 
 
528
 
    if (!threadMGR->threadLock)
529
 
        return SECSuccess;
530
 
    PR_Lock(threadMGR->threadLock);
531
 
    while (threadMGR->numRunning > 0) {
532
 
        PR_WaitCondVar(threadMGR->threadEndQ, PR_INTERVAL_NO_TIMEOUT);
533
 
        for (i = 0; i < threadMGR->numUsed; ++i) {
534
 
            slot = &threadMGR->threads[i];
535
 
            if (slot->running == rs_zombie)  {
536
 
                /* Handle cleanup of thread here. */
537
 
 
538
 
                /* Now make sure the thread has ended OK. */
539
 
                PR_JoinThread(slot->prThread);
540
 
                slot->running = rs_idle;
541
 
                --threadMGR->numRunning;
542
 
 
543
 
                /* notify the thread launcher. */
544
 
                PR_NotifyCondVar(threadMGR->threadStartQ);
545
 
            }
546
 
        }
547
 
    }
548
 
 
549
 
    /* Safety Sam sez: make sure count is right. */
550
 
    for (i = 0; i < threadMGR->numUsed; ++i) {
551
 
        slot = &threadMGR->threads[i];
552
 
        if (slot->running != rs_idle)  {
553
 
            fprintf(stderr, "Thread in slot %d is in state %d!\n", 
554
 
                             i, slot->running);
555
 
        }
556
 
    }
557
 
    PR_Unlock(threadMGR->threadLock);
558
 
    return SECSuccess;
559
 
}
560
 
 
561
 
void
562
 
destroy_thread_data(GlobalThreadMgr *threadMGR)
563
 
{
564
 
    PORT_Memset(threadMGR->threads, 0, sizeof(threadMGR->threads));
565
 
 
566
 
    if (threadMGR->threadEndQ) {
567
 
        PR_DestroyCondVar(threadMGR->threadEndQ);
568
 
        threadMGR->threadEndQ = NULL;
569
 
    }
570
 
    if (threadMGR->threadStartQ) {
571
 
        PR_DestroyCondVar(threadMGR->threadStartQ);
572
 
        threadMGR->threadStartQ = NULL;
573
 
    }
574
 
    if (threadMGR->threadLock) {
575
 
        PR_DestroyLock(threadMGR->threadLock);
576
 
        threadMGR->threadLock = NULL;
577
 
    }
578
 
}
579
 
 
580
 
/**************************************************************************
581
 
** End   thread management routines.
582
 
**************************************************************************/
583
 
 
584
 
void 
585
 
lockedVars_Init( lockedVars * lv)
586
 
{
587
 
    lv->count   = 0;
588
 
    lv->waiters = 0;
589
 
    lv->lock    = PR_NewLock();
590
 
    lv->condVar = PR_NewCondVar(lv->lock);
591
 
}
592
 
 
593
 
void
594
 
lockedVars_Destroy( lockedVars * lv)
595
 
{
596
 
    PR_DestroyCondVar(lv->condVar);
597
 
    lv->condVar = NULL;
598
 
 
599
 
    PR_DestroyLock(lv->lock);
600
 
    lv->lock = NULL;
601
 
}
602
 
 
603
 
void
604
 
lockedVars_WaitForDone(lockedVars * lv)
605
 
{
606
 
    PR_Lock(lv->lock);
607
 
    while (lv->count > 0) {
608
 
        PR_WaitCondVar(lv->condVar, PR_INTERVAL_NO_TIMEOUT);
609
 
    }
610
 
    PR_Unlock(lv->lock);
611
 
}
612
 
 
613
 
int     /* returns count */
614
 
lockedVars_AddToCount(lockedVars * lv, int addend)
615
 
{
616
 
    int rv;
617
 
 
618
 
    PR_Lock(lv->lock);
619
 
    rv = lv->count += addend;
620
 
    if (rv <= 0) {
621
 
        PR_NotifyCondVar(lv->condVar);
622
 
    }
623
 
    PR_Unlock(lv->lock);
624
 
    return rv;
625
 
}
626
 
 
627
 
 
628
 
/*
629
 
 * Dump cert chain in to cert.* files. This function is will
630
 
 * create collisions while dumping cert chains if called from
631
 
 * multiple treads. But it should not be a problem since we
632
 
 * consider vfyserv to be single threaded(see bug 353477).
633
 
 */
634
 
 
635
 
void
636
 
dumpCertChain(CERTCertificate *cert, SECCertUsage usage)
637
 
{
638
 
    CERTCertificateList *certList;
639
 
    int count = 0;
640
 
 
641
 
    certList = CERT_CertChainFromCert(cert, usage, PR_TRUE);
642
 
    if (certList == NULL) {
643
 
        errWarn("CERT_CertChainFromCert");
644
 
        return;
645
 
    }
646
 
 
647
 
    for(count = 0; count < (unsigned int)certList->len; count++) {
648
 
        char certFileName[16];
649
 
        PRFileDesc *cfd;
650
 
 
651
 
        PR_snprintf(certFileName, sizeof certFileName, "cert.%03d",
652
 
                    count);
653
 
        cfd = PR_Open(certFileName, PR_WRONLY|PR_CREATE_FILE|PR_TRUNCATE, 
654
 
                      0664);
655
 
        if (!cfd) {
656
 
            PR_fprintf(PR_STDOUT,
657
 
                       "Error: couldn't save cert der in file '%s'\n",
658
 
                       certFileName);
659
 
        } else {
660
 
            PR_Write(cfd,  certList->certs[count].data,  certList->certs[count].len);
661
 
            PR_Close(cfd);
662
 
            PR_fprintf(PR_STDOUT, "Cert file %s was created.\n", certFileName);
663
 
        }
664
 
    }
665
 
    CERT_DestroyCertificateList(certList);
666
 
}