summaryrefslogtreecommitdiff
path: root/p11-kit/rpc-message.c
blob: 810d417cd683c053a8def2818112359415dc1187 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* p11-rpc-message.c - our marshalled PKCS#11 protocol.

   Copyright (C) 2008, Stef Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "private.h"
#include "rpc-message.h"

#include <assert.h>
#include <string.h>

void
_p11_rpc_message_init (RpcMessage *msg,
                       BufferAllocator allocator)
{
	assert (allocator != NULL);

	memset (msg, 0, sizeof (*msg));

	if (_p11_buffer_init_full (&msg->buffer, 64, allocator))
		_p11_rpc_message_reset (msg);
}

void
_p11_rpc_message_clear (RpcMessage *msg)
{
	void *allocated;
	void **data;

	assert (msg != NULL);
	assert (msg->buffer.allocator);

	_p11_buffer_uninit (&msg->buffer);

	/* Free up the extra allocated memory */
	allocated = msg->extra;
	while (allocated != NULL) {
		data = (void **)allocated;

		/* Pointer to the next allocation */
		allocated = *data;
		(msg->buffer.allocator) (data, 0);
	}
	msg->extra = NULL;
}

void *
_p11_rpc_message_alloc_extra (RpcMessage *msg,
                              size_t length)
{
	void **data;

	assert (msg != NULL);

	if (length > 0x7fffffff)
		return NULL;

	data = (msg->buffer.allocator) (NULL, sizeof (void *) + length);
	if (data == NULL)
		return NULL;

	/* Munch up the memory to help catch bugs */
	memset (data, 0xff, sizeof (void *) + length);

	/* Store pointer to next allocated block at beginning */
	*data = msg->extra;
	msg->extra = data;

	/* Data starts after first pointer */
	return (void *)(data + 1);
}

void
_p11_rpc_message_reset (RpcMessage *msg)
{
	assert (msg);

	msg->call_id = 0;
	msg->call_type = 0;
	msg->signature = NULL;
	msg->sigverify = NULL;
	msg->parsed = 0;

	_p11_buffer_reset (&msg->buffer);
}

int
_p11_rpc_message_prep (RpcMessage *msg,
                       int call_id,
                       RpcMessageType type)
{
	int len;

	assert (type);
	assert (call_id >= RPC_CALL_ERROR);
	assert (call_id < RPC_CALL_MAX);

	_p11_rpc_message_reset (msg);

	if (call_id != RPC_CALL_ERROR) {

		/* The call id and signature */
		if (type == RPC_REQUEST)
			msg->signature = rpc_calls[call_id].request;
		else if (type == RPC_RESPONSE)
			msg->signature = rpc_calls[call_id].response;
		else
			assert (0 && "invalid message type");
		assert (msg->signature);
		msg->sigverify = msg->signature;
	}

	msg->call_id = call_id;
	msg->call_type = type;

	/* Encode the two of them */
	_p11_buffer_add_uint32 (&msg->buffer, call_id);
	if (msg->signature) {
		len = strlen (msg->signature);
		_p11_buffer_add_byte_array (&msg->buffer, (unsigned char*)msg->signature, len);
	}

	msg->parsed = 0;
	return !_p11_buffer_has_error (&msg->buffer);
}

int
_p11_rpc_message_parse (RpcMessage *msg, RpcMessageType type)
{
	const unsigned char *val;
	size_t len;
	uint32_t call_id;

	msg->parsed = 0;

	/* Pull out the call identifier */
	if (!_p11_buffer_get_uint32 (&msg->buffer, msg->parsed, &(msg->parsed), &call_id)) {
		_p11_message ("invalid message: couldn't read call identifier");
		return 0;
	}

	msg->signature = msg->sigverify = NULL;

	/* If it's an error code then no more processing */
	if (call_id == RPC_CALL_ERROR) {
		if (type == RPC_REQUEST) {
			_p11_message ("invalid message: error code in request");
			return 0;
		}

		return 1;
	}

	/* The call id and signature */
	if (call_id <= 0 || call_id >= RPC_CALL_MAX) {
		_p11_message ("invalid message: bad call id: %d", call_id);
		return 0;
	}
	if (type == RPC_REQUEST)
		msg->signature = rpc_calls[call_id].request;
	else if (type == RPC_RESPONSE)
		msg->signature = rpc_calls[call_id].response;
	else
		assert (0 && "invalid message type");
	msg->call_id = call_id;
	msg->call_type = type;
	msg->sigverify = msg->signature;

	/* Verify the incoming signature */
	if (!_p11_buffer_get_byte_array (&msg->buffer, msg->parsed, &(msg->parsed), &val, &len)) {
		_p11_message ("invalid message: couldn't read signature");
		return 0;
	}

	if ((strlen (msg->signature) != len) || (memcmp (val, msg->signature, len) != 0)) {
		_p11_message ("invalid message: signature doesn't match");
		return 0;
	}

	return 1;
}

