ports

morpheus ports
git clone git://git.2f30.org/ports
Log | Files | Refs | LICENSE

certdata2pem.c (4491B)


      1 /* Copyright (C) 2013, Felix Janda <felix.janda@posteo.de>
      2 
      3 Permission to use, copy, modify, and/or distribute this software for
      4 any purpose with or without fee is hereby granted, provided that the
      5 above copyright notice and this permission notice appear in all copies.
      6 
      7 SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     10 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     12 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     13 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     14 */
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <err.h>
     20 
     21 void xwrite(FILE *f, void *p, size_t size)
     22 {
     23   if (fwrite(p, 1, size, f) != size) err(1, 0);
     24 }
     25 
     26 int main(void)
     27 {
     28   FILE *f;
     29   char cert[4096], ecert[4096*4/3 + 100];
     30   char *line = 0, *tmp, *filename, *label, *pcert = 0;
     31   ssize_t len;
     32   size_t size, certsize;
     33   int trust;
     34   char **blacklist = 0, **node;
     35 
     36   filename = "./blacklist.txt";
     37   if (!(f = fopen(filename, "r"))) err(1, "%s", filename);
     38   while ((len = getline(&line, &size, f)) != -1) {
     39     if ((line[0] != '#') && (len > 1)) {
     40       if (!(node = malloc(sizeof(void*) + len))) err(1, 0);
     41       *node = (char*)blacklist;
     42       memcpy(node + 1, line, len);
     43       blacklist = node;
     44     }
     45   }
     46   fclose(f);
     47 
     48   filename = "./certdata.txt";
     49   if (!(f = fopen(filename, "r"))) err(1, "%s", filename);
     50   while ((len = getline(&line, &size, f)) != -1) {
     51     tmp = line;
     52     if (line[0] == '#') continue;
     53     if (pcert) {
     54       if (!strcmp(line, "END\n")) {
     55         char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     56                        "abcdefghijklmnopqrstuvwxyz0123456789+/";
     57         size_t i, j, k, val;
     58 
     59         for (i = 0, val = 0, tmp = ecert; i < (size_t)(pcert - cert); i++) {
     60           val = (val << 8) + (unsigned char)cert[i];
     61           if (i % 3 == 2) {
     62             for (j = 0; j < 4; j++, val >>= 6) tmp[3 - j] = base64[val & 0x3f];
     63             tmp += 4;
     64           }
     65           if (i && !(i % 48)) {
     66             *tmp = '\n';
     67             tmp++;
     68           }
     69         }
     70         if (k = i % 3) {
     71           tmp[2] = '=';
     72           tmp[3] = '=';
     73           val <<= 6 - 2*k;
     74           for (j = 0; j < k + 1; j++, val >>= 6) tmp[k - j] = base64[val & 0x3f];
     75           tmp += 4;
     76         }
     77         certsize = tmp - ecert;
     78         pcert = 0;
     79       } else while (sscanf(tmp, "\\%hho", pcert) == 1) pcert++, tmp += 4;
     80     } else if (!memcmp(line, "CKA_LABEL UTF8 ", 15)) {
     81 
     82       char *p2, *tmp2;
     83       len -= 15;
     84       if (!(label = malloc(len))) err(1, 0);
     85       memcpy(label, line + 15, len);
     86       trust = 0;
     87       for (node = blacklist; node; node = (char**)*node)
     88         if (!strcmp(label, (char*)(node + 1))) trust = 4;
     89       if (!(p2 = malloc(len + 2))) err(1, 0);
     90       for (tmp = label + 1, tmp2 = p2; *tmp != '"'; tmp++, tmp2++) {
     91         switch (*tmp) {
     92         case '\\':
     93           if (sscanf(tmp, "\\x%hhx", tmp2)!=1) errx(1, "Bad triple: %s\n", tmp);
     94           tmp += 3;
     95           break;
     96         case '/':
     97         case ' ':
     98           *tmp2 = '_';
     99           break;
    100         case '(':
    101         case ')':
    102           *tmp2 = '=';
    103           break;
    104         default:
    105           *tmp2 = *tmp;
    106         }
    107       }
    108       strcpy(tmp2, ".crt");
    109       free(label);
    110       label = p2;
    111     } else if (!strcmp(line, "CKA_VALUE MULTILINE_OCTAL\n")) pcert = cert;
    112     else if (!memcmp(line, "CKA_TRUST_SERVER_AUTH CK_TRUST CKT_NSS_", 39)) {
    113       tmp += 39;
    114       if (!strcmp(tmp, "TRUSTED_DELEGATOR\n")) trust |= 1;
    115       else if (!strcmp(tmp, "NOT_TRUSTED\n")) trust |= 2;
    116     } else if (!memcmp(line,
    117                        "CKA_TRUST_EMAIL_PROTECTION CK_TRUST CKT_NSS_", 44)) {
    118       tmp += 44;
    119       if (!strcmp(tmp, "TRUSTED_DELEGATOR\n")) trust |= 1;
    120       else if (!strcmp(tmp, "NOT_TRUSTED\n")) trust |= 2;
    121       if (!trust) printf("Ignoring %s\n", label);
    122       if (trust == 1) {
    123         FILE *out;
    124         if (!(out = fopen(label, "w"))) err(1, "%s", label);
    125         xwrite(out, "-----BEGIN CERTIFICATE-----\n", 28);
    126         xwrite(out, ecert, certsize);
    127         xwrite(out, "\n-----END CERTIFICATE-----\n", 27);
    128         fclose(out);
    129       }
    130     }
    131   }
    132   fclose(f);
    133   
    134   while (blacklist) {
    135     node = (char**)*blacklist;
    136     free(blacklist);
    137     blacklist = node;
    138   }
    139   free(line);
    140   free(label);
    141   return 0;
    142 }