Coverage Report

Created: 2026-11-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/pk11wrap/dev3hack.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef PKIT_H
6
#include "pkit.h"
7
#endif /* PKIT_H */
8
9
#ifndef DEVM_H
10
#include "devm.h"
11
#endif /* DEVM_H */
12
13
#include "pki3hack.h"
14
#include "dev3hack.h"
15
#include "pkim.h"
16
17
#ifndef BASE_H
18
#include "base.h"
19
#endif /* BASE_H */
20
21
#include "pk11func.h"
22
#include "secmodti.h"
23
#include "secerr.h"
24
25
NSS_IMPLEMENT nssSession *
26
nssSession_ImportNSS3Session(NSSArena *arenaOpt,
27
                             CK_SESSION_HANDLE session,
28
                             PRLock *lock, PRBool rw)
29
37
{
30
37
    nssSession *rvSession = NULL;
31
37
    if (session != CK_INVALID_HANDLE) {
32
37
        rvSession = nss_ZNEW(arenaOpt, nssSession);
33
37
        if (rvSession) {
34
37
            rvSession->handle = session;
35
37
            rvSession->lock = lock;
36
37
            rvSession->ownLock = PR_FALSE;
37
37
            rvSession->isRW = rw;
38
37
        }
39
37
    }
40
37
    return rvSession;
41
37
}
42
43
NSS_IMPLEMENT nssSession *
44
nssSlot_CreateSession(
45
    NSSSlot *slot,
46
    NSSArena *arenaOpt,
47
    PRBool readWrite)
