aboutsummaryrefslogtreecommitdiffstats
path: root/cache.c
blob: 8b306721ed22fd2147809f9164a78d19118f9531 (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
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
/*
 * Simple memory cache for ini file parser
 *
 *  This should be copied from somewhere but, unfortunately is reinvented.
 *
 * Copyright (C) 2019 James.Bottomley@HansenPartnership.com
 *
 * SPDX-License-Identifier: LGPL-2.1-only
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "openssl-pkcs11.h"

struct kv {
	const char *key;
	const char *value;
	int len;
};

struct s_s {
	const char *section;
	struct kv *kvs;
	int kv_count;
};


static struct s_s *s = NULL;
static int section_count = 0;

static struct s_s *section_get(const char *section)
{
	int i;

	for (i = 0; i < section_count; i++)
		if (strcmp(s[i].section, section) == 0)
			return &s[i];
	return NULL;
}

static void remove_section(int sn)
{
	section_count--;
	/* last entry, just keep the reduced count (realloc will cope) */
	if (sn == section_count) {
		return;
	}

	/* just move everything over it and reduce the section count */
	memcpy(&s[sn], &s[sn + 1], sizeof(struct s_s)*(section_count - sn));
}

static struct s_s *section_get_alloc(const char *section)
{
	struct s_s *s_s = section_get(section);

	if (s_s)
		return s_s;
	s = reallocarray(s, section_count + 1, sizeof(*s_s));
	if (!s) {
		fprintf(stderr, "realloc of section %s failed: %s",
			section, strerror(errno));
		return NULL;
	}
	s_s = &s[section_count++];
	memset(s_s, 0, sizeof(*s_s));
	s_s->section = section;
	return s_s;
}

static struct kv *kv_get(struct s_s *s_s, const char *key)
{
	int i;

	for (i = 0; i < s_s->kv_count; i++)
		if (strcmp(s_s->kvs[i].key, key) == 0)
			return &s_s->kvs[i];
	return NULL;
}

static struct kv *kv_get_alloc(struct s_s *s_s, const char *key)
{
	struct kv *kv = kv_get(s_s, key);

	if (kv)
		return kv;

	s_s->kvs = reallocarray(s_s->kvs, s_s->kv_count + 1, sizeof(*kv));
		if (!s) {
		fprintf(stderr, "realloc of section %s kvs failed: %s",
			s_s->section, strerror(errno));
		return NULL;
	}
	kv = &s_s->kvs[s_s->kv_count++];
	memset(kv, 0, sizeof(*kv));
	kv->key = key;
	return kv;
}

static void cache_add_internal(struct s_s *s_s, const char *key,
			       const char *value, int len)
{
	struct kv *kv = kv_get_alloc(s_s, key);
	if (!kv)
		return;
	if (kv->value && kv->len == CACHE_PKEY)
		/* discard const */
		crypto_cache_free_pkey((void *)kv->value);
	if (len == 0)
		len = strlen(value);

	kv->value = value;
	kv->len = len;
}

void cache_add(const char *section, const char *key, const char *value, int len)
{
	struct s_s *s_s = section_get_alloc(section);

	if (!s_s)
		return;
	cache_add_internal(s_s, key, value, len);
}

void cache_add_by_secnum(int sec_num, const char *key, const char *value,
			 int len)
{
	struct s_s *s_s;

	if (sec_num > section_count)
		return;

	s_s = &s[sec_num];

	cache_add_internal(s_s, key, value, len);
}

static const char *
cache_get_internal(struct s_s *sec, const char *key, int *len)
{
	struct kv *kv;

	kv = kv_get(sec, key);

	if (!kv)
		return NULL;
	if (len)
		*len = kv->len;
	return kv->value;
}

const char *cache_get(const char *section, const char *key, int *len)
{
	struct s_s *sec = section_get(section);

	if (!sec)
		return NULL;

	return cache_get_internal(sec, key, len);
}

const char *cache_get_by_secnum(int sec_num, const char *key, int *len)
{
	struct s_s *sec;

	if (sec_num >= section_count)
		return NULL;
	sec = &s[sec_num];

	return cache_get_internal(sec, key, len);
}

const char *cache_get_section(int sc)
{
	if (sc >= section_count)
		return NULL;
	return s[sc].section;
}

int cache_get_sections(void)
{
	return section_count;
}

void cache_load_crypto_keys(void)
{
	int i;

	/* every section apart from global must have public key and  a
	 * private key */
	for (i = 1; i < section_count; i++) {
		struct kv *kv;
		int ret;

		kv = kv_get(&s[i], INI_PUBLIC_KEY);
		if (!kv) {
			kv = kv_get(&s[i], INI_CERT);
			if (kv)
				ret = crypto_load_cert(i, kv->value);
		} else {
			ret = crypto_load_public_key(i, kv->value);
		}
		if (!kv || ret) {
			fprintf(stderr, "token '%s' has no valid '%s' or '%s' value\n",
				s[i].section, INI_PUBLIC_KEY, INI_CERT);
			/*
			 * a bit nasty, but removing the section
			 * in-place means we have to do over the value
			 * of i we just used
			 */
			remove_section(i--);
			continue;
		}
	}
}