aboutsummaryrefslogtreecommitdiff
path: root/rpc_client.c
blob: 3d4fceea8debcd9d8fdad5fc4d298f6e71611df7 (plain) (blame)
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
/*
 * rpc_client.c
 * ------------
 * Remote procedure call client-side private API implementation.
 *
 * Authors: Rob Austein
 * Copyright (c) 2015, NORDUnet A/S All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the NORDUnet nor the names of its contributors may
 *   be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "hal.h"
#include "hal_internal.h"

/*
 * RPC calls.  Not implemented yet.
 */

#warning These are all placeholders, waiting to be filled in with the real RPC calls

static hal_error_t get_random(void *buffer, const size_t length)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t set_pin(const hal_client_handle_t client,
                           const hal_user_t user,
                           const char * const newpin, const size_t newpin_len)
{
  return HAL_ERROR_IMPOSSIBLE;
}

/*
 * We may end up wanting to wrap a client-side cache around the
 * login()/logout()/logout_all() calls and reimplement is_logged_in()
 * on the client side using that cache, so that access checks don't
 * need to cross the RPC boundary.  Then again, we might not, if the
 * RPC call is fast enough, so implementing all before the RPC would
 * qualify as premature optimization.  There aren't all that many
 * things on the client side that would use this anyway, so the whole
 * question may be moot.
 *
 * For now, we leave all of these as plain RPC calls, but we may want
 * to revisit this if the is_logged_in() call turns into a bottleneck.
 */

static hal_error_t login(const hal_client_handle_t client,
                         const hal_user_t user,
                         const char * const pin, const size_t pin_len)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t logout(const hal_client_handle_t client)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t logout_all(void)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t is_logged_in(const hal_client_handle_t client,
                                const hal_user_t user)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t hash_get_digest_len(const hal_digest_algorithm_t alg, size_t *length)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t hash_get_digest_algorithm_id(const hal_digest_algorithm_t alg,
                                                uint8_t *id, size_t *len, const size_t len_max)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t hash_get_algorithm(const hal_hash_handle_t hash, hal_digest_algorithm_t *alg)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t hash_initialize(const hal_client_handle_t client,
                                   const hal_session_handle_t session,
                                   hal_hash_handle_t *hash,
                                   const hal_digest_algorithm_t alg,
                                   const uint8_t * const key, const size_t key_len)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t hash_update(const hal_hash_handle_t hash,
                               const uint8_t * data, const size_t length)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t hash_finalize(const hal_hash_handle_t hash,
                                 uint8_t *digest, const size_t length)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_load(const hal_client_handle_t client,
                             const hal_session_handle_t session,
                             hal_pkey_handle_t *pkey,
                             const hal_key_type_t type,
                             const hal_curve_name_t curve,
                             const uint8_t * const name, const size_t name_len,
                             const uint8_t * const der, const size_t der_len,
                             const hal_key_flags_t flags)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_find(const hal_client_handle_t client,
                             const hal_session_handle_t session,
                             hal_pkey_handle_t *pkey,
                             const hal_key_type_t type,
                             const uint8_t * const name, const size_t name_len)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_generate_rsa(const hal_client_handle_t client,
                                     const hal_session_handle_t session,
                                     hal_pkey_handle_t *pkey,
                                     const uint8_t * const name, const size_t name_len,
                                     const unsigned key_len,
                                     const uint8_t * const exp, const size_t exp_len,
                                     const hal_key_flags_t flags)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_generate_ec(const hal_client_handle_t client,
                                    const hal_session_handle_t session,
                                    hal_pkey_handle_t *pkey,
                                    const uint8_t * const name, const size_t name_len,
                                    const hal_curve_name_t curve,
                                    const hal_key_flags_t flags)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_close(const hal_pkey_handle_t pkey)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_delete(const hal_pkey_handle_t pkey)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_get_key_type(const hal_pkey_handle_t pkey,
                                     hal_key_type_t *type)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_get_key_flags(const hal_pkey_handle_t pkey,
                                      hal_key_flags_t *flags)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static size_t pkey_get_public_key_len(const hal_pkey_handle_t pkey)
{
  return 0;
}

