added base64 en-/decoder and keypacker
[NextIRCd.git] / src / crypt_keypacker.c
1 /* crypt_keypacker.c - NextIRCd
2  * Copyright (C) 2012-2013  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "crypt_keypacker.h"
19 #include "tools.h"
20 #include <stdlib.h>
21
22 /* Pack Key
23 * - remove -----xy----- lines
24 * - remove line breaks
25 */
26 char *crypt_rsa_packkey(char *key) {
27         char *packed = malloc(strlen(key));
28         
29         int remove_line = 0;
30         int linenum = 0;
31         int i, j = 0;
32         for(i = 0; key[i]; i++) {
33                 if(key[i] == '\r')
34                         continue;
35                 if(key[i] == '\n') {
36                         if(linenum == 0) {
37                                 key[i] = 0;
38                                 if(match("*PRIVATE KEY*", key))
39                                         packed[j++] = '1';
40                                 else
41                                         packed[j++] = '0';
42                                 key[i] = '\n';
43                         }
44                         linenum++;
45                         remove_line = 0;
46                         continue;
47                 }
48                 if(remove_line)
49                         continue;
50                 if(key[i] == '-') {
51                         remove_line = 1;
52                         continue;
53                 }
54                 packed[j++] = key[i];
55         }
56         return packed;
57 }
58
59 char *crypt_rsa_unpackkey(char *packed) {
60         int size = strlen(packed);
61         char *key = malloc(size + (size/64)+1 + 128);
62         int kpos = 0;
63         if(packed[0] == '1')
64                 kpos += sprintf(key + kpos, "-----BEGIN RSA PRIVATE KEY-----");
65         else
66                 kpos += sprintf(key + kpos, "-----BEGIN PUBLIC KEY-----");
67         
68         int i;
69         for(i = 1; i < size; i++) {
70                 if((i-1) % 64 == 0)
71                         key[kpos++] = '\n';
72                 key[kpos++] = packed[i];
73         }
74         
75         if(packed[0] == '1')
76                 kpos += sprintf(key + kpos, "-----END RSA PRIVATE KEY-----\n");
77         else
78                 kpos += sprintf(key + kpos, "-----END PUBLIC KEY-----\n");
79         
80         return key;
81 }