File: socket_client.c

package info (click to toggle)
opencryptoki 3.23.0%2Bdfsg-0.3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,604 kB
  • sloc: ansic: 214,248; sh: 2,759; makefile: 289; yacc: 242; pascal: 152; exp: 126; lex: 93; cpp: 9
file content (510 lines) | stat: -rw-r--r-- 15,164 bytes parent folder | download
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * This program is provided under the terms of the Common Public License,
 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
 * found in the file LICENSE file or at
 * https://opensource.org/licenses/cpl1.0.php
 */

/* (C) COPYRIGHT Google Inc. 2013 */

//
// Pkcs11 Api Socket client routines
//

#include <stdio.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <syslog.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <poll.h>

#include "apiproto.h"
#include "slotmgr.h"
#include "apictl.h"
#include "trace.h"
#include "ock_syslog.h"
#include "events.h"

extern API_Proc_Struct_t *Anchor;

int connect_socket(const char *file_path)
{
    int socketfd;
    struct sockaddr_un daemon_address;
    struct stat file_info;
    struct group *grp;
    struct passwd *pwd;

    if (stat(file_path, &file_info)) {
        OCK_SYSLOG(LOG_ERR,
                   "connect_socket: failed to find socket file, errno=%d",
                   errno);
        return -1;
    }

    grp = getgrnam(PKCS_GROUP);
    if (!grp) {
        OCK_SYSLOG(LOG_ERR,
                   "connect_socket: %s group does not exist, errno=%d",
                   PKCS_GROUP, errno);
        return -1;
    }

    pwd = getpwnam(PKCSSLOTD_USER);
    if (!pwd) {
        OCK_SYSLOG(LOG_ERR,
                   "connect_socket: %s user does not exist, errno=%d",
                   PKCSSLOTD_USER, errno);
        return -1;
    }

    if (file_info.st_uid != pwd->pw_uid || file_info.st_gid != grp->gr_gid) {
        OCK_SYSLOG(LOG_ERR,
                   "connect_socket: incorrect permissions on socket file");
        return -1;
    }

    if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        OCK_SYSLOG(LOG_ERR,
                   "connect_socket: failed to create socket, errno=%d",
                   errno);
        return -1;
    }

    memset(&daemon_address, 0, sizeof(struct sockaddr_un));
    daemon_address.sun_family = AF_UNIX;
    strncpy(daemon_address.sun_path, file_path,
            sizeof(daemon_address.sun_path));
    daemon_address.sun_path[sizeof(daemon_address.sun_path) - 1] = '\0';

    if (connect(socketfd, (struct sockaddr *) &daemon_address,
                sizeof(struct sockaddr_un)) != 0) {
        OCK_SYSLOG(LOG_ERR,
                   "connect_socket: failed to connect to slotmanager daemon, "
                   "errno=%d",
                   errno);
        goto error;
    }

    return socketfd;

error:
    close(socketfd);
    return -1;
}

static ssize_t read_all(int socketfd, char *buffer, size_t size)
{
    size_t bytes_received = 0;
    ssize_t n;

    while (bytes_received < size) {
        n = read(socketfd, buffer + bytes_received, size - bytes_received);
        if (n < 0) {
            // read error
            if (errno == EINTR)
                continue;
            return -errno;
        }
        if (n == 0)
            break;

        bytes_received += n;
    }

    return bytes_received;
}

static ssize_t send_all(int socketfd, char *buffer, size_t size)
{
    size_t bytes_sent = 0;
    ssize_t n;

    while (bytes_sent < size) {
        n = send(socketfd, buffer + bytes_sent, size - bytes_sent, 0);
        if (n < 0) {
            // send error
            if (errno == EINTR)
                continue;
            return -errno;
        }
        if (n == 0)
            break;

        bytes_sent += n;
    }

    return bytes_sent;
}

