--- /dev/null
+/* crypt_base64.c - NextIRCd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "crypt_base64.h"
+#include <stdint.h>
+#include <stdlib.h>
+
+static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/'};
+static char *decoding_table = NULL;
+static int mod_table[] = {0, 2, 1};
+
+
+char *crypt_base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) {
+ *output_length = 4 * ((input_length + 2) / 3);
+
+ char *encoded_data = malloc(*output_length);
+ if (encoded_data == NULL)
+ return NULL;
+
+ for (int i = 0, j = 0; i < input_length;) {
+ uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
+ uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
+ uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
+
+ uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
+
+ encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
+ encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
+ encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
+ encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
+ }
+
+ for (int i = 0; i < mod_table[input_length % 3]; i++)
+ encoded_data[*output_length - 1 - i] = '=';
+
+ return encoded_data;
+}
+
+
+unsigned char *crypt_base64_decode(const char *data, size_t input_length, size_t *output_length) {
+ if (decoding_table == NULL)
+ crypt_base64_init();
+ if (input_length % 4 != 0)
+ return NULL;
+
+ *output_length = input_length / 4 * 3;
+ if (data[input_length - 1] == '=') (*output_length)--;
+ if (data[input_length - 2] == '=') (*output_length)--;
+
+ unsigned char *decoded_data = malloc(*output_length);
+ if (decoded_data == NULL)
+ return NULL;
+
+ for (int i = 0, j = 0; i < input_length;) {
+ uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+ uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+ uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+ uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+
+ uint32_t triple = (sextet_a << 3 * 6)
+ + (sextet_b << 2 * 6)
+ + (sextet_c << 1 * 6)
+ + (sextet_d << 0 * 6);
+
+ if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
+ if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
+ if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
+ }
+
+ return decoded_data;
+}
+
+
+void crypt_base64_init() {
+ decoding_table = malloc(256);
+ for (int i = 0; i < 64; i++)
+ decoding_table[(unsigned char) encoding_table[i]] = i;
+}
+
+void crypt_base64_deinit() {
+ free(decoding_table);
+}
--- /dev/null
+/* crypt_base64.h - NextIRCd
+ * Copyright (C) 2012-2013 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _crypt_base64_h
+#define _crypt_base64_h
+
+void crypt_base64_init();
+void crypt_base64_deinit();
+
+char *crypt_base64_encode(const unsigned char *data, size_t input_length, size_t *output_length);
+unsigned char *crypt_base64_decode(const char *data, size_t input_length, size_t *output_length);
+
+#endif
--- /dev/null
+/* crypt_keypacker.c - NextIRCd
+ * Copyright (C) 2012-2013 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "crypt_keypacker.h"
+#include "tools.h"
+#include <stdlib.h>
+
+/* Pack Key
+* - remove -----xy----- lines
+* - remove line breaks
+*/
+char *crypt_rsa_packkey(char *key) {
+ char *packed = malloc(strlen(key));
+
+ int remove_line = 0;
+ int linenum = 0;
+ int i, j = 0;
+ for(i = 0; key[i]; i++) {
+ if(key[i] == '\r')
+ continue;
+ if(key[i] == '\n') {
+ if(linenum == 0) {
+ key[i] = 0;
+ if(match("*PRIVATE KEY*", key))
+ packed[j++] = '1';
+ else
+ packed[j++] = '0';
+ key[i] = '\n';
+ }
+ linenum++;
+ remove_line = 0;
+ continue;
+ }
+ if(remove_line)
+ continue;
+ if(key[i] == '-') {
+ remove_line = 1;
+ continue;
+ }
+ packed[j++] = key[i];
+ }
+ return packed;
+}
+
+char *crypt_rsa_unpackkey(char *packed) {
+ int size = strlen(packed);
+ char *key = malloc(size + (size/64)+1 + 128);
+ int kpos = 0;
+ if(packed[0] == '1')
+ kpos += sprintf(key + kpos, "-----BEGIN RSA PRIVATE KEY-----");
+ else
+ kpos += sprintf(key + kpos, "-----BEGIN PUBLIC KEY-----");
+
+ int i;
+ for(i = 1; i < size; i++) {
+ if((i-1) % 64 == 0)
+ key[kpos++] = '\n';
+ key[kpos++] = packed[i];
+ }
+
+ if(packed[0] == '1')
+ kpos += sprintf(key + kpos, "-----END RSA PRIVATE KEY-----\n");
+ else
+ kpos += sprintf(key + kpos, "-----END PUBLIC KEY-----\n");
+
+ return key;
+}
--- /dev/null
+/* crypt_keypacker.h - NextIRCd
+ * Copyright (C) 2012-2013 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _crypt_keypacker_h
+#define _crypt_keypacker_h
+
+char *crypt_rsa_packkey(char *key);
+char *crypt_rsa_unpackkey(char *key);
+
+#endif