summaryrefslogtreecommitdiff
path: root/ckcapi-object.h
blob: 98451f615635a8e530b6224b818fc0c45c76e78b (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
/* 
 * Copyright (C) 2007 Stef Walter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef CKCAPI_OBJECT_H
#define CKCAPI_OBJECT_H

#include "ckcapi.h"

/* Debug print something about an object */
#define DBGO(obj, msg) \
	ckcapi_debug("O%d: %s", (obj) ? (obj)->id : 0, (msg))

/* A function to load data for an object */
typedef CK_RV (*CkCapiLoadData)(CkCapiSession* sess, struct _CkCapiObject* obj, 
								CkCapiObjectData** objdata);

/* A function to free some data */
typedef void (*CkCapiRelease)(void* data);

/* Object functions */
typedef struct _CkCapiObjectVtable
{
	CkCapiLoadData load_data;
	CkCapiRelease release;
}
CkCapiObjectVtable;

/* Represents a object we've seen */
struct _CkCapiObject
{
	CK_OBJECT_HANDLE id;
	CK_SLOT_ID slot;
	CK_SESSION_HANDLE session;
	const CkCapiObjectVtable* obj_funcs;
	void* unique_key;
	size_t unique_len;
};

/* A function to get an attribute from ObjectData */
typedef CK_RV (*CkCapiGetAttribute)(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attr);

/* Object data functions */
typedef struct _CkCapiObjectDataVtable
{
	CkCapiGetAttribute get_bool;
	CkCapiGetAttribute get_ulong;
	CkCapiGetAttribute get_bytes;
	CkCapiGetAttribute get_date;
	CkCapiRelease release;
}
CkCapiObjectDataVtable;

/* 
 * Base class for object data. Different types of 
 * objects extend this with more detailed data 
 */
struct _CkCapiObjectData
{
	CK_OBJECT_HANDLE object;
	const CkCapiObjectDataVtable* data_funcs;
};

/* Match object data against all the given match attributes */
CK_BBOOL			ckcapi_object_data_match		(CkCapiObjectData* objdata, 
													 CK_ATTRIBUTE_PTR matches, CK_ULONG count);

/* Match a single attribute against object data */
CK_BBOOL			ckcapi_object_data_match_attr	(CkCapiObjectData* objdata, 
													 CK_ATTRIBUTE_PTR match);

/* Get a bunch of attributes from object data */
CK_RV				ckcapi_object_data_get_attrs	(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attrs, 
													 CK_ULONG count);

/* Debug print something about an object data */
#define DBGOD(objdata, msg) \
	ckcapi_debug("O%d: %s", (objdata) ? (objdata)->obj : 0, (msg))

/* 
 * Each object has a unique key which guarantees that we're 
 * not loading the same objects over and over again. 
 * Usually these are contiguous members of a struct. These 
 * macros help calculate the address and length of such a 
 * unique key 
 */

/* The unique key starts at the address of the starting struct member */
#define UNIQUE_KEY_AT(obj, mem)	\
	(void*)(&((obj->mem)))

/* Calculates key length between first and last struct members */
#define UNIQUE_KEY_LEN(obj, first, last) \
	UNIQUE_KEY_VAR_LEN(obj, first, last, sizeof(obj->last))

/* Calcs key len between first and a certain num of bytes past last struct member */
#define UNIQUE_KEY_VAR_LEN(obj, first, last, len) \
	((((char*)&((obj->last))) - ((char*)&((obj->first)))) + (len))

/* Used internally to have a unique id for different object types */
enum
{
	OBJECT_CERT =	 1,
	OBJECT_BUILTIN = 2,
	OBJECT_TRUST =	 3
};

#endif /* CKCAPI_OBJECT_H */