push
[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 #include <string.h>
22 #include <stdio.h>
23
24 /* Pack Key
25 * - remove -----xy----- lines
26 * - remove line breaks
27 */
28 char *crypt_rsa_packkey(char *key) {
29         char *packed = malloc(strlen(key));
30         
31         int remove_line = 0;
32         int linenum = 0;
33         int i, j = 0;
34         for(i = 0; key[i]; i++) {
35                 if(key[i] == '\r')
36                         continue;
37                 if(key[i] == '\n') {
38                         if(linenum == 0) {
39                                 key[i] = 0;
40                                 if(match("*PRIVATE KEY*", key))
41                                         packed[j++] = '1';
42                                 else
43                                         packed[j++] = '0';
44                                 key[i] = '\n';
45                         }
46                         linenum++;
47                         remove_line = 0;
48                         continue;
49                 }
50                 if(remove_line)
51                         continue;
52                 if(key[i] == '-') {
53                         remove_line = 1;
54                         continue;
55                 }
56                 packed[j++] = key[i];
57         }
58         return packed;
59 }
60
61 char *crypt_rsa_unpackkey(char *packed) {
62         int size = strlen(packed);
63         char *key = malloc(size + (size/64)+1 + 128);
64         int kpos = 0;
65         if(packed[0] == '1')
66                 kpos += sprintf(key + kpos, "-----BEGIN RSA PRIVATE KEY-----");
67         else
68                 kpos += sprintf(key + kpos, "-----BEGIN PUBLIC KEY-----");
69         
70         int i;
71         for(i = 1; i < size; i++) {
72                 if((i-1) % 64 == 0)
73                         key[kpos++] = '\n';
74                 key[kpos++] = packed[i];
75         }
76         
77         if(packed[0] == '1')
78                 kpos += sprintf(key + kpos, "-----END RSA PRIVATE KEY-----\n");
79         else
80                 kpos += sprintf(key + kpos, "-----END PUBLIC KEY-----\n");
81         
82         return key;
83 }