int
_p11_rpc_message_verify_part (RpcMessage *msg, const char* part)
{
	int len, ok;

	if (!msg->sigverify)
		return 1;

	len = strlen (part);
	ok = (strncmp (msg->sigverify, part, len) == 0);
	if (ok)
		msg->sigverify += len;
	return ok;
}

int
_p11_rpc_message_write_attribute_buffer (RpcMessage *msg, CK_ATTRIBUTE_PTR arr,
                                        CK_ULONG num)
{
	CK_ATTRIBUTE_PTR attr;
	CK_ULONG i;

	assert (!num || arr);
	assert (msg);

	/* Make sure this is in the rigth order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "fA"));

	/* Write the number of items */
	_p11_buffer_add_uint32 (&msg->buffer, num);

	for (i = 0; i < num; ++i) {
		attr = &(arr[i]);

		/* The attribute type */
		_p11_buffer_add_uint32 (&msg->buffer, attr->type);

		/* And the attribute buffer length */
		_p11_buffer_add_uint32 (&msg->buffer, attr->pValue ? attr->ulValueLen : 0);
	}

	return !_p11_buffer_has_error (&msg->buffer);
}

int
_p11_rpc_message_write_attribute_array (RpcMessage *msg,
                                       CK_ATTRIBUTE_PTR arr, CK_ULONG num)
{
	CK_ULONG i;
	CK_ATTRIBUTE_PTR attr;
	unsigned char validity;

	assert (!num || arr);
	assert (msg);

	/* Make sure this is in the rigth order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "aA"));

	/* Write the number of items */
	_p11_buffer_add_uint32 (&msg->buffer, num);

	for (i = 0; i < num; ++i) {
		attr = &(arr[i]);

		/* The attribute type */
		_p11_buffer_add_uint32 (&msg->buffer, attr->type);

		/* Write out the attribute validity */
		validity = (((CK_LONG)attr->ulValueLen) == -1) ? 0 : 1;
		_p11_buffer_add_byte (&msg->buffer, validity);

		/* The attribute length and value */
		if (validity) {
			_p11_buffer_add_uint32 (&msg->buffer, attr->ulValueLen);
			_p11_buffer_add_byte_array (&msg->buffer, attr->pValue, attr->ulValueLen);
		}
	}

	return !_p11_buffer_has_error (&msg->buffer);
}

