aboutsummaryrefslogtreecommitdiff
path: root/src/libsec/port/decodepem.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2004-03-21 14:04:56 +0000
committerrsc <devnull@localhost>2004-03-21 14:04:56 +0000
commit0fc65b37a1e7585ca2347bf61dcb8bc3a6b146a4 (patch)
treedd9189a823998f494082adb769451f12be056566 /src/libsec/port/decodepem.c
parent768206abfcf505fb034a0151bf263bc0b1f2380c (diff)
downloadplan9port-0fc65b37a1e7585ca2347bf61dcb8bc3a6b146a4.tar.gz
plan9port-0fc65b37a1e7585ca2347bf61dcb8bc3a6b146a4.tar.bz2
plan9port-0fc65b37a1e7585ca2347bf61dcb8bc3a6b146a4.zip
Add most of libsec.
Diffstat (limited to 'src/libsec/port/decodepem.c')
-rw-r--r--src/libsec/port/decodepem.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/libsec/port/decodepem.c b/src/libsec/port/decodepem.c
new file mode 100644
index 00000000..194a4558
--- /dev/null
+++ b/src/libsec/port/decodepem.c
@@ -0,0 +1,61 @@
+#include <u.h>
+#include <libc.h>
+#include <mp.h>
+#include <libsec.h>
+
+#define STRLEN(s) (sizeof(s)-1)
+
+uchar*
+decodepem(char *s, char *type, int *len)
+{
+ uchar *d;
+ char *t, *e, *tt;
+ int n;
+
+ *len = 0;
+
+ /*
+ * find the correct section of the file, stripping garbage at the beginning and end.
+ * the data is delimited by -----BEGIN <type>-----\n and -----END <type>-----\n
+ */
+ n = strlen(type);
+ e = strchr(s, '\0');
+ for(t = s; t != nil && t < e; ){
+ tt = t;
+ t = strchr(tt, '\n');
+ if(t != nil)
+ t++;
+ if(strncmp(tt, "-----BEGIN ", STRLEN("-----BEGIN ")) == 0
+ && strncmp(&tt[STRLEN("-----BEGIN ")], type, n) == 0
+ && strncmp(&tt[STRLEN("-----BEGIN ")+n], "-----\n", STRLEN("-----\n")) == 0)
+ break;
+ }
+ for(tt = t; tt != nil && tt < e; tt++){
+ if(strncmp(tt, "-----END ", STRLEN("-----END ")) == 0
+ && strncmp(&tt[STRLEN("-----END ")], type, n) == 0
+ && strncmp(&tt[STRLEN("-----END ")+n], "-----\n", STRLEN("-----\n")) == 0)
+ break;
+ tt = strchr(tt, '\n');
+ if(tt == nil)
+ break;
+ }
+ if(tt == nil || tt == e){
+ werrstr("incorrect .pem file format: bad header or trailer");
+ return nil;
+ }
+
+ n = ((tt - t) * 6 + 7) / 8;
+ d = malloc(n);
+ if(d == nil){
+ werrstr("out of memory");
+ return nil;
+ }
+ n = dec64(d, n, t, tt - t);
+ if(n < 0){
+ free(d);
+ werrstr("incorrect .pem file format: bad base64 encoded data");
+ return nil;
+ }
+ *len = n;
+ return d;
+}