URI:
       treadcert.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       treadcert.c (1026B)
       ---
            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 
           53 PEMChain *
           54 readcertchain(char *filename)
           55 {
           56         char *chfile;
           57         PEMChain *chp;
           58 
           59         chfile = readfile(filename);
           60         if (chfile == nil) {
           61                 werrstr("can't read %s", filename);
           62                 return nil;
           63         }
           64         chp = decodepemchain(chfile, "CERTIFICATE");
           65         return chp;
           66 }