int
_p11_rpc_message_read_byte (RpcMessage *msg, CK_BYTE *val)
{
	assert (msg);

	/* Make sure this is in the right order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "y"));
	return _p11_buffer_get_byte (&msg->buffer, msg->parsed, &msg->parsed, val);
}

int
_p11_rpc_message_write_byte (RpcMessage *msg, CK_BYTE val)
{
	assert (msg);

	/* Make sure this is in the right order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "y"));
	return _p11_buffer_add_byte (&msg->buffer, val);
}

int
_p11_rpc_message_read_ulong (RpcMessage *msg, CK_ULONG *val)
{
	uint64_t v;
	assert (msg);

	/* Make sure this is in the right order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "u"));

	if (!_p11_buffer_get_uint64 (&msg->buffer, msg->parsed, &msg->parsed, &v))
		return 0;
	if (val)
		*val = (CK_ULONG)v;
	return 1;
}

int
_p11_rpc_message_write_ulong (RpcMessage *msg, CK_ULONG val)
{
	assert (msg);

	/* Make sure this is in the rigth order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "u"));
	return _p11_buffer_add_uint64 (&msg->buffer, val);
}

int
_p11_rpc_message_write_byte_buffer (RpcMessage *msg, CK_ULONG count)
{
	assert (msg);

	/* Make sure this is in the right order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "fy"));
	return _p11_buffer_add_uint32 (&msg->buffer, count);
}

int
_p11_rpc_message_write_byte_array (RpcMessage *msg, CK_BYTE_PTR arr, CK_ULONG num)
{
	assert (msg);

	/* Make sure this is in the right order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "ay"));

	/* No array, no data, just length */
	if (!arr) {
		_p11_buffer_add_byte (&msg->buffer, 0);
		_p11_buffer_add_uint32 (&msg->buffer, num);
	} else {
		_p11_buffer_add_byte (&msg->buffer, 1);
		_p11_buffer_add_byte_array (&msg->buffer, arr, num);
	}

	return !_p11_buffer_has_error (&msg->buffer);
}

int
_p11_rpc_message_write_ulong_buffer (RpcMessage *msg, CK_ULONG count)
{
	assert (msg);

	/* Make sure this is in the right order */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "fu"));
	return _p11_buffer_add_uint32 (&msg->buffer, count);
}

int
_p11_rpc_message_write_ulong_array (RpcMessage *msg, CK_ULONG_PTR array, CK_ULONG n_array)
{
	CK_ULONG i;

	assert (msg);

	/* Check that we're supposed to have this at this point */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "au"));

	/* We send a byte which determines whether there's actual data present or not */
	_p11_buffer_add_byte (&msg->buffer, array ? 1 : 0);
	_p11_buffer_add_uint32 (&msg->buffer, n_array);

	/* Now send the data if valid */
	if (array) {
		for (i = 0; i < n_array; ++i)
			_p11_buffer_add_uint64 (&msg->buffer, array[i]);
	}

	return !_p11_buffer_has_error (&msg->buffer);
}

int
_p11_rpc_message_read_version (RpcMessage *msg, CK_VERSION* version)
{
	assert (msg);
	assert (version);

	/* Check that we're supposed to have this at this point */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "v"));

	return _p11_buffer_get_byte (&msg->buffer, msg->parsed, &msg->parsed, &version->major) &&
	       _p11_buffer_get_byte (&msg->buffer, msg->parsed, &msg->parsed, &version->minor);
}

int
_p11_rpc_message_write_version (RpcMessage *msg, CK_VERSION* version)
{
	assert (msg);
	assert (version);

	/* Check that we're supposed to have this at this point */
	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "v"));

	_p11_buffer_add_byte (&msg->buffer, version->major);
	_p11_buffer_add_byte (&msg->buffer, version->minor);

	return !_p11_buffer_has_error (&msg->buffer);
}

int
_p11_rpc_message_read_space_string (RpcMessage *msg, CK_UTF8CHAR* buffer, CK_ULONG length)
{
	const unsigned char *data;
	size_t n_data;

	assert (msg);
	assert (buffer);
	assert (length);

	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "s"));

	if (!_p11_buffer_get_byte_array (&msg->buffer, msg->parsed, &msg->parsed, &data, &n_data))
		return 0;

	if (n_data != length) {
		_p11_message ("invalid length space padded string received: %d != %d", length, n_data);
		return 0;
	}

	memcpy (buffer, data, length);
	return 1;
}

int
_p11_rpc_message_write_space_string (RpcMessage *msg,
                                    CK_UTF8CHAR *buffer,
                                    CK_ULONG length)
{
	assert (msg);
	assert (buffer);
	assert (length);

	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "s"));

	return _p11_buffer_add_byte_array (&msg->buffer, buffer, length);
}

int
_p11_rpc_message_write_zero_string (RpcMessage *msg,
                                   CK_UTF8CHAR *string)
{
	assert (msg);
	assert (string);

	assert (!msg->signature || _p11_rpc_message_verify_part (msg, "z"));

	return _p11_buffer_add_string (&msg->buffer, (const char*)string);
}