decodepem.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <mp.h>
  4. #include <libsec.h>
  5. #define STRLEN(s) (sizeof(s)-1)
  6. uchar*
  7. decodePEM(char *s, char *type, int *len, char **new_s)
  8. {
  9. uchar *d;
  10. char *t, *e, *tt;
  11. int n;
  12. *len = 0;
  13. /*
  14. * find the correct section of the file, stripping garbage at the beginning and end.
  15. * the data is delimited by -----BEGIN <type>-----\n and -----END <type>-----\n
  16. */
  17. n = strlen(type);
  18. e = strchr(s, '\0');
  19. for(t = s; t != nil && t < e; ){
  20. tt = t;
  21. t = strchr(tt, '\n');
  22. if(t != nil)
  23. t++;
  24. if(strncmp(tt, "-----BEGIN ", STRLEN("-----BEGIN ")) == 0
  25. && strncmp(&tt[STRLEN("-----BEGIN ")], type, n) == 0
  26. && strncmp(&tt[STRLEN("-----BEGIN ")+n], "-----\n", STRLEN("-----\n")) == 0)
  27. break;
  28. }
  29. for(tt = t; tt != nil && tt < e; tt++){
  30. if(strncmp(tt, "-----END ", STRLEN("-----END ")) == 0
  31. && strncmp(&tt[STRLEN("-----END ")], type, n) == 0
  32. && strncmp(&tt[STRLEN("-----END ")+n], "-----\n", STRLEN("-----\n")) == 0)
  33. break;
  34. tt = strchr(tt, '\n');
  35. if(tt == nil)
  36. break;
  37. }
  38. if(tt == nil || tt == e){
  39. werrstr("incorrect .pem file format: bad header or trailer");
  40. return nil;
  41. }
  42. if(new_s)
  43. *new_s = tt+1;
  44. n = ((tt - t) * 6 + 7) / 8;
  45. d = malloc(n);
  46. if(d == nil){
  47. werrstr("out of memory");
  48. return nil;
  49. }
  50. n = dec64(d, n, t, tt - t);
  51. if(n < 0){
  52. free(d);
  53. werrstr("incorrect .pem file format: bad base64 encoded data");
  54. return nil;
  55. }
  56. *len = n;
  57. return d;
  58. }
  59. PEMChain*
  60. decodepemchain(char *s, char *type)
  61. {
  62. PEMChain *first = nil, *last = nil, *chp;
  63. uchar *d;
  64. char *e;
  65. int n;
  66. e = strchr(s, '\0');
  67. while (s < e) {
  68. d = decodePEM(s, type, &n, &s);
  69. if(d == nil)
  70. break;
  71. chp = malloc(sizeof(PEMChain));
  72. chp->next = nil;
  73. chp->pem = d;
  74. chp->pemlen = n;
  75. if (first == nil)
  76. first = chp;
  77. else
  78. last->next = chp;
  79. last = chp;
  80. }
  81. return first;
  82. }