1 #include <u.h> 2 #include <libc.h> 3 #include <auth.h> 4 #include <mp.h> 5 #include <libsec.h> 6 7 static char* 8 readfile(char *name) 9 { 10 int fd; 11 char *s; 12 Dir *d; 13 14 fd = open(name, OREAD); 15 if(fd < 0) 16 return nil; 17 if((d = dirfstat(fd)) == nil) 18 return nil; 19 s = malloc(d->length + 1); 20 if(s == nil || readn(fd, s, d->length) != d->length){ 21 free(s); 22 free(d); 23 close(fd); 24 return nil; 25 } 26 close(fd); 27 s[d->length] = '\0'; 28 free(d); 29 return s; 30 } 31 32 uchar* 33 readcert(char *filename, int *pcertlen) 34 { 35 char *pem; 36 uchar *binary; 37 38 pem = readfile(filename); 39 if(pem == nil){ 40 werrstr("can't read %s", filename); 41 return nil; 42 } 43 binary = decodePEM(pem, "CERTIFICATE", pcertlen, nil); 44 free(pem); 45 if(binary == nil){ 46 werrstr("can't parse %s", filename); 47 return nil; 48 } 49 return binary; 50 } 51 52 PEMChain * 53 readcertchain(char *filename) 54 { 55 char *chfile; 56 PEMChain *chp; 57 58 chfile = readfile(filename); 59 if (chfile == nil) { 60 werrstr("can't read %s", filename); 61 return nil; 62 } 63 chp = decodepemchain(chfile, "CERTIFICATE"); 64 return chp; 65 } 66 67