~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/pk11wrap/dev3hack.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

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
#ifdef DEBUG
 
38
static const char CVS_ID[] = "@(#) $RCSfile: dev3hack.c,v $ $Revision: 1.21 $ $Date: 2005/01/20 02:25:48 $";
 
39
#endif /* DEBUG */
 
40
 
 
41
#ifndef NSS_3_4_CODE
 
42
#define NSS_3_4_CODE
 
43
#endif /* NSS_3_4_CODE */
 
44
 
 
45
#ifndef PKIT_H
 
46
#include "pkit.h"
 
47
#endif /* PKIT_H */
 
48
 
 
49
#ifndef DEVM_H
 
50
#include "devm.h"
 
51
#endif /* DEVM_H */
 
52
 
 
53
#include "pki3hack.h"
 
54
#include "dev3hack.h"
 
55
#include "pkim.h"
 
56
 
 
57
#ifndef BASE_H
 
58
#include "base.h"
 
59
#endif /* BASE_H */
 
60
 
 
61
#include "pk11func.h"
 
62
#include "secmodti.h"
 
63
 
 
64
NSS_IMPLEMENT nssSession *
 
65
nssSession_ImportNSS3Session(NSSArena *arenaOpt,
 
66
                             CK_SESSION_HANDLE session, 
 
67
                             PZLock *lock, PRBool rw)
 
68
{
 
69
    nssSession *rvSession;
 
70
    rvSession = nss_ZNEW(arenaOpt, nssSession);
 
71
    rvSession->handle = session;
 
72
    rvSession->lock = lock;
 
73
    rvSession->ownLock = PR_FALSE;
 
74
    rvSession->isRW = rw;
 
75
    return rvSession;
 
76
}
 
77
 
 
78
NSS_IMPLEMENT nssSession *
 
79
nssSlot_CreateSession
 
80
(
 
81
  NSSSlot *slot,
 
82
  NSSArena *arenaOpt,
 
83
  PRBool readWrite
 
84
)
 
85
{
 
86
    nssSession *rvSession;
 
87
    rvSession = nss_ZNEW(arenaOpt, nssSession);
 
88
    if (!rvSession) {
 
89
        return (nssSession *)NULL;
 
90
    }
 
91
    if (readWrite) {
 
92
        rvSession->handle = PK11_GetRWSession(slot->pk11slot);
 
93
        if (rvSession->handle == CK_INVALID_HANDLE) {
 
94
            nss_ZFreeIf(rvSession);
 
95
            return NULL;
 
96
        }
 
97
        rvSession->isRW = PR_TRUE;
 
98
        rvSession->slot = slot;
 
99
        /*
 
100
         * The session doesn't need its own lock.  Here's why.
 
101
         * 1. If we are reusing the default RW session of the slot,
 
102
         *    the slot lock is already locked to protect the session.
 
103
         * 2. If the module is not thread safe, the slot (or rather
 
104
         *    module) lock is already locked.
 
105
         * 3. If the module is thread safe and we are using a new
 
106
         *    session, no higher-level lock has been locked and we
 
107
         *    would need a lock for the new session.  However, the
 
108
         *    NSS_3_4_CODE usage of the session is that it is always
 
109
         *    used and destroyed within the same function and never
 
110
         *    shared with another thread.
 
111
         * So the session is either already protected by another
 
112
         * lock or only used by one thread.
 
113
         */
 
114
        rvSession->lock = NULL;
 
115
        rvSession->ownLock = PR_FALSE;
 
116
        return rvSession;
 
117
    } else {
 
118
        return NULL;
 
119
    }
 
120
}
 
121
 
 
122
NSS_IMPLEMENT PRStatus
 
123
nssSession_Destroy
 
124
(
 
125
  nssSession *s
 
126
)
 
127
{
 
128
    CK_RV ckrv = CKR_OK;
 
129
    if (s) {
 
130
        if (s->isRW) {
 
131
            PK11_RestoreROSession(s->slot->pk11slot, s->handle);
 
132
        }
 
133
        nss_ZFreeIf(s);
 
134
    }
 
135
    return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE;
 
136
}
 
137
 
 
138
static NSSSlot *
 
139
nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
 
140
{
 
141
    NSSSlot *rvSlot;
 
142
    NSSArena *arena;
 
143
    arena = nssArena_Create();
 
144
    if (!arena) {
 
145
        return NULL;
 
146
    }
 
147
    rvSlot = nss_ZNEW(arena, NSSSlot);
 
148
    if (!rvSlot) {
 
149
        nssArena_Destroy(arena);
 
150
        return NULL;
 
151
    }
 
152
    rvSlot->base.refCount = 1;
 
153
    rvSlot->base.lock = PZ_NewLock(nssILockOther);
 
154
    rvSlot->base.arena = arena;
 
155
    rvSlot->pk11slot = nss3slot;
 
156
    rvSlot->epv = nss3slot->functionList;
 
157
    rvSlot->slotID = nss3slot->slotID;
 
158
    /* Grab the slot name from the PKCS#11 fixed-length buffer */
 
159
    rvSlot->base.name = nssUTF8_Duplicate(nss3slot->slot_name,td->arena);
 
160
    rvSlot->lock = (nss3slot->isThreadSafe) ? NULL : nss3slot->sessionLock;
 
161
    return rvSlot;
 
162
}
 
163
 
 
164
NSS_IMPLEMENT NSSToken *
 
165
nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
 
