]>
Commit | Line | Data |
---|---|---|
0fe5f3c5 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2016 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <linux/if_alg.h> | |
21 | #include <stdbool.h> | |
22 | #include <sys/socket.h> | |
23 | ||
24 | #include "alloc-util.h" | |
25 | #include "fd-util.h" | |
26 | #include "hexdecoct.h" | |
27 | #include "khash.h" | |
28 | #include "macro.h" | |
29 | #include "missing.h" | |
30 | #include "string-util.h" | |
31 | #include "util.h" | |
32 | ||
33 | /* On current kernels the maximum digest (according to "grep digestsize /proc/crypto | sort -u") is actually 32, but | |
34 | * let's add some extra room, the few wasted bytes don't really matter... */ | |
35 | #define LONGEST_DIGEST 128 | |
36 | ||
37 | struct khash { | |
38 | int fd; | |
39 | char *algorithm; | |
40 | uint8_t digest[LONGEST_DIGEST+1]; | |
41 | size_t digest_size; | |
42 | bool digest_valid; | |
43 | }; | |
44 | ||
45 | int khash_new_with_key(khash **ret, const char *algorithm, const void *key, size_t key_size) { | |
46 | union { | |
47 | struct sockaddr sa; | |
48 | struct sockaddr_alg alg; | |
49 | } sa = { | |
50 | .alg.salg_family = AF_ALG, | |
51 | .alg.salg_type = "hash", | |
52 | }; | |
53 | ||
54 | _cleanup_(khash_unrefp) khash *h = NULL; | |
55 | _cleanup_close_ int fd = -1; | |
56 | ssize_t n; | |
57 | ||
58 | assert(ret); | |
59 | assert(key || key_size == 0); | |
60 | ||
61 | /* Filter out an empty algorithm early, as we do not support an algorithm by that name. */ | |
62 | if (isempty(algorithm)) | |
63 | return -EINVAL; | |
64 | ||
65 | /* Overly long hash algorithm names we definitely do not support */ | |
66 | if (strlen(algorithm) >= sizeof(sa.alg.salg_name)) | |
67 | return -EOPNOTSUPP; | |
68 | ||
69 | fd = socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0); | |
70 | if (fd < 0) | |
71 | return -errno; | |
72 | ||
73 | strcpy((char*) sa.alg.salg_name, algorithm); | |
74 | if (bind(fd, &sa.sa, sizeof(sa)) < 0) { | |
75 | if (errno == ENOENT) | |
76 | return -EOPNOTSUPP; | |
77 | return -errno; | |
78 | } | |
79 | ||
80 | if (key) { | |
81 | if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, key, key_size) < 0) | |
82 | return -errno; | |
83 | } | |
84 | ||
85 | h = new0(khash, 1); | |
86 | if (!h) | |
87 | return -ENOMEM; | |
88 | ||
89 | h->fd = accept4(fd, NULL, 0, SOCK_CLOEXEC); | |
90 | if (h->fd < 0) | |
91 | return -errno; | |
92 | ||
93 | h->algorithm = strdup(algorithm); | |
94 | if (!h->algorithm) | |
95 | return -ENOMEM; | |
96 | ||
97 | /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */ | |
98 | (void) send(h->fd, NULL, 0, 0); | |
99 | ||
100 | /* Figure out the digest size */ | |
101 | n = recv(h->fd, h->digest, sizeof(h->digest), 0); | |
102 | if (n < 0) | |
103 | return -errno; | |
104 | if (n >= LONGEST_DIGEST) /* longer than what we expected? If so, we don't support this */ | |
105 | return -EOPNOTSUPP; | |
106 | ||
107 | h->digest_size = (size_t) n; | |
108 | h->digest_valid = true; | |
109 | ||
110 | /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */ | |
111 | (void) send(h->fd, NULL, 0, 0); | |
112 | ||
113 | *ret = h; | |
114 | h = NULL; | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | int khash_new(khash **ret, const char *algorithm) { | |
120 | return khash_new_with_key(ret, algorithm, NULL, 0); | |
121 | } | |
122 | ||
123 | khash* khash_unref(khash *h) { | |
124 | if (!h) | |
125 | return NULL; | |
126 | ||
127 | safe_close(h->fd); | |
128 | free(h->algorithm); | |
129 | free(h); | |
130 | ||
131 | return NULL; | |
132 | } | |
133 | ||
134 | int khash_dup(khash *h, khash **ret) { | |
135 | _cleanup_(khash_unrefp) khash *copy = NULL; | |
136 | ||
137 | assert(h); | |
138 | assert(ret); | |
139 | ||
140 | copy = newdup(khash, h, 1); | |
141 | if (!copy) | |
142 | return -ENOMEM; | |
143 | ||
144 | copy->fd = -1; | |
145 | copy->algorithm = strdup(h->algorithm); | |
ef1fd941 | 146 | if (!copy->algorithm) |
0fe5f3c5 LP |
147 | return -ENOMEM; |
148 | ||
149 | copy->fd = accept4(h->fd, NULL, 0, SOCK_CLOEXEC); | |
150 | if (copy->fd < 0) | |
151 | return -errno; | |
152 | ||
153 | *ret = copy; | |
154 | copy = NULL; | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | const char *khash_get_algorithm(khash *h) { | |
160 | assert(h); | |
161 | ||
162 | return h->algorithm; | |
163 | } | |
164 | ||
165 | size_t khash_get_size(khash *h) { | |
166 | assert(h); | |
167 | ||
168 | return h->digest_size; | |
169 | } | |
170 | ||
171 | int khash_reset(khash *h) { | |
172 | ssize_t n; | |
173 | ||
174 | assert(h); | |
175 | ||
176 | n = send(h->fd, NULL, 0, 0); | |
177 | if (n < 0) | |
178 | return -errno; | |
179 | ||
180 | h->digest_valid = false; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | int khash_put(khash *h, const void *buffer, size_t size) { | |
186 | ssize_t n; | |
187 | ||
188 | assert(h); | |
189 | assert(buffer || size == 0); | |
190 | ||
191 | if (size <= 0) | |
192 | return 0; | |
193 | ||
194 | n = send(h->fd, buffer, size, MSG_MORE); | |
195 | if (n < 0) | |
196 | return -errno; | |
197 | ||
198 | h->digest_valid = false; | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
203 | int khash_put_iovec(khash *h, const struct iovec *iovec, size_t n) { | |
204 | struct msghdr mh = { | |
205 | mh.msg_iov = (struct iovec*) iovec, | |
206 | mh.msg_iovlen = n, | |
207 | }; | |
208 | ssize_t k; | |
209 | ||
210 | assert(h); | |
211 | assert(iovec || n == 0); | |
212 | ||
213 | if (n <= 0) | |
214 | return 0; | |
215 | ||
216 | k = sendmsg(h->fd, &mh, MSG_MORE); | |
217 | if (k < 0) | |
218 | return -errno; | |
219 | ||
220 | h->digest_valid = false; | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | static int retrieve_digest(khash *h) { | |
226 | ssize_t n; | |
227 | ||
228 | assert(h); | |
229 | ||
230 | if (h->digest_valid) | |
231 | return 0; | |
232 | ||
233 | n = recv(h->fd, h->digest, h->digest_size, 0); | |
234 | if (n < 0) | |
235 | return n; | |
236 | if ((size_t) n != h->digest_size) /* digest size changed? */ | |
237 | return -EIO; | |
238 | ||
239 | h->digest_valid = true; | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | int khash_digest_data(khash *h, const void **ret) { | |
245 | int r; | |
246 | ||
247 | assert(h); | |
248 | assert(ret); | |
249 | ||
250 | r = retrieve_digest(h); | |
251 | if (r < 0) | |
252 | return r; | |
253 | ||
254 | *ret = h->digest; | |
255 | return 0; | |
256 | } | |
257 | ||
258 | int khash_digest_string(khash *h, char **ret) { | |
259 | int r; | |
260 | char *p; | |
261 | ||
262 | assert(h); | |
263 | assert(ret); | |
264 | ||
265 | r = retrieve_digest(h); | |
266 | if (r < 0) | |
267 | return r; | |
268 | ||
269 | p = hexmem(h->digest, h->digest_size); | |
270 | if (!p) | |
271 | return -ENOMEM; | |
272 | ||
273 | *ret = p; | |
274 | return 0; | |
275 | } |