forked from nhorman/rng-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rngd_pkcs11.c
161 lines (137 loc) · 4.42 KB
/
rngd_pkcs11.c
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
/*
* Copyright (c) 2017, Neil Horman
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#define _GNU_SOURCE
#ifndef HAVE_CONFIG_H
#error Invalid or missing autoconf build environment
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <libp11.h>
#include "rngd.h"
#include "rngd_entsource.h"
static PKCS11_CTX *ctx = NULL;
static PKCS11_SLOT *slots, *slot;
static unsigned int nslots;
int xread_pkcs11(void *buf, size_t size, struct rng *ent_src)
{
int rc;
int count = ent_src->rng_options[PKCS11_OPT_CHUNK].int_val;
int chunk_len = size / count;
size_t left = size;
void *ptr = buf;
/* If our count is greater than our size */
if (!chunk_len)
chunk_len = left;
while (left > 0) {
if (left > chunk_len) {
rc = PKCS11_generate_random(slot, ptr, chunk_len);
ptr += chunk_len;
left -= chunk_len;
} else {
rc = PKCS11_generate_random(slot, ptr, left);
left = 0;
}
if (rc < 0)
return 1;
}
return 0;
}
int validate_pkcs11_options(struct rng *ent_src)
{
struct stat sbuf;
if (stat(ent_src->rng_options[PKCS11_OPT_ENGINE].str_val, &sbuf) == -1) {
if (errno == ENOENT) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING,"No PKCS11 endpoint %s found\n",
ent_src->rng_options[PKCS11_OPT_ENGINE].str_val);
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING,"Install opensc/opensc-pkcs11/etc"
" if you would like to gather smartcard entropy\n");
} else
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "PKCS11 Engine %s Error: %s\n",
ent_src->rng_options[PKCS11_OPT_ENGINE].str_val,
strerror(errno));
return 1;
}
if (!ent_src->rng_options[PKCS11_OPT_CHUNK].int_val) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "PKCS11 Engine: chunk size cannot be 0\n");
return 1;
}
if (ent_src->rng_options[PKCS11_OPT_CHUNK].int_val > FIPS_RNG_BUFFER_SIZE) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "PKCS11 Engine: chunk size cannot be larger than %d\n",
FIPS_RNG_BUFFER_SIZE);
return 1;
}
return 0;
}
/*
* Init PKCS11
*/
int init_pkcs11_entropy_source(struct rng *ent_src)
{
ctx = PKCS11_CTX_new();
int rc;
if (validate_pkcs11_options(ent_src))
return 1;
if (!ctx) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "Unable to allocate new pkcs11 context\n");
return 1;
}
rc = PKCS11_CTX_load(ctx, ent_src->rng_options[PKCS11_OPT_ENGINE].str_val);
if (rc) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "Unable to load pkcs11 engine: %s\n",
ERR_reason_error_string(ERR_get_error()));
rc = 1;
goto free_ctx;
}
rc = PKCS11_enumerate_slots(ctx, &slots, &nslots);
if (rc < 0) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "No pkcs11 slots available\n");
rc = 1;
goto unload_engine;
}
slot = PKCS11_find_token(ctx, slots, nslots);
if (slot == NULL || slot->token == NULL) {
message_entsrc(ent_src,LOG_DAEMON|LOG_WARNING, "No pkcs11 tokens available\n");
rc = 1;
goto release_slots;
}
message_entsrc(ent_src,LOG_DAEMON|LOG_INFO, "Slot manufacturer......: %s\n", slot->manufacturer);
message_entsrc(ent_src,LOG_DAEMON|LOG_INFO, "Slot description.......: %s\n", slot->description);
message_entsrc(ent_src,LOG_DAEMON|LOG_INFO, "Slot token label.......: %s\n", slot->token->label);
message_entsrc(ent_src,LOG_DAEMON|LOG_INFO, "Slot token manufacturer: %s\n", slot->token->manufacturer);
message_entsrc(ent_src,LOG_DAEMON|LOG_INFO, "Slot token model.......: %s\n", slot->token->model);
message_entsrc(ent_src,LOG_DAEMON|LOG_INFO, "Slot token serial......: %s\n", slot->token->serialnr);
rc = 0;
goto out;
release_slots:
PKCS11_release_all_slots(ctx, slots, nslots);
unload_engine:
PKCS11_CTX_unload(ctx);
free_ctx:
PKCS11_CTX_free(ctx);
out:
return rc;
}
void close_pkcs11_entropy_source(struct rng *ent_src)
{
PKCS11_release_all_slots(ctx, slots, nslots);
PKCS11_CTX_unload(ctx);
PKCS11_CTX_free(ctx);
return;
}