thumb.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <auth.h>
  13. #include <mp.h>
  14. #include <libsec.h>
  15. enum{ ThumbTab = 1<<10 };
  16. static void *
  17. emalloc(int n)
  18. {
  19. void *p;
  20. if(n==0)
  21. n=1;
  22. p = malloc(n);
  23. if(p == nil){
  24. exits("out of memory");
  25. }
  26. memset(p, 0, n);
  27. return p;
  28. }
  29. void
  30. freeThumbprints(Thumbprint *table)
  31. {
  32. Thumbprint *hd, *p, *q;
  33. for(hd = table; hd < table+ThumbTab; hd++){
  34. for(p = hd->next; p; p = q){
  35. q = p->next;
  36. free(p);
  37. }
  38. }
  39. free(table);
  40. }
  41. int
  42. okThumbprint(uint8_t *sum, Thumbprint *table)
  43. {
  44. Thumbprint *p;
  45. int i = ((sum[0]<<8) + sum[1]) & (ThumbTab-1);
  46. for(p = table[i].next; p; p = p->next)
  47. if(memcmp(sum, p->sha1, SHA1dlen) == 0)
  48. return 1;
  49. return 0;
  50. }
  51. static void
  52. loadThumbprints(int8_t *file, Thumbprint *table, Thumbprint *crltab)
  53. {
  54. Thumbprint *entry;
  55. Biobuf *bin;
  56. int8_t *line, *field[50];
  57. uint8_t sum[SHA1dlen];
  58. int i;
  59. bin = Bopen(file, OREAD);
  60. if(bin == nil)
  61. return;
  62. for(; (line = Brdstr(bin, '\n', 1)) != 0; free(line)){
  63. if(tokenize(line, field, nelem(field)) < 2)
  64. continue;
  65. if(strcmp(field[0], "#include") == 0){
  66. loadThumbprints(field[1], table, crltab);
  67. continue;
  68. }
  69. if(strcmp(field[0], "x509") != 0 || strncmp(field[1], "sha1=", strlen("sha1=")) != 0)
  70. continue;
  71. field[1] += strlen("sha1=");
  72. dec16(sum, sizeof(sum), field[1], strlen(field[1]));
  73. if(crltab && okThumbprint(sum, crltab))
  74. continue;
  75. entry = (Thumbprint*)emalloc(sizeof(*entry));
  76. memcpy(entry->sha1, sum, SHA1dlen);
  77. i = ((sum[0]<<8) + sum[1]) & (ThumbTab-1);
  78. entry->next = table[i].next;
  79. table[i].next = entry;
  80. }
  81. Bterm(bin);
  82. }
  83. Thumbprint *
  84. initThumbprints(int8_t *ok, int8_t *crl)
  85. {
  86. Thumbprint *table, *crltab = nil;
  87. if(crl){
  88. crltab = emalloc(ThumbTab * sizeof(*table));
  89. loadThumbprints(crl, crltab, nil);
  90. }
  91. table = emalloc(ThumbTab * sizeof(*table));
  92. loadThumbprints(ok, table, crltab);
  93. free(crltab);
  94. return table;
  95. }