oid2der.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2010 Martin Willi
  3. * Copyright (C) 2010 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <asn1/asn1.h>
  17. /**
  18. * convert string OID to DER encoding
  19. */
  20. int main(int argc, char *argv[])
  21. {
  22. int i, nr = 0;
  23. chunk_t oid;
  24. char *decoded;
  25. bool decode = FALSE;
  26. if (streq(argv[1], "-d"))
  27. {
  28. decode = TRUE;
  29. nr++;
  30. }
  31. while (argc > ++nr)
  32. {
  33. if (decode)
  34. {
  35. oid = chunk_from_hex(chunk_from_str(argv[nr]), NULL);
  36. decoded = asn1_oid_to_string(oid);
  37. printf("%s\n", decoded);
  38. free(decoded);
  39. free(oid.ptr);
  40. continue;
  41. }
  42. oid = asn1_oid_from_string(argv[nr]);
  43. if (oid.len)
  44. {
  45. for (i = 0; i < oid.len; i++)
  46. {
  47. printf("0x%02x,", oid.ptr[i]);
  48. }
  49. printf("\n");
  50. free(oid.ptr);
  51. }
  52. else
  53. {
  54. return 1;
  55. }
  56. }
  57. return 0;
  58. }