key_components.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "ssh.h"
  2. #include "mpint.h"
  3. key_components *key_components_new(void)
  4. {
  5. key_components *kc = snew(key_components);
  6. kc->ncomponents = 0;
  7. kc->componentsize = 0;
  8. kc->components = NULL;
  9. return kc;
  10. }
  11. static void key_components_add_str(key_components *kc, const char *name,
  12. KeyComponentType type, ptrlen data)
  13. {
  14. sgrowarray(kc->components, kc->componentsize, kc->ncomponents);
  15. size_t n = kc->ncomponents++;
  16. kc->components[n].name = dupstr(name);
  17. kc->components[n].type = type;
  18. kc->components[n].str = strbuf_dup_nm(data);
  19. }
  20. void key_components_add_text(key_components *kc,
  21. const char *name, const char *value)
  22. {
  23. key_components_add_str(kc, name, KCT_TEXT, ptrlen_from_asciz(value));
  24. }
  25. void key_components_add_text_pl(key_components *kc,
  26. const char *name, ptrlen value)
  27. {
  28. key_components_add_str(kc, name, KCT_TEXT, value);
  29. }
  30. void key_components_add_binary(key_components *kc,
  31. const char *name, ptrlen value)
  32. {
  33. key_components_add_str(kc, name, KCT_BINARY, value);
  34. }
  35. void key_components_add_mp(key_components *kc,
  36. const char *name, mp_int *value)
  37. {
  38. sgrowarray(kc->components, kc->componentsize, kc->ncomponents);
  39. size_t n = kc->ncomponents++;
  40. kc->components[n].name = dupstr(name);
  41. kc->components[n].type = KCT_MPINT;
  42. kc->components[n].mp = mp_copy(value);
  43. }
  44. void key_components_add_uint(key_components *kc,
  45. const char *name, uintmax_t value)
  46. {
  47. mp_int *mpvalue = mp_from_integer(value);
  48. key_components_add_mp(kc, name, mpvalue);
  49. mp_free(mpvalue);
  50. }
  51. void key_components_add_copy(key_components *kc,
  52. const char *name, const key_component *value)
  53. {
  54. switch (value->type) {
  55. case KCT_TEXT:
  56. case KCT_BINARY:
  57. key_components_add_str(kc, name, value->type,
  58. ptrlen_from_strbuf(value->str));
  59. break;
  60. case KCT_MPINT:
  61. key_components_add_mp(kc, name, value->mp);
  62. break;
  63. }
  64. }
  65. void key_components_free(key_components *kc)
  66. {
  67. for (size_t i = 0; i < kc->ncomponents; i++) {
  68. key_component *comp = &kc->components[i];
  69. sfree(comp->name);
  70. switch (comp->type) {
  71. case KCT_MPINT:
  72. mp_free(comp->mp);
  73. break;
  74. case KCT_TEXT:
  75. case KCT_BINARY:
  76. strbuf_free(comp->str);
  77. break;
  78. default:
  79. unreachable("bad key component type");
  80. }
  81. }
  82. sfree(kc->components);
  83. sfree(kc);
  84. }