//
// Will fill out the Slot_Mgr_Socket_t and Slot_Mgr_Client_Cred_t structures
// in the Anchor global data structure with the values passed by the pkcsslotd
// via a socket RPC.
int init_socket_data(int socketfd)
{
    ssize_t n;

    n = read_all(socketfd, (char *)&Anchor->ClientCred,
                 sizeof(Anchor->ClientCred));
    if (n < 0) {
        // read error
        OCK_SYSLOG(LOG_ERR, "init_socket_data: read error \
                   on daemon socket, errno=%zd", -n);
        return FALSE;
    }
    if (n != sizeof(Anchor->ClientCred)) {
        // eof but we still expect some bytes
        OCK_SYSLOG(LOG_ERR, "init_socket_data: read returned \
                   with eof but we still \
                   expect %lu bytes from daemon",
                   sizeof(Anchor->ClientCred) - n);
        return FALSE;
    }

    n = read_all(socketfd, (char *)&Anchor->SocketDataP,
                 sizeof(Anchor->SocketDataP));
    if (n < 0) {
        // read error
        OCK_SYSLOG(LOG_ERR, "init_socket_data: read error \
                   on daemon socket, errno=%zd", -n);
        return FALSE;
    }
    if (n != sizeof(Anchor->SocketDataP)) {
        // eof but we still expect some bytes
        OCK_SYSLOG(LOG_ERR, "init_socket_data: read returned \
                   with eof but we still \
                   expect %lu bytes from daemon",
                   sizeof(Anchor->SocketDataP) - n);
        return FALSE;
    }

    return TRUE;
}

static bool match_token_label_filter(event_msg_t *event, API_Slot_t *sltp)
{
    if (event->token_label[0] == ' ' || event->token_label[0] == '\0')
        return true;

    return memcmp(event->token_label,
                  sltp->TokData->nv_token_data->token_info.label,
                  sizeof(event->token_label)) == 0;
}

struct type_model {
    unsigned int type;
    char model[member_size(CK_TOKEN_INFO_32, model)];
};

static const struct type_model type_model_flt[] = {
        { .type = EVENT_TOK_TYPE_CCA,  .model = "CCA             " },
        { .type = EVENT_TOK_TYPE_EP11, .model = "EP11            " },
};

static bool match_token_type_filter(event_msg_t *event, API_Slot_t *sltp)
{
    size_t i;

    if (event->token_type == EVENT_TOK_TYPE_ALL)
        return true;

    for (i = 0; i < sizeof(type_model_flt) / sizeof(struct type_model); i++) {
        if (memcmp(sltp->TokData->nv_token_data->token_info.model,
                   type_model_flt[i].model,
                   sizeof(type_model_flt[i].model)) == 0 &&
            (event->token_type & type_model_flt[i].type) != 0)
            return true;
    }

    return false;
}

static int handle_event(API_Proc_Struct_t *anchor, event_msg_t *event,
                        char *payload, event_reply_t *reply)
{
    CK_SLOT_ID slotID;
    API_Slot_t *sltp;
    CK_RV rc;

    /* If its not for our process, ignore it, don't increment reply counters */
    if (event->process_id != 0 &&
        event->process_id != anchor->ClientCred.real_pid)
        return 0;

    for (slotID = 0; slotID < NUMBER_SLOTS_MANAGED; slotID++) {
        sltp = &anchor->SltList[slotID];
        if (sltp->DLLoaded == FALSE || sltp->FcnList == NULL)
            continue;

        if (!match_token_label_filter(event, sltp))
            continue;
        if (!match_token_type_filter(event, sltp))
            continue;

        if (sltp->FcnList->ST_HandleEvent != NULL)
            rc = sltp->FcnList->ST_HandleEvent(sltp->TokData, event->type,
                                               event->flags, payload,
                                               event->payload_len);
        else
            rc = CKR_FUNCTION_NOT_SUPPORTED;

        TRACE_DEVEL("Slot %lu ST_HandleEvent rc: 0x%lx\n", slotID, rc);
        switch (rc) {
        case CKR_OK:
            reply->positive_replies++;
            break;
        case CKR_FUNCTION_NOT_SUPPORTED:
            reply->nothandled_replies++;
            break;
        default:
            reply->negative_replies++;
            break;
        }
    }

    return 0;
}

