summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/pkcs11/pkcs11_rng.c
blob: 20e4b6f766f4d7fb8b380727cfdc0f44698f2c8f (plain)
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
/*
 * Copyright (C) 2011 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that 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.
 */

#include "pkcs11_rng.h"

#include <debug.h>

#include "pkcs11_manager.h"

typedef struct private_pkcs11_rng_t private_pkcs11_rng_t;

/**
 * Private data of an pkcs11_rng_t object.
 */
struct private_pkcs11_rng_t {

	/**
	 * Public interface.
	 */
	pkcs11_rng_t public;

	/**
	 * PKCS#11 library
	 */
	pkcs11_library_t *lib;

	/**
	 * Mechanism for this rng
	 */
	CK_SESSION_HANDLE session;

};

METHOD(rng_t, get_bytes, bool,
	private_pkcs11_rng_t *this, size_t bytes, u_int8_t *buffer)
{
	CK_RV rv;
	rv = this->lib->f->C_GenerateRandom(this->session, buffer, bytes);
	if (rv != CKR_OK)
	{
		DBG1(DBG_CFG, "C_GenerateRandom() failed: %N", ck_rv_names, rv);
		return FALSE;
	}
	return TRUE;
}

METHOD(rng_t, allocate_bytes, bool,
	private_pkcs11_rng_t *this, size_t bytes, chunk_t *chunk)
{
	*chunk = chunk_alloc(bytes);
	if (!get_bytes(this, chunk->len, chunk->ptr))
	{
		chunk_clear(chunk);
		return FALSE;
	}
	return TRUE;
}

METHOD(rng_t, destroy, void,
	private_pkcs11_rng_t *this)
{
	this->lib->f->C_CloseSession(this->session);
	free(this);
}

/**
 * Find a token with its own RNG
 */
static pkcs11_library_t *find_token(CK_SESSION_HANDLE *session)
{
	enumerator_t *tokens;
	pkcs11_manager_t *manager;
	pkcs11_library_t *current, *found = NULL;
	CK_SLOT_ID slot;

	manager = lib->get(lib, "pkcs11-manager");
	if (!manager)
	{
		return NULL;
	}
	tokens = manager->create_token_enumerator(manager);
	while (tokens->enumerate(tokens, &current, &slot))
	{
		CK_TOKEN_INFO info;
		CK_RV rv;
		rv = current->f->C_GetTokenInfo(slot, &info);
		if (rv != CKR_OK)
		{
			continue;
		}
		if (info.flags & CKF_RNG)
		{
			if (current->f->C_OpenSession(slot, CKF_SERIAL_SESSION,
										  NULL, NULL, session) == CKR_OK)
			{
				found = current;
				break;
			}
		}
	}
	tokens->destroy(tokens);
	return found;
}

/*
 * Described in header.
 */
pkcs11_rng_t *pkcs11_rng_create(rng_quality_t quality)
{
	private_pkcs11_rng_t *this;

	INIT(this,
		.public = {
			.rng = {
				.get_bytes = _get_bytes,
				.allocate_bytes = _allocate_bytes,
				.destroy = _destroy,
			},
		},
	);

	this->lib = find_token(&this->session);
	if (!this->lib)
	{
		free(this);
		return NULL;
	}

	return &this->public;
}