1 0fc65b37 2004-03-21 devnull #include <u.h>
2 0fc65b37 2004-03-21 devnull #include <libc.h>
3 0fc65b37 2004-03-21 devnull #include <mp.h>
4 0fc65b37 2004-03-21 devnull #include <libsec.h>
6 0fc65b37 2004-03-21 devnull #define STRLEN(s) (sizeof(s)-1)
9 1b1434eb 2004-12-26 devnull decodepem(char *s, char *type, int *len, char **news)
11 0fc65b37 2004-03-21 devnull uchar *d;
12 0fc65b37 2004-03-21 devnull char *t, *e, *tt;
15 0fc65b37 2004-03-21 devnull *len = 0;
18 0fc65b37 2004-03-21 devnull * find the correct section of the file, stripping garbage at the beginning and end.
19 0fc65b37 2004-03-21 devnull * the data is delimited by -----BEGIN <type>-----\n and -----END <type>-----\n
21 0fc65b37 2004-03-21 devnull n = strlen(type);
22 0fc65b37 2004-03-21 devnull e = strchr(s, '\0');
23 0fc65b37 2004-03-21 devnull for(t = s; t != nil && t < e; ){
25 0fc65b37 2004-03-21 devnull t = strchr(tt, '\n');
26 0fc65b37 2004-03-21 devnull if(t != nil)
28 0fc65b37 2004-03-21 devnull if(strncmp(tt, "-----BEGIN ", STRLEN("-----BEGIN ")) == 0
29 0fc65b37 2004-03-21 devnull && strncmp(&tt[STRLEN("-----BEGIN ")], type, n) == 0
30 0fc65b37 2004-03-21 devnull && strncmp(&tt[STRLEN("-----BEGIN ")+n], "-----\n", STRLEN("-----\n")) == 0)
33 0fc65b37 2004-03-21 devnull for(tt = t; tt != nil && tt < e; tt++){
34 0fc65b37 2004-03-21 devnull if(strncmp(tt, "-----END ", STRLEN("-----END ")) == 0
35 0fc65b37 2004-03-21 devnull && strncmp(&tt[STRLEN("-----END ")], type, n) == 0
36 0fc65b37 2004-03-21 devnull && strncmp(&tt[STRLEN("-----END ")+n], "-----\n", STRLEN("-----\n")) == 0)
38 0fc65b37 2004-03-21 devnull tt = strchr(tt, '\n');
39 0fc65b37 2004-03-21 devnull if(tt == nil)
42 0fc65b37 2004-03-21 devnull if(tt == nil || tt == e){
43 0fc65b37 2004-03-21 devnull werrstr("incorrect .pem file format: bad header or trailer");
44 0fc65b37 2004-03-21 devnull return nil;
48 1b1434eb 2004-12-26 devnull *news = tt+1;
49 0fc65b37 2004-03-21 devnull n = ((tt - t) * 6 + 7) / 8;
50 0fc65b37 2004-03-21 devnull d = malloc(n);
51 0fc65b37 2004-03-21 devnull if(d == nil){
52 0fc65b37 2004-03-21 devnull werrstr("out of memory");
53 0fc65b37 2004-03-21 devnull return nil;
55 0fc65b37 2004-03-21 devnull n = dec64(d, n, t, tt - t);
56 0fc65b37 2004-03-21 devnull if(n < 0){
58 0fc65b37 2004-03-21 devnull werrstr("incorrect .pem file format: bad base64 encoded data");
59 0fc65b37 2004-03-21 devnull return nil;
61 0fc65b37 2004-03-21 devnull *len = n;
62 0fc65b37 2004-03-21 devnull return d;
65 1b1434eb 2004-12-26 devnull PEMChain*
66 1b1434eb 2004-12-26 devnull decodepemchain(char *s, char *type)
68 1b1434eb 2004-12-26 devnull PEMChain *first = nil, *last = nil, *chp;
69 1b1434eb 2004-12-26 devnull uchar *d;
73 1b1434eb 2004-12-26 devnull e = strchr(s, '\0');
74 1b1434eb 2004-12-26 devnull while (s < e) {
75 1b1434eb 2004-12-26 devnull d = decodepem(s, type, &n, &s);
76 1b1434eb 2004-12-26 devnull if(d == nil)
78 1b1434eb 2004-12-26 devnull chp = malloc(sizeof(PEMChain));
79 1b1434eb 2004-12-26 devnull chp->next = nil;
80 1b1434eb 2004-12-26 devnull chp->pem = d;
81 1b1434eb 2004-12-26 devnull chp->pemlen = n;
82 1b1434eb 2004-12-26 devnull if (first == nil)
83 1b1434eb 2004-12-26 devnull first = chp;
85 1b1434eb 2004-12-26 devnull last->next = chp;
86 1b1434eb 2004-12-26 devnull last = chp;
88 1b1434eb 2004-12-26 devnull return first;