166
{
 
167
    NSSToken *rvToken;
 
168
    NSSArena *arena;
 
169
    arena = nssArena_Create();
 
170
    if (!arena) {
 
171
        return NULL;
 
172
    }
 
173
    rvToken = nss_ZNEW(arena, NSSToken);
 
174
    if (!rvToken) {
 
175
        nssArena_Destroy(arena);
 
176
        return NULL;
 
177
    }
 
178
    rvToken->base.refCount = 1;
 
179
    rvToken->base.lock = PZ_NewLock(nssILockOther);
 
180
    rvToken->base.arena = arena;
 
181
    rvToken->pk11slot = nss3slot;
 
182
    rvToken->epv = nss3slot->functionList;
 
183
    rvToken->defaultSession = nssSession_ImportNSS3Session(td->arena,
 
184
                                                       nss3slot->session,
 
185
                                                       nss3slot->sessionLock,
 
186
                                                       nss3slot->defRWSession);
 
187
    /* The above test was used in 3.4, for this cache have it always on */
 
188
    if (!PK11_IsInternal(nss3slot) && PK11_IsHW(nss3slot)) {
 
189
        rvToken->cache = nssTokenObjectCache_Create(rvToken, 
 
190
                                                    PR_TRUE, PR_TRUE, PR_TRUE);
 
191
        if (!rvToken->cache) {
 
192
            nssArena_Destroy(arena);
 
193
            return (NSSToken *)NULL;
 
194
        }
 
195
    }
 
196
    rvToken->trustDomain = td;
 
197
    /* Grab the token name from the PKCS#11 fixed-length buffer */
 
198
    rvToken->base.name = nssUTF8_Duplicate(nss3slot->token_name,td->arena);
 
199
    rvToken->slot = nssSlot_CreateFromPK11SlotInfo(td, nss3slot);
 
200
    rvToken->slot->token = rvToken;
 
201
    rvToken->defaultSession->slot = rvToken->slot;
 
202
    return rvToken;
 
203
}
 
204
 
 
205
NSS_IMPLEMENT void
 
206
nssToken_UpdateName(NSSToken *token)
 
207
{
 
208
    if (!token) {
 
209
        return;
 
210
    }
 
211
    token->base.name = nssUTF8_Duplicate(token->pk11slot->token_name,token->base.arena);
 
212
}
 
213
 
 
214
NSS_IMPLEMENT PRBool
 
215
nssSlot_IsPermanent
 
216
(
 
217
  NSSSlot *slot
 
218
)
 
219
{
 
220
    return slot->pk11slot->isPerm;
 
221
}
 
222
 
 
223
NSS_IMPLEMENT PRBool
 
224
nssSlot_IsFriendly
 
225
(
 
226
  NSSSlot *slot
 
227
)
 
228
{
 
229
    return PK11_IsFriendly(slot->pk11slot);
 
230
}
 
231
 
 
232
NSS_IMPLEMENT PRStatus
 
233
nssToken_Refresh(NSSToken *token)
 
234
{
 
235
    PK11SlotInfo *nss3slot;
 
236
 
 
237
    if (!token) {
 
238
        return PR_SUCCESS;
 
239
    }
 
240
    nss3slot = token->pk11slot;
 
241
    token->defaultSession = nssSession_ImportNSS3Session(token->slot->base.arena,
 
242
                                                       nss3slot->session,
 
243
                                                       nss3slot->sessionLock,
 
244
                                                       nss3slot->defRWSession);
 
245
    return PR_SUCCESS;
 
246
}
 
247
 
 
248
NSS_IMPLEMENT PRStatus
 
249
nssSlot_Refresh
 
250
(
 
251
  NSSSlot *slot
 
252
)
 
253
{
 
254
    PK11SlotInfo *nss3slot = slot->pk11slot;
 
255
    PRBool doit = PR_FALSE;
 
256
    if (slot->token->base.name[0] == 0) {
 
257
        doit = PR_TRUE;
 
258
    }
 
259
    if (PK11_InitToken(nss3slot, PR_FALSE) != SECSuccess) {
 
260
        return PR_FAILURE;
 
261
    }
 
262
    if (doit) {
 
263
        nssTrustDomain_UpdateCachedTokenCerts(slot->token->trustDomain, 
 
264
                                              slot->token);
 
265
    }
 
266
    return nssToken_Refresh(slot->token);
 
267
}
 
268
 
 
269
NSS_IMPLEMENT PRStatus
 
270
nssToken_GetTrustOrder
 
271
(
 
272
  NSSToken *tok
 
273
)
 
274
{
 
275
    PK11SlotInfo *slot;
 
276
    SECMODModule *module;
 
277
    slot = tok->pk11slot;
 
278
    module = PK11_GetModule(slot);
 
279
    return module->trustOrder;
 
280
}
 
281
 
 
282
NSS_IMPLEMENT PRBool
 
283
nssSlot_IsLoggedIn
 
284
(
 
285
  NSSSlot *slot
 
286
)
 
287
{
 
288
    if (!slot->pk11slot->needLogin) {
 
289
        return PR_TRUE;
 
290
    }
 
291
    return PK11_IsLoggedIn(slot->pk11slot, NULL);
 
292
}
 
293
 
 
294
 
 
295
NSSTrustDomain *
 
296
nssToken_GetTrustDomain(NSSToken *token)
 
297
{
 
298
    return token->trustDomain;
 
299
}
 
300
 
 
301
NSS_EXTERN PRStatus
 
302
nssTrustDomain_RemoveTokenCertsFromCache
 
303
(
 
304
  NSSTrustDomain *td,
 
305
  NSSToken *token
 
306
);
 
307
 
 
308
NSS_IMPLEMENT PRStatus
 
309
nssToken_NotifyCertsNotVisible
 
310
(
 
311
  NSSToken *tok
 
312
)
 
313
{
 
314
    return nssTrustDomain_RemoveTokenCertsFromCache(tok->trustDomain, tok);
 
315
}
 
316