| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* p11-rpc-private.h - various ids and signatures for our protocol |
| 3 |
|
| 4 |
Copyright (C) 2008, Stef Walter |
| 5 |
|
| 6 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef GKM_RPC_CALLS_H |
| 24 |
#define GKM_RPC_CALLS_H |
| 25 |
|
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdarg.h> |
| 28 |
|
| 29 |
#include "egg/egg-buffer.h" |
| 30 |
|
| 31 |
#include "pkcs11/pkcs11.h" |
| 32 |
|
| 33 |
|
| 34 |
/* Whether to print debug output or not */ |
| 35 |
#define DEBUG_OUTPUT 0 |
| 36 |
|
| 37 |
|
| 38 |
/* The calls, must be in sync with array below */ |
| 39 |
enum { |
| 40 |
GKM_RPC_CALL_ERROR = 0, |
| 41 |
|
| 42 |
GKM_RPC_CALL_C_Initialize, |
| 43 |
GKM_RPC_CALL_C_Finalize, |
| 44 |
GKM_RPC_CALL_C_GetInfo, |
| 45 |
GKM_RPC_CALL_C_GetSlotList, |
| 46 |
GKM_RPC_CALL_C_GetSlotInfo, |
| 47 |
GKM_RPC_CALL_C_GetTokenInfo, |
| 48 |
GKM_RPC_CALL_C_GetMechanismList, |
| 49 |
GKM_RPC_CALL_C_GetMechanismInfo, |
| 50 |
GKM_RPC_CALL_C_InitToken, |
| 51 |
GKM_RPC_CALL_C_WaitForSlotEvent, |
| 52 |
|
| 53 |
GKM_RPC_CALL_C_OpenSession, |
| 54 |
|
| 55 |
GKM_RPC_CALL_C_CloseSession, |
| 56 |
GKM_RPC_CALL_C_CloseAllSessions, |
| 57 |
GKM_RPC_CALL_C_GetFunctionStatus, |
| 58 |
GKM_RPC_CALL_C_CancelFunction, |
| 59 |
|
| 60 |
GKM_RPC_CALL_C_GetSessionInfo, |
| 61 |
GKM_RPC_CALL_C_InitPIN, |
| 62 |
GKM_RPC_CALL_C_SetPIN, |
| 63 |
GKM_RPC_CALL_C_GetOperationState, |
| 64 |
GKM_RPC_CALL_C_SetOperationState, |
| 65 |
GKM_RPC_CALL_C_Login, |
| 66 |
GKM_RPC_CALL_C_Logout, |
| 67 |
GKM_RPC_CALL_C_CreateObject, |
| 68 |
GKM_RPC_CALL_C_CopyObject, |
| 69 |
GKM_RPC_CALL_C_DestroyObject, |
| 70 |
GKM_RPC_CALL_C_GetObjectSize, |
| 71 |
GKM_RPC_CALL_C_GetAttributeValue, |
| 72 |
GKM_RPC_CALL_C_SetAttributeValue, |
| 73 |
GKM_RPC_CALL_C_FindObjectsInit, |
| 74 |
GKM_RPC_CALL_C_FindObjects, |
| 75 |
GKM_RPC_CALL_C_FindObjectsFinal, |
| 76 |
GKM_RPC_CALL_C_EncryptInit, |
| 77 |
GKM_RPC_CALL_C_Encrypt, |
| 78 |
GKM_RPC_CALL_C_EncryptUpdate, |
| 79 |
GKM_RPC_CALL_C_EncryptFinal, |
| 80 |
GKM_RPC_CALL_C_DecryptInit, |
| 81 |
GKM_RPC_CALL_C_Decrypt, |
| 82 |
GKM_RPC_CALL_C_DecryptUpdate, |
| 83 |
GKM_RPC_CALL_C_DecryptFinal, |
| 84 |
GKM_RPC_CALL_C_DigestInit, |
| 85 |
GKM_RPC_CALL_C_Digest, |
| 86 |
GKM_RPC_CALL_C_DigestUpdate, |
| 87 |
GKM_RPC_CALL_C_DigestKey, |
| 88 |
GKM_RPC_CALL_C_DigestFinal, |
| 89 |
GKM_RPC_CALL_C_SignInit, |
| 90 |
GKM_RPC_CALL_C_Sign, |
| 91 |
GKM_RPC_CALL_C_SignUpdate, |
| 92 |
GKM_RPC_CALL_C_SignFinal, |
| 93 |
GKM_RPC_CALL_C_SignRecoverInit, |
| 94 |
GKM_RPC_CALL_C_SignRecover, |
| 95 |
GKM_RPC_CALL_C_VerifyInit, |
| 96 |
GKM_RPC_CALL_C_Verify, |
| 97 |
GKM_RPC_CALL_C_VerifyUpdate, |
| 98 |
GKM_RPC_CALL_C_VerifyFinal, |
| 99 |
GKM_RPC_CALL_C_VerifyRecoverInit, |
| 100 |
GKM_RPC_CALL_C_VerifyRecover, |
| 101 |
GKM_RPC_CALL_C_DigestEncryptUpdate, |
| 102 |
GKM_RPC_CALL_C_DecryptDigestUpdate, |
| 103 |
GKM_RPC_CALL_C_SignEncryptUpdate, |
| 104 |
GKM_RPC_CALL_C_DecryptVerifyUpdate, |
| 105 |
GKM_RPC_CALL_C_GenerateKey, |
| 106 |
GKM_RPC_CALL_C_GenerateKeyPair, |
| 107 |
GKM_RPC_CALL_C_WrapKey, |
| 108 |
GKM_RPC_CALL_C_UnwrapKey, |
| 109 |
GKM_RPC_CALL_C_DeriveKey, |
| 110 |
GKM_RPC_CALL_C_SeedRandom, |
| 111 |
GKM_RPC_CALL_C_GenerateRandom, |
| 112 |
|
| 113 |
GKM_RPC_CALL_MAX |
| 114 |
}; |
| 115 |
|
| 116 |
typedef struct _GkmRpcCall { |
| 117 |
int call_id; |
| 118 |
const char* name; |
| 119 |
const char* request; |
| 120 |
const char* response; |
| 121 |
} GkmRpcCall; |
| 122 |
|
| 123 |
/* |
| 124 |
* a_ = prefix denotes array of _ |
| 125 |
* A = CK_ATTRIBUTE |
| 126 |
* f_ = prefix denotes buffer for _ |
| 127 |
* M = CK_MECHANISM |
| 128 |
* u = CK_ULONG |
| 129 |
* s = space padded string |
| 130 |
* v = CK_VERSION |
| 131 |
* y = CK_BYTE |
| 132 |
* z = null terminated string |
| 133 |
*/ |
| 134 |
|
| 135 |
static const GkmRpcCall gkm_rpc_calls[] = { |
| 136 |
{ GKM_RPC_CALL_ERROR, "ERROR", NULL, NULL }, |
| 137 |
{ GKM_RPC_CALL_C_Initialize, "C_Initialize", "ay", "" }, |
| 138 |
{ GKM_RPC_CALL_C_Finalize, "C_Finalize", "", "" }, |
| 139 |
{ GKM_RPC_CALL_C_GetInfo, "C_GetInfo", "", "vsusv" }, |
| 140 |
{ GKM_RPC_CALL_C_GetSlotList, "C_GetSlotList", "yfu", "au" }, |
| 141 |
{ GKM_RPC_CALL_C_GetSlotInfo, "C_GetSlotInfo", "u", "ssuvv" }, |
| 142 |
{ GKM_RPC_CALL_C_GetTokenInfo, "C_GetTokenInfo", "u", "ssssuuuuuuuuuuuvvs" }, |
| 143 |
{ GKM_RPC_CALL_C_GetMechanismList, "C_GetMechanismList", "ufu", "au" }, |
| 144 |
{ GKM_RPC_CALL_C_GetMechanismInfo, "C_GetMechanismInfo", "uu", "uuu" }, |
| 145 |
{ GKM_RPC_CALL_C_InitToken, "C_InitToken", "uayz", "" }, |
| 146 |
{ GKM_RPC_CALL_C_WaitForSlotEvent, "C_WaitForSlotEvent", "u", "u" }, |
| 147 |
{ GKM_RPC_CALL_C_OpenSession, "C_OpenSession", "uu", "u" }, |
| 148 |
{ GKM_RPC_CALL_C_CloseSession, "C_CloseSession", "u", "" }, |
| 149 |
{ GKM_RPC_CALL_C_CloseAllSessions, "C_CloseAllSessions", "u", "" }, |
| 150 |
{ GKM_RPC_CALL_C_GetFunctionStatus, "C_GetFunctionStatus", "u", "" }, |
| 151 |
{ GKM_RPC_CALL_C_CancelFunction, "C_CancelFunction", "u", "" }, |
| 152 |
{ GKM_RPC_CALL_C_GetSessionInfo, "C_GetSessionInfo", "u", "uuuu" }, |
| 153 |
{ GKM_RPC_CALL_C_InitPIN, "C_InitPIN", "uay", "" }, |
| 154 |
{ GKM_RPC_CALL_C_SetPIN, "C_SetPIN", "uayay", "" }, |
| 155 |
{ GKM_RPC_CALL_C_GetOperationState, "C_GetOperationState", "ufy", "ay" }, |
| 156 |
{ GKM_RPC_CALL_C_SetOperationState, "C_SetOperationState", "uayuu", "" }, |
| 157 |
{ GKM_RPC_CALL_C_Login, "C_Login", "uuay", "" }, |
| 158 |
{ GKM_RPC_CALL_C_Logout, "C_Logout", "u", "" }, |
| 159 |
{ GKM_RPC_CALL_C_CreateObject, "C_CreateObject", "uaA", "u" }, |
| 160 |
{ GKM_RPC_CALL_C_CopyObject, "C_CopyObject", "uuaA", "u" }, |
| 161 |
{ GKM_RPC_CALL_C_DestroyObject, "C_DestroyObject", "uu", "" }, |
| 162 |
{ GKM_RPC_CALL_C_GetObjectSize, "C_GetObjectSize", "uu", "u" }, |
| 163 |
{ GKM_RPC_CALL_C_GetAttributeValue, "C_GetAttributeValue", "uufA", "aAu" }, |
| 164 |
{ GKM_RPC_CALL_C_SetAttributeValue, "C_SetAttributeValue", "uuaA", "" }, |
| 165 |
{ GKM_RPC_CALL_C_FindObjectsInit, "C_FindObjectsInit", "uaA", "" }, |
| 166 |
{ GKM_RPC_CALL_C_FindObjects, "C_FindObjects", "ufu", "au" }, |
| 167 |
{ GKM_RPC_CALL_C_FindObjectsFinal, "C_FindObjectsFinal", "u", "" }, |
| 168 |
{ GKM_RPC_CALL_C_EncryptInit, "C_EncryptInit", "uMu", "" }, |
| 169 |
{ GKM_RPC_CALL_C_Encrypt, "C_Encrypt", "uayfy", "ay" }, |
| 170 |
{ GKM_RPC_CALL_C_EncryptUpdate, "C_EncryptUpdate", "uayfy", "ay" }, |
| 171 |
{ GKM_RPC_CALL_C_EncryptFinal, "C_EncryptFinal", "ufy", "ay" }, |
| 172 |
{ GKM_RPC_CALL_C_DecryptInit, "C_DecryptInit", "uMu", "" }, |
| 173 |
{ GKM_RPC_CALL_C_Decrypt, "C_Decrypt", "uayfy", "ay" }, |
| 174 |
{ GKM_RPC_CALL_C_DecryptUpdate, "C_DecryptUpdate", "uayfy", "ay" }, |
| 175 |
{ GKM_RPC_CALL_C_DecryptFinal, "C_DecryptFinal", "ufy", "ay" }, |
| 176 |
{ GKM_RPC_CALL_C_DigestInit, "C_DigestInit", "uM", "" }, |
| 177 |
{ GKM_RPC_CALL_C_Digest, "C_Digest", "uayfy", "ay" }, |
| 178 |
{ GKM_RPC_CALL_C_DigestUpdate, "C_DigestUpdate", "uay", "" }, |
| 179 |
{ GKM_RPC_CALL_C_DigestKey, "C_DigestKey", "uu", "" }, |
| 180 |
{ GKM_RPC_CALL_C_DigestFinal, "C_DigestFinal", "ufy", "ay" }, |
| 181 |
{ GKM_RPC_CALL_C_SignInit, "C_SignInit", "uMu", "" }, |
| 182 |
{ GKM_RPC_CALL_C_Sign, "C_Sign", "uayfy", "ay" }, |
| 183 |
{ GKM_RPC_CALL_C_SignUpdate, "C_SignUpdate", "uay", "" }, |
| 184 |
{ GKM_RPC_CALL_C_SignFinal, "C_SignFinal", "ufy", "ay" }, |
| 185 |
{ GKM_RPC_CALL_C_SignRecoverInit, "C_SignRecoverInit", "uMu", "" }, |
| 186 |
{ GKM_RPC_CALL_C_SignRecover, "C_SignRecover", "uayfy", "ay" }, |
| 187 |
{ GKM_RPC_CALL_C_VerifyInit, "C_VerifyInit", "uMu", "" }, |
| 188 |
{ GKM_RPC_CALL_C_Verify, "C_Verify", "uayay", "" }, |
| 189 |
{ GKM_RPC_CALL_C_VerifyUpdate, "C_VerifyUpdate", "uay", "" }, |
| 190 |
{ GKM_RPC_CALL_C_VerifyFinal, "C_VerifyFinal", "uay", "" }, |
| 191 |
{ GKM_RPC_CALL_C_VerifyRecoverInit, "C_VerifyRecoverInit", "uMu", "" }, |
| 192 |
{ GKM_RPC_CALL_C_VerifyRecover, "C_VerifyRecover", "uayfy", "ay" }, |
| 193 |
{ GKM_RPC_CALL_C_DigestEncryptUpdate, "C_DigestEncryptUpdate", "uayfy", "ay" }, |
| 194 |
{ GKM_RPC_CALL_C_DecryptDigestUpdate, "C_DecryptDigestUpdate", "uayfy", "ay" }, |
| 195 |
{ GKM_RPC_CALL_C_SignEncryptUpdate, "C_SignEncryptUpdate", "uayfy", "ay" }, |
| 196 |
{ GKM_RPC_CALL_C_DecryptVerifyUpdate, "C_DecryptVerifyUpdate", "uayfy", "ay" }, |
| 197 |
{ GKM_RPC_CALL_C_GenerateKey, "C_GenerateKey", "uMaA", "u" }, |
| 198 |
{ GKM_RPC_CALL_C_GenerateKeyPair, "C_GenerateKeyPair", "uMaAaA", "uu" }, |
| 199 |
{ GKM_RPC_CALL_C_WrapKey, "C_WrapKey", "uMuufy", "ay" }, |
| 200 |
{ GKM_RPC_CALL_C_UnwrapKey, "C_UnwrapKey", "uMuayaA", "u" }, |
| 201 |
{ GKM_RPC_CALL_C_DeriveKey, "C_DeriveKey", "uMuaA", "u" }, |
| 202 |
{ GKM_RPC_CALL_C_SeedRandom, "C_SeedRandom", "uay", "" }, |
| 203 |
{ GKM_RPC_CALL_C_GenerateRandom, "C_GenerateRandom", "ufy", "ay" }, |
| 204 |
}; |
| 205 |
|
| 206 |
#ifdef _DEBUG |
| 207 |
#define GKM_RPC_CHECK_CALLS() \ |
| 208 |
{ int i; for (i = 0; i < GKM_RPC_CALL_MAX; ++i) assert (gkm_rpc_calls[i].call_id == i); } |
| 209 |
#endif |
| 210 |
|
| 211 |
#define GKM_RPC_HANDSHAKE \ |
| 212 |
((unsigned char*)"PRIVATE-GNOME-KEYRING-PKCS11-PROTOCOL-V-1") |
| 213 |
#define GKM_RPC_HANDSHAKE_LEN \ |
| 214 |
(strlen ((char *)GKM_RPC_HANDSHAKE)) |
| 215 |
|
| 216 |
#define GKM_RPC_SOCKET_EXT "pkcs11" |
| 217 |
|
| 218 |
typedef enum _GkmRpcMessageType { |
| 219 |
GKM_RPC_REQUEST = 1, |
| 220 |
GKM_RPC_RESPONSE |
| 221 |
} GkmRpcMessageType; |
| 222 |
|
| 223 |
typedef struct _GkmRpcMessage { |
| 224 |
int call_id; |
| 225 |
GkmRpcMessageType call_type; |
| 226 |
const char *signature; |
| 227 |
EggBuffer buffer; |
| 228 |
|
| 229 |
size_t parsed; |
| 230 |
const char *sigverify; |
| 231 |
} GkmRpcMessage; |
| 232 |
|
| 233 |
GkmRpcMessage* gkm_rpc_message_new (EggBufferAllocator allocator); |
| 234 |
|
| 235 |
void gkm_rpc_message_free (GkmRpcMessage *msg); |
| 236 |
|
| 237 |
void gkm_rpc_message_reset (GkmRpcMessage *msg); |
| 238 |
|
| 239 |
int gkm_rpc_message_equals (GkmRpcMessage *m1, |
| 240 |
GkmRpcMessage *m2); |
| 241 |
|
| 242 |
#define gkm_rpc_message_is_verified(msg) (!(msg)->sigverify || (msg)->sigverify[0] == 0) |
| 243 |
|
| 244 |
#define gkm_rpc_message_buffer_error(msg) (egg_buffer_has_error(&(msg)->buffer)) |
| 245 |
|
| 246 |
int gkm_rpc_message_prep (GkmRpcMessage *msg, |
| 247 |
int call_id, |
| 248 |
GkmRpcMessageType type); |
| 249 |
|
| 250 |
int gkm_rpc_message_parse (GkmRpcMessage *msg, |
| 251 |
GkmRpcMessageType type); |
| 252 |
|
| 253 |
int gkm_rpc_message_verify_part (GkmRpcMessage *msg, |
| 254 |
const char* part); |
| 255 |
|
| 256 |
int gkm_rpc_message_write_byte (GkmRpcMessage *msg, |
| 257 |
CK_BYTE val); |
| 258 |
|
| 259 |
int gkm_rpc_message_write_ulong (GkmRpcMessage *msg, |
| 260 |
CK_ULONG val); |
| 261 |
|
| 262 |
int gkm_rpc_message_write_zero_string (GkmRpcMessage *msg, |
| 263 |
CK_UTF8CHAR* string); |
| 264 |
|
| 265 |
int gkm_rpc_message_write_space_string (GkmRpcMessage *msg, |
| 266 |
CK_UTF8CHAR* buffer, |
| 267 |
CK_ULONG length); |
| 268 |
|
| 269 |
int gkm_rpc_message_write_byte_buffer (GkmRpcMessage *msg, |
| 270 |
CK_ULONG count); |
| 271 |
|
| 272 |
int gkm_rpc_message_write_byte_array (GkmRpcMessage *msg, |
| 273 |
CK_BYTE_PTR arr, |
| 274 |
CK_ULONG num); |
| 275 |
|
| 276 |
int gkm_rpc_message_write_ulong_buffer (GkmRpcMessage *msg, |
| 277 |
CK_ULONG count); |
| 278 |
|
| 279 |
int gkm_rpc_message_write_ulong_array (GkmRpcMessage *msg, |
| 280 |
CK_ULONG_PTR arr, |
| 281 |
CK_ULONG num); |
| 282 |
|
| 283 |
int gkm_rpc_message_write_attribute_buffer (GkmRpcMessage *msg, |
| 284 |
CK_ATTRIBUTE_PTR arr, |
| 285 |
CK_ULONG num); |
| 286 |
|
| 287 |
int gkm_rpc_message_write_attribute_array (GkmRpcMessage *msg, |
| 288 |
CK_ATTRIBUTE_PTR arr, |
| 289 |
CK_ULONG num); |
| 290 |
|
| 291 |
int gkm_rpc_message_write_version (GkmRpcMessage *msg, |
| 292 |
CK_VERSION* version); |
| 293 |
|
| 294 |
|
| 295 |
int gkm_rpc_message_read_byte (GkmRpcMessage *msg, |
| 296 |
CK_BYTE* val); |
| 297 |
|
| 298 |
int gkm_rpc_message_read_ulong (GkmRpcMessage *msg, |
| 299 |
CK_ULONG* val); |
| 300 |
|
| 301 |
int gkm_rpc_message_read_space_string (GkmRpcMessage *msg, |
| 302 |
CK_UTF8CHAR* buffer, |
| 303 |
CK_ULONG length); |
| 304 |
|
| 305 |
int gkm_rpc_message_read_version (GkmRpcMessage *msg, |
| 306 |
CK_VERSION* version); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
void gkm_rpc_log (const char *line); |
| 311 |
|
| 312 |
void gkm_rpc_warn (const char* msg, ...); |
| 313 |
|
| 314 |
void gkm_rpc_debug (const char* msg, ...); |
| 315 |
|
| 316 |
#ifdef G_DISABLE_ASSERT |
| 317 |
#define assert(x) |
| 318 |
#else |
| 319 |
#include <assert.h> |
| 320 |
#endif |
| 321 |
|
| 322 |
/* |
| 323 |
* PKCS#11 mechanism parameters are not easy to serialize. They're |
| 324 |
* completely different for so many mechanisms, they contain |
| 325 |
* pointers to arbitrary memory, and many callers don't initialize |
| 326 |
* them completely or properly. |
| 327 |
* |
| 328 |
* We only support certain mechanisms. |
| 329 |
* |
| 330 |
* Also callers do yucky things like leaving parts of the structure |
| 331 |
* pointing to garbage if they don't think it's going to be used. |
| 332 |
*/ |
| 333 |
|
| 334 |
int gkm_rpc_mechanism_is_supported (CK_MECHANISM_TYPE mech); |
| 335 |
void gkm_rpc_mechanism_list_purge (CK_MECHANISM_TYPE_PTR mechs, CK_ULONG_PTR n_mechs); |
| 336 |
int gkm_rpc_mechanism_has_sane_parameters (CK_MECHANISM_TYPE type); |
| 337 |
int gkm_rpc_mechanism_has_no_parameters (CK_MECHANISM_TYPE mech); |
| 338 |
|
| 339 |
#endif /* GKM_RPC_CALLS_H */ |