readcert.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <mp.h>
  12. #include <libsec.h>
  13. static int8_t*
  14. readfile(int8_t *name)
  15. {
  16. int fd;
  17. int8_t *s;
  18. Dir *d;
  19. fd = open(name, OREAD);
  20. if(fd < 0)
  21. return nil;
  22. if((d = dirfstat(fd)) == nil)
  23. return nil;
  24. s = malloc(d->length + 1);
  25. if(s == nil || readn(fd, s, d->length) != d->length){
  26. free(s);
  27. free(d);
  28. close(fd);
  29. return nil;
  30. }
  31. close(fd);
  32. s[d->length] = '\0';
  33. free(d);
  34. return s;
  35. }
  36. uint8_t*
  37. readcert(int8_t *filename, int *pcertlen)
  38. {
  39. int8_t *pem;
  40. uint8_t *binary;
  41. pem = readfile(filename);
  42. if(pem == nil){
  43. werrstr("can't read %s", filename);
  44. return nil;
  45. }
  46. binary = decodepem(pem, "CERTIFICATE", pcertlen);
  47. free(pem);
  48. if(binary == nil){
  49. werrstr("can't parse %s", filename);
  50. return nil;
  51. }
  52. return binary;
  53. }