summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/ckfw/dbm/token.c
blob: 6873bf0ece3d2d179911ec3a427314b950d3333a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ckdbm.h"

static CK_RV
nss_dbm_mdToken_Setup(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
    CK_RV rv = CKR_OK;

    token->arena = NSSCKFWToken_GetArena(fwToken, &rv);
    token->session_db = nss_dbm_db_open(token->arena, fwInstance, (char *)NULL,
                                        O_RDWR | O_CREAT, &rv);
    if ((nss_dbm_db_t *)NULL == token->session_db) {
        return rv;
    }

    /* Add a label record if there isn't one? */

    return CKR_OK;
}

static void
nss_dbm_mdToken_Invalidate(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;

    if ((nss_dbm_db_t *)NULL != token->session_db) {
        nss_dbm_db_close(token->session_db);
        token->session_db = (nss_dbm_db_t *)NULL;
    }
}

static CK_RV
nss_dbm_mdToken_InitToken(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    NSSItem *pin,
    NSSUTF8 *label)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
    nss_dbm_instance_t *instance = (nss_dbm_instance_t *)mdInstance->etc;
    CK_RV rv;

    /* Wipe the session object data */

    if ((nss_dbm_db_t *)NULL != token->session_db) {
        nss_dbm_db_close(token->session_db);
    }

    token->session_db = nss_dbm_db_open(token->arena, fwInstance, (char *)NULL,
                                        O_RDWR | O_CREAT, &rv);
    if ((nss_dbm_db_t *)NULL == token->session_db) {
        return rv;
    }

    /* Wipe the token object data */

    if (token->slot->flags & O_RDWR) {
        if ((nss_dbm_db_t *)NULL != token->slot->token_db) {
            nss_dbm_db_close(token->slot->token_db);
        }

        token->slot->token_db = nss_dbm_db_open(instance->arena, fwInstance,
                                                token->slot->filename,
                                                token->slot->flags | O_CREAT | O_TRUNC,
                                                &rv);
        if ((nss_dbm_db_t *)NULL == token->slot->token_db) {
            return rv;
        }

        /* PIN is irrelevant */

        rv = nss_dbm_db_set_label(token->slot->token_db, label);
        if (CKR_OK != rv) {
            return rv;
        }
    }

    return CKR_OK;
}

static NSSUTF8 *
nss_dbm_mdToken_GetLabel(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_RV *pError)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;

    if ((NSSUTF8 *)NULL == token->label) {
        token->label = nss_dbm_db_get_label(token->slot->token_db, token->arena, pError);
    }

    /* If no label has been set, return *something* */
    if ((NSSUTF8 *)NULL == token->label) {
        return token->slot->filename;
    }

    return token->label;
}

static NSSUTF8 *
nss_dbm_mdToken_GetManufacturerID(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_RV *pError)
{
    return "mozilla.org NSS";
}

static NSSUTF8 *
nss_dbm_mdToken_GetModel(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_RV *pError)
{
    return "dbm";
}

/* GetSerialNumber is irrelevant */
/* GetHasRNG defaults to CK_FALSE */

static CK_BBOOL
nss_dbm_mdToken_GetIsWriteProtected(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;

    if (token->slot->flags & O_RDWR) {
        return CK_FALSE;
    } else {
        return CK_TRUE;
    }
}

/* GetLoginRequired defaults to CK_FALSE */
/* GetUserPinInitialized defaults to CK_FALSE */
/* GetRestoreKeyNotNeeded is irrelevant */
/* GetHasClockOnToken defaults to CK_FALSE */
/* GetHasProtectedAuthenticationPath defaults to CK_FALSE */
/* GetSupportsDualCryptoOperations is irrelevant */

static CK_ULONG
nss_dbm_mdToken_effectively_infinite(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    return CK_EFFECTIVELY_INFINITE;
}

static CK_VERSION
nss_dbm_mdToken_GetHardwareVersion(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
    return nss_dbm_db_get_format_version(token->slot->token_db);
}

/* GetFirmwareVersion is irrelevant */
/* GetUTCTime is irrelevant */

static NSSCKMDSession *
nss_dbm_mdToken_OpenSession(
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    NSSCKFWSession *fwSession,
    CK_BBOOL rw,
    CK_RV *pError)
{
    nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
    return nss_dbm_mdSession_factory(token, fwSession, fwInstance, rw, pError);
}

/* GetMechanismCount defaults to zero */
/* GetMechanismTypes is irrelevant */
/* GetMechanism is irrelevant */

NSS_IMPLEMENT NSSCKMDToken *
nss_dbm_mdToken_factory(
    nss_dbm_slot_t *slot,
    CK_RV *pError)
{
    nss_dbm_token_t *token;
    NSSCKMDToken *rv;

    token = nss_ZNEW(slot->instance->arena, nss_dbm_token_t);
    if ((nss_dbm_token_t *)NULL == token) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKMDToken *)NULL;
    }

    rv = nss_ZNEW(slot->instance->arena, NSSCKMDToken);
    if ((NSSCKMDToken *)NULL == rv) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKMDToken *)NULL;
    }

    token->slot = slot;

    rv->etc = (void *)token;
    rv->Setup = nss_dbm_mdToken_Setup;
    rv->Invalidate = nss_dbm_mdToken_Invalidate;
    rv->InitToken = nss_dbm_mdToken_InitToken;
    rv->GetLabel = nss_dbm_mdToken_GetLabel;
    rv->GetManufacturerID = nss_dbm_mdToken_GetManufacturerID;
    rv->GetModel = nss_dbm_mdToken_GetModel;
    /*  GetSerialNumber is irrelevant */
    /*  GetHasRNG defaults to CK_FALSE */
    rv->GetIsWriteProtected = nss_dbm_mdToken_GetIsWriteProtected;
    /*  GetLoginRequired defaults to CK_FALSE */
    /*  GetUserPinInitialized defaults to CK_FALSE */
    /*  GetRestoreKeyNotNeeded is irrelevant */
    /*  GetHasClockOnToken defaults to CK_FALSE */
    /*  GetHasProtectedAuthenticationPath defaults to CK_FALSE */
    /*  GetSupportsDualCryptoOperations is irrelevant */
    rv->GetMaxSessionCount = nss_dbm_mdToken_effectively_infinite;
    rv->GetMaxRwSessionCount = nss_dbm_mdToken_effectively_infinite;
    /*  GetMaxPinLen is irrelevant */
    /*  GetMinPinLen is irrelevant */
    /*  GetTotalPublicMemory defaults to CK_UNAVAILABLE_INFORMATION */
    /*  GetFreePublicMemory defaults to CK_UNAVAILABLE_INFORMATION */
    /*  GetTotalPrivateMemory defaults to CK_UNAVAILABLE_INFORMATION */
    /*  GetFreePrivateMemory defaults to CK_UNAVAILABLE_INFORMATION */
    rv->GetHardwareVersion = nss_dbm_mdToken_GetHardwareVersion;
    /*  GetFirmwareVersion is irrelevant */
    /*  GetUTCTime is irrelevant */
    rv->OpenSession = nss_dbm_mdToken_OpenSession;
    rv->null = NULL;

    return rv;
}