summaryrefslogtreecommitdiff
path: root/ckcapi-builtin.c
blob: d31c0b2d1fa6e6d73cee42a58e4118ca27c08e57 (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

#include "ckcapi.h"
#include "pkcs11/pkcs11n.h"

/* --------------------------------------------------------------------------
 * BUILT IN VALUES 
 */

static const CK_BBOOL ck_true = CK_TRUE;
static const CK_BBOOL ck_false = CK_FALSE;

static const CK_OBJECT_CLASS cko_netscape_builtin_root_list = CKO_NETSCAPE_BUILTIN_ROOT_LIST;

static const char ck_root_label[] = "Windows Certificate Roots";

/* --------------------------------------------------------------------------
 * BUILT IN OBJECTS
 */

#define CK_END_LIST (CK_ULONG)-1

static const CK_ATTRIBUTE builtin_root[] = {
	{ CKA_TOKEN, (void*)&ck_true, sizeof(CK_BBOOL) },
	{ CKA_CLASS, (void*)&cko_netscape_builtin_root_list, sizeof(CK_OBJECT_CLASS) },
	{ CKA_PRIVATE, (void*)&ck_false, sizeof(CK_BBOOL) },
	{ CKA_MODIFIABLE, (void*)&ck_false, sizeof(CK_BBOOL) },
	{ CKA_LABEL, (void*)ck_root_label, sizeof(ck_root_label) },
	{ CK_END_LIST, NULL, 0 }
};

static const CK_ATTRIBUTE_PTR all_builtins[] = {
	(CK_ATTRIBUTE_PTR)&builtin_root,
	NULL,
};

/* This is filled in later */
static CK_ULONG num_builtins = 0;

/* --------------------------------------------------------------------------
 * IMPLEMENTATION
 */

/* Represents a loaded builtin object */
typedef struct _BuiltinObject
{
	CkCapiObject obj;

	/* 
	 * Together these form the unique key. Must be 
	 * laid out together in memory.
	 */
	unsigned int otype;
	CK_ULONG builtin_index;
}
BuiltinObject;

typedef struct _BuiltinObjectData
{
	CkCapiObjectData base;
	CK_ATTRIBUTE_PTR attr;
}
BuiltinObjectData;

static CK_RV
builtin_attribute(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attr)
{
	BuiltinObjectData* bdata = (BuiltinObjectData*)objdata;
	CK_ATTRIBUTE_PTR builtin = bdata->attr;

	ASSERT(attr);
	ASSERT(bdata);

	while(builtin->type != CK_END_LIST)
	{
		if(builtin->type == attr->type)
		{
			if(builtin->ulValueLen == 0)
				return CKR_ATTRIBUTE_TYPE_INVALID;

			if(attr->pValue)
			{
				attr->ulValueLen = builtin->ulValueLen;
				return CKR_OK;
			}

			if(builtin->ulValueLen > attr->ulValueLen)
			{
				attr->ulValueLen = builtin->ulValueLen;
				return CKR_BUFFER_TOO_SMALL;
			}

			attr->ulValueLen = builtin->ulValueLen;
			memcpy(attr->pValue, builtin->pValue, builtin->ulValueLen);
			return CKR_OK;
		}

		builtin++;
	}

	return CKR_ATTRIBUTE_TYPE_INVALID;
}

static void
builtin_data_release(void* data)
{
	BuiltinObjectData* bdata = (BuiltinObjectData*)data;
	ASSERT(bdata);
	free(bdata);
}

static const CkCapiObjectDataVtable builtin_objdata_vtable = {
	builtin_attribute,
	builtin_attribute,
	builtin_attribute,
	builtin_attribute,
	builtin_data_release,
};

static CK_RV 
builtin_load_data(CkCapiSession* sess, CkCapiObject* obj, CkCapiObjectData** objdata)
{
	BuiltinObject* bobj = (BuiltinObject*)obj;
	BuiltinObjectData* bdata;

	ASSERT(bobj);
	ASSERT(objdata);
	ASSERT(num_builtins > 0);

	if(bobj->builtin_index > num_builtins)
		return CKR_OBJECT_HANDLE_INVALID;

	bdata = (BuiltinObjectData*)calloc(sizeof(BuiltinObjectData), 1);
	if(!bdata)
		return CKR_HOST_MEMORY;

	bdata->attr = all_builtins[bobj->builtin_index];

	bdata->base.object = obj->id;
	bdata->base.data_funcs = &builtin_objdata_vtable;

	*objdata = &(bdata->base);
	return CKR_OK;
}

static void 
builtin_object_release(void* data)
{
	BuiltinObject* bobj = (BuiltinObject*)data;
	ASSERT(bobj);
	free(bobj);
}

static const CkCapiObjectVtable builtin_object_vtable = {
	builtin_load_data,
	builtin_object_release,
};

static CK_RV
register_builtin_object(CkCapiSession* sess, CK_ULONG index, CkCapiObject** obj)
{
	BuiltinObject* bobj;
	CK_RV ret;

	bobj = calloc(sizeof(BuiltinObject), 1);
	if(!bobj)
		return CKR_HOST_MEMORY;

	bobj->otype = OBJECT_BUILTIN;
	bobj->builtin_index = index;

	bobj->obj.id = 0;
	bobj->obj.obj_funcs = &builtin_object_vtable;
	bobj->obj.unique_key = UNIQUE_KEY_AT(bobj, otype);
	bobj->obj.unique_len = UNIQUE_KEY_LEN(bobj, otype, builtin_index);

	ret = ckcapi_object_register(sess, &(bobj->obj));
	if(ret != CKR_OK)
	{
		free(bobj);
		return ret;
	}

	ASSERT(bobj->obj.id != 0);
	*obj = &(bobj->obj);
	return CKR_OK;
}

CK_RV
ckcapi_builtin_find(CkCapiSession* sess, CK_OBJECT_CLASS cls, CK_ATTRIBUTE_PTR match, 
					CK_ULONG count, CkCapiArray* arr)
{
	CkCapiObject* obj;
	BuiltinObjectData bdata;
	CK_RV ret = CKR_OK;
	CK_ULONG i;

	/* First time around count total number */
	if(!num_builtins)
	{
		while(all_builtins[num_builtins])
			++num_builtins;
		ASSERT(num_builtins > 0);
	}

	/* Match each certificate */
	for(i = 0; i < num_builtins; ++i)
	{
		bdata.attr = all_builtins[i];
		bdata.base.object = 0;
		bdata.base.data_funcs = &builtin_objdata_vtable;

		if(ckcapi_object_data_match(&bdata.base, match, count))
		{
			ret = register_builtin_object(sess, i, &obj);
			if(ret != CKR_OK)
				break;

			ckcapi_array_append(arr, obj->id);
		}
	}

	return ret;
}