/src/nss/lib/ssl/sslsnce.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This file implements the SERVER Session ID cache. |
3 | | * NOTE: The contents of this file are NOT used by the client. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
8 | | |
9 | | /* Note: ssl_FreeSID() in sslnonce.c gets used for both client and server |
10 | | * cache sids! |
11 | | * |
12 | | * About record locking among different server processes: |
13 | | * |
14 | | * All processes that are part of the same conceptual server (serving on |
15 | | * the same address and port) MUST share a common SSL session cache. |
16 | | * This code makes the content of the shared cache accessible to all |
17 | | * processes on the same "server". This code works on Unix and Win32 only. |
18 | | * |
19 | | * We use NSPR anonymous shared memory and move data to & from shared memory. |
20 | | * We must do explicit locking of the records for all reads and writes. |
21 | | * The set of Cache entries are divided up into "sets" of 128 entries. |
22 | | * Each set is protected by a lock. There may be one or more sets protected |
23 | | * by each lock. That is, locks to sets are 1:N. |
24 | | * There is one lock for the entire cert cache. |
25 | | * There is one lock for the set of wrapped sym wrap keys. |
26 | | * |
27 | | * The anonymous shared memory is laid out as if it were declared like this: |
28 | | * |
29 | | * struct { |
30 | | * cacheDescriptor desc; |
31 | | * sidCacheLock sidCacheLocks[ numSIDCacheLocks]; |
32 | | * sidCacheLock keyCacheLock; |
33 | | * sidCacheLock certCacheLock; |
34 | | * sidCacheSet sidCacheSets[ numSIDCacheSets ]; |
35 | | * sidCacheEntry sidCacheData[ numSIDCacheEntries]; |
36 | | * certCacheEntry certCacheData[numCertCacheEntries]; |
37 | | * SSLWrappedSymWrappingKey keyCacheData[SSL_NUM_WRAP_KEYS][SSL_NUM_WRAP_MECHS]; |
38 | | * PRUint8 keyNameSuffix[SELF_ENCRYPT_KEY_VAR_NAME_LEN] |
39 | | * encKeyCacheEntry ticketEncKey; // Wrapped |
40 | | * encKeyCacheEntry ticketMacKey; // Wrapped |
41 | | * PRBool ticketKeysValid; |
42 | | * sidCacheLock srvNameCacheLock; |
43 | | * srvNameCacheEntry srvNameData[ numSrvNameCacheEntries ]; |
44 | | * } cacheMemCacheData; |
45 | | */ |
46 | | #include "seccomon.h" |
47 | | |
48 | | #if defined(XP_UNIX) || defined(XP_WIN32) || defined(XP_OS2) |
49 | | |
50 | | #include "cert.h" |
51 | | #include "ssl.h" |
52 | | #include "sslimpl.h" |
53 | | #include "sslproto.h" |
54 | | #include "pk11func.h" |
55 | | #include "base64.h" |
56 | | #include "keyhi.h" |
57 | | #include "blapit.h" |
58 | | #include "nss.h" /* for NSS_RegisterShutdown */ |
59 | | #include "sechash.h" |
60 | | #include "selfencrypt.h" |
61 | | #include <stdio.h> |
62 | | |
63 | | #if defined(XP_UNIX) |
64 | | |
65 | | #include <syslog.h> |
66 | | #include <fcntl.h> |
67 | | #include <unistd.h> |
68 | | #include <errno.h> |
69 | | #include <signal.h> |
70 | | #include "unix_err.h" |
71 | | |
72 | | #else |
73 | | |
74 | | #ifdef XP_WIN32 |
75 | | #include <wtypes.h> |
76 | | #include "win32err.h" |
77 | | #endif |
78 | | |
79 | | #endif |
80 | | #include <sys/types.h> |
81 | | |
82 | | #include "nspr.h" |
83 | | #include "sslmutex.h" |
84 | | |
85 | | /* |
86 | | ** Format of a cache entry in the shared memory. |
87 | | */ |
88 | | PR_STATIC_ASSERT(sizeof(PRTime) == 8); |
89 | | struct sidCacheEntryStr { |
90 | | /* 16 */ PRIPv6Addr addr; /* client's IP address */ |
91 | | /* 8 */ PRTime creationTime; |
92 | | /* 8 */ PRTime lastAccessTime; |
93 | | /* 8 */ PRTime expirationTime; |
94 | | /* 2 */ PRUint16 version; |
95 | | /* 1 */ PRUint8 valid; |
96 | | /* 1 */ PRUint8 sessionIDLength; |
97 | | /* 32 */ PRUint8 sessionID[SSL3_SESSIONID_BYTES]; |
98 | | /* 2 */ PRUint16 authType; |
99 | | /* 2 */ PRUint16 authKeyBits; |
100 | | /* 2 */ PRUint16 keaType; |
101 | | /* 2 */ PRUint16 keaKeyBits; |
102 | | /* 4 */ PRUint32 signatureScheme; |
103 | | /* 4 */ PRUint32 keaGroup; |
104 | | /* 92 - common header total */ |
105 | | |
106 | | union { |
107 | | struct { |
108 | | /* 2 */ ssl3CipherSuite cipherSuite; |
109 | | /* 52 */ ssl3SidKeys keys; /* keys, wrapped as needed. */ |
110 | | |
111 | | /* 4 */ PRUint32 masterWrapMech; |
112 | | /* 4 */ PRInt32 certIndex; |
113 | | /* 4 */ PRInt32 srvNameIndex; |
114 | | /* 32 */ PRUint8 srvNameHash[SHA256_LENGTH]; /* SHA256 name hash */ |
115 | | /* 2 */ PRUint16 namedCurve; |
116 | | /*100 */} ssl3; |
117 | | |
118 | | /* force sizeof(sidCacheEntry) to be a multiple of cache line size */ |
119 | | struct { |
120 | | /*116 */ PRUint8 filler[116]; /* 92+116==208, a multiple of 16 */ |
121 | | } forceSize; |
122 | | } u; |
123 | | }; |
124 | | typedef struct sidCacheEntryStr sidCacheEntry; |
125 | | |
126 | | /* The length of this struct is supposed to be a power of 2, e.g. 4KB */ |
127 | | struct certCacheEntryStr { |
128 | | PRUint16 certLength; /* 2 */ |
129 | | PRUint16 sessionIDLength; /* 2 */ |
130 | | PRUint8 sessionID[SSL3_SESSIONID_BYTES]; /* 32 */ |
131 | | PRUint8 cert[SSL_MAX_CACHED_CERT_LEN]; /* 4060 */ |
132 | | }; /* total 4096 */ |
133 | | typedef struct certCacheEntryStr certCacheEntry; |
134 | | |
135 | | struct sidCacheLockStr { |
136 | | PRUint32 timeStamp; |
137 | | sslMutex mutex; |
138 | | sslPID pid; |
139 | | }; |
140 | | typedef struct sidCacheLockStr sidCacheLock; |
141 | | |
142 | | struct sidCacheSetStr { |
143 | | PRIntn next; |
144 | | }; |
145 | | typedef struct sidCacheSetStr sidCacheSet; |
146 | | |
147 | | struct encKeyCacheEntryStr { |
148 | | PRUint8 bytes[512]; |
149 | | PRInt32 length; |
150 | | }; |
151 | | typedef struct encKeyCacheEntryStr encKeyCacheEntry; |
152 | | |
153 | 0 | #define SSL_MAX_DNS_HOST_NAME 1024 |
154 | | |
155 | | struct srvNameCacheEntryStr { |
156 | | PRUint16 type; /* 2 */ |
157 | | PRUint16 nameLen; /* 2 */ |
158 | | PRUint8 name[SSL_MAX_DNS_HOST_NAME + 12]; /* 1034 */ |
159 | | PRUint8 nameHash[SHA256_LENGTH]; /* 32 */ |
160 | | /* 1072 */ |
161 | | }; |
162 | | typedef struct srvNameCacheEntryStr srvNameCacheEntry; |
163 | | |
164 | | struct cacheDescStr { |
165 | | |
166 | | PRUint32 cacheMemSize; |
167 | | |
168 | | PRUint32 numSIDCacheLocks; |
169 | | PRUint32 numSIDCacheSets; |
170 | | PRUint32 numSIDCacheSetsPerLock; |
171 | | |
172 | | PRUint32 numSIDCacheEntries; |
173 | | PRUint32 sidCacheSize; |
174 | | |
175 | | PRUint32 numCertCacheEntries; |
176 | | PRUint32 certCacheSize; |
177 | | |
178 | | PRUint32 numKeyCacheEntries; |
179 | | PRUint32 keyCacheSize; |
180 | | |
181 | | PRUint32 numSrvNameCacheEntries; |
182 | | PRUint32 srvNameCacheSize; |
183 | | |
184 | | PRUint32 ssl3Timeout; |
185 | | |
186 | | PRUint32 numSIDCacheLocksInitialized; |
187 | | |
188 | | /* These values are volatile, and are accessed through sharedCache-> */ |
189 | | PRUint32 nextCertCacheEntry; /* certCacheLock protects */ |
190 | | PRBool stopPolling; |
191 | | PRBool everInherited; |
192 | | |
193 | | /* The private copies of these values are pointers into shared mem */ |
194 | | /* The copies of these values in shared memory are merely offsets */ |
195 | | sidCacheLock *sidCacheLocks; |
196 | | sidCacheLock *keyCacheLock; |
197 | | sidCacheLock *certCacheLock; |
198 | | sidCacheLock *srvNameCacheLock; |
199 | | sidCacheSet *sidCacheSets; |
200 | | sidCacheEntry *sidCacheData; |
201 | | certCacheEntry *certCacheData; |
202 | | SSLWrappedSymWrappingKey *keyCacheData; |
203 | | PRUint8 *ticketKeyNameSuffix; |
204 | | encKeyCacheEntry *ticketEncKey; |
205 | | encKeyCacheEntry *ticketMacKey; |
206 | | PRUint32 *ticketKeysValid; |
207 | | srvNameCacheEntry *srvNameCacheData; |
208 | | |
209 | | /* Only the private copies of these pointers are valid */ |
210 | | char *cacheMem; |
211 | | struct cacheDescStr *sharedCache; /* shared copy of this struct */ |
212 | | PRFileMap *cacheMemMap; |
213 | | PRThread *poller; |
214 | | PRUint32 mutexTimeout; |
215 | | PRBool shared; |
216 | | }; |
217 | | typedef struct cacheDescStr cacheDesc; |
218 | | |
219 | | static cacheDesc globalCache; |
220 | | |
221 | | static const char envVarName[] = { SSL_ENV_VAR_NAME }; |
222 | | |
223 | | static PRBool isMultiProcess = PR_FALSE; |
224 | | |
225 | 0 | #define DEF_SID_CACHE_ENTRIES 10000 |
226 | | #define DEF_CERT_CACHE_ENTRIES 250 |
227 | 0 | #define MIN_CERT_CACHE_ENTRIES 125 /* the effective size in old releases. */ |
228 | | #define DEF_KEY_CACHE_ENTRIES 250 |
229 | 0 | #define DEF_NAME_CACHE_ENTRIES 1000 |
230 | | |
231 | 0 | #define SID_CACHE_ENTRIES_PER_SET 128 |
232 | | #define SID_ALIGNMENT 16 |
233 | | |
234 | 0 | #define DEF_SSL3_TIMEOUT 86400L /* 24 hours */ |
235 | 0 | #define MAX_SSL3_TIMEOUT 86400L /* 24 hours */ |
236 | 0 | #define MIN_SSL3_TIMEOUT 5 /* seconds */ |
237 | | |
238 | | #if defined(AIX) || defined(LINUX) || defined(NETBSD) || defined(OPENBSD) |
239 | | #define MAX_SID_CACHE_LOCKS 8 /* two FDs per lock */ |
240 | | #else |
241 | | #define MAX_SID_CACHE_LOCKS 256 |
242 | | #endif |
243 | | |
244 | 0 | #define SID_HOWMANY(val, size) (((val) + ((size)-1)) / (size)) |
245 | 0 | #define SID_ROUNDUP(val, size) ((size)*SID_HOWMANY((val), (size))) |
246 | | |
247 | | static sslPID myPid; |
248 | | static PRUint32 ssl_max_sid_cache_locks = MAX_SID_CACHE_LOCKS; |
249 | | |
250 | | /* forward static function declarations */ |
251 | | static PRUint32 SIDindex(cacheDesc *cache, const PRIPv6Addr *addr, PRUint8 *s, |
252 | | unsigned nl); |
253 | | #if defined(XP_UNIX) |
254 | | static SECStatus LaunchLockPoller(cacheDesc *cache); |
255 | | static SECStatus StopLockPoller(cacheDesc *cache); |
256 | | #endif |
257 | | |
258 | | struct inheritanceStr { |
259 | | PRUint32 cacheMemSize; |
260 | | PRUint32 fmStrLen; |
261 | | }; |
262 | | |
263 | | typedef struct inheritanceStr inheritance; |
264 | | |
265 | | #if defined(_WIN32) || defined(XP_OS2) |
266 | | |
267 | | #define DEFAULT_CACHE_DIRECTORY "\\temp" |
268 | | |
269 | | #endif /* _win32 */ |
270 | | |
271 | | #if defined(XP_UNIX) |
272 | | |
273 | 0 | #define DEFAULT_CACHE_DIRECTORY "/tmp" |
274 | | |
275 | | #endif /* XP_UNIX */ |
276 | | |
277 | | /************************************************************************/ |
278 | | |
279 | | /* SSL Session Cache has a smaller set of functions to initialize than |
280 | | * ssl does. some ssl_functions can't be initialized before NSS has been |
281 | | * initialized, and the cache may be configured before NSS is initialized |
282 | | * so thus the special init function */ |
283 | | static SECStatus |
284 | | ssl_InitSessionCache() |
285 | 0 | { |
286 | | /* currently only one function, which is itself idempotent */ |
287 | 0 | return ssl_InitializePRErrorTable(); |
288 | 0 | } |
289 | | |
290 | | /* This is used to set locking times for the cache. It is not used to set the |
291 | | * PRTime attributes of sessions, which are driven by ss->now(). */ |
292 | | static PRUint32 |
293 | | ssl_CacheNow() |
294 | 0 | { |
295 | 0 | return PR_Now() / PR_USEC_PER_SEC; |
296 | 0 | } |
297 | | |
298 | | static PRUint32 |
299 | | LockSidCacheLock(sidCacheLock *lock, PRUint32 now) |
300 | 0 | { |
301 | 0 | SECStatus rv = sslMutex_Lock(&lock->mutex); |
302 | 0 | if (rv != SECSuccess) |
303 | 0 | return 0; |
304 | 0 | if (!now) { |
305 | 0 | now = ssl_CacheNow(); |
306 | 0 | } |
307 | |
|
308 | 0 | lock->timeStamp = now; |
309 | 0 | lock->pid = myPid; |
310 | 0 | return now; |
311 | 0 | } |
312 | | |
313 | | static SECStatus |
314 | | UnlockSidCacheLock(sidCacheLock *lock) |
315 | 0 | { |
316 | 0 | SECStatus rv; |
317 | |
|
318 | 0 | lock->pid = 0; |
319 | 0 | rv = sslMutex_Unlock(&lock->mutex); |
320 | 0 | return rv; |
321 | 0 | } |
322 | | |
323 | | /* Returns non-zero |now| or ssl_CacheNow() on success, zero on failure. */ |
324 | | static PRUint32 |
325 | | LockSet(cacheDesc *cache, PRUint32 set, PRUint32 now) |
326 | 0 | { |
327 | 0 | PRUint32 lockNum = set % cache->numSIDCacheLocks; |
328 | 0 | sidCacheLock *lock = cache->sidCacheLocks + lockNum; |
329 | |
|
330 | 0 | return LockSidCacheLock(lock, now); |
331 | 0 | } |
332 | | |
333 | | static SECStatus |
334 | | UnlockSet(cacheDesc *cache, PRUint32 set) |
335 | 0 | { |
336 | 0 | PRUint32 lockNum = set % cache->numSIDCacheLocks; |
337 | 0 | sidCacheLock *lock = cache->sidCacheLocks + lockNum; |
338 | |
|
339 | 0 | return UnlockSidCacheLock(lock); |
340 | 0 | } |
341 | | |
342 | | /************************************************************************/ |
343 | | |
344 | | /* Put a certificate in the cache. Update the cert index in the sce. |
345 | | */ |
346 | | static PRUint32 |
347 | | CacheCert(cacheDesc *cache, CERTCertificate *cert, sidCacheEntry *sce) |
348 | 0 | { |
349 | 0 | PRUint32 now; |
350 | 0 | certCacheEntry cce; |
351 | |
|
352 | 0 | if ((cert->derCert.len > SSL_MAX_CACHED_CERT_LEN) || |
353 | 0 | (cert->derCert.len <= 0) || |
354 | 0 | (cert->derCert.data == NULL)) { |
355 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
356 | 0 | return 0; |
357 | 0 | } |
358 | | |
359 | 0 | cce.sessionIDLength = sce->sessionIDLength; |
360 | 0 | PORT_Memcpy(cce.sessionID, sce->sessionID, cce.sessionIDLength); |
361 | |
|
362 | 0 | cce.certLength = cert->derCert.len; |
363 | 0 | PORT_Memcpy(cce.cert, cert->derCert.data, cce.certLength); |
364 | | |
365 | | /* get lock on cert cache */ |
366 | 0 | now = LockSidCacheLock(cache->certCacheLock, 0); |
367 | 0 | if (now) { |
368 | | |
369 | | /* Find where to place the next cert cache entry. */ |
370 | 0 | cacheDesc *sharedCache = cache->sharedCache; |
371 | 0 | PRUint32 ndx = sharedCache->nextCertCacheEntry; |
372 | | |
373 | | /* write the entry */ |
374 | 0 | cache->certCacheData[ndx] = cce; |
375 | | |
376 | | /* remember where we put it. */ |
377 | 0 | sce->u.ssl3.certIndex = ndx; |
378 | | |
379 | | /* update the "next" cache entry index */ |
380 | 0 | sharedCache->nextCertCacheEntry = |
381 | 0 | (ndx + 1) % cache->numCertCacheEntries; |
382 | |
|
383 | 0 | UnlockSidCacheLock(cache->certCacheLock); |
384 | 0 | } |
385 | 0 | return now; |
386 | 0 | } |
387 | | |
388 | | /* Server configuration hash tables need to account the SECITEM.type |
389 | | * field as well. These functions accomplish that. */ |
390 | | static PLHashNumber |
391 | | Get32BitNameHash(const SECItem *name) |
392 | 0 | { |
393 | 0 | PLHashNumber rv = SECITEM_Hash(name); |
394 | |
|
395 | 0 | PRUint8 *rvc = (PRUint8 *)&rv; |
396 | 0 | rvc[name->len % sizeof(rv)] ^= name->type; |
397 | |
|
398 | 0 | return rv; |
399 | 0 | } |
400 | | |
401 | | /* Put a name in the cache. Update the cert index in the sce. |
402 | | */ |
403 | | static PRUint32 |
404 | | CacheSrvName(cacheDesc *cache, SECItem *name, sidCacheEntry *sce) |
405 | 0 | { |
406 | 0 | PRUint32 now; |
407 | 0 | PRUint32 ndx; |
408 | 0 | srvNameCacheEntry snce; |
409 | |
|
410 | 0 | if (!name || name->len <= 0 || |
411 | 0 | name->len > SSL_MAX_DNS_HOST_NAME) { |
412 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
413 | 0 | return 0; |
414 | 0 | } |
415 | | |
416 | 0 | snce.type = name->type; |
417 | 0 | snce.nameLen = name->len; |
418 | 0 | PORT_Memcpy(snce.name, name->data, snce.nameLen); |
419 | 0 | HASH_HashBuf(HASH_AlgSHA256, snce.nameHash, name->data, name->len); |
420 | | |
421 | | /* get index of the next name */ |
422 | 0 | ndx = Get32BitNameHash(name); |
423 | | /* get lock on cert cache */ |
424 | 0 | now = LockSidCacheLock(cache->srvNameCacheLock, 0); |
425 | 0 | if (now) { |
426 | 0 | if (cache->numSrvNameCacheEntries > 0) { |
427 | | /* Fit the index into array */ |
428 | 0 | ndx %= cache->numSrvNameCacheEntries; |
429 | | /* write the entry */ |
430 | 0 | cache->srvNameCacheData[ndx] = snce; |
431 | | /* remember where we put it. */ |
432 | 0 | sce->u.ssl3.srvNameIndex = ndx; |
433 | | /* Copy hash into sid hash */ |
434 | 0 | PORT_Memcpy(sce->u.ssl3.srvNameHash, snce.nameHash, SHA256_LENGTH); |
435 | 0 | } |
436 | 0 | UnlockSidCacheLock(cache->srvNameCacheLock); |
437 | 0 | } |
438 | 0 | return now; |
439 | 0 | } |
440 | | |
441 | | /* |
442 | | ** Convert local SID to shared memory one |
443 | | */ |
444 | | static void |
445 | | ConvertFromSID(sidCacheEntry *to, sslSessionID *from) |
446 | 0 | { |
447 | 0 | to->valid = 1; |
448 | 0 | to->version = from->version; |
449 | 0 | to->addr = from->addr; |
450 | 0 | to->creationTime = from->creationTime; |
451 | 0 | to->lastAccessTime = from->lastAccessTime; |
452 | 0 | to->expirationTime = from->expirationTime; |
453 | 0 | to->authType = from->authType; |
454 | 0 | to->authKeyBits = from->authKeyBits; |
455 | 0 | to->keaType = from->keaType; |
456 | 0 | to->keaKeyBits = from->keaKeyBits; |
457 | 0 | to->keaGroup = from->keaGroup; |
458 | 0 | to->signatureScheme = from->sigScheme; |
459 | |
|
460 | 0 | to->u.ssl3.cipherSuite = from->u.ssl3.cipherSuite; |
461 | 0 | to->u.ssl3.keys = from->u.ssl3.keys; |
462 | 0 | to->u.ssl3.masterWrapMech = from->u.ssl3.masterWrapMech; |
463 | 0 | to->sessionIDLength = from->u.ssl3.sessionIDLength; |
464 | 0 | to->u.ssl3.certIndex = -1; |
465 | 0 | to->u.ssl3.srvNameIndex = -1; |
466 | 0 | PORT_Memcpy(to->sessionID, from->u.ssl3.sessionID, |
467 | 0 | to->sessionIDLength); |
468 | 0 | to->u.ssl3.namedCurve = 0U; |
469 | 0 | if (from->authType == ssl_auth_ecdsa || |
470 | 0 | from->authType == ssl_auth_ecdh_rsa || |
471 | 0 | from->authType == ssl_auth_ecdh_ecdsa) { |
472 | 0 | PORT_Assert(from->namedCurve); |
473 | 0 | to->u.ssl3.namedCurve = (PRUint16)from->namedCurve->name; |
474 | 0 | } |
475 | |
|
476 | 0 | SSL_TRC(8, ("%d: SSL3: ConvertSID: time=%d addr=0x%08x%08x%08x%08x " |
477 | 0 | "cipherSuite=%d", |
478 | 0 | myPid, to->creationTime / PR_USEC_PER_SEC, |
479 | 0 | to->addr.pr_s6_addr32[0], to->addr.pr_s6_addr32[1], |
480 | 0 | to->addr.pr_s6_addr32[2], to->addr.pr_s6_addr32[3], |
481 | 0 | to->u.ssl3.cipherSuite)); |
482 | 0 | } |
483 | | |
484 | | /* |
485 | | ** Convert shared memory cache-entry to local memory based one |
486 | | ** This is only called from ServerSessionIDLookup(). |
487 | | */ |
488 | | static sslSessionID * |
489 | | ConvertToSID(sidCacheEntry *from, |
490 | | certCacheEntry *pcce, |
491 | | srvNameCacheEntry *psnce, |
492 | | CERTCertDBHandle *dbHandle) |
493 | 0 | { |
494 | 0 | sslSessionID *to; |
495 | |
|
496 | 0 | to = PORT_ZNew(sslSessionID); |
497 | 0 | if (!to) { |
498 | 0 | return 0; |
499 | 0 | } |
500 | | |
501 | 0 | to->u.ssl3.sessionIDLength = from->sessionIDLength; |
502 | 0 | to->u.ssl3.cipherSuite = from->u.ssl3.cipherSuite; |
503 | 0 | to->u.ssl3.keys = from->u.ssl3.keys; |
504 | 0 | to->u.ssl3.masterWrapMech = from->u.ssl3.masterWrapMech; |
505 | 0 | if (from->u.ssl3.srvNameIndex != -1 && psnce) { |
506 | 0 | SECItem name; |
507 | 0 | SECStatus rv; |
508 | 0 | name.type = psnce->type; |
509 | 0 | name.len = psnce->nameLen; |
510 | 0 | name.data = psnce->name; |
511 | 0 | rv = SECITEM_CopyItem(NULL, &to->u.ssl3.srvName, &name); |
512 | 0 | if (rv != SECSuccess) { |
513 | 0 | goto loser; |
514 | 0 | } |
515 | 0 | } |
516 | | |
517 | 0 | PORT_Memcpy(to->u.ssl3.sessionID, from->sessionID, from->sessionIDLength); |
518 | |
|
519 | 0 | to->urlSvrName = NULL; |
520 | |
|
521 | 0 | to->u.ssl3.masterModuleID = (SECMODModuleID)-1; /* invalid value */ |
522 | 0 | to->u.ssl3.masterSlotID = (CK_SLOT_ID)-1; /* invalid value */ |
523 | 0 | to->u.ssl3.masterWrapIndex = 0; |
524 | 0 | to->u.ssl3.masterWrapSeries = 0; |
525 | 0 | to->u.ssl3.masterValid = PR_FALSE; |
526 | |
|
527 | 0 | to->u.ssl3.clAuthModuleID = (SECMODModuleID)-1; /* invalid value */ |
528 | 0 | to->u.ssl3.clAuthSlotID = (CK_SLOT_ID)-1; /* invalid value */ |
529 | 0 | to->u.ssl3.clAuthSeries = 0; |
530 | 0 | to->u.ssl3.clAuthValid = PR_FALSE; |
531 | |
|
532 | 0 | if (from->u.ssl3.certIndex != -1 && pcce) { |
533 | 0 | SECItem derCert; |
534 | |
|
535 | 0 | derCert.len = pcce->certLength; |
536 | 0 | derCert.data = pcce->cert; |
537 | |
|
538 | 0 | to->peerCert = CERT_NewTempCertificate(dbHandle, &derCert, NULL, |
539 | 0 | PR_FALSE, PR_TRUE); |
540 | 0 | if (to->peerCert == NULL) |
541 | 0 | goto loser; |
542 | 0 | } |
543 | 0 | if (from->authType == ssl_auth_ecdsa || |
544 | 0 | from->authType == ssl_auth_ecdh_rsa || |
545 | 0 | from->authType == ssl_auth_ecdh_ecdsa) { |
546 | 0 | to->namedCurve = |
547 | 0 | ssl_LookupNamedGroup((SSLNamedGroup)from->u.ssl3.namedCurve); |
548 | 0 | } |
549 | |
|
550 | 0 | to->version = from->version; |
551 | 0 | to->creationTime = from->creationTime; |
552 | 0 | to->lastAccessTime = from->lastAccessTime; |
553 | 0 | to->expirationTime = from->expirationTime; |
554 | 0 | to->cached = in_server_cache; |
555 | 0 | to->addr = from->addr; |
556 | 0 | to->references = 1; |
557 | 0 | to->authType = from->authType; |
558 | 0 | to->authKeyBits = from->authKeyBits; |
559 | 0 | to->keaType = from->keaType; |
560 | 0 | to->keaKeyBits = from->keaKeyBits; |
561 | 0 | to->keaGroup = from->keaGroup; |
562 | 0 | to->sigScheme = from->signatureScheme; |
563 | |
|
564 | 0 | return to; |
565 | | |
566 | 0 | loser: |
567 | 0 | if (to) { |
568 | 0 | SECITEM_FreeItem(&to->u.ssl3.srvName, PR_FALSE); |
569 | 0 | PORT_Free(to); |
570 | 0 | } |
571 | 0 | return NULL; |
572 | 0 | } |
573 | | |
574 | | /* |
575 | | ** Perform some mumbo jumbo on the ip-address and the session-id value to |
576 | | ** compute a hash value. |
577 | | */ |
578 | | static PRUint32 |
579 | | SIDindex(cacheDesc *cache, const PRIPv6Addr *addr, PRUint8 *s, unsigned nl) |
580 | 0 | { |
581 | 0 | PRUint32 rv; |
582 | 0 | PRUint32 x[8]; |
583 | |
|
584 | 0 | memset(x, 0, sizeof x); |
585 | 0 | if (nl > sizeof x) |
586 | 0 | nl = sizeof x; |
587 | 0 | memcpy(x, s, nl); |
588 | |
|
589 | 0 | rv = (addr->pr_s6_addr32[0] ^ addr->pr_s6_addr32[1] ^ |
590 | 0 | addr->pr_s6_addr32[2] ^ addr->pr_s6_addr32[3] ^ |
591 | 0 | x[0] ^ x[1] ^ x[2] ^ x[3] ^ x[4] ^ x[5] ^ x[6] ^ x[7]) % |
592 | 0 | cache->numSIDCacheSets; |
593 | 0 | return rv; |
594 | 0 | } |
595 | | |
596 | | /* |
597 | | ** Look something up in the cache. This will invalidate old entries |
598 | | ** in the process. Caller has locked the cache set! |
599 | | ** Returns PR_TRUE if found a valid match. PR_FALSE otherwise. |
600 | | */ |
601 | | static sidCacheEntry * |
602 | | FindSID(cacheDesc *cache, PRUint32 setNum, PRUint32 now, |
603 | | const PRIPv6Addr *addr, unsigned char *sessionID, |
604 | | unsigned sessionIDLength) |
605 | 0 | { |
606 | 0 | PRUint32 ndx = cache->sidCacheSets[setNum].next; |
607 | 0 | int i; |
608 | |
|
609 | 0 | sidCacheEntry *set = cache->sidCacheData + |
610 | 0 | (setNum * SID_CACHE_ENTRIES_PER_SET); |
611 | |
|
612 | 0 | for (i = SID_CACHE_ENTRIES_PER_SET; i > 0; --i) { |
613 | 0 | sidCacheEntry *sce; |
614 | |
|
615 | 0 | ndx = (ndx - 1) % SID_CACHE_ENTRIES_PER_SET; |
616 | 0 | sce = set + ndx; |
617 | |
|
618 | 0 | if (!sce->valid) |
619 | 0 | continue; |
620 | | |
621 | 0 | if (now > sce->expirationTime) { |
622 | | /* SessionID has timed out. Invalidate the entry. */ |
623 | 0 | SSL_TRC(7, ("%d: timed out sid entry addr=%08x%08x%08x%08x now=%x " |
624 | 0 | "time+=%x", |
625 | 0 | myPid, sce->addr.pr_s6_addr32[0], |
626 | 0 | sce->addr.pr_s6_addr32[1], sce->addr.pr_s6_addr32[2], |
627 | 0 | sce->addr.pr_s6_addr32[3], now, |
628 | 0 | sce->expirationTime)); |
629 | 0 | sce->valid = 0; |
630 | 0 | continue; |
631 | 0 | } |
632 | | |
633 | | /* |
634 | | ** Next, examine specific session-id/addr data to see if the cache |
635 | | ** entry matches our addr+session-id value |
636 | | */ |
637 | 0 | if (sessionIDLength == sce->sessionIDLength && |
638 | 0 | !memcmp(&sce->addr, addr, sizeof(PRIPv6Addr)) && |
639 | 0 | !memcmp(sce->sessionID, sessionID, sessionIDLength)) { |
640 | | /* Found it */ |
641 | 0 | return sce; |
642 | 0 | } |
643 | 0 | } |
644 | | |
645 | 0 | PORT_SetError(SSL_ERROR_SESSION_NOT_FOUND); |
646 | 0 | return NULL; |
647 | 0 | } |
648 | | |
649 | | /************************************************************************/ |
650 | | |
651 | | /* This is the primary function for finding entries in the server's sid cache. |
652 | | * Although it is static, this function is called via the global function |
653 | | * pointer ssl_sid_lookup. |
654 | | * |
655 | | * sslNow is the time that the calling socket understands, which might be |
656 | | * different than what the cache uses to maintain its locks. |
657 | | */ |
658 | | static sslSessionID * |
659 | | ServerSessionIDLookup(PRTime sslNow, const PRIPv6Addr *addr, |
660 | | unsigned char *sessionID, |
661 | | unsigned int sessionIDLength, |
662 | | CERTCertDBHandle *dbHandle) |
663 | 0 | { |
664 | 0 | sslSessionID *sid = 0; |
665 | 0 | sidCacheEntry *psce; |
666 | 0 | certCacheEntry *pcce = 0; |
667 | 0 | srvNameCacheEntry *psnce = 0; |
668 | 0 | cacheDesc *cache = &globalCache; |
669 | 0 | PRUint32 now; |
670 | 0 | PRUint32 set; |
671 | 0 | PRInt32 cndx; |
672 | 0 | sidCacheEntry sce; |
673 | 0 | certCacheEntry cce; |
674 | 0 | srvNameCacheEntry snce; |
675 | |
|
676 | 0 | set = SIDindex(cache, addr, sessionID, sessionIDLength); |
677 | 0 | now = LockSet(cache, set, 0); |
678 | 0 | if (!now) |
679 | 0 | return NULL; |
680 | | |
681 | 0 | psce = FindSID(cache, set, now, addr, sessionID, sessionIDLength); |
682 | 0 | if (psce) { |
683 | 0 | if ((cndx = psce->u.ssl3.certIndex) != -1) { |
684 | 0 | PRUint32 gotLock = LockSidCacheLock(cache->certCacheLock, now); |
685 | 0 | if (gotLock) { |
686 | 0 | pcce = &cache->certCacheData[cndx]; |
687 | | |
688 | | /* See if the cert's session ID matches the sce cache. */ |
689 | 0 | if ((pcce->sessionIDLength == psce->sessionIDLength) && |
690 | 0 | !PORT_Memcmp(pcce->sessionID, psce->sessionID, |
691 | 0 | pcce->sessionIDLength)) { |
692 | 0 | cce = *pcce; |
693 | 0 | } else { |
694 | | /* The cert doesen't match the SID cache entry, |
695 | | ** so invalidate the SID cache entry. |
696 | | */ |
697 | 0 | psce->valid = 0; |
698 | 0 | psce = 0; |
699 | 0 | pcce = 0; |
700 | 0 | } |
701 | 0 | UnlockSidCacheLock(cache->certCacheLock); |
702 | 0 | } else { |
703 | | /* what the ??. Didn't get the cert cache lock. |
704 | | ** Don't invalidate the SID cache entry, but don't find it. |
705 | | */ |
706 | 0 | PORT_AssertNotReached("Didn't get cert Cache Lock!"); |
707 | 0 | psce = 0; |
708 | 0 | pcce = 0; |
709 | 0 | } |
710 | 0 | } |
711 | 0 | if (psce && ((cndx = psce->u.ssl3.srvNameIndex) != -1)) { |
712 | 0 | PRUint32 gotLock = LockSidCacheLock(cache->srvNameCacheLock, |
713 | 0 | now); |
714 | 0 | if (gotLock) { |
715 | 0 | psnce = &cache->srvNameCacheData[cndx]; |
716 | |
|
717 | 0 | if (!PORT_Memcmp(psnce->nameHash, psce->u.ssl3.srvNameHash, |
718 | 0 | SHA256_LENGTH)) { |
719 | 0 | snce = *psnce; |
720 | 0 | } else { |
721 | | /* The name doesen't match the SID cache entry, |
722 | | ** so invalidate the SID cache entry. |
723 | | */ |
724 | 0 | psce->valid = 0; |
725 | 0 | psce = 0; |
726 | 0 | psnce = 0; |
727 | 0 | } |
728 | 0 | UnlockSidCacheLock(cache->srvNameCacheLock); |
729 | 0 | } else { |
730 | | /* what the ??. Didn't get the cert cache lock. |
731 | | ** Don't invalidate the SID cache entry, but don't find it. |
732 | | */ |
733 | 0 | PORT_AssertNotReached("Didn't get name Cache Lock!"); |
734 | 0 | psce = 0; |
735 | 0 | psnce = 0; |
736 | 0 | } |
737 | 0 | } |
738 | 0 | if (psce) { |
739 | 0 | psce->lastAccessTime = sslNow; |
740 | 0 | sce = *psce; /* grab a copy while holding the lock */ |
741 | 0 | } |
742 | 0 | } |
743 | 0 | UnlockSet(cache, set); |
744 | 0 | if (psce) { |
745 | | /* sce conains a copy of the cache entry. |
746 | | ** Convert shared memory format to local format |
747 | | */ |
748 | 0 | sid = ConvertToSID(&sce, pcce ? &cce : 0, psnce ? &snce : 0, dbHandle); |
749 | 0 | } |
750 | 0 | return sid; |
751 | 0 | } |
752 | | |
753 | | /* |
754 | | ** Place a sid into the cache, if it isn't already there. |
755 | | */ |
756 | | void |
757 | | ssl_ServerCacheSessionID(sslSessionID *sid, PRTime creationTime) |
758 | 0 | { |
759 | 0 | PORT_Assert(sid); |
760 | |
|
761 | 0 | sidCacheEntry sce; |
762 | 0 | PRUint32 now = 0; |
763 | 0 | cacheDesc *cache = &globalCache; |
764 | |
|
765 | 0 | if (sid->u.ssl3.sessionIDLength == 0) { |
766 | 0 | return; |
767 | 0 | } |
768 | | |
769 | 0 | if (sid->cached == never_cached || sid->cached == invalid_cache) { |
770 | 0 | PRUint32 set; |
771 | 0 | SECItem *name; |
772 | |
|
773 | 0 | PORT_Assert(sid->creationTime != 0); |
774 | 0 | if (!sid->creationTime) |
775 | 0 | sid->lastAccessTime = sid->creationTime = creationTime; |
776 | | /* override caller's expiration time, which uses client timeout |
777 | | * duration, not server timeout duration. |
778 | | */ |
779 | 0 | sid->expirationTime = |
780 | 0 | sid->creationTime + cache->ssl3Timeout * PR_USEC_PER_SEC; |
781 | 0 | SSL_TRC(8, ("%d: SSL: CacheMT: cached=%d addr=0x%08x%08x%08x%08x time=%x " |
782 | 0 | "cipherSuite=%d", |
783 | 0 | myPid, sid->cached, |
784 | 0 | sid->addr.pr_s6_addr32[0], sid->addr.pr_s6_addr32[1], |
785 | 0 | sid->addr.pr_s6_addr32[2], sid->addr.pr_s6_addr32[3], |
786 | 0 | sid->creationTime / PR_USEC_PER_SEC, |
787 | 0 | sid->u.ssl3.cipherSuite)); |
788 | 0 | PRINT_BUF(8, (0, "sessionID:", sid->u.ssl3.sessionID, |
789 | 0 | sid->u.ssl3.sessionIDLength)); |
790 | |
|
791 | 0 | ConvertFromSID(&sce, sid); |
792 | |
|
793 | 0 | name = &sid->u.ssl3.srvName; |
794 | 0 | if (name->len && name->data) { |
795 | 0 | now = CacheSrvName(cache, name, &sce); |
796 | 0 | } |
797 | 0 | if (sid->peerCert != NULL) { |
798 | 0 | now = CacheCert(cache, sid->peerCert, &sce); |
799 | 0 | } |
800 | |
|
801 | 0 | set = SIDindex(cache, &sce.addr, sce.sessionID, sce.sessionIDLength); |
802 | 0 | now = LockSet(cache, set, now); |
803 | 0 | if (now) { |
804 | 0 | PRUint32 next = cache->sidCacheSets[set].next; |
805 | 0 | PRUint32 ndx = set * SID_CACHE_ENTRIES_PER_SET + next; |
806 | | |
807 | | /* Write out new cache entry */ |
808 | 0 | cache->sidCacheData[ndx] = sce; |
809 | |
|
810 | 0 | cache->sidCacheSets[set].next = |
811 | 0 | (next + 1) % SID_CACHE_ENTRIES_PER_SET; |
812 | |
|
813 | 0 | UnlockSet(cache, set); |
814 | 0 | sid->cached = in_server_cache; |
815 | 0 | } |
816 | 0 | } |
817 | 0 | } |
818 | | |
819 | | /* |
820 | | ** Although this is static, it is called from ssl via global function pointer |
821 | | ** ssl_sid_uncache. This invalidates the referenced cache entry. |
822 | | */ |
823 | | void |
824 | | ssl_ServerUncacheSessionID(sslSessionID *sid) |
825 | 0 | { |
826 | 0 | cacheDesc *cache = &globalCache; |
827 | 0 | PRUint8 *sessionID; |
828 | 0 | unsigned int sessionIDLength; |
829 | 0 | PRErrorCode err; |
830 | 0 | PRUint32 set; |
831 | 0 | PRUint32 now; |
832 | 0 | sidCacheEntry *psce; |
833 | |
|
834 | 0 | if (sid == NULL) |
835 | 0 | return; |
836 | | |
837 | | /* Uncaching a SID should never change the error code. |
838 | | ** So save it here and restore it before exiting. |
839 | | */ |
840 | 0 | err = PR_GetError(); |
841 | |
|
842 | 0 | sessionID = sid->u.ssl3.sessionID; |
843 | 0 | sessionIDLength = sid->u.ssl3.sessionIDLength; |
844 | 0 | SSL_TRC(8, ("%d: SSL3: UncacheMT: valid=%d addr=0x%08x%08x%08x%08x time=%x " |
845 | 0 | "cipherSuite=%d", |
846 | 0 | myPid, sid->cached, |
847 | 0 | sid->addr.pr_s6_addr32[0], sid->addr.pr_s6_addr32[1], |
848 | 0 | sid->addr.pr_s6_addr32[2], sid->addr.pr_s6_addr32[3], |
849 | 0 | sid->creationTime / PR_USEC_PER_SEC, |
850 | 0 | sid->u.ssl3.cipherSuite)); |
851 | 0 | PRINT_BUF(8, (0, "sessionID:", sessionID, sessionIDLength)); |
852 | 0 | set = SIDindex(cache, &sid->addr, sessionID, sessionIDLength); |
853 | 0 | now = LockSet(cache, set, 0); |
854 | 0 | if (now) { |
855 | 0 | psce = FindSID(cache, set, now, &sid->addr, sessionID, sessionIDLength); |
856 | 0 | if (psce) { |
857 | 0 | psce->valid = 0; |
858 | 0 | } |
859 | 0 | UnlockSet(cache, set); |
860 | 0 | } |
861 | 0 | sid->cached = invalid_cache; |
862 | 0 | PORT_SetError(err); |
863 | 0 | } |
864 | | |
865 | | #ifdef XP_OS2 |
866 | | |
867 | | #define INCL_DOSPROCESS |
868 | | #include <os2.h> |
869 | | |
870 | | long |
871 | | gettid(void) |
872 | | { |
873 | | PTIB ptib; |
874 | | PPIB ppib; |
875 | | DosGetInfoBlocks(&ptib, &ppib); |
876 | | return ((long)ptib->tib_ordinal); /* thread id */ |
877 | | } |
878 | | #endif |
879 | | |
880 | | static void |
881 | | CloseCache(cacheDesc *cache) |
882 | 0 | { |
883 | 0 | int locks_initialized = cache->numSIDCacheLocksInitialized; |
884 | |
|
885 | 0 | if (cache->cacheMem) { |
886 | 0 | if (cache->sharedCache) { |
887 | 0 | sidCacheLock *pLock = cache->sidCacheLocks; |
888 | 0 | for (; locks_initialized > 0; --locks_initialized, ++pLock) { |
889 | | /* If everInherited is true, this shared cache was (and may |
890 | | ** still be) in use by multiple processes. We do not wish to |
891 | | ** destroy the mutexes while they are still in use, but we do |
892 | | ** want to free mutex resources associated with this process. |
893 | | */ |
894 | 0 | sslMutex_Destroy(&pLock->mutex, |
895 | 0 | cache->sharedCache->everInherited); |
896 | 0 | } |
897 | 0 | } |
898 | 0 | if (cache->shared) { |
899 | 0 | PR_MemUnmap(cache->cacheMem, cache->cacheMemSize); |
900 | 0 | } else { |
901 | 0 | PORT_Free(cache->cacheMem); |
902 | 0 | } |
903 | 0 | cache->cacheMem = NULL; |
904 | 0 | } |
905 | 0 | if (cache->cacheMemMap) { |
906 | 0 | PR_CloseFileMap(cache->cacheMemMap); |
907 | 0 | cache->cacheMemMap = NULL; |
908 | 0 | } |
909 | 0 | memset(cache, 0, sizeof *cache); |
910 | 0 | } |
911 | | |
912 | | static SECStatus |
913 | | InitCache(cacheDesc *cache, int maxCacheEntries, int maxCertCacheEntries, |
914 | | int maxSrvNameCacheEntries, PRUint32 ssl3_timeout, |
915 | | const char *directory, PRBool shared) |
916 | 0 | { |
917 | 0 | ptrdiff_t ptr; |
918 | 0 | sidCacheLock *pLock; |
919 | 0 | char *cacheMem; |
920 | 0 | PRFileMap *cacheMemMap; |
921 | 0 | char *cfn = NULL; /* cache file name */ |
922 | 0 | int locks_initialized = 0; |
923 | 0 | int locks_to_initialize = 0; |
924 | 0 | PRUint32 init_time; |
925 | |
|
926 | 0 | if ((!cache) || (maxCacheEntries < 0) || (!directory)) { |
927 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
928 | 0 | return SECFailure; |
929 | 0 | } |
930 | | |
931 | 0 | if (cache->cacheMem) { |
932 | | /* Already done */ |
933 | 0 | return SECSuccess; |
934 | 0 | } |
935 | | |
936 | | /* make sure loser can clean up properly */ |
937 | 0 | cache->shared = shared; |
938 | 0 | cache->cacheMem = cacheMem = NULL; |
939 | 0 | cache->cacheMemMap = cacheMemMap = NULL; |
940 | 0 | cache->sharedCache = (cacheDesc *)0; |
941 | |
|
942 | 0 | cache->numSIDCacheLocksInitialized = 0; |
943 | 0 | cache->nextCertCacheEntry = 0; |
944 | 0 | cache->stopPolling = PR_FALSE; |
945 | 0 | cache->everInherited = PR_FALSE; |
946 | 0 | cache->poller = NULL; |
947 | 0 | cache->mutexTimeout = 0; |
948 | |
|
949 | 0 | cache->numSIDCacheEntries = maxCacheEntries ? maxCacheEntries |
950 | 0 | : DEF_SID_CACHE_ENTRIES; |
951 | 0 | cache->numSIDCacheSets = |
952 | 0 | SID_HOWMANY(cache->numSIDCacheEntries, SID_CACHE_ENTRIES_PER_SET); |
953 | |
|
954 | 0 | cache->numSIDCacheEntries = |
955 | 0 | cache->numSIDCacheSets * SID_CACHE_ENTRIES_PER_SET; |
956 | |
|
957 | 0 | cache->numSIDCacheLocks = |
958 | 0 | PR_MIN(cache->numSIDCacheSets, ssl_max_sid_cache_locks); |
959 | |
|
960 | 0 | cache->numSIDCacheSetsPerLock = |
961 | 0 | SID_HOWMANY(cache->numSIDCacheSets, cache->numSIDCacheLocks); |
962 | |
|
963 | 0 | cache->numCertCacheEntries = (maxCertCacheEntries > 0) ? maxCertCacheEntries |
964 | 0 | : 0; |
965 | 0 | cache->numSrvNameCacheEntries = (maxSrvNameCacheEntries >= 0) ? maxSrvNameCacheEntries |
966 | 0 | : DEF_NAME_CACHE_ENTRIES; |
967 | | |
968 | | /* compute size of shared memory, and offsets of all pointers */ |
969 | 0 | ptr = 0; |
970 | 0 | cache->cacheMem = (char *)ptr; |
971 | 0 | ptr += SID_ROUNDUP(sizeof(cacheDesc), SID_ALIGNMENT); |
972 | |
|
973 | 0 | cache->sidCacheLocks = (sidCacheLock *)ptr; |
974 | 0 | cache->keyCacheLock = cache->sidCacheLocks + cache->numSIDCacheLocks; |
975 | 0 | cache->certCacheLock = cache->keyCacheLock + 1; |
976 | 0 | cache->srvNameCacheLock = cache->certCacheLock + 1; |
977 | 0 | ptr = (ptrdiff_t)(cache->srvNameCacheLock + 1); |
978 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
979 | |
|
980 | 0 | cache->sidCacheSets = (sidCacheSet *)ptr; |
981 | 0 | ptr = (ptrdiff_t)(cache->sidCacheSets + cache->numSIDCacheSets); |
982 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
983 | |
|
984 | 0 | cache->sidCacheData = (sidCacheEntry *)ptr; |
985 | 0 | ptr = (ptrdiff_t)(cache->sidCacheData + cache->numSIDCacheEntries); |
986 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
987 | |
|
988 | 0 | cache->certCacheData = (certCacheEntry *)ptr; |
989 | 0 | cache->sidCacheSize = |
990 | 0 | (char *)cache->certCacheData - (char *)cache->sidCacheData; |
991 | |
|
992 | 0 | if (cache->numCertCacheEntries < MIN_CERT_CACHE_ENTRIES) { |
993 | | /* This is really a poor way to computer this! */ |
994 | 0 | cache->numCertCacheEntries = cache->sidCacheSize / sizeof(certCacheEntry); |
995 | 0 | if (cache->numCertCacheEntries < MIN_CERT_CACHE_ENTRIES) |
996 | 0 | cache->numCertCacheEntries = MIN_CERT_CACHE_ENTRIES; |
997 | 0 | } |
998 | 0 | ptr = (ptrdiff_t)(cache->certCacheData + cache->numCertCacheEntries); |
999 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1000 | |
|
1001 | 0 | cache->keyCacheData = (SSLWrappedSymWrappingKey *)ptr; |
1002 | 0 | cache->certCacheSize = |
1003 | 0 | (char *)cache->keyCacheData - (char *)cache->certCacheData; |
1004 | |
|
1005 | 0 | cache->numKeyCacheEntries = SSL_NUM_WRAP_KEYS * SSL_NUM_WRAP_MECHS; |
1006 | 0 | ptr = (ptrdiff_t)(cache->keyCacheData + cache->numKeyCacheEntries); |
1007 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1008 | |
|
1009 | 0 | cache->keyCacheSize = (char *)ptr - (char *)cache->keyCacheData; |
1010 | |
|
1011 | 0 | cache->ticketKeyNameSuffix = (PRUint8 *)ptr; |
1012 | 0 | ptr = (ptrdiff_t)(cache->ticketKeyNameSuffix + |
1013 | 0 | SELF_ENCRYPT_KEY_VAR_NAME_LEN); |
1014 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1015 | |
|
1016 | 0 | cache->ticketEncKey = (encKeyCacheEntry *)ptr; |
1017 | 0 | ptr = (ptrdiff_t)(cache->ticketEncKey + 1); |
1018 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1019 | |
|
1020 | 0 | cache->ticketMacKey = (encKeyCacheEntry *)ptr; |
1021 | 0 | ptr = (ptrdiff_t)(cache->ticketMacKey + 1); |
1022 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1023 | |
|
1024 | 0 | cache->ticketKeysValid = (PRUint32 *)ptr; |
1025 | 0 | ptr = (ptrdiff_t)(cache->ticketKeysValid + 1); |
1026 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1027 | |
|
1028 | 0 | cache->srvNameCacheData = (srvNameCacheEntry *)ptr; |
1029 | 0 | cache->srvNameCacheSize = |
1030 | 0 | cache->numSrvNameCacheEntries * sizeof(srvNameCacheEntry); |
1031 | 0 | ptr = (ptrdiff_t)(cache->srvNameCacheData + cache->numSrvNameCacheEntries); |
1032 | 0 | ptr = SID_ROUNDUP(ptr, SID_ALIGNMENT); |
1033 | |
|
1034 | 0 | cache->cacheMemSize = ptr; |
1035 | |
|
1036 | 0 | if (ssl3_timeout) { |
1037 | 0 | if (ssl3_timeout > MAX_SSL3_TIMEOUT) { |
1038 | 0 | ssl3_timeout = MAX_SSL3_TIMEOUT; |
1039 | 0 | } |
1040 | 0 | if (ssl3_timeout < MIN_SSL3_TIMEOUT) { |
1041 | 0 | ssl3_timeout = MIN_SSL3_TIMEOUT; |
1042 | 0 | } |
1043 | 0 | cache->ssl3Timeout = ssl3_timeout; |
1044 | 0 | } else { |
1045 | 0 | cache->ssl3Timeout = DEF_SSL3_TIMEOUT; |
1046 | 0 | } |
1047 | |
|
1048 | 0 | if (shared) { |
1049 | | /* Create file names */ |
1050 | 0 | #if defined(XP_UNIX) |
1051 | | /* there's some confusion here about whether PR_OpenAnonFileMap wants |
1052 | | ** a directory name or a file name for its first argument. |
1053 | | cfn = PR_smprintf("%s/.sslsvrcache.%d", directory, myPid); |
1054 | | */ |
1055 | 0 | cfn = PR_smprintf("%s", directory); |
1056 | | #elif defined(XP_WIN32) |
1057 | | cfn = PR_smprintf("%s/svrcache_%d_%x.ssl", directory, myPid, |
1058 | | GetCurrentThreadId()); |
1059 | | #elif defined(XP_OS2) |
1060 | | cfn = PR_smprintf("%s/svrcache_%d_%x.ssl", directory, myPid, |
1061 | | gettid()); |
1062 | | #else |
1063 | | #error "Don't know how to create file name for this platform!" |
1064 | | #endif |
1065 | 0 | if (!cfn) { |
1066 | 0 | goto loser; |
1067 | 0 | } |
1068 | | |
1069 | | /* Create cache */ |
1070 | 0 | cacheMemMap = PR_OpenAnonFileMap(cfn, cache->cacheMemSize, |
1071 | 0 | PR_PROT_READWRITE); |
1072 | |
|
1073 | 0 | PR_smprintf_free(cfn); |
1074 | 0 | if (!cacheMemMap) { |
1075 | 0 | goto loser; |
1076 | 0 | } |
1077 | | |
1078 | 0 | cacheMem = PR_MemMap(cacheMemMap, 0, cache->cacheMemSize); |
1079 | 0 | } else { |
1080 | 0 | cacheMem = PORT_Alloc(cache->cacheMemSize); |
1081 | 0 | } |
1082 | | |
1083 | 0 | if (!cacheMem) { |
1084 | 0 | goto loser; |
1085 | 0 | } |
1086 | | |
1087 | | /* Initialize shared memory. This may not be necessary on all platforms */ |
1088 | 0 | memset(cacheMem, 0, cache->cacheMemSize); |
1089 | | |
1090 | | /* Copy cache descriptor header into shared memory */ |
1091 | 0 | memcpy(cacheMem, cache, sizeof *cache); |
1092 | | |
1093 | | /* save private copies of these values */ |
1094 | 0 | cache->cacheMemMap = cacheMemMap; |
1095 | 0 | cache->cacheMem = cacheMem; |
1096 | 0 | cache->sharedCache = (cacheDesc *)cacheMem; |
1097 | | |
1098 | | /* Fix pointers in our private copy of cache descriptor to point to |
1099 | | ** spaces in shared memory |
1100 | | */ |
1101 | 0 | cache->sidCacheLocks = (sidCacheLock *)(cache->cacheMem + (ptrdiff_t)cache->sidCacheLocks); |
1102 | 0 | cache->keyCacheLock = (sidCacheLock *)(cache->cacheMem + (ptrdiff_t)cache->keyCacheLock); |
1103 | 0 | cache->certCacheLock = (sidCacheLock *)(cache->cacheMem + (ptrdiff_t)cache->certCacheLock); |
1104 | 0 | cache->srvNameCacheLock = (sidCacheLock *)(cache->cacheMem + (ptrdiff_t)cache->srvNameCacheLock); |
1105 | 0 | cache->sidCacheSets = (sidCacheSet *)(cache->cacheMem + (ptrdiff_t)cache->sidCacheSets); |
1106 | 0 | cache->sidCacheData = (sidCacheEntry *)(cache->cacheMem + (ptrdiff_t)cache->sidCacheData); |
1107 | 0 | cache->certCacheData = (certCacheEntry *)(cache->cacheMem + (ptrdiff_t)cache->certCacheData); |
1108 | 0 | cache->keyCacheData = (SSLWrappedSymWrappingKey *)(cache->cacheMem + (ptrdiff_t)cache->keyCacheData); |
1109 | 0 | cache->ticketKeyNameSuffix = (PRUint8 *)(cache->cacheMem + (ptrdiff_t)cache->ticketKeyNameSuffix); |
1110 | 0 | cache->ticketEncKey = (encKeyCacheEntry *)(cache->cacheMem + (ptrdiff_t)cache->ticketEncKey); |
1111 | 0 | cache->ticketMacKey = (encKeyCacheEntry *)(cache->cacheMem + (ptrdiff_t)cache->ticketMacKey); |
1112 | 0 | cache->ticketKeysValid = (PRUint32 *)(cache->cacheMem + (ptrdiff_t)cache->ticketKeysValid); |
1113 | 0 | cache->srvNameCacheData = (srvNameCacheEntry *)(cache->cacheMem + (ptrdiff_t)cache->srvNameCacheData); |
1114 | | |
1115 | | /* initialize the locks */ |
1116 | 0 | init_time = ssl_CacheNow(); |
1117 | 0 | pLock = cache->sidCacheLocks; |
1118 | 0 | for (locks_to_initialize = cache->numSIDCacheLocks + 3; |
1119 | 0 | locks_initialized < locks_to_initialize; |
1120 | 0 | ++locks_initialized, ++pLock) { |
1121 | |
|
1122 | 0 | SECStatus err = sslMutex_Init(&pLock->mutex, shared); |
1123 | 0 | if (err) { |
1124 | 0 | cache->numSIDCacheLocksInitialized = locks_initialized; |
1125 | 0 | goto loser; |
1126 | 0 | } |
1127 | 0 | pLock->timeStamp = init_time; |
1128 | 0 | pLock->pid = 0; |
1129 | 0 | } |
1130 | 0 | cache->numSIDCacheLocksInitialized = locks_initialized; |
1131 | |
|
1132 | 0 | return SECSuccess; |
1133 | | |
1134 | 0 | loser: |
1135 | 0 | CloseCache(cache); |
1136 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1137 | 0 | return SECFailure; |
1138 | 0 | } |
1139 | | |
1140 | | PRUint32 |
1141 | | SSL_GetMaxServerCacheLocks(void) |
1142 | 0 | { |
1143 | 0 | return ssl_max_sid_cache_locks + 2; |
1144 | | /* The extra two are the cert cache lock and the key cache lock. */ |
1145 | 0 | } |
1146 | | |
1147 | | SECStatus |
1148 | | SSL_SetMaxServerCacheLocks(PRUint32 maxLocks) |
1149 | 0 | { |
1150 | | /* Minimum is 1 sid cache lock, 1 cert cache lock and 1 key cache lock. |
1151 | | ** We'd like to test for a maximum value, but not all platforms' header |
1152 | | ** files provide a symbol or function or other means of determining |
1153 | | ** the maximum, other than trial and error. |
1154 | | */ |
1155 | 0 | if (maxLocks < 3) { |
1156 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1157 | 0 | return SECFailure; |
1158 | 0 | } |
1159 | 0 | ssl_max_sid_cache_locks = maxLocks - 2; |
1160 | | /* The extra two are the cert cache lock and the key cache lock. */ |
1161 | 0 | return SECSuccess; |
1162 | 0 | } |
1163 | | |
1164 | | PR_STATIC_ASSERT(sizeof(sidCacheEntry) % 16 == 0); |
1165 | | PR_STATIC_ASSERT(sizeof(certCacheEntry) == 4096); |
1166 | | PR_STATIC_ASSERT(sizeof(srvNameCacheEntry) == 1072); |
1167 | | |
1168 | | static SECStatus |
1169 | | ssl_ConfigServerSessionIDCacheInstanceWithOpt(cacheDesc *cache, |
1170 | | PRUint32 ssl3_timeout, |
1171 | | const char *directory, |
1172 | | PRBool shared, |
1173 | | int maxCacheEntries, |
1174 | | int maxCertCacheEntries, |
1175 | | int maxSrvNameCacheEntries) |
1176 | 0 | { |
1177 | 0 | SECStatus rv; |
1178 | |
|
1179 | 0 | rv = ssl_InitSessionCache(); |
1180 | 0 | if (rv != SECSuccess) { |
1181 | 0 | return rv; |
1182 | 0 | } |
1183 | | |
1184 | 0 | myPid = SSL_GETPID(); |
1185 | 0 | if (!directory) { |
1186 | 0 | directory = DEFAULT_CACHE_DIRECTORY; |
1187 | 0 | } |
1188 | 0 | rv = InitCache(cache, maxCacheEntries, maxCertCacheEntries, |
1189 | 0 | maxSrvNameCacheEntries, ssl3_timeout, directory, shared); |
1190 | 0 | if (rv) { |
1191 | 0 | return SECFailure; |
1192 | 0 | } |
1193 | | |
1194 | 0 | ssl_sid_lookup = ServerSessionIDLookup; |
1195 | 0 | return SECSuccess; |
1196 | 0 | } |
1197 | | |
1198 | | SECStatus |
1199 | | SSL_ConfigServerSessionIDCacheInstance(cacheDesc *cache, |
1200 | | int maxCacheEntries, |
1201 | | PRUint32 ssl2_timeout, |
1202 | | PRUint32 ssl3_timeout, |
1203 | | const char *directory, PRBool shared) |
1204 | 0 | { |
1205 | 0 | return ssl_ConfigServerSessionIDCacheInstanceWithOpt(cache, |
1206 | 0 | ssl3_timeout, |
1207 | 0 | directory, |
1208 | 0 | shared, |
1209 | 0 | maxCacheEntries, |
1210 | 0 | -1, -1); |
1211 | 0 | } |
1212 | | |
1213 | | SECStatus |
1214 | | SSL_ConfigServerSessionIDCache(int maxCacheEntries, |
1215 | | PRUint32 ssl2_timeout, |
1216 | | PRUint32 ssl3_timeout, |
1217 | | const char *directory) |
1218 | 0 | { |
1219 | 0 | ssl_InitSessionCacheLocks(PR_FALSE); |
1220 | 0 | return SSL_ConfigServerSessionIDCacheInstance(&globalCache, |
1221 | 0 | maxCacheEntries, ssl2_timeout, ssl3_timeout, directory, PR_FALSE); |
1222 | 0 | } |
1223 | | |
1224 | | SECStatus |
1225 | | SSL_ShutdownServerSessionIDCacheInstance(cacheDesc *cache) |
1226 | 0 | { |
1227 | 0 | CloseCache(cache); |
1228 | 0 | return SECSuccess; |
1229 | 0 | } |
1230 | | |
1231 | | SECStatus |
1232 | | SSL_ShutdownServerSessionIDCache(void) |
1233 | 0 | { |
1234 | 0 | #if defined(XP_UNIX) |
1235 | | /* Stop the thread that polls cache for expired locks on Unix */ |
1236 | 0 | StopLockPoller(&globalCache); |
1237 | 0 | #endif |
1238 | 0 | SSL3_ShutdownServerCache(); |
1239 | 0 | return SSL_ShutdownServerSessionIDCacheInstance(&globalCache); |
1240 | 0 | } |
1241 | | |
1242 | | /* Use this function, instead of SSL_ConfigServerSessionIDCache, |
1243 | | * if the cache will be shared by multiple processes. |
1244 | | */ |
1245 | | static SECStatus |
1246 | | ssl_ConfigMPServerSIDCacheWithOpt(PRUint32 ssl3_timeout, |
1247 | | const char *directory, |
1248 | | int maxCacheEntries, |
1249 | | int maxCertCacheEntries, |
1250 | | int maxSrvNameCacheEntries) |
1251 | 0 | { |
1252 | 0 | char *envValue; |
1253 | 0 | char *inhValue; |
1254 | 0 | cacheDesc *cache = &globalCache; |
1255 | 0 | PRUint32 fmStrLen; |
1256 | 0 | SECStatus result; |
1257 | 0 | PRStatus prStatus; |
1258 | 0 | SECStatus putEnvFailed; |
1259 | 0 | inheritance inherit; |
1260 | 0 | char fmString[PR_FILEMAP_STRING_BUFSIZE]; |
1261 | |
|
1262 | 0 | isMultiProcess = PR_TRUE; |
1263 | 0 | result = ssl_ConfigServerSessionIDCacheInstanceWithOpt(cache, |
1264 | 0 | ssl3_timeout, directory, PR_TRUE, |
1265 | 0 | maxCacheEntries, maxCacheEntries, maxSrvNameCacheEntries); |
1266 | 0 | if (result != SECSuccess) |
1267 | 0 | return result; |
1268 | | |
1269 | 0 | prStatus = PR_ExportFileMapAsString(cache->cacheMemMap, |
1270 | 0 | sizeof fmString, fmString); |
1271 | 0 | if ((prStatus != PR_SUCCESS) || !(fmStrLen = strlen(fmString))) { |
1272 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1273 | 0 | return SECFailure; |
1274 | 0 | } |
1275 | | |
1276 | 0 | inherit.cacheMemSize = cache->cacheMemSize; |
1277 | 0 | inherit.fmStrLen = fmStrLen; |
1278 | |
|
1279 | 0 | inhValue = BTOA_DataToAscii((unsigned char *)&inherit, sizeof inherit); |
1280 | 0 | if (!inhValue || !strlen(inhValue)) { |
1281 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1282 | 0 | return SECFailure; |
1283 | 0 | } |
1284 | 0 | envValue = PR_smprintf("%s,%s", inhValue, fmString); |
1285 | 0 | if (!envValue || !strlen(envValue)) { |
1286 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1287 | 0 | return SECFailure; |
1288 | 0 | } |
1289 | 0 | PORT_Free(inhValue); |
1290 | |
|
1291 | 0 | putEnvFailed = (SECStatus)NSS_PutEnv(envVarName, envValue); |
1292 | 0 | PR_smprintf_free(envValue); |
1293 | 0 | if (putEnvFailed) { |
1294 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1295 | 0 | result = SECFailure; |
1296 | 0 | } |
1297 | |
|
1298 | 0 | #if defined(XP_UNIX) |
1299 | | /* Launch thread to poll cache for expired locks on Unix */ |
1300 | 0 | LaunchLockPoller(cache); |
1301 | 0 | #endif |
1302 | 0 | return result; |
1303 | 0 | } |
1304 | | |
1305 | | /* Use this function, instead of SSL_ConfigServerSessionIDCache, |
1306 | | * if the cache will be shared by multiple processes. |
1307 | | */ |
1308 | | SECStatus |
1309 | | SSL_ConfigMPServerSIDCache(int maxCacheEntries, |
1310 | | PRUint32 ssl2_timeout, |
1311 | | PRUint32 ssl3_timeout, |
1312 | | const char *directory) |
1313 | 0 | { |
1314 | 0 | return ssl_ConfigMPServerSIDCacheWithOpt(ssl3_timeout, |
1315 | 0 | directory, |
1316 | 0 | maxCacheEntries, |
1317 | 0 | -1, -1); |
1318 | 0 | } |
1319 | | |
1320 | | SECStatus |
1321 | | SSL_ConfigServerSessionIDCacheWithOpt( |
1322 | | PRUint32 ssl2_timeout, |
1323 | | PRUint32 ssl3_timeout, |
1324 | | const char *directory, |
1325 | | int maxCacheEntries, |
1326 | | int maxCertCacheEntries, |
1327 | | int maxSrvNameCacheEntries, |
1328 | | PRBool enableMPCache) |
1329 | 0 | { |
1330 | 0 | if (!enableMPCache) { |
1331 | 0 | ssl_InitSessionCacheLocks(PR_FALSE); |
1332 | 0 | return ssl_ConfigServerSessionIDCacheInstanceWithOpt(&globalCache, |
1333 | 0 | ssl3_timeout, directory, PR_FALSE, |
1334 | 0 | maxCacheEntries, maxCertCacheEntries, maxSrvNameCacheEntries); |
1335 | 0 | } else { |
1336 | 0 | return ssl_ConfigMPServerSIDCacheWithOpt(ssl3_timeout, directory, |
1337 | 0 | maxCacheEntries, maxCertCacheEntries, maxSrvNameCacheEntries); |
1338 | 0 | } |
1339 | 0 | } |
1340 | | |
1341 | | SECStatus |
1342 | | SSL_InheritMPServerSIDCacheInstance(cacheDesc *cache, const char *envString) |
1343 | 0 | { |
1344 | 0 | unsigned char *decoString = NULL; |
1345 | 0 | char *fmString = NULL; |
1346 | 0 | char *myEnvString = NULL; |
1347 | 0 | unsigned int decoLen; |
1348 | 0 | inheritance inherit; |
1349 | 0 | cacheDesc my; |
1350 | | #ifdef WINNT |
1351 | | sidCacheLock *newLocks; |
1352 | | int locks_initialized = 0; |
1353 | | int locks_to_initialize = 0; |
1354 | | #endif |
1355 | 0 | SECStatus status = ssl_InitSessionCache(); |
1356 | |
|
1357 | 0 | if (status != SECSuccess) { |
1358 | 0 | return status; |
1359 | 0 | } |
1360 | | |
1361 | 0 | myPid = SSL_GETPID(); |
1362 | | |
1363 | | /* If this child was created by fork(), and not by exec() on unix, |
1364 | | ** then isMultiProcess will already be set. |
1365 | | ** If not, we'll set it below. |
1366 | | */ |
1367 | 0 | if (isMultiProcess) { |
1368 | 0 | if (cache && cache->sharedCache) { |
1369 | 0 | cache->sharedCache->everInherited = PR_TRUE; |
1370 | 0 | } |
1371 | 0 | return SECSuccess; /* already done. */ |
1372 | 0 | } |
1373 | | |
1374 | 0 | ssl_InitSessionCacheLocks(PR_FALSE); |
1375 | |
|
1376 | 0 | ssl_sid_lookup = ServerSessionIDLookup; |
1377 | |
|
1378 | 0 | if (!envString) { |
1379 | 0 | envString = PR_GetEnvSecure(envVarName); |
1380 | 0 | if (!envString) { |
1381 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1382 | 0 | return SECFailure; |
1383 | 0 | } |
1384 | 0 | } |
1385 | 0 | myEnvString = PORT_Strdup(envString); |
1386 | 0 | if (!myEnvString) |
1387 | 0 | return SECFailure; |
1388 | 0 | fmString = strchr(myEnvString, ','); |
1389 | 0 | if (!fmString) |
1390 | 0 | goto loser; |
1391 | 0 | *fmString++ = 0; |
1392 | |
|
1393 | 0 | decoString = ATOB_AsciiToData(myEnvString, &decoLen); |
1394 | 0 | if (!decoString) { |
1395 | 0 | goto loser; |
1396 | 0 | } |
1397 | 0 | if (decoLen != sizeof inherit) { |
1398 | 0 | goto loser; |
1399 | 0 | } |
1400 | | |
1401 | 0 | PORT_Memcpy(&inherit, decoString, sizeof inherit); |
1402 | |
|
1403 | 0 | if (strlen(fmString) != inherit.fmStrLen) { |
1404 | 0 | goto loser; |
1405 | 0 | } |
1406 | | |
1407 | 0 | memset(cache, 0, sizeof *cache); |
1408 | 0 | cache->cacheMemSize = inherit.cacheMemSize; |
1409 | | |
1410 | | /* Create cache */ |
1411 | 0 | cache->cacheMemMap = PR_ImportFileMapFromString(fmString); |
1412 | 0 | if (!cache->cacheMemMap) { |
1413 | 0 | goto loser; |
1414 | 0 | } |
1415 | 0 | cache->cacheMem = PR_MemMap(cache->cacheMemMap, 0, cache->cacheMemSize); |
1416 | 0 | if (!cache->cacheMem) { |
1417 | 0 | goto loser; |
1418 | 0 | } |
1419 | 0 | cache->sharedCache = (cacheDesc *)cache->cacheMem; |
1420 | |
|
1421 | 0 | if (cache->sharedCache->cacheMemSize != cache->cacheMemSize) { |
1422 | 0 | goto loser; |
1423 | 0 | } |
1424 | | |
1425 | | /* We're now going to overwrite the local cache instance with the |
1426 | | ** shared copy of the cache struct, then update several values in |
1427 | | ** the local cache using the values for cache->cacheMemMap and |
1428 | | ** cache->cacheMem computed just above. So, we copy cache into |
1429 | | ** the automatic variable "my", to preserve the variables while |
1430 | | ** cache is overwritten. |
1431 | | */ |
1432 | 0 | my = *cache; /* save values computed above. */ |
1433 | 0 | memcpy(cache, cache->sharedCache, sizeof *cache); /* overwrite */ |
1434 | | |
1435 | | /* Fix pointers in our private copy of cache descriptor to point to |
1436 | | ** spaces in shared memory, whose address is now in "my". |
1437 | | */ |
1438 | 0 | cache->sidCacheLocks = (sidCacheLock *)(my.cacheMem + (ptrdiff_t)cache->sidCacheLocks); |
1439 | 0 | cache->keyCacheLock = (sidCacheLock *)(my.cacheMem + (ptrdiff_t)cache->keyCacheLock); |
1440 | 0 | cache->certCacheLock = (sidCacheLock *)(my.cacheMem + (ptrdiff_t)cache->certCacheLock); |
1441 | 0 | cache->srvNameCacheLock = (sidCacheLock *)(my.cacheMem + (ptrdiff_t)cache->srvNameCacheLock); |
1442 | 0 | cache->sidCacheSets = (sidCacheSet *)(my.cacheMem + (ptrdiff_t)cache->sidCacheSets); |
1443 | 0 | cache->sidCacheData = (sidCacheEntry *)(my.cacheMem + (ptrdiff_t)cache->sidCacheData); |
1444 | 0 | cache->certCacheData = (certCacheEntry *)(my.cacheMem + (ptrdiff_t)cache->certCacheData); |
1445 | 0 | cache->keyCacheData = (SSLWrappedSymWrappingKey *)(my.cacheMem + (ptrdiff_t)cache->keyCacheData); |
1446 | 0 | cache->ticketKeyNameSuffix = (PRUint8 *)(my.cacheMem + (ptrdiff_t)cache->ticketKeyNameSuffix); |
1447 | 0 | cache->ticketEncKey = (encKeyCacheEntry *)(my.cacheMem + (ptrdiff_t)cache->ticketEncKey); |
1448 | 0 | cache->ticketMacKey = (encKeyCacheEntry *)(my.cacheMem + (ptrdiff_t)cache->ticketMacKey); |
1449 | 0 | cache->ticketKeysValid = (PRUint32 *)(my.cacheMem + (ptrdiff_t)cache->ticketKeysValid); |
1450 | 0 | cache->srvNameCacheData = (srvNameCacheEntry *)(my.cacheMem + (ptrdiff_t)cache->srvNameCacheData); |
1451 | |
|
1452 | 0 | cache->cacheMemMap = my.cacheMemMap; |
1453 | 0 | cache->cacheMem = my.cacheMem; |
1454 | 0 | cache->sharedCache = (cacheDesc *)cache->cacheMem; |
1455 | |
|
1456 | | #ifdef WINNT |
1457 | | /* On Windows NT we need to "fix" the sidCacheLocks here to support fibers |
1458 | | ** When NT fibers are used in a multi-process server, a second level of |
1459 | | ** locking is needed to prevent a deadlock, in case a fiber acquires the |
1460 | | ** cross-process mutex, yields, and another fiber is later scheduled on |
1461 | | ** the same native thread and tries to acquire the cross-process mutex. |
1462 | | ** We do this by using a PRLock in the sslMutex. However, it is stored in |
1463 | | ** shared memory as part of sidCacheLocks, and we don't want to overwrite |
1464 | | ** the PRLock of the parent process. So we need to make new, private |
1465 | | ** copies of sidCacheLocks before modifying the sslMutex with our own |
1466 | | ** PRLock |
1467 | | */ |
1468 | | |
1469 | | /* note from jpierre : this should be free'd in child processes when |
1470 | | ** a function is added to delete the SSL session cache in the future. |
1471 | | */ |
1472 | | locks_to_initialize = cache->numSIDCacheLocks + 3; |
1473 | | newLocks = PORT_NewArray(sidCacheLock, locks_to_initialize); |
1474 | | if (!newLocks) |
1475 | | goto loser; |
1476 | | /* copy the old locks */ |
1477 | | memcpy(newLocks, cache->sidCacheLocks, |
1478 | | locks_to_initialize * sizeof(sidCacheLock)); |
1479 | | cache->sidCacheLocks = newLocks; |
1480 | | /* fix the locks */ |
1481 | | for (; locks_initialized < locks_to_initialize; ++locks_initialized) { |
1482 | | /* now, make a local PRLock in this sslMutex for this child process */ |
1483 | | SECStatus err; |
1484 | | err = sslMutex_2LevelInit(&newLocks[locks_initialized].mutex); |
1485 | | if (err != SECSuccess) { |
1486 | | cache->numSIDCacheLocksInitialized = locks_initialized; |
1487 | | goto loser; |
1488 | | } |
1489 | | } |
1490 | | cache->numSIDCacheLocksInitialized = locks_initialized; |
1491 | | |
1492 | | /* also fix the key and cert cache which use the last 2 lock entries */ |
1493 | | cache->keyCacheLock = cache->sidCacheLocks + cache->numSIDCacheLocks; |
1494 | | cache->certCacheLock = cache->keyCacheLock + 1; |
1495 | | cache->srvNameCacheLock = cache->certCacheLock + 1; |
1496 | | #endif |
1497 | |
|
1498 | 0 | PORT_Free(myEnvString); |
1499 | 0 | PORT_Free(decoString); |
1500 | | |
1501 | | /* mark that we have inherited this. */ |
1502 | 0 | cache->sharedCache->everInherited = PR_TRUE; |
1503 | 0 | isMultiProcess = PR_TRUE; |
1504 | |
|
1505 | 0 | return SECSuccess; |
1506 | | |
1507 | 0 | loser: |
1508 | 0 | PORT_Free(myEnvString); |
1509 | 0 | if (decoString) |
1510 | 0 | PORT_Free(decoString); |
1511 | 0 | CloseCache(cache); |
1512 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1513 | 0 | return SECFailure; |
1514 | 0 | } |
1515 | | |
1516 | | SECStatus |
1517 | | SSL_InheritMPServerSIDCache(const char *envString) |
1518 | 0 | { |
1519 | 0 | return SSL_InheritMPServerSIDCacheInstance(&globalCache, envString); |
1520 | 0 | } |
1521 | | |
1522 | | #if defined(XP_UNIX) |
1523 | | |
1524 | 0 | #define SID_LOCK_EXPIRATION_TIMEOUT 30 /* seconds */ |
1525 | | |
1526 | | static void |
1527 | | LockPoller(void *arg) |
1528 | 0 | { |
1529 | 0 | cacheDesc *cache = (cacheDesc *)arg; |
1530 | 0 | cacheDesc *sharedCache = cache->sharedCache; |
1531 | 0 | sidCacheLock *pLock; |
1532 | 0 | PRIntervalTime timeout; |
1533 | 0 | PRUint32 now; |
1534 | 0 | PRUint32 then; |
1535 | 0 | int locks_polled = 0; |
1536 | 0 | int locks_to_poll = cache->numSIDCacheLocks + 2; |
1537 | 0 | PRUint32 expiration = cache->mutexTimeout; |
1538 | |
|
1539 | 0 | timeout = PR_SecondsToInterval(expiration); |
1540 | 0 | while (!sharedCache->stopPolling) { |
1541 | 0 | PR_Sleep(timeout); |
1542 | 0 | if (sharedCache->stopPolling) |
1543 | 0 | break; |
1544 | | |
1545 | 0 | now = ssl_CacheNow(); |
1546 | 0 | then = now - expiration; |
1547 | 0 | for (pLock = cache->sidCacheLocks, locks_polled = 0; |
1548 | 0 | locks_to_poll > locks_polled && !sharedCache->stopPolling; |
1549 | 0 | ++locks_polled, ++pLock) { |
1550 | 0 | pid_t pid; |
1551 | |
|
1552 | 0 | if (pLock->timeStamp < then && |
1553 | 0 | pLock->timeStamp != 0 && |
1554 | 0 | (pid = pLock->pid) != 0) { |
1555 | | |
1556 | | /* maybe we should try the lock? */ |
1557 | 0 | int result = kill(pid, 0); |
1558 | 0 | if (result < 0 && errno == ESRCH) { |
1559 | 0 | SECStatus rv; |
1560 | | /* No process exists by that pid any more. |
1561 | | ** Treat this mutex as abandoned. |
1562 | | */ |
1563 | 0 | pLock->timeStamp = now; |
1564 | 0 | pLock->pid = 0; |
1565 | 0 | rv = sslMutex_Unlock(&pLock->mutex); |
1566 | 0 | if (rv != SECSuccess) { |
1567 | | /* Now what? */ |
1568 | 0 | } |
1569 | 0 | } |
1570 | 0 | } |
1571 | 0 | } /* end of loop over locks */ |
1572 | 0 | } /* end of entire polling loop */ |
1573 | 0 | } |
1574 | | |
1575 | | /* Launch thread to poll cache for expired locks */ |
1576 | | static SECStatus |
1577 | | LaunchLockPoller(cacheDesc *cache) |
1578 | 0 | { |
1579 | 0 | const char *timeoutString; |
1580 | 0 | PRThread *pollerThread; |
1581 | |
|
1582 | 0 | cache->mutexTimeout = SID_LOCK_EXPIRATION_TIMEOUT; |
1583 | 0 | timeoutString = PR_GetEnvSecure("NSS_SSL_SERVER_CACHE_MUTEX_TIMEOUT"); |
1584 | 0 | if (timeoutString) { |
1585 | 0 | long newTime = strtol(timeoutString, 0, 0); |
1586 | 0 | if (newTime == 0) |
1587 | 0 | return SECSuccess; /* application doesn't want poller thread */ |
1588 | 0 | if (newTime > 0) |
1589 | 0 | cache->mutexTimeout = (PRUint32)newTime; |
1590 | | /* if error (newTime < 0) ignore it and use default */ |
1591 | 0 | } |
1592 | | |
1593 | 0 | pollerThread = |
1594 | 0 | PR_CreateThread(PR_USER_THREAD, LockPoller, cache, PR_PRIORITY_NORMAL, |
1595 | 0 | PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); |
1596 | 0 | if (!pollerThread) { |
1597 | 0 | return SECFailure; |
1598 | 0 | } |
1599 | 0 | cache->poller = pollerThread; |
1600 | 0 | return SECSuccess; |
1601 | 0 | } |
1602 | | |
1603 | | /* Stop the thread that polls cache for expired locks */ |
1604 | | static SECStatus |
1605 | | StopLockPoller(cacheDesc *cache) |
1606 | 0 | { |
1607 | 0 | if (!cache->poller) { |
1608 | 0 | return SECSuccess; |
1609 | 0 | } |
1610 | 0 | cache->sharedCache->stopPolling = PR_TRUE; |
1611 | 0 | if (PR_Interrupt(cache->poller) != PR_SUCCESS) { |
1612 | 0 | return SECFailure; |
1613 | 0 | } |
1614 | 0 | if (PR_JoinThread(cache->poller) != PR_SUCCESS) { |
1615 | 0 | return SECFailure; |
1616 | 0 | } |
1617 | 0 | cache->poller = NULL; |
1618 | 0 | return SECSuccess; |
1619 | 0 | } |
1620 | | #endif |
1621 | | |
1622 | | /************************************************************************ |
1623 | | * Code dealing with shared wrapped symmetric wrapping keys below * |
1624 | | ************************************************************************/ |
1625 | | |
1626 | | /* The asymmetric key we use for wrapping the self-encryption keys. This is a |
1627 | | * global structure that can be initialized without a socket. Access is |
1628 | | * synchronized on the reader-writer lock. This is setup either by calling |
1629 | | * SSL_SetSessionTicketKeyPair() or by configuring a certificate of the |
1630 | | * ssl_auth_rsa_decrypt type. */ |
1631 | | static struct { |
1632 | | PRCallOnceType setup; |
1633 | | PRRWLock *lock; |
1634 | | SECKEYPublicKey *pubKey; |
1635 | | SECKEYPrivateKey *privKey; |
1636 | | PRBool configured; |
1637 | | } ssl_self_encrypt_key_pair; |
1638 | | |
1639 | | /* The symmetric self-encryption keys. This requires a socket to construct |
1640 | | * and requires that the global structure be initialized before use. |
1641 | | */ |
1642 | | static sslSelfEncryptKeys ssl_self_encrypt_keys; |
1643 | | |
1644 | | /* Externalize the self encrypt keys. Purely used for testing. */ |
1645 | | sslSelfEncryptKeys * |
1646 | | ssl_GetSelfEncryptKeysInt() |
1647 | 0 | { |
1648 | 0 | return &ssl_self_encrypt_keys; |
1649 | 0 | } |
1650 | | |
1651 | | static void |
1652 | | ssl_CleanupSelfEncryptKeyPair() |
1653 | 0 | { |
1654 | 0 | if (ssl_self_encrypt_key_pair.pubKey) { |
1655 | 0 | PORT_Assert(ssl_self_encrypt_key_pair.privKey); |
1656 | 0 | SECKEY_DestroyPublicKey(ssl_self_encrypt_key_pair.pubKey); |
1657 | 0 | SECKEY_DestroyPrivateKey(ssl_self_encrypt_key_pair.privKey); |
1658 | 0 | } |
1659 | 0 | } |
1660 | | |
1661 | | void |
1662 | | ssl_ResetSelfEncryptKeys() |
1663 | 0 | { |
1664 | 0 | if (ssl_self_encrypt_keys.encKey) { |
1665 | 0 | PORT_Assert(ssl_self_encrypt_keys.macKey); |
1666 | 0 | PK11_FreeSymKey(ssl_self_encrypt_keys.encKey); |
1667 | 0 | PK11_FreeSymKey(ssl_self_encrypt_keys.macKey); |
1668 | 0 | } |
1669 | 0 | PORT_Memset(&ssl_self_encrypt_keys, 0, |
1670 | 0 | sizeof(ssl_self_encrypt_keys)); |
1671 | 0 | } |
1672 | | |
1673 | | static SECStatus |
1674 | | ssl_SelfEncryptShutdown(void *appData, void *nssData) |
1675 | 0 | { |
1676 | 0 | ssl_CleanupSelfEncryptKeyPair(); |
1677 | 0 | PR_DestroyRWLock(ssl_self_encrypt_key_pair.lock); |
1678 | 0 | PORT_Memset(&ssl_self_encrypt_key_pair, 0, |
1679 | 0 | sizeof(ssl_self_encrypt_key_pair)); |
1680 | |
|
1681 | 0 | ssl_ResetSelfEncryptKeys(); |
1682 | 0 | return SECSuccess; |
1683 | 0 | } |
1684 | | |
1685 | | static PRStatus |
1686 | | ssl_SelfEncryptSetup(void) |
1687 | 0 | { |
1688 | 0 | SECStatus rv = NSS_RegisterShutdown(ssl_SelfEncryptShutdown, NULL); |
1689 | 0 | if (rv != SECSuccess) { |
1690 | 0 | return PR_FAILURE; |
1691 | 0 | } |
1692 | 0 | ssl_self_encrypt_key_pair.lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, NULL); |
1693 | 0 | if (!ssl_self_encrypt_key_pair.lock) { |
1694 | 0 | return PR_FAILURE; |
1695 | 0 | } |
1696 | 0 | return PR_SUCCESS; |
1697 | 0 | } |
1698 | | |
1699 | | /* Configure a self encryption key pair. |explicitConfig| is set to true for |
1700 | | * calls to SSL_SetSessionTicketKeyPair(), false for implicit configuration. |
1701 | | * This assumes that the setup has been run. */ |
1702 | | static SECStatus |
1703 | | ssl_SetSelfEncryptKeyPair(SECKEYPublicKey *pubKey, |
1704 | | SECKEYPrivateKey *privKey, |
1705 | | PRBool explicitConfig) |
1706 | 0 | { |
1707 | 0 | SECKEYPublicKey *pubKeyCopy, *oldPubKey; |
1708 | 0 | SECKEYPrivateKey *privKeyCopy, *oldPrivKey; |
1709 | |
|
1710 | 0 | PORT_Assert(ssl_self_encrypt_key_pair.lock); |
1711 | 0 | pubKeyCopy = SECKEY_CopyPublicKey(pubKey); |
1712 | 0 | privKeyCopy = SECKEY_CopyPrivateKey(privKey); |
1713 | |
|
1714 | 0 | if (!pubKeyCopy || !privKeyCopy) { |
1715 | 0 | SECKEY_DestroyPublicKey(pubKeyCopy); |
1716 | 0 | SECKEY_DestroyPrivateKey(privKeyCopy); |
1717 | 0 | PORT_SetError(SEC_ERROR_NO_MEMORY); |
1718 | 0 | return SECFailure; |
1719 | 0 | } |
1720 | | |
1721 | 0 | PR_RWLock_Wlock(ssl_self_encrypt_key_pair.lock); |
1722 | 0 | oldPubKey = ssl_self_encrypt_key_pair.pubKey; |
1723 | 0 | oldPrivKey = ssl_self_encrypt_key_pair.privKey; |
1724 | 0 | ssl_self_encrypt_key_pair.pubKey = pubKeyCopy; |
1725 | 0 | ssl_self_encrypt_key_pair.privKey = privKeyCopy; |
1726 | 0 | ssl_self_encrypt_key_pair.configured = explicitConfig; |
1727 | 0 | PR_RWLock_Unlock(ssl_self_encrypt_key_pair.lock); |
1728 | |
|
1729 | 0 | if (oldPubKey) { |
1730 | 0 | PORT_Assert(oldPrivKey); |
1731 | 0 | SECKEY_DestroyPublicKey(oldPubKey); |
1732 | 0 | SECKEY_DestroyPrivateKey(oldPrivKey); |
1733 | 0 | } |
1734 | |
|
1735 | 0 | return SECSuccess; |
1736 | 0 | } |
1737 | | |
1738 | | /* This is really the self-encryption keys but it has the |
1739 | | * wrong name for historical API stability reasons. */ |
1740 | | SECStatus |
1741 | | SSL_SetSessionTicketKeyPair(SECKEYPublicKey *pubKey, |
1742 | | SECKEYPrivateKey *privKey) |
1743 | 0 | { |
1744 | 0 | if (SECKEY_GetPublicKeyType(pubKey) != rsaKey || |
1745 | 0 | SECKEY_GetPrivateKeyType(privKey) != rsaKey) { |
1746 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1747 | 0 | return SECFailure; |
1748 | 0 | } |
1749 | | |
1750 | 0 | if (PR_SUCCESS != PR_CallOnce(&ssl_self_encrypt_key_pair.setup, |
1751 | 0 | &ssl_SelfEncryptSetup)) { |
1752 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1753 | 0 | return SECFailure; |
1754 | 0 | } |
1755 | | |
1756 | 0 | return ssl_SetSelfEncryptKeyPair(pubKey, privKey, PR_TRUE); |
1757 | 0 | } |
1758 | | |
1759 | | /* When configuring a server cert, we should save the RSA key in case it is |
1760 | | * needed for self-encryption. This saves the latest copy, unless there has |
1761 | | * been an explicit call to SSL_SetSessionTicketKeyPair(). */ |
1762 | | SECStatus |
1763 | | ssl_MaybeSetSelfEncryptKeyPair(const sslKeyPair *keyPair) |
1764 | 0 | { |
1765 | 0 | PRBool configured; |
1766 | |
|
1767 | 0 | if (PR_SUCCESS != PR_CallOnce(&ssl_self_encrypt_key_pair.setup, |
1768 | 0 | &ssl_SelfEncryptSetup)) { |
1769 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1770 | 0 | return SECFailure; |
1771 | 0 | } |
1772 | | |
1773 | 0 | PR_RWLock_Rlock(ssl_self_encrypt_key_pair.lock); |
1774 | 0 | configured = ssl_self_encrypt_key_pair.configured; |
1775 | 0 | PR_RWLock_Unlock(ssl_self_encrypt_key_pair.lock); |
1776 | 0 | if (configured) { |
1777 | 0 | return SECSuccess; |
1778 | 0 | } |
1779 | 0 | return ssl_SetSelfEncryptKeyPair(keyPair->pubKey, |
1780 | 0 | keyPair->privKey, PR_FALSE); |
1781 | 0 | } |
1782 | | |
1783 | | static SECStatus |
1784 | | ssl_GetSelfEncryptKeyPair(SECKEYPublicKey **pubKey, |
1785 | | SECKEYPrivateKey **privKey) |
1786 | 0 | { |
1787 | 0 | if (PR_SUCCESS != PR_CallOnce(&ssl_self_encrypt_key_pair.setup, |
1788 | 0 | &ssl_SelfEncryptSetup)) { |
1789 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1790 | 0 | return SECFailure; |
1791 | 0 | } |
1792 | | |
1793 | 0 | SECKEYPublicKey *pubKeyCopy = NULL; |
1794 | 0 | SECKEYPrivateKey *privKeyCopy = NULL; |
1795 | 0 | PRBool noKey = PR_FALSE; |
1796 | |
|
1797 | 0 | PR_RWLock_Rlock(ssl_self_encrypt_key_pair.lock); |
1798 | 0 | if (ssl_self_encrypt_key_pair.pubKey && ssl_self_encrypt_key_pair.privKey) { |
1799 | 0 | pubKeyCopy = SECKEY_CopyPublicKey(ssl_self_encrypt_key_pair.pubKey); |
1800 | 0 | privKeyCopy = SECKEY_CopyPrivateKey(ssl_self_encrypt_key_pair.privKey); |
1801 | 0 | } else { |
1802 | 0 | noKey = PR_TRUE; |
1803 | 0 | } |
1804 | 0 | PR_RWLock_Unlock(ssl_self_encrypt_key_pair.lock); |
1805 | |
|
1806 | 0 | if (noKey) { |
1807 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1808 | 0 | return SECFailure; |
1809 | 0 | } |
1810 | | |
1811 | 0 | if (!pubKeyCopy || !privKeyCopy) { |
1812 | 0 | SECKEY_DestroyPublicKey(pubKeyCopy); |
1813 | 0 | SECKEY_DestroyPrivateKey(privKeyCopy); |
1814 | 0 | PORT_SetError(SEC_ERROR_NO_MEMORY); |
1815 | 0 | return SECFailure; |
1816 | 0 | } |
1817 | | |
1818 | 0 | *pubKey = pubKeyCopy; |
1819 | 0 | *privKey = privKeyCopy; |
1820 | 0 | return SECSuccess; |
1821 | 0 | } |
1822 | | |
1823 | | static SECStatus |
1824 | | ssl_GenerateSelfEncryptKeys(void *pwArg, PRUint8 *keyName, |
1825 | | PK11SymKey **aesKey, PK11SymKey **macKey); |
1826 | | |
1827 | | static PRStatus |
1828 | | ssl_GenerateSelfEncryptKeysOnce(void *arg) |
1829 | 0 | { |
1830 | 0 | SECStatus rv; |
1831 | | |
1832 | | /* Get a copy of the session keys from shared memory. */ |
1833 | 0 | PORT_Memcpy(ssl_self_encrypt_keys.keyName, |
1834 | 0 | SELF_ENCRYPT_KEY_NAME_PREFIX, |
1835 | 0 | sizeof(SELF_ENCRYPT_KEY_NAME_PREFIX)); |
1836 | | /* This function calls ssl_GetSelfEncryptKeyPair(), which initializes the |
1837 | | * key pair stuff. That allows this to use the same shutdown function. */ |
1838 | 0 | rv = ssl_GenerateSelfEncryptKeys(arg, ssl_self_encrypt_keys.keyName, |
1839 | 0 | &ssl_self_encrypt_keys.encKey, |
1840 | 0 | &ssl_self_encrypt_keys.macKey); |
1841 | 0 | if (rv != SECSuccess) { |
1842 | 0 | return PR_FAILURE; |
1843 | 0 | } |
1844 | | |
1845 | 0 | return PR_SUCCESS; |
1846 | 0 | } |
1847 | | |
1848 | | SECStatus |
1849 | | ssl_GetSelfEncryptKeys(sslSocket *ss, PRUint8 *keyName, |
1850 | | PK11SymKey **encKey, PK11SymKey **macKey) |
1851 | 0 | { |
1852 | 0 | if (PR_SUCCESS != PR_CallOnceWithArg(&ssl_self_encrypt_keys.setup, |
1853 | 0 | &ssl_GenerateSelfEncryptKeysOnce, |
1854 | 0 | ss->pkcs11PinArg)) { |
1855 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1856 | 0 | return SECFailure; |
1857 | 0 | } |
1858 | | |
1859 | 0 | if (!ssl_self_encrypt_keys.encKey || !ssl_self_encrypt_keys.macKey) { |
1860 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1861 | 0 | return SECFailure; |
1862 | 0 | } |
1863 | | |
1864 | 0 | PORT_Memcpy(keyName, ssl_self_encrypt_keys.keyName, |
1865 | 0 | sizeof(ssl_self_encrypt_keys.keyName)); |
1866 | 0 | *encKey = ssl_self_encrypt_keys.encKey; |
1867 | 0 | *macKey = ssl_self_encrypt_keys.macKey; |
1868 | 0 | return SECSuccess; |
1869 | 0 | } |
1870 | | |
1871 | | /* If lockTime is zero, it implies that the lock is not held, and must be |
1872 | | * aquired here. |
1873 | | */ |
1874 | | static SECStatus |
1875 | | getSvrWrappingKey(unsigned int symWrapMechIndex, |
1876 | | unsigned int wrapKeyIndex, |
1877 | | SSLWrappedSymWrappingKey *wswk, |
1878 | | cacheDesc *cache, |
1879 | | PRUint32 lockTime) |
1880 | 0 | { |
1881 | 0 | PRUint32 ndx = (wrapKeyIndex * SSL_NUM_WRAP_MECHS) + symWrapMechIndex; |
1882 | 0 | SSLWrappedSymWrappingKey *pwswk = cache->keyCacheData + ndx; |
1883 | 0 | PRUint32 now = 0; |
1884 | 0 | PRBool rv = SECFailure; |
1885 | |
|
1886 | 0 | if (!cache->cacheMem) { /* cache is uninitialized */ |
1887 | 0 | PORT_SetError(SSL_ERROR_SERVER_CACHE_NOT_CONFIGURED); |
1888 | 0 | return SECFailure; |
1889 | 0 | } |
1890 | 0 | if (!lockTime) { |
1891 | 0 | now = LockSidCacheLock(cache->keyCacheLock, 0); |
1892 | 0 | if (!now) { |
1893 | 0 | return SECFailure; |
1894 | 0 | } |
1895 | 0 | } |
1896 | 0 | if (pwswk->wrapKeyIndex == wrapKeyIndex && |
1897 | 0 | pwswk->wrapMechIndex == symWrapMechIndex && |
1898 | 0 | pwswk->wrappedSymKeyLen != 0) { |
1899 | 0 | *wswk = *pwswk; |
1900 | 0 | rv = SECSuccess; |
1901 | 0 | } |
1902 | 0 | if (now) { |
1903 | 0 | UnlockSidCacheLock(cache->keyCacheLock); |
1904 | 0 | } |
1905 | 0 | return rv; |
1906 | 0 | } |
1907 | | |
1908 | | SECStatus |
1909 | | ssl_GetWrappingKey(unsigned int wrapMechIndex, |
1910 | | unsigned int wrapKeyIndex, |
1911 | | SSLWrappedSymWrappingKey *wswk) |
1912 | 0 | { |
1913 | 0 | PORT_Assert(wrapMechIndex < SSL_NUM_WRAP_MECHS); |
1914 | 0 | PORT_Assert(wrapKeyIndex < SSL_NUM_WRAP_KEYS); |
1915 | 0 | if (wrapMechIndex >= SSL_NUM_WRAP_MECHS || |
1916 | 0 | wrapKeyIndex >= SSL_NUM_WRAP_KEYS) { |
1917 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1918 | 0 | return SECFailure; |
1919 | 0 | } |
1920 | | |
1921 | 0 | return getSvrWrappingKey(wrapMechIndex, wrapKeyIndex, wswk, |
1922 | 0 | &globalCache, 0); |
1923 | 0 | } |
1924 | | |
1925 | | /* Wrap and cache a session ticket key. */ |
1926 | | static SECStatus |
1927 | | WrapSelfEncryptKey(SECKEYPublicKey *svrPubKey, PK11SymKey *symKey, |
1928 | | const char *keyName, encKeyCacheEntry *cacheEntry) |
1929 | 0 | { |
1930 | 0 | SECItem wrappedKey = { siBuffer, NULL, 0 }; |
1931 | |
|
1932 | 0 | wrappedKey.len = SECKEY_PublicKeyStrength(svrPubKey); |
1933 | 0 | PORT_Assert(wrappedKey.len <= sizeof(cacheEntry->bytes)); |
1934 | 0 | if (wrappedKey.len > sizeof(cacheEntry->bytes)) |
1935 | 0 | return PR_FALSE; |
1936 | 0 | wrappedKey.data = cacheEntry->bytes; |
1937 | |
|
1938 | 0 | if (PK11_PubWrapSymKey(CKM_RSA_PKCS, svrPubKey, symKey, &wrappedKey) != |
1939 | 0 | SECSuccess) { |
1940 | 0 | SSL_DBG(("%d: SSL[%s]: Unable to wrap self encrypt key %s.", |
1941 | 0 | SSL_GETPID(), "unknown", keyName)); |
1942 | 0 | return SECFailure; |
1943 | 0 | } |
1944 | 0 | cacheEntry->length = wrappedKey.len; |
1945 | 0 | return SECSuccess; |
1946 | 0 | } |
1947 | | |
1948 | | static SECStatus |
1949 | | GenerateSelfEncryptKeys(void *pwArg, PRUint8 *keyName, PK11SymKey **aesKey, |
1950 | | PK11SymKey **macKey) |
1951 | 0 | { |
1952 | 0 | PK11SlotInfo *slot; |
1953 | 0 | CK_MECHANISM_TYPE mechanismArray[2]; |
1954 | 0 | PK11SymKey *aesKeyTmp = NULL; |
1955 | 0 | PK11SymKey *macKeyTmp = NULL; |
1956 | 0 | cacheDesc *cache = &globalCache; |
1957 | 0 | PRUint8 ticketKeyNameSuffixLocal[SELF_ENCRYPT_KEY_VAR_NAME_LEN]; |
1958 | 0 | PRUint8 *ticketKeyNameSuffix; |
1959 | |
|
1960 | 0 | if (!cache->cacheMem) { |
1961 | | /* cache is not initalized. Use stack buffer */ |
1962 | 0 | ticketKeyNameSuffix = ticketKeyNameSuffixLocal; |
1963 | 0 | } else { |
1964 | 0 | ticketKeyNameSuffix = cache->ticketKeyNameSuffix; |
1965 | 0 | } |
1966 | |
|
1967 | 0 | if (PK11_GenerateRandom(ticketKeyNameSuffix, |
1968 | 0 | SELF_ENCRYPT_KEY_VAR_NAME_LEN) != |
1969 | 0 | SECSuccess) { |
1970 | 0 | SSL_DBG(("%d: SSL[%s]: Unable to generate random key name bytes.", |
1971 | 0 | SSL_GETPID(), "unknown")); |
1972 | 0 | return SECFailure; |
1973 | 0 | } |
1974 | | |
1975 | 0 | mechanismArray[0] = CKM_AES_CBC; |
1976 | 0 | mechanismArray[1] = CKM_SHA256_HMAC; |
1977 | |
|
1978 | 0 | slot = PK11_GetBestSlotMultiple(mechanismArray, 2, pwArg); |
1979 | 0 | if (slot) { |
1980 | 0 | aesKeyTmp = PK11_KeyGen(slot, mechanismArray[0], NULL, |
1981 | 0 | AES_256_KEY_LENGTH, pwArg); |
1982 | 0 | macKeyTmp = PK11_KeyGen(slot, mechanismArray[1], NULL, |
1983 | 0 | SHA256_LENGTH, pwArg); |
1984 | 0 | PK11_FreeSlot(slot); |
1985 | 0 | } |
1986 | |
|
1987 | 0 | if (aesKeyTmp == NULL || macKeyTmp == NULL) { |
1988 | 0 | SSL_DBG(("%d: SSL[%s]: Unable to generate session ticket keys.", |
1989 | 0 | SSL_GETPID(), "unknown")); |
1990 | 0 | goto loser; |
1991 | 0 | } |
1992 | 0 | PORT_Memcpy(keyName, ticketKeyNameSuffix, SELF_ENCRYPT_KEY_VAR_NAME_LEN); |
1993 | 0 | *aesKey = aesKeyTmp; |
1994 | 0 | *macKey = macKeyTmp; |
1995 | 0 | return SECSuccess; |
1996 | | |
1997 | 0 | loser: |
1998 | 0 | if (aesKeyTmp) |
1999 | 0 | PK11_FreeSymKey(aesKeyTmp); |
2000 | 0 | if (macKeyTmp) |
2001 | 0 | PK11_FreeSymKey(macKeyTmp); |
2002 | 0 | return SECFailure; |
2003 | 0 | } |
2004 | | |
2005 | | static SECStatus |
2006 | | GenerateAndWrapSelfEncryptKeys(SECKEYPublicKey *svrPubKey, void *pwArg, |
2007 | | PRUint8 *keyName, PK11SymKey **aesKey, |
2008 | | PK11SymKey **macKey) |
2009 | 0 | { |
2010 | 0 | PK11SymKey *aesKeyTmp = NULL; |
2011 | 0 | PK11SymKey *macKeyTmp = NULL; |
2012 | 0 | cacheDesc *cache = &globalCache; |
2013 | 0 | SECStatus rv; |
2014 | |
|
2015 | 0 | rv = GenerateSelfEncryptKeys(pwArg, keyName, &aesKeyTmp, &macKeyTmp); |
2016 | 0 | if (rv != SECSuccess) { |
2017 | 0 | return SECFailure; |
2018 | 0 | } |
2019 | | |
2020 | 0 | if (cache->cacheMem) { |
2021 | | /* Export the keys to the shared cache in wrapped form. */ |
2022 | 0 | rv = WrapSelfEncryptKey(svrPubKey, aesKeyTmp, "enc key", cache->ticketEncKey); |
2023 | 0 | if (rv != SECSuccess) { |
2024 | 0 | goto loser; |
2025 | 0 | } |
2026 | 0 | rv = WrapSelfEncryptKey(svrPubKey, macKeyTmp, "mac key", cache->ticketMacKey); |
2027 | 0 | if (rv != SECSuccess) { |
2028 | 0 | goto loser; |
2029 | 0 | } |
2030 | 0 | } |
2031 | 0 | *aesKey = aesKeyTmp; |
2032 | 0 | *macKey = macKeyTmp; |
2033 | 0 | return SECSuccess; |
2034 | | |
2035 | 0 | loser: |
2036 | 0 | PK11_FreeSymKey(aesKeyTmp); |
2037 | 0 | PK11_FreeSymKey(macKeyTmp); |
2038 | 0 | return SECFailure; |
2039 | 0 | } |
2040 | | |
2041 | | static SECStatus |
2042 | | UnwrapCachedSelfEncryptKeys(SECKEYPrivateKey *svrPrivKey, PRUint8 *keyName, |
2043 | | PK11SymKey **aesKey, PK11SymKey **macKey) |
2044 | 0 | { |
2045 | 0 | SECItem wrappedKey = { siBuffer, NULL, 0 }; |
2046 | 0 | PK11SymKey *aesKeyTmp = NULL; |
2047 | 0 | PK11SymKey *macKeyTmp = NULL; |
2048 | 0 | cacheDesc *cache = &globalCache; |
2049 | |
|
2050 | 0 | wrappedKey.data = cache->ticketEncKey->bytes; |
2051 | 0 | wrappedKey.len = cache->ticketEncKey->length; |
2052 | 0 | PORT_Assert(wrappedKey.len <= sizeof(cache->ticketEncKey->bytes)); |
2053 | 0 | aesKeyTmp = PK11_PubUnwrapSymKey(svrPrivKey, &wrappedKey, |
2054 | 0 | CKM_AES_CBC, CKA_DECRYPT, 0); |
2055 | |
|
2056 | 0 | wrappedKey.data = cache->ticketMacKey->bytes; |
2057 | 0 | wrappedKey.len = cache->ticketMacKey->length; |
2058 | 0 | PORT_Assert(wrappedKey.len <= sizeof(cache->ticketMacKey->bytes)); |
2059 | 0 | macKeyTmp = PK11_PubUnwrapSymKey(svrPrivKey, &wrappedKey, |
2060 | 0 | CKM_SHA256_HMAC, CKA_SIGN, 0); |
2061 | |
|
2062 | 0 | if (aesKeyTmp == NULL || macKeyTmp == NULL) { |
2063 | 0 | SSL_DBG(("%d: SSL[%s]: Unable to unwrap session ticket keys.", |
2064 | 0 | SSL_GETPID(), "unknown")); |
2065 | 0 | goto loser; |
2066 | 0 | } |
2067 | 0 | SSL_DBG(("%d: SSL[%s]: Successfully unwrapped session ticket keys.", |
2068 | 0 | SSL_GETPID(), "unknown")); |
2069 | |
|
2070 | 0 | PORT_Memcpy(keyName, cache->ticketKeyNameSuffix, |
2071 | 0 | SELF_ENCRYPT_KEY_VAR_NAME_LEN); |
2072 | 0 | *aesKey = aesKeyTmp; |
2073 | 0 | *macKey = macKeyTmp; |
2074 | 0 | return SECSuccess; |
2075 | | |
2076 | 0 | loser: |
2077 | 0 | if (aesKeyTmp) |
2078 | 0 | PK11_FreeSymKey(aesKeyTmp); |
2079 | 0 | if (macKeyTmp) |
2080 | 0 | PK11_FreeSymKey(macKeyTmp); |
2081 | 0 | return SECFailure; |
2082 | 0 | } |
2083 | | |
2084 | | static SECStatus |
2085 | | ssl_GenerateSelfEncryptKeys(void *pwArg, PRUint8 *keyName, |
2086 | | PK11SymKey **encKey, PK11SymKey **macKey) |
2087 | 0 | { |
2088 | 0 | SECKEYPrivateKey *svrPrivKey = NULL; |
2089 | 0 | SECKEYPublicKey *svrPubKey = NULL; |
2090 | 0 | PRUint32 now; |
2091 | 0 | cacheDesc *cache = &globalCache; |
2092 | |
|
2093 | 0 | SECStatus rv = ssl_GetSelfEncryptKeyPair(&svrPubKey, &svrPrivKey); |
2094 | 0 | if (rv != SECSuccess || !cache->cacheMem) { |
2095 | | /* No key pair for wrapping, or the cache is uninitialized. Generate |
2096 | | * keys and return them without caching. */ |
2097 | 0 | rv = GenerateSelfEncryptKeys(pwArg, keyName, encKey, macKey); |
2098 | 0 | } else { |
2099 | 0 | now = LockSidCacheLock(cache->keyCacheLock, 0); |
2100 | 0 | if (!now) { |
2101 | 0 | goto loser; |
2102 | 0 | } |
2103 | | |
2104 | 0 | if (*(cache->ticketKeysValid)) { |
2105 | 0 | rv = UnwrapCachedSelfEncryptKeys(svrPrivKey, keyName, encKey, macKey); |
2106 | 0 | } else { |
2107 | | /* Keys do not exist, create them. */ |
2108 | 0 | rv = GenerateAndWrapSelfEncryptKeys(svrPubKey, pwArg, keyName, |
2109 | 0 | encKey, macKey); |
2110 | 0 | if (rv == SECSuccess) { |
2111 | 0 | *(cache->ticketKeysValid) = 1; |
2112 | 0 | } |
2113 | 0 | } |
2114 | 0 | UnlockSidCacheLock(cache->keyCacheLock); |
2115 | 0 | } |
2116 | 0 | SECKEY_DestroyPublicKey(svrPubKey); |
2117 | 0 | SECKEY_DestroyPrivateKey(svrPrivKey); |
2118 | 0 | return rv; |
2119 | | |
2120 | 0 | loser: |
2121 | 0 | UnlockSidCacheLock(cache->keyCacheLock); |
2122 | 0 | SECKEY_DestroyPublicKey(svrPubKey); |
2123 | 0 | SECKEY_DestroyPrivateKey(svrPrivKey); |
2124 | 0 | return SECFailure; |
2125 | 0 | } |
2126 | | |
2127 | | /* The caller passes in the new value it wants |
2128 | | * to set. This code tests the wrapped sym key entry in the shared memory. |
2129 | | * If it is uninitialized, this function writes the caller's value into |
2130 | | * the disk entry, and returns false. |
2131 | | * Otherwise, it overwrites the caller's wswk with the value obtained from |
2132 | | * the disk, and returns PR_TRUE. |
2133 | | * This is all done while holding the locks/mutexes necessary to make |
2134 | | * the operation atomic. |
2135 | | */ |
2136 | | SECStatus |
2137 | | ssl_SetWrappingKey(SSLWrappedSymWrappingKey *wswk) |
2138 | 0 | { |
2139 | 0 | cacheDesc *cache = &globalCache; |
2140 | 0 | PRBool rv = SECFailure; |
2141 | 0 | PRUint32 ndx; |
2142 | 0 | PRUint32 now; |
2143 | 0 | SSLWrappedSymWrappingKey myWswk; |
2144 | |
|
2145 | 0 | if (!cache->cacheMem) { /* cache is uninitialized */ |
2146 | 0 | PORT_SetError(SSL_ERROR_SERVER_CACHE_NOT_CONFIGURED); |
2147 | 0 | return SECFailure; |
2148 | 0 | } |
2149 | | |
2150 | 0 | PORT_Assert(wswk->wrapMechIndex < SSL_NUM_WRAP_MECHS); |
2151 | 0 | PORT_Assert(wswk->wrapKeyIndex < SSL_NUM_WRAP_KEYS); |
2152 | 0 | if (wswk->wrapMechIndex >= SSL_NUM_WRAP_MECHS || |
2153 | 0 | wswk->wrapKeyIndex >= SSL_NUM_WRAP_KEYS) { |
2154 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2155 | 0 | return SECFailure; |
2156 | 0 | } |
2157 | | |
2158 | 0 | ndx = (wswk->wrapKeyIndex * SSL_NUM_WRAP_MECHS) + wswk->wrapMechIndex; |
2159 | 0 | PORT_Memset(&myWswk, 0, sizeof myWswk); /* eliminate UMRs. */ |
2160 | |
|
2161 | 0 | now = LockSidCacheLock(cache->keyCacheLock, 0); |
2162 | 0 | if (!now) { |
2163 | 0 | return SECFailure; |
2164 | 0 | } |
2165 | 0 | rv = getSvrWrappingKey(wswk->wrapMechIndex, wswk->wrapKeyIndex, |
2166 | 0 | &myWswk, cache, now); |
2167 | 0 | if (rv == SECSuccess) { |
2168 | | /* we found it on disk, copy it out to the caller. */ |
2169 | 0 | PORT_Memcpy(wswk, &myWswk, sizeof *wswk); |
2170 | 0 | } else { |
2171 | | /* Wasn't on disk, and we're still holding the lock, so write it. */ |
2172 | 0 | cache->keyCacheData[ndx] = *wswk; |
2173 | 0 | } |
2174 | 0 | UnlockSidCacheLock(cache->keyCacheLock); |
2175 | 0 | return rv; |
2176 | 0 | } |
2177 | | |
2178 | | #else /* MAC version or other platform */ |
2179 | | |
2180 | | #include "seccomon.h" |
2181 | | #include "cert.h" |
2182 | | #include "ssl.h" |
2183 | | #include "sslimpl.h" |
2184 | | |
2185 | | SECStatus |
2186 | | SSL_ConfigServerSessionIDCache(int maxCacheEntries, |
2187 | | PRUint32 ssl2_timeout, |
2188 | | PRUint32 ssl3_timeout, |
2189 | | const char *directory) |
2190 | | { |
2191 | | PR_ASSERT(!"SSL servers are not supported on this platform. (SSL_ConfigServerSessionIDCache)"); |
2192 | | return SECFailure; |
2193 | | } |
2194 | | |
2195 | | SECStatus |
2196 | | SSL_ConfigMPServerSIDCache(int maxCacheEntries, |
2197 | | PRUint32 ssl2_timeout, |
2198 | | PRUint32 ssl3_timeout, |
2199 | | const char *directory) |
2200 | | { |
2201 | | PR_ASSERT(!"SSL servers are not supported on this platform. (SSL_ConfigMPServerSIDCache)"); |
2202 | | return SECFailure; |
2203 | | } |
2204 | | |
2205 | | SECStatus |
2206 | | SSL_InheritMPServerSIDCache(const char *envString) |
2207 | | { |
2208 | | PR_ASSERT(!"SSL servers are not supported on this platform. (SSL_InheritMPServerSIDCache)"); |
2209 | | return SECFailure; |
2210 | | } |
2211 | | |
2212 | | SECStatus |
2213 | | ssl_GetWrappingKey(unsigned int wrapMechIndex, |
2214 | | unsigned int wrapKeyIndex, |
2215 | | SSLWrappedSymWrappingKey *wswk) |
2216 | | { |
2217 | | PR_ASSERT(!"SSL servers are not supported on this platform. (ssl_GetWrappingKey)"); |
2218 | | return SECFailure; |
2219 | | } |
2220 | | |
2221 | | /* This is a kind of test-and-set. The caller passes in the new value it wants |
2222 | | * to set. This code tests the wrapped sym key entry in the shared memory. |
2223 | | * If it is uninitialized, this function writes the caller's value into |
2224 | | * the disk entry, and returns false. |
2225 | | * Otherwise, it overwrites the caller's wswk with the value obtained from |
2226 | | * the disk, and returns PR_TRUE. |
2227 | | * This is all done while holding the locks/mutexes necessary to make |
2228 | | * the operation atomic. |
2229 | | */ |
2230 | | SECStatus |
2231 | | ssl_SetWrappingKey(SSLWrappedSymWrappingKey *wswk) |
2232 | | { |
2233 | | PR_ASSERT(!"SSL servers are not supported on this platform. (ssl_SetWrappingKey)"); |
2234 | | return SECFailure; |
2235 | | } |
2236 | | |
2237 | | PRUint32 |
2238 | | SSL_GetMaxServerCacheLocks(void) |
2239 | | { |
2240 | | PR_ASSERT(!"SSL servers are not supported on this platform. (SSL_GetMaxServerCacheLocks)"); |
2241 | | return -1; |
2242 | | } |
2243 | | |
2244 | | SECStatus |
2245 | | SSL_SetMaxServerCacheLocks(PRUint32 maxLocks) |
2246 | | { |
2247 | | PR_ASSERT(!"SSL servers are not supported on this platform. (SSL_SetMaxServerCacheLocks)"); |
2248 | | return SECFailure; |
2249 | | } |
2250 | | |
2251 | | #endif /* XP_UNIX || XP_WIN32 */ |