summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/ckfw/builtins/bobject.c
blob: 1c0babdd66cd80fceec24a4e6b80808d86db2bfb (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
/* 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 "builtins.h"

/*
 * builtins/object.c
 *
 * This file implements the NSSCKMDObject object for the
 * "builtin objects" cryptoki module.
 */

/*
 * Finalize - unneeded
 * Destroy - CKR_SESSION_READ_ONLY
 * IsTokenObject - CK_TRUE
 * GetAttributeCount
 * GetAttributeTypes
 * GetAttributeSize
 * GetAttribute
 * SetAttribute - unneeded
 * GetObjectSize
 */

static CK_RV
builtins_mdObject_Destroy(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    return CKR_SESSION_READ_ONLY;
}

static CK_BBOOL
builtins_mdObject_IsTokenObject(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    return CK_TRUE;
}

static CK_ULONG
builtins_mdObject_GetAttributeCount(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_RV *pError)
{
    builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
    return io->n;
}

static CK_RV
builtins_mdObject_GetAttributeTypes(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_ATTRIBUTE_TYPE_PTR typeArray,
    CK_ULONG ulCount)
{
    builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
    CK_ULONG i;

    if (io->n != ulCount) {
        return CKR_BUFFER_TOO_SMALL;
    }

    for (i = 0; i < io->n; i++) {
        typeArray[i] = io->types[i];
    }

    return CKR_OK;
}

static CK_ULONG
builtins_mdObject_GetAttributeSize(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_ATTRIBUTE_TYPE attribute,
    CK_RV *pError)
{
    builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
    CK_ULONG i;

    for (i = 0; i < io->n; i++) {
        if (attribute == io->types[i]) {
            return (CK_ULONG)(io->items[i].size);
        }
    }

    *pError = CKR_ATTRIBUTE_TYPE_INVALID;
    return 0;
}

static NSSCKFWItem
builtins_mdObject_GetAttribute(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_ATTRIBUTE_TYPE attribute,
    CK_RV *pError)
{
    NSSCKFWItem mdItem;
    builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
    CK_ULONG i;

    mdItem.needsFreeing = PR_FALSE;
    mdItem.item = (NSSItem *)NULL;

    for (i = 0; i < io->n; i++) {
        if (attribute == io->types[i]) {
            mdItem.item = (NSSItem *)&io->items[i];
            return mdItem;
        }
    }

    *pError = CKR_ATTRIBUTE_TYPE_INVALID;
    return mdItem;
}

static CK_ULONG
builtins_mdObject_GetObjectSize(
    NSSCKMDObject *mdObject,
    NSSCKFWObject *fwObject,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    CK_RV *pError)
{
    builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
    CK_ULONG i;
    CK_ULONG rv = sizeof(CK_ULONG);

    for (i = 0; i < io->n; i++) {
        rv += sizeof(CK_ATTRIBUTE_TYPE) + sizeof(NSSItem) + io->items[i].size;
    }

    return rv;
}

static const NSSCKMDObject
    builtins_prototype_mdObject = {
        (void *)NULL, /* etc */
        NULL,         /* Finalize */
        builtins_mdObject_Destroy,
        builtins_mdObject_IsTokenObject,
        builtins_mdObject_GetAttributeCount,
        builtins_mdObject_GetAttributeTypes,
        builtins_mdObject_GetAttributeSize,
        builtins_mdObject_GetAttribute,
        NULL, /* FreeAttribute */
        NULL, /* SetAttribute */
        builtins_mdObject_GetObjectSize,
        (void *)NULL /* null terminator */
    };

NSS_IMPLEMENT NSSCKMDObject *
nss_builtins_CreateMDObject(
    NSSArena *arena,
    builtinsInternalObject *io,
    CK_RV *pError)
{
    if ((void *)NULL == io->mdObject.etc) {
        (void)nsslibc_memcpy(&io->mdObject, &builtins_prototype_mdObject,
                             sizeof(builtins_prototype_mdObject));
        io->mdObject.etc = (void *)io;
    }

    return &io->mdObject;
}