48
748
{
49
748
    nssSession *rvSession;
50
51
748
    if (!readWrite) {
52
        /* nss3hack version only returns rw swssions */
53
0
        return NULL;
54
0
    }
55
748
    rvSession = nss_ZNEW(arenaOpt, nssSession);
56
748
    if (!rvSession) {
57
0
        return (nssSession *)NULL;
58
0
    }
59
60
748
    rvSession->handle = PK11_GetRWSession(slot->pk11slot);
61
748
    if (rvSession->handle == CK_INVALID_HANDLE) {
62
0
        nss_ZFreeIf(rvSession);
63
0
        return NULL;
64
0
    }
65
748
    rvSession->isRW = PR_TRUE;
66
748
    rvSession->slot = slot;
67
    /*
68
     * The session doesn't need its own lock.  Here's why.
69
     * 1. If we are reusing the default RW session of the slot,
70
     *    the slot lock is already locked to protect the session.
71
     * 2. If the module is not thread safe, the slot (or rather
72
     *    module) lock is already locked.
73
     * 3. If the module is thread safe and we are using a new
74
     *    session, no higher-level lock has been locked and we
75
     *    would need a lock for the new session.  However, the
76
     *    current usage of the session is that it is always
77
     *    used and destroyed within the same function and never
78
     *    shared with another thread.
79
     * So the session is either already protected by another
80
     * lock or only used by one thread.
81
     */
82
748
    rvSession->lock = NULL;
83
748
    rvSession->ownLock = PR_FALSE;
84
748
    return rvSession;
85
748
}
86
87
NSS_IMPLEMENT PRStatus
88
nssSession_Destroy(nssSession *s)
89
748
{
90
748
    PRStatus rv = PR_SUCCESS;
91
748
    if (s) {
92
748
        if (s->isRW) {
93
748
            PK11_RestoreROSession(s->slot->pk11slot, s->handle);
94
748
        }
95
748
        rv = nss_ZFreeIf(s);
96
748
    }
97
748
    return rv;
98
748
}
99
100
static NSSSlot *
101
nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
102
36
{
103
36
    NSSSlot *rvSlot;
104
36
    NSSArena *arena;
105
36
    arena = nssArena_Create();
106
36
    if (!arena) {
107
0
        return NULL;
108
0
    }
109
36
    rvSlot = nss_ZNEW(arena, NSSSlot);
110
36
    if (!rvSlot) {
111
0
        nssArena_Destroy(arena);
112
0
        return NULL;
113
0
    }
114
36
    rvSlot->base.refCount = 1;
115
36
    rvSlot->base.lock = PR_NewLock();
116
36
    rvSlot->base.arena = arena;
117
36
    rvSlot->pk11slot = PK11_ReferenceSlot(nss3slot);
118
36
    rvSlot->epv = nss3slot->functionList;
119
36
    rvSlot->slotID = nss3slot->slotID;
120
    /* Grab the slot name from the PKCS#11 fixed-length buffer */
121
36
    rvSlot->base.name = nssUTF8_Duplicate(nss3slot->slot_name, td->arena);
122
36
    rvSlot->lock = (nss3slot->isThreadSafe) ? NULL : nss3slot->sessionLock;
123
36
    rvSlot->isPresentLock = PR_NewLock();
124
36
    rvSlot->isPresentCondition = PR_NewCondVar(rvSlot->isPresentLock);
125
36
    rvSlot->isPresentThread = NULL;
126
36
    rvSlot->lastTokenPingState = nssSlotLastPingState_Reset;
127
36
    return rvSlot;
128
36
}
129
130
NSSToken *
131
nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
132
36
{
133
36
    NSSToken *rvToken;
134
36
    NSSArena *arena;
135
136
    /* Don't create a token object for a disabled slot */
137
36
    if (nss3slot->disabled) {
138
0
        PORT_SetError(SEC_ERROR_NO_TOKEN);
139
0
        return NULL;
140
0
    }
141
36
    arena = nssArena_Create();
142
36
    if (!arena) {
143
0
        return NULL;
144
0
    }
145
36
    rvToken = nss_ZNEW(arena, NSSToken);
146
36
    if (!rvToken) {
147
0
        nssArena_Destroy(arena);
148
0
        return NULL;
149
0
    }
150
36
    rvToken->base.refCount = 1;
151
36
    rvToken->base.lock = PR_NewLock();
152
36
    if (!rvToken->base.lock) {
153
0
        nssArena_Destroy(arena);
154
0
        return NULL;
155
0
    }
156
36
    rvToken->base.arena = arena;
157
36
    rvToken->pk11slot = PK11_ReferenceSlot(nss3slot);
158
36
    rvToken->epv = nss3slot->functionList;
159
36
    rvToken->defaultSession = nssSession_ImportNSS3Session(td->arena,
160
36
                                                           nss3slot->session,
161
36
                                                           nss3slot->sessionLock,
162
36
                                                           nss3slot->defRWSession);
163
#if 0 /* we should do this instead of blindly continuing. */
164
    if (!rvToken->defaultSession) {
165
    PORT_SetError(SEC_ERROR_NO_TOKEN);
166
        goto loser;
167
    }
168
#endif
169
36
    if (!PK11_IsInternal(nss3slot) && PK11_IsHW(nss3slot)) {
170
0
        rvToken->cache = nssTokenObjectCache_Create(rvToken,
171
0
                                                    PR_TRUE, PR_TRUE, PR_TRUE);
172
0
        if (!rvToken->cache)
173
0
            goto loser;
174
0
    }
175
36
    rvToken->trustDomain = td;
176
    /* Grab the token name from the PKCS#11 fixed-length buffer */
177
36
    rvToken->base.name = nssUTF8_Duplicate(nss3slot->token_name, td->arena);
178
36
    rvToken->slot = nssSlot_CreateFromPK11SlotInfo(td, nss3slot);
179
36
    if (!rvToken->slot) {
180
0
        goto loser;
181
0
    }
182
36
    if (rvToken->defaultSession)
183
36
        rvToken->defaultSession->slot = rvToken->slot;
184
36
    return rvToken;
185
0
loser:
186
0
    PR_DestroyLock(rvToken->base.lock);
187
0
    nssArena_Destroy(arena);
188
0
    return NULL;
189
36
}
190
191
NSS_IMPLEMENT void
192
nssToken_UpdateName(NSSToken *token)
193
37
{
194
37
    if (!token) {
195
36
        return;
196
36
    }
197
1
    token->base.name = nssUTF8_Duplicate(token->pk11slot->token_name, token->base.arena);
198
1
}
199
200
NSS_IMPLEMENT PRBool
201
nssSlot_IsPermanent(NSSSlot *slot)
202
867k
{
203
867k
    return slot->pk11slot->isPerm;
204
867k
}
205
206
NSS_IMPLEMENT PRBool
207
nssSlot_IsFriendly(NSSSlot *slot)
208
0
{
209
0
    return PK11_IsFriendly(slot->pk11slot);
210
0
}
211
212
NSS_IMPLEMENT PRStatus
213
nssToken_Refresh(NSSToken *token)
214
37
{
215
37
    PK11SlotInfo *nss3slot;
216
217
37
    if (!token) {
218
36
        return PR_SUCCESS;
219
36
    }
220
1
    nss3slot = token->pk11slot;
221
1
    token->defaultSession =
222
1
        nssSession_ImportNSS3Session(token->slot->base.arena,
223
1
                                     nss3slot->session,
224
1
                                     nss3slot->sessionLock,
225
1
                                     nss3slot->defRWSession);
226
1
    return token->defaultSession ? PR_SUCCESS : PR_FAILURE;
227
37
}
228
229
NSS_IMPLEMENT PRStatus
230
nssToken_GetTrustOrder(NSSToken *tok)
231
0
{
232
0
    PK11SlotInfo *slot;
233
0
    SECMODModule *module;
234
0
    slot = tok->pk11slot;
235
0
    module = PK11_GetModule(slot);
236
0
    return module->trustOrder;
237
0
}
238
239
NSS_IMPLEMENT PRBool
240
nssSlot_IsLoggedIn(NSSSlot *slot)
241
3.03k
{
242
3.03k
    if (!slot->pk11slot->needLogin) {
243
3.03k
        return PR_TRUE;
244
3.03k
    }
245
0
    return PK11_IsLoggedIn(slot->pk11slot, NULL);
246
3.03k
}
247
248
NSS_EXTERN PRStatus
249
nssTrustDomain_RemoveTokenCertsFromCache(
250
    NSSTrustDomain *td,
251
    NSSToken *token);
252
253
NSS_IMPLEMENT PRStatus
254
nssToken_NotifyCertsNotVisible(
255
    NSSToken *tok)
256
0
{
257
0
    return nssTrustDomain_RemoveTokenCertsFromCache(tok->trustDomain, tok);
258
0
}