static hal_error_t pkey_get_public_key(const hal_pkey_handle_t pkey,
                                       uint8_t *der, size_t *der_len, const size_t der_max)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_remote_sign(const hal_session_handle_t session,
                                    const hal_pkey_handle_t pkey,
                                    const hal_hash_handle_t hash,
                                    const uint8_t * const input,  const size_t input_len,
                                    uint8_t * signature, size_t *signature_len, const size_t signature_max)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_remote_verify(const hal_session_handle_t session,
                                      const hal_pkey_handle_t pkey,
                                      const hal_hash_handle_t hash,
                                      const uint8_t * const input, const size_t input_len,
                                      const uint8_t * const signature, const size_t signature_len)
{
  return HAL_ERROR_IMPOSSIBLE;
}

static hal_error_t pkey_list(hal_pkey_info_t *result,
                             unsigned *result_len,
                             const unsigned result_max)
{
  return HAL_ERROR_IMPOSSIBLE;
}


/*
 * "Mixed" mode pkey operations, where the public key operation itself
 * takes place on the HSM but the hashing takes place locally.  If
 * we're given a hash context in this case, it's local, so we have to
 * pull the digest from the hash context and send that to the HSM.
 */

static hal_error_t pkey_mixed_sign(const hal_session_handle_t session,
                                   const hal_pkey_handle_t pkey,
                                   const hal_hash_handle_t hash,
                                   const uint8_t * const input,  const size_t input_len,
                                   uint8_t * signature, size_t *signature_len, const size_t signature_max)
{
  if (input != NULL)
    return pkey_remote_sign(session, pkey, hash, input, input_len,
                            signature, signature_len, signature_max);

  hal_digest_algorithm_t alg;
  size_t digest_len;
  hal_error_t err;

  if ((err = hal_rpc_hash_get_algorithm(hash, &alg))           != HAL_OK ||
      (err = hal_rpc_hash_get_digest_length(alg, &digest_len)) != HAL_OK)
    return err;

  uint8_t digest[digest_len];

  if ((err = hal_rpc_hash_finalize(hash, digest, digest_len)) != HAL_OK)
    return err;

  return pkey_remote_sign(session, pkey, hal_hash_handle_none, digest, digest_len,
                          signature, signature_len, signature_max);
}

static hal_error_t pkey_mixed_verify(const hal_session_handle_t session,
                                     const hal_pkey_handle_t pkey,
                                     const hal_hash_handle_t hash,
                                     const uint8_t * const input, const size_t input_len,
                                     const uint8_t * const signature, const size_t signature_len)
{
  if (input != NULL)
    return pkey_remote_verify(session, pkey, hash, input, input_len,
                              signature, signature_len);

  hal_digest_algorithm_t alg;
  size_t digest_len;
  hal_error_t err;

  if ((err = hal_rpc_hash_get_algorithm(hash, &alg))           != HAL_OK ||
      (err = hal_rpc_hash_get_digest_length(alg, &digest_len)) != HAL_OK)
    return err;

  uint8_t digest[digest_len];

  if ((err = hal_rpc_hash_finalize(hash, digest, digest_len)) != HAL_OK)
    return err;

  return pkey_remote_verify(session, pkey, hal_hash_handle_none, digest, digest_len,
                            signature, signature_len);
}

/*
 * Dispatch vectors.
 */

const hal_rpc_misc_dispatch_t hal_rpc_remote_misc_dispatch = {
  set_pin, login, logout, logout_all, is_logged_in, get_random
};

const hal_rpc_hash_dispatch_t hal_rpc_remote_hash_dispatch = {
  hash_get_digest_len, hash_get_digest_algorithm_id, hash_get_algorithm,
  hash_initialize, hash_update, hash_finalize
};

const hal_rpc_pkey_dispatch_t hal_rpc_remote_pkey_dispatch = {
  pkey_load, pkey_find, pkey_generate_rsa, pkey_generate_ec, pkey_close, pkey_delete,
  pkey_get_key_type, pkey_get_key_flags, pkey_get_public_key_len, pkey_get_public_key,
  pkey_remote_sign, pkey_remote_verify,
  pkey_list
};

const hal_rpc_pkey_dispatch_t hal_rpc_mixed_pkey_dispatch = {
  pkey_load, pkey_find, pkey_generate_rsa, pkey_generate_ec, pkey_close, pkey_delete,
  pkey_get_key_type, pkey_get_key_flags, pkey_get_public_key_len, pkey_get_public_key,
  pkey_mixed_sign, pkey_mixed_verify,
  pkey_list
};

/*
 * Local variables:
 * indent-tabs-mode: nil
 * End:
 */