forked from gamringer/php-pkcs11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkcs11int.h
226 lines (193 loc) · 10.5 KB
/
pkcs11int.h
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Guillaume Amringer |
+----------------------------------------------------------------------+
*/
#ifndef PKCSINT_H
#define PKCSINT_H
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "ext/standard/info.h"
#include "php_pkcs11.h"
#include <stdbool.h>
#include <dlfcn.h>
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
typedef struct _pkcs11_object {
bool initialised;
void *pkcs11module;
CK_FUNCTION_LIST_PTR functionList;
zend_object std;
} pkcs11_object;
typedef struct _pkcs11_session_object {
pkcs11_object *pkcs11;
CK_SESSION_HANDLE session;
CK_SLOT_ID slotID;
zend_object std;
} pkcs11_session_object;
typedef struct _pkcs11_object_object {
pkcs11_session_object *session;
CK_OBJECT_HANDLE object;
zend_object std;
} pkcs11_object_object;
typedef struct _pkcs11_key_object {
pkcs11_session_object *session;
CK_OBJECT_HANDLE key;
zend_object std;
} pkcs11_key_object;
typedef struct _pkcs11_keypair_object {
pkcs11_key_object *pkey;
pkcs11_key_object *skey;
zend_object std;
} pkcs11_keypair_object;
typedef struct _pkcs11_mechanism_object {
CK_MECHANISM mechanism;
zend_object std;
} pkcs11_mechanism_object;
typedef struct _pkcs11_rsapssparams_object {
CK_RSA_PKCS_PSS_PARAMS params;
zend_object std;
} pkcs11_rsapssparams_object;
typedef struct _pkcs11_rsaoaepparams_object {
CK_RSA_PKCS_OAEP_PARAMS params;
zend_object std;
} pkcs11_rsaoaepparams_object;
typedef struct _pkcs11_gcmparams_object {
CK_GCM_PARAMS params;
zend_object std;
} pkcs11_gcmparams_object;
typedef struct _pkcs11_chacha20params_object {
CK_CHACHA20_PARAMS params;
zend_object std;
} pkcs11_chacha20params_object;
typedef struct _pkcs11_salsa20params_object {
CK_SALSA20_PARAMS params;
zend_object std;
} pkcs11_salsa20params_object;
typedef struct _pkcs11_salsa20chacha20poly1305params_object {
CK_SALSA20_CHACHA20_POLY1305_PARAMS params;
zend_object std;
} pkcs11_salsa20chacha20poly1305params_object;
typedef struct _pkcs11_ecdh1deriveparams_object {
CK_ECDH1_DERIVE_PARAMS params;
zend_object std;
} pkcs11_ecdh1deriveparams_object;
typedef struct _pkcs11_signaturecontext_object {
pkcs11_key_object *key;
zend_object std;
} pkcs11_signaturecontext_object;
typedef struct _pkcs11_verificationcontext_object {
pkcs11_key_object *key;
zend_object std;
} pkcs11_verificationcontext_object;
typedef struct _pkcs11_digestcontext_object {
pkcs11_session_object *session;
zend_object std;
} pkcs11_digestcontext_object;
typedef struct _pkcs11_encryptioncontext_object {
pkcs11_key_object *key;
zend_object std;
} pkcs11_encryptioncontext_object;
typedef struct _pkcs11_decryptioncontext_object {
pkcs11_key_object *key;
zend_object std;
} pkcs11_decryptioncontext_object;
#define Z_PKCS11_P(zv) pkcs11_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_SESSION_P(zv) pkcs11_session_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_OBJECT_P(zv) pkcs11_object_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_KEY_P(zv) pkcs11_key_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_KEYPAIR_P(zv) pkcs11_keypair_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_MECHANISM_P(zv) pkcs11_mechanism_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_RSAPSSPARAMS_P(zv) pkcs11_rsapssparams_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_RSAOAEPPARAMS_P(zv) pkcs11_rsaoaepparams_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_GCMPARAMS_P(zv) pkcs11_gcmparams_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_CHACHA20PARAMS_P(zv) pkcs11_chacha20params_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_SALSA20PARAMS_P(zv) pkcs11_salsa20params_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_SALSA20CHACHA20POLY1305PARAMS_P(zv) pkcs11_salsa20chacha20poly1305params_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_ECDH1DERIVEPARAMS_P(zv) pkcs11_ecdh1deriveparams_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_SIGNATURECONTEXT_P(zv) pkcs11_signaturecontext_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_VERIFICATIONCONTEXT_P(zv) pkcs11_verificationcontext_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_DIGESTCONTEXT_P(zv) pkcs11_digestcontext_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_ENCRYPTIONCONTEXT_P(zv) pkcs11_encryptioncontext_from_zend_object(Z_OBJ_P((zv)))
#define Z_PKCS11_DECRYPTIONCONTEXT_P(zv) pkcs11_decryptioncontext_from_zend_object(Z_OBJ_P((zv)))
#define DECLARE_MAGIC_FUNCS(tt, classname) \
static inline tt##_object *tt##_from_zend_object(zend_object *obj) { \
return (tt##_object *) ((char *) (obj) - XtOffsetOf(tt##_object, std)); \
} \
extern void register_##tt(); \
extern zend_class_entry *ce_Pkcs11_##classname;
DECLARE_MAGIC_FUNCS(pkcs11, Module)
DECLARE_MAGIC_FUNCS(pkcs11_session, Session)
DECLARE_MAGIC_FUNCS(pkcs11_object, P11Object)
DECLARE_MAGIC_FUNCS(pkcs11_key, Key)
DECLARE_MAGIC_FUNCS(pkcs11_keypair, KeyPair)
DECLARE_MAGIC_FUNCS(pkcs11_mechanism, Mechanism)
DECLARE_MAGIC_FUNCS(pkcs11_rsapssparams, RsaPssParams)
DECLARE_MAGIC_FUNCS(pkcs11_rsaoaepparams, RsaOaepParams)
DECLARE_MAGIC_FUNCS(pkcs11_gcmparams, GcmParams)
DECLARE_MAGIC_FUNCS(pkcs11_chacha20params, ChaCha20Params)
DECLARE_MAGIC_FUNCS(pkcs11_salsa20params, Salsa20Params)
DECLARE_MAGIC_FUNCS(pkcs11_salsa20chacha20poly1305params, Salsa20Chacha20Poly1305Params)
DECLARE_MAGIC_FUNCS(pkcs11_ecdh1deriveparams, Ecdh1DeriveParams)
DECLARE_MAGIC_FUNCS(pkcs11_signaturecontext, SignatureContext)
DECLARE_MAGIC_FUNCS(pkcs11_verificationcontext, VerificationContext)
DECLARE_MAGIC_FUNCS(pkcs11_digestcontext, DigestContext)
DECLARE_MAGIC_FUNCS(pkcs11_encryptioncontext, EncryptionContext)
DECLARE_MAGIC_FUNCS(pkcs11_decryptioncontext, DecryptionContext)
#define DEFINE_MAGIC_FUNCS(tt, lowername, classname) \
static zend_object *tt##_ctor(zend_class_entry *ce) { \
tt##_object *objval = zend_object_alloc(sizeof(tt##_object), ce); \
\
zend_object_std_init(&objval->std, ce); \
object_properties_init(&objval->std, ce); \
objval->std.handlers = &tt##_handlers; \
\
return &objval->std; \
} \
static void tt##_dtor(zend_object *zobj) { \
tt##_object *objval = tt##_from_zend_object(zobj); \
tt##_shutdown(objval); \
zend_object_std_dtor(&objval->std); \
} \
void register_##tt() { \
zend_class_entry ce; \
memcpy(&tt##_handlers, &std_object_handlers, sizeof(zend_object_handlers)); \
INIT_NS_CLASS_ENTRY(ce, "Pkcs11", #classname, lowername##_class_functions); \
ce.create_object = tt##_ctor; \
tt##_handlers.offset = XtOffsetOf(tt##_object, std); \
tt##_handlers.clone_obj = NULL; \
tt##_handlers.free_obj = tt##_dtor; \
ce_Pkcs11_##classname = zend_register_internal_class(&ce); \
ce_Pkcs11_##classname->serialize = zend_class_serialize_deny; \
ce_Pkcs11_##classname->unserialize = zend_class_unserialize_deny; \
}
extern void pkcs11_error(CK_RV rv, char *error);
extern void general_error(char *generic, char *specific);
extern void parseTemplate(HashTable **template, CK_ATTRIBUTE_PTR *templateObj, int *templateItemCount);
extern void freeTemplate(CK_ATTRIBUTE_PTR templateObj);
extern void getObjectClass(pkcs11_session_object *session, CK_OBJECT_HANDLE_PTR hObject, CK_ULONG_PTR classId);
extern int call_obj_func(zend_object *object, char *function_name, zval *retval_ptr, uint32_t param_count, zval params[]);
CK_RV php_C_GenerateRandom(const pkcs11_session_object * const objval, zend_long php_RandomLen, zval *retval);
CK_RV php_C_SeedRandom(const pkcs11_session_object * const objval, zend_string *php_pSeed);
extern CK_RV php_C_GetSessionInfo(const pkcs11_session_object * const objval, zval *retval);
#endif