2016-11-25 17:18:40 +01:00
|
|
|
#include "os.h"
|
|
|
|
#include <libsec.h>
|
|
|
|
|
|
|
|
static char*
|
|
|
|
readfile(char *name)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char *s;
|
|
|
|
Dir *d;
|
|
|
|
|
2019-11-26 02:25:23 +01:00
|
|
|
fd = sys_open(name, OREAD);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(fd < 0)
|
|
|
|
return nil;
|
2017-04-19 23:33:14 +02:00
|
|
|
if((d = jehanne_dirfstat(fd)) == nil) {
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_close(fd);
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
}
|
2017-04-19 23:33:14 +02:00
|
|
|
s = jehanne_malloc(d->length + 1);
|
|
|
|
if(s == nil || jehanne_readn(fd, s, d->length) != d->length){
|
|
|
|
jehanne_free(s);
|
|
|
|
jehanne_free(d);
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_close(fd);
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
}
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_close(fd);
|
2016-11-25 17:18:40 +01:00
|
|
|
s[d->length] = '\0';
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_free(d);
|
2016-11-25 17:18:40 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t*
|
|
|
|
readcert(char *filename, int *pcertlen)
|
|
|
|
{
|
|
|
|
char *pem;
|
|
|
|
uint8_t *binary;
|
|
|
|
|
|
|
|
pem = readfile(filename);
|
|
|
|
if(pem == nil){
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_werrstr("can't read %s: %r", filename);
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
binary = decodePEM(pem, "CERTIFICATE", pcertlen, nil);
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_free(pem);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(binary == nil){
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_werrstr("can't parse %s", filename);
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return binary;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEMChain *
|
|
|
|
readcertchain(char *filename)
|
|
|
|
{
|
|
|
|
char *chfile;
|
|
|
|
|
|
|
|
chfile = readfile(filename);
|
|
|
|
if (chfile == nil) {
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_werrstr("can't read %s: %r", filename);
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return decodepemchain(chfile, "CERTIFICATE");
|
|
|
|
}
|
|
|
|
|