1
0

cfg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <jose/cfg.h>
  18. #undef jose_cfg_err
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <jansson.h>
  24. struct jose_cfg {
  25. size_t refs;
  26. jose_cfg_err_t *err;
  27. void *misc;
  28. };
  29. jose_malloc_t jose_malloc = malloc;
  30. jose_realloc_t jose_realloc = realloc;
  31. jose_free_t jose_free = free;
  32. jose_calloc_t jose_calloc = calloc;
  33. static struct {
  34. uint64_t nmbr;
  35. const char *name;
  36. } errnames[] = {
  37. #define XX(n) { n, # n }
  38. XX(JOSE_CFG_ERR_JWK_INVALID),
  39. XX(JOSE_CFG_ERR_JWK_MISMATCH),
  40. XX(JOSE_CFG_ERR_JWK_DENIED),
  41. XX(JOSE_CFG_ERR_ALG_NOTSUP),
  42. XX(JOSE_CFG_ERR_ALG_NOINFER),
  43. XX(JOSE_CFG_ERR_JWS_INVALID),
  44. #undef XX
  45. {}
  46. };
  47. static const char *
  48. getname(uint64_t err)
  49. {
  50. if (err < _JOSE_CFG_ERR_BASE)
  51. return strerror(err);
  52. for (size_t i = 0; errnames[i].name; i++) {
  53. if (errnames[i].nmbr == err)
  54. return errnames[i].name;
  55. }
  56. return "UNKNOWN";
  57. }
  58. static void
  59. dflt_err(void *misc, const char *file, int line, uint64_t err,
  60. const char *fmt, va_list ap)
  61. {
  62. fprintf(stderr, "%s:%d:", file, line);
  63. if (err != 0)
  64. fprintf(stderr, "%s:", getname(err));
  65. vfprintf(stderr, fmt, ap);
  66. fprintf(stderr, "\n");
  67. }
  68. static const jose_cfg_t dflt = { .err = dflt_err };
  69. jose_cfg_t *
  70. jose_cfg(void)
  71. {
  72. jose_cfg_t *cfg = NULL;
  73. cfg = jose_calloc(1, sizeof(*cfg));
  74. if (cfg)
  75. *cfg = dflt;
  76. return jose_cfg_incref(cfg);
  77. }
  78. void
  79. jose_cfg_auto(jose_cfg_t **cfg)
  80. {
  81. if (cfg)
  82. jose_cfg_decref(*cfg);
  83. }
  84. jose_cfg_t *
  85. jose_cfg_incref(jose_cfg_t *cfg)
  86. {
  87. if (cfg)
  88. cfg->refs++;
  89. return cfg;
  90. }
  91. void
  92. jose_cfg_decref(jose_cfg_t *cfg)
  93. {
  94. if (cfg->refs-- == 1)
  95. jose_free(cfg);
  96. }
  97. void
  98. jose_cfg_set_err_func(jose_cfg_t *cfg, jose_cfg_err_t *err, void *misc)
  99. {
  100. cfg->err = err ? err : dflt.err;
  101. cfg->misc = misc;
  102. }
  103. void *
  104. jose_cfg_get_err_misc(jose_cfg_t *cfg)
  105. {
  106. return cfg->err;
  107. }
  108. void
  109. jose_cfg_err(jose_cfg_t *cfg, const char *file, int line, uint64_t err,
  110. const char *fmt, ...)
  111. {
  112. const jose_cfg_t *c = cfg ? cfg : &dflt;
  113. va_list ap;
  114. va_start(ap, fmt);
  115. c->err(c->misc, file, line, err, fmt, ap);
  116. va_end(ap);
  117. }
  118. int
  119. jose_set_alloc(jose_malloc_t pmalloc, jose_realloc_t prealloc, jose_free_t pfree, jose_calloc_t pcalloc)
  120. {
  121. /* all of the allocator functions must be set */
  122. if (pmalloc == NULL || prealloc == NULL || pfree == NULL || pcalloc == NULL)
  123. return EINVAL;
  124. jose_malloc = pmalloc;
  125. jose_realloc = prealloc;
  126. jose_free = pfree;
  127. jose_calloc = pcalloc;
  128. /* Configure Jansson to use the same allocators as JOSE */
  129. json_set_alloc_funcs(jose_malloc, jose_free);
  130. return 0;
  131. }
  132. void
  133. jose_get_alloc(jose_malloc_t *pmalloc, jose_realloc_t *prealloc, jose_free_t *pfree, jose_calloc_t *pcalloc)
  134. {
  135. if (pmalloc) *pmalloc = jose_malloc;
  136. if (prealloc) *prealloc = jose_realloc;
  137. if (pfree) *pfree = jose_free;
  138. if (pcalloc) *pcalloc = jose_calloc;
  139. }
  140. void
  141. jose_reset_alloc(void)
  142. {
  143. jose_malloc = malloc;
  144. jose_realloc = realloc;
  145. jose_free = free;
  146. jose_calloc = calloc;
  147. /* Reset Jansson to use default allocators */
  148. json_set_alloc_funcs(malloc, free);
  149. }