certdata2pem.c 4.4 KB

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