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
|
/*-
* Copyright (c) 2018, Juniper Networks, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#define NEED_BRSSL_H
#include <libsecureboot.h>
#include <brssl.h>
#include "decode.h"
/**
* @brief decode ascii armor
*
* once we get rid of the trailing checksum
* we can treat as PEM.
*
* @sa rfc4880:6.2
*/
unsigned char *
dearmor(char *pem, size_t nbytes, size_t *len)
{
#ifdef USE_BEARSSL
pem_object *po;
size_t npo;
#else
BIO *bp;
char *name = NULL;
char *header = NULL;
#endif
unsigned char *data = NULL;
char *cp;
char *ep;
/* we need to remove the Armor tail */
if ((cp = strstr((char *)pem, "\n=")) &&
(ep = strstr(cp, "\n---"))) {
memmove(cp, ep, nbytes - (size_t)(ep - pem));
nbytes -= (size_t)(ep - cp);
pem[nbytes] = '\0';
}
#ifdef USE_BEARSSL
/* we also need to remove any headers */
if ((cp = strstr((char *)pem, "---\n")) &&
(ep = strstr(cp, "\n\n"))) {
cp += 4;
ep += 2;
memmove(cp, ep, nbytes - (size_t)(ep - pem));
nbytes -= (size_t)(ep - cp);
pem[nbytes] = '\0';
}
if ((po = decode_pem(pem, nbytes, &npo))) {
data = po->data;
*len = po->data_len;
}
#else
if ((bp = BIO_new_mem_buf(pem, (int)nbytes))) {
long llen = (long)nbytes;
if (!PEM_read_bio(bp, &name, &header, &data, &llen))
data = NULL;
BIO_free(bp);
*len = (size_t)llen;
}
#endif
return (data);
}
#ifdef MAIN_DEARMOR
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
/*
* Mostly a unit test.
*/
int
main(int argc, char *argv[])
{
const char *infile, *outfile;
unsigned char *data;
size_t n, x;
int fd;
int o;
infile = outfile = NULL;
while ((o = getopt(argc, argv, "i:o:")) != -1) {
switch (o) {
case 'i':
infile = optarg;
break;
case 'o':
outfile = optarg;
break;
default:
errx(1, "unknown option: -%c", o);
}
}
if (!infile)
errx(1, "need -i infile");
if (outfile) {
if ((fd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC)) < 0)
err(1, "cannot open %s", outfile);
} else {
fd = 1; /* stdout */
}
data = read_file(infile, &n);
if (!(data[0] & OPENPGP_TAG_ISTAG))
data = dearmor(data, n, &n);
for (x = 0; x < n; ) {
o = write(fd, &data[x], (n - x));
if (o < 0)
err(1, "cannot write");
x += o;
}
if (fd != 1)
close(fd);
free(data);
return (0);
}
#endif
|