#include <sys/cdefs.h>
#define NEED_BRSSL_H
#include <libsecureboot.h>
#include <brssl.h>
#include "decode.h"
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;
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
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>
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;
}
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