struct cleanup_data {
    API_Proc_Struct_t *anchor;
#if OPENSSL_VERSION_PREREQ(3, 0)
    OSSL_LIB_CTX *prev_libctx;
#endif
};

static void event_thread_cleanup(void *arg)
{
    struct cleanup_data *cleanup = arg;

#if OPENSSL_VERSION_PREREQ(3, 0)
    OSSL_LIB_CTX_set0_default(cleanup->prev_libctx);
#else
    UNUSED(cleanup);
#endif

    TRACE_DEVEL("Event thread %lu terminating\n", pthread_self());
}

static void *event_thread(void *arg)
{
    API_Proc_Struct_t *anchor = arg;
    struct cleanup_data cleanup;
#if OPENSSL_VERSION_PREREQ(3, 0)
    OSSL_LIB_CTX *prev_libctx;
#endif
    int oldstate, oldtype;
    struct pollfd pollfd;
    event_msg_t event;
    char *payload;
    event_reply_t reply;
    ssize_t num;
    int rc;

    UNUSED(arg);

    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);

    TRACE_DEVEL("Event thread %lu running\n", pthread_self());

    if (anchor->socketfd < 0) {
        TRACE_ERROR("socket is already closed.\n");
        TRACE_DEVEL("Event thread %lu terminating\n", pthread_self());
        return NULL;
    }

#if OPENSSL_VERSION_PREREQ(3, 0)
    /* Ensure that the event thread uses Opencryptoki's own library context */
    prev_libctx = OSSL_LIB_CTX_set0_default(Anchor->openssl_libctx);
    if (prev_libctx == NULL) {
        TRACE_ERROR("OSSL_LIB_CTX_set0_default failed\n");
        TRACE_DEVEL("Event thread %lu terminating\n", pthread_self());
        return NULL;
    }
#endif

    /* Enable cancellation */
    cleanup.anchor = anchor;
#if OPENSSL_VERSION_PREREQ(3, 0)
    cleanup.prev_libctx = prev_libctx;
