Debian Codegraph @

apps/app-internal.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #include <stdarg.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32
33 #include "app-internal.h"
34
35 static unsigned int verbosity = KCAPI_LOG_NONE;
36 static char appname[16];
37
38 static char hex_char(unsigned int bin, int u)
39 {
40 char hex_char_map_l[] = { '0', '1', '2', '3', '4', '5', '6', '7',
41 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
42 char hex_char_map_u[] = { '0', '1', '2', '3', '4', '5', '6', '7',
43 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
44 if (bin < sizeof(hex_char_map_l))
45 return (u) ? hex_char_map_u[bin] : hex_char_map_l[bin];
46 return 'X';
47 }
48
49 /**
50 * Convert binary string into hex representation
51 * @bin [in] input buffer with binary data
52 * @binlen [in] length of bin
53 * @hex [out] output buffer to store hex data
54 * @hexlen [in] length of already allocated hex buffer (should be at least
55 * twice binlen -- if not, only a fraction of binlen is converted)
56 * @u [in] case of hex characters (0=>lower case, 1=>upper case)
57 */
58 void bin2hex(const uint8_t *bin, size_t binlen,
59 char *hex, size_t hexlen, int u)
60 {
61 uint32_t i = 0;
62 size_t chars = (binlen > (hexlen / 2)) ? (hexlen / 2) : binlen;
63
64 for (i = 0; i < chars; i++) {
65 hex[(i*2)] = hex_char((bin[i] >> 4), u);
66 hex[((i*2)+1)] = hex_char((bin[i] & 0x0f), u);
67 }
68 }
69
70 void bin2print(const uint8_t *bin, size_t binlen,
71 const char *filename, FILE *outfile, uint32_t lfcr)
72 {
73 char *hex;
74 size_t hexlen = binlen * 2 + 1;
75
76 hex = calloc(1, hexlen);
77 if (!hex)
78 return;
79 bin2hex(bin, binlen, hex, hexlen - 1 , 0);
80 /* fipshmac does not want the file name :-( */
81 if (outfile != stdout)
82 fprintf(outfile, "%s", hex);
83 else
84 if (filename)
85 fprintf(outfile, "%s %s", hex, filename);
86 else
87 fprintf(outfile, "%s", hex);
88
89 if (lfcr == 1)
90 fputc(0x0a, outfile);
91 if (lfcr == 2)
92 fputc(0x00, outfile);
93
94 free(hex);
95 }
96
97 void dolog(enum kcapi_verbosity severity, const char *fmt, ...)
98 {
99 va_list args;
100 char msg[1024];
101 char sev[16];
102
103 if (severity > verbosity)
104 return;
105
106 memset(sev, 0, sizeof(sev));
107 memset(msg, 0, sizeof(msg));
108
109 va_start(args, fmt);
110 vsnprintf(msg, sizeof(msg) - 1, fmt, args);
111 va_end(args);
112
113 switch (severity) {
114 case KCAPI_LOG_DEBUG:
115 snprintf(sev, sizeof(sev), "Debug");
116 break;
117 case KCAPI_LOG_VERBOSE:
118 snprintf(sev, sizeof(sev), "Verbose");
119 break;
120 case KCAPI_LOG_WARN:
121 snprintf(sev, sizeof(sev), "Warning");
122 break;
123 case KCAPI_LOG_ERR:
124 snprintf(sev, sizeof(sev), "Error");
125 break;
126 default:
127 snprintf(sev, sizeof(sev), "Unknown");
128 }
129 fprintf(stderr, "%s - %s: %s\n", appname, sev, msg);
130 }
131
132 void dolog_bin(enum kcapi_verbosity severity,
133 const uint8_t *bin, uint32_t binlen, const char *explanation)
134 {
135 char *hex;
136 uint32_t hexlen = binlen * 2 + 1;
137
138 hex = calloc(1, hexlen);
139 if (!hex)
140 return;
141 bin2hex(bin, binlen, hex, hexlen - 1 , 0);
142 dolog(severity, "%s: %s", explanation, hex);
143 free(hex);
144 }
145
146 void set_verbosity(const char *name, enum kcapi_verbosity level)
147 {
148 strncpy(appname, name, sizeof(appname) - 1);
149 appname[sizeof(appname) - 1] = '\0';
150 kcapi_set_verbosity(level);
151 verbosity = level;
152 }
153
154 static uint8_t bin_char(char hex)
155 {
156 if (48 <= hex && 57 >= hex)
157 return (uint8_t)(hex - 48);
158 if (65 <= hex && 70 >= hex)
159 return (uint8_t)(hex - 55);
160 if (97 <= hex && 102 >= hex)
161 return (uint8_t)(hex - 87);
162 return 0;
163 }
164
165 /**
166 * Convert hex representation into binary string
167 * @hex input buffer with hex representation
168 * @hexlen length of hex
169 * @bin output buffer with binary data
170 * @binlen length of already allocated bin buffer (should be at least
171 * half of hexlen -- if not, only a fraction of hexlen is converted)
172 */
173 void hex2bin(const char *hex, uint32_t hexlen, uint8_t *bin, uint32_t binlen)
174 {
175 uint32_t i;
176 uint32_t chars = (binlen > (hexlen / 2)) ? (hexlen / 2) : binlen;
177
178 /*
179 * handle odd-length of strings where the first digit is the least
180 * significant nibble
181 */
182 if (hexlen & 1) {
183 bin[0] = bin_char(hex[0]);
184 bin++;
185 hex++;
186 }
187
188 for (i = 0; i < chars; i++) {
189 bin[i] = (uint8_t)(bin_char(hex[(i*2)]) << 4);
190 bin[i] |= bin_char(hex[((i*2)+1)]);
191 }
192 }
193
194 /**
195 * Allocate sufficient space for binary representation of hex
196 * and convert hex into bin
197 *
198 * Caller must free bin
199 * @hex input buffer with hex representation
200 * @hexlen length of hex
201 * @bin return value holding the pointer to the newly allocated buffer
202 * @binlen return value holding the allocated size of bin
203 *
204 * return: 0 on success, !0 otherwise
205 */
206 int hex2bin_alloc(const char *hex, uint32_t hexlen,
207 uint8_t **bin, uint32_t *binlen)
208 {
209 uint8_t *out = NULL;
210 uint32_t outlen = 0;
211 int ret;
212
213 if (!hexlen)
214 return -EINVAL;
215
216 outlen = (hexlen + 1) / 2;
217
218 ret = posix_memalign((void *)&out, 16, outlen);
219 if (ret)
220 return -ret;
221
222 hex2bin(hex, hexlen, out, outlen);
223 *bin = out;
224 *binlen = outlen;
225 return 0;
226 }
227
228 ssize_t read_complete(int fd, uint8_t *buf, size_t buflen)
229 {
230 ssize_t ret;
231 ssize_t rc = 0;
232
233 if (buflen > INT_MAX)
234 return -EINVAL;
235
236 do {
237 ret = read(fd, buf, buflen);
238 if (0 < ret) {
239 buflen -= (size_t)ret;
240 buf += ret;
241 }
242 rc += ret;
243 if (ret)
244 break;
245 } while ((0 < ret || EINTR == errno || ERESTART == errno)
246 && buflen > 0);
247
248 return rc;
249 }
250
251 int check_filetype(int fd, struct stat *sb, const char *filename)
252 {
253 int ret = fstat(fd, sb);
254 if (ret) {
255 dolog(KCAPI_LOG_ERR,
256 "fstat() failed: %s", strerror(errno));
257 return -errno;
258 }
259
260 /* Do not return an error in case we cannot validate the data. */
261 if ((sb->st_mode & S_IFMT) != S_IFREG &&
262 (sb->st_mode & S_IFMT) != S_IFLNK) {
263 dolog(KCAPI_LOG_ERR,
264 "%s is no regular file or symlink", filename);
265 return -EINVAL;
266 }
267
268 return 0;
269 }