-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.c
167 lines (143 loc) · 3.78 KB
/
util.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
161
162
163
164
165
166
167
/*
* Copyright 2021 The HIBA Authors
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "log.h"
#include "hiba.h"
#include "misc.h"
#include "openbsd-compat/bsd-misc.h"
#include "ssherr.h"
#include "sshkey.h"
#include "xmalloc.h"
void
decode_file(char *file, struct hibacert **outcert, struct hibaext **outext) {
FILE *f = NULL;
int ret;
int direct = 0;
struct stat st;
size_t nbytes = 0;
ssize_t linesize = 0;
struct sshkey *key = NULL;
char *line = NULL;
if (!outcert || !outext)
return;
*outcert = NULL;
*outext = NULL;
if (strcmp(file, "-") != 0 && stat(file, &st) == -1) {
if ((errno != ENOENT) && (errno != ENAMETOOLONG)) {
fatal("decode_file: %s: %s", file, strerror(errno));
}
debug2("decode_file: reading from command line");
linesize = strlen(file);
line = file;
direct = 1;
} else if (strcmp(file, "-") == 0) {
f = stdin;
} else if ((f = fopen(file, "r")) == NULL) {
fatal("decode_file: fopen %s: %s", file, strerror(errno));
}
while (direct || (linesize = getline(&line, &nbytes, f)) != -1) {
char *cp;
struct sshbuf *d;
sshkey_free(key);
key = NULL;
/* Trim leading space and comments */
cp = line + strspn(line, " \t");
linesize -= (cp - line);
if (*cp == '#' || *cp == '\0')
continue;
if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
fatal("decode_file: sshkey_new");
/* Try to read input as a wrapper base64 key. */
ret = sshkey_read(key, &cp);
debug3("decode_file: sshkey_read returned %d: %s", ret, ssh_err(ret));
if (ret < 0) {
/* Try to read input as a base64 key blob. */
d = sshbuf_new();
if (sshbuf_b64tod(d, cp) == 0) {
sshkey_free(key);
ret = sshkey_fromb(d, &key);
debug3("decode_file: sshkey_fromb returned %d: %s", ret, ssh_err(ret));
}
sshbuf_free(d);
}
if (ret == 0) {
/* Maybe a certificate. */
struct hibacert *cert;
if (!sshkey_is_cert(key))
fatal("decode_file: provided file is not a certificate nor a HIBA extension");
cert = hibacert_new();
if ((ret = hibacert_parse(cert, key)) < 0)
fatal("decode_file: failed to decode hiba extension from cert: %s", hiba_err(ret));
*outcert = cert;
} else {
/* Maybe a HIBA extension. */
struct sshbuf *buf = sshbuf_from(cp, linesize);
struct hibaext *ext = NULL;
sshkey_free(key);
key = NULL;
ext = hibaext_new();
if ((ret = hibaext_decode(ext, buf)) < 0)
fatal("decode_file: failed to decode hiba extension: %s", hiba_err(ret));
*outext = ext;
sshbuf_free(buf);
}
if (direct) {
line = NULL;
break;
}
}
if (f)
fclose(f);
free(line);
}
#define CHUNK_SZ 1024
void
open_grl(const char *file, unsigned char **ptr, u_int64_t *sz, int *mmapped) {
int f;
struct stat st;
if (!file || !sz || !ptr || !mmapped)
return;
*sz = 0;
*ptr = NULL;
*mmapped = 0;
if (strcmp(file, "-") != 0 && stat(file, &st) == -1) {
debug3("open_grl: %s: %s", file, strerror(errno));
return;
} else if (strcmp(file, "-") == 0) {
while (1) {
*ptr = xreallocarray(*ptr, *sz + CHUNK_SZ, sizeof(char));
*sz += fread(*ptr + *sz, 1, CHUNK_SZ, stdin);
if (ferror(stdin)) {
fatal("open_grl: read stdin: %s", strerror(errno));
} else if (feof(stdin) != 0) {
return;
}
}
} else if ((f = open(file, O_RDONLY)) < 0) {
fatal("open_grl: open %s: %s", file, strerror(errno));
}
*ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, f, 0);
*mmapped = 1;
*sz = st.st_size;
close(f);
}
void
close_grl(unsigned char *ptr, u_int64_t sz, int mmapped) {
if (mmapped) {
munmap(ptr, sz);
} else {
free(ptr);
}
}