6 #define STRLEN(s) (sizeof(s)-1)
9 decodepem(char *s, char *type, int *len, char **news)
18 * find the correct section of the file, stripping garbage at the beginning and end.
19 * the data is delimited by -----BEGIN <type>-----\n and -----END <type>-----\n
23 for(t = s; t != nil && t < e; ){
28 if(strncmp(tt, "-----BEGIN ", STRLEN("-----BEGIN ")) == 0
29 && strncmp(&tt[STRLEN("-----BEGIN ")], type, n) == 0
30 && strncmp(&tt[STRLEN("-----BEGIN ")+n], "-----\n", STRLEN("-----\n")) == 0)
33 for(tt = t; tt != nil && tt < e; tt++){
34 if(strncmp(tt, "-----END ", STRLEN("-----END ")) == 0
35 && strncmp(&tt[STRLEN("-----END ")], type, n) == 0
36 && strncmp(&tt[STRLEN("-----END ")+n], "-----\n", STRLEN("-----\n")) == 0)
38 tt = strchr(tt, '\n');
42 if(tt == nil || tt == e){
43 werrstr("incorrect .pem file format: bad header or trailer");
49 n = ((tt - t) * 6 + 7) / 8;
52 werrstr("out of memory");
55 n = dec64(d, n, t, tt - t);
58 werrstr("incorrect .pem file format: bad base64 encoded data");
66 decodepemchain(char *s, char *type)
68 PEMChain *first = nil, *last = nil, *chp;
75 d = decodepem(s, type, &n, &s);
78 chp = malloc(sizeof(PEMChain));