#endif
    pthread_cleanup_push(event_thread_cleanup, &cleanup);
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);

    pollfd.fd = anchor->socketfd;
    pollfd.events = POLLIN | POLLHUP | POLLERR;

    while (1) {
        pollfd.revents = 0;
        rc = poll(&pollfd, 1, -1);
        if (rc < 0) {
            if (errno == EINTR)
                continue;
            pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
            TRACE_ERROR("poll failed: %d\n", errno);
            break;
        }

        if (rc == 0)
            continue;

        if (pollfd.revents & (POLLHUP | POLLERR)) {
            pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
            TRACE_ERROR("Error on socket, possibly closed by slot daemon\n");
            break;
        }
        if ((pollfd.revents & POLLIN) == 0)
            continue;

        /* Disable for cancellation while we are working on an event */
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);

        TRACE_DEVEL("Receive new event ....\n");

        num = read_all(anchor->socketfd, (char *)&event, sizeof(event));
        if (num != sizeof(event)) {
            TRACE_ERROR("Error receiving the event, rc: %zd\n", num);
            break;
        }

        TRACE_DEBUG("Event version:      %u\n", event.version);
        TRACE_DEBUG("Event type:         0x%08x\n", event.type);
        TRACE_DEBUG("Event flags:        0x%08x\n", event.flags);
        TRACE_DEBUG("Event token_type:   0x%08x\n", event.token_type);
        TRACE_DEBUG("Event token_name:   '%.32s'\n", event.token_label);
        TRACE_DEBUG("Event process_id:   %u\n", event.process_id);
        TRACE_DEBUG("Event payload_len:  %u\n", event.payload_len);

        if (event.version != EVENT_VERSION_1) {
            TRACE_ERROR("Event version invalid: %u\n", event.version);
            break;
        }

        payload = NULL;
        if (event.payload_len > 0) {
            payload = malloc(event.payload_len);
            if (payload == NULL) {
                TRACE_ERROR("Failed to allocate buffer for event payload\n");
                break;
            }

            num = read_all(anchor->socketfd, payload, event.payload_len);
            if (num != (ssize_t)event.payload_len) {
                TRACE_ERROR("Error receiving the event payload, rc: %zd\n", num);
                if (payload != NULL)
                    free(payload);
                break;
            }

            TRACE_DEBUG("Event payload:\n");
            TRACE_DEBUG_DUMP("  ", payload, event.payload_len);
        }

        memset(&reply, 0, sizeof(reply));
        reply.version = EVENT_VERSION_1;
        rc = handle_event(anchor, &event, payload, &reply);
        if (rc != 0) {
            TRACE_ERROR("Error handling the event, rc: %d\n", rc);
            if (payload != NULL)
                free(payload);
            break;
        }

        TRACE_DEBUG("Reply version:      %u\n", reply.version);
        TRACE_DEBUG("Reply positive:     %u\n", reply.positive_replies);
        TRACE_DEBUG("Reply negative:     %u\n", reply.negative_replies);
        TRACE_DEBUG("Reply not-handled:  %u\n", reply.nothandled_replies);

        if (event.flags & EVENT_FLAGS_REPLY_REQ) {
            num = send_all(anchor->socketfd, (char *)&reply, sizeof(reply));
            if (num != sizeof(reply)) {
                TRACE_ERROR("Error sending the event reply, rc: %zd\n", num);
                if (payload != NULL)
                    free(payload);
                break;
            }
        }

        if (payload != NULL)
            free(payload);

        /* Re-enable for  and test if we got canceled in the meantime */
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
        pthread_testcancel();
    }

    /*
     * Close the socket if we encounter an unrecoverable error (e.g. received
     * invalid event) and stop the thread because of that.
     * If the thread is stopped via stop_event_thread(), then it gets canceled
     * via pthread_cancel(), and will not reach this place, thus the socket is
     * not closed. This is intended, and the socket will then be closed by
     * C_Finalize(). The atfork 'prepare' handler in the parent process also
     * stops the thread (via stop_event_thread()), and the socket must not be
     * closed in this case, because the thread is restarted in the atfork
     * 'parent' handler, and should continue to receive events from the
     * socket.
     */
    close(anchor->socketfd);
    anchor->socketfd = -1;

#if OPENSSL_VERSION_PREREQ(3, 0)
    OSSL_LIB_CTX_set0_default(prev_libctx);
#endif

    pthread_cleanup_pop(1);
    return NULL;
}

int start_event_thread(void)
{
    int rc;

    rc =  pthread_create(&Anchor->event_thread, NULL, event_thread, Anchor);
    if (rc != 0) {
        OCK_SYSLOG(LOG_ERR, "start_event_thread: pthread_create failed, "
                   "errno=%d", rc);
        TRACE_ERROR("Failed to start event thread, errno=%d\n", rc);
        return rc;
    }

    TRACE_DEVEL("Event thread %lu has been started\n", Anchor->event_thread);
    return 0;
}

int stop_event_thread(void)
{
    int rc;
    void *status;

    TRACE_DEVEL("Canceling event thread %lu\n", Anchor->event_thread);
    rc = pthread_cancel(Anchor->event_thread);
    if (rc != 0 && rc != ESRCH)
        return rc;

    TRACE_DEVEL("Waiting for event thread %lu to terminate\n",
                Anchor->event_thread);
    rc = pthread_join(Anchor->event_thread, &status);
    if (rc != 0)
        return rc;

    if (status != PTHREAD_CANCELED) {
        TRACE_ERROR("Event thread was stopped, but did not return the "
                   "expected status\n");
    }

    TRACE_DEVEL("Event thread %lu has terminated\n", Anchor->event_thread);

    Anchor->event_thread = 0;
    return 0;
}