obj_xref.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/objects.h>
  10. #include "obj_xref.h"
  11. #include "internal/nelem.h"
  12. #include <openssl/err.h>
  13. static STACK_OF(nid_triple) *sig_app, *sigx_app;
  14. static int sig_cmp(const nid_triple *a, const nid_triple *b)
  15. {
  16. return a->sign_id - b->sign_id;
  17. }
  18. DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
  19. IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
  20. static int sig_sk_cmp(const nid_triple *const *a, const nid_triple *const *b)
  21. {
  22. return (*a)->sign_id - (*b)->sign_id;
  23. }
  24. DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
  25. static int sigx_cmp(const nid_triple *const *a, const nid_triple *const *b)
  26. {
  27. int ret;
  28. ret = (*a)->hash_id - (*b)->hash_id;
  29. if (ret)
  30. return ret;
  31. return (*a)->pkey_id - (*b)->pkey_id;
  32. }
  33. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
  34. int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)
  35. {
  36. nid_triple tmp;
  37. const nid_triple *rv = NULL;
  38. tmp.sign_id = signid;
  39. if (sig_app != NULL) {
  40. int idx = sk_nid_triple_find(sig_app, &tmp);
  41. rv = sk_nid_triple_value(sig_app, idx);
  42. }
  43. #ifndef OBJ_XREF_TEST2
  44. if (rv == NULL) {
  45. rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt));
  46. }
  47. #endif
  48. if (rv == NULL)
  49. return 0;
  50. if (pdig_nid)
  51. *pdig_nid = rv->hash_id;
  52. if (ppkey_nid)
  53. *ppkey_nid = rv->pkey_id;
  54. return 1;
  55. }
  56. int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)
  57. {
  58. nid_triple tmp;
  59. const nid_triple *t = &tmp;
  60. const nid_triple **rv = NULL;
  61. tmp.hash_id = dig_nid;
  62. tmp.pkey_id = pkey_nid;
  63. if (sigx_app) {
  64. int idx = sk_nid_triple_find(sigx_app, &tmp);
  65. if (idx >= 0) {
  66. t = sk_nid_triple_value(sigx_app, idx);
  67. rv = &t;
  68. }
  69. }
  70. #ifndef OBJ_XREF_TEST2
  71. if (rv == NULL) {
  72. rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref));
  73. }
  74. #endif
  75. if (rv == NULL)
  76. return 0;
  77. if (psignid)
  78. *psignid = (*rv)->sign_id;
  79. return 1;
  80. }
  81. int OBJ_add_sigid(int signid, int dig_id, int pkey_id)
  82. {
  83. nid_triple *ntr;
  84. if (sig_app == NULL)
  85. sig_app = sk_nid_triple_new(sig_sk_cmp);
  86. if (sig_app == NULL)
  87. return 0;
  88. if (sigx_app == NULL)
  89. sigx_app = sk_nid_triple_new(sigx_cmp);
  90. if (sigx_app == NULL)
  91. return 0;
  92. if ((ntr = OPENSSL_malloc(sizeof(*ntr))) == NULL) {
  93. OBJerr(OBJ_F_OBJ_ADD_SIGID, ERR_R_MALLOC_FAILURE);
  94. return 0;
  95. }
  96. ntr->sign_id = signid;
  97. ntr->hash_id = dig_id;
  98. ntr->pkey_id = pkey_id;
  99. if (!sk_nid_triple_push(sig_app, ntr)) {
  100. OPENSSL_free(ntr);
  101. return 0;
  102. }
  103. if (!sk_nid_triple_push(sigx_app, ntr))
  104. return 0;
  105. sk_nid_triple_sort(sig_app);
  106. sk_nid_triple_sort(sigx_app);
  107. return 1;
  108. }
  109. static void sid_free(nid_triple *tt)
  110. {
  111. OPENSSL_free(tt);
  112. }
  113. void OBJ_sigid_free(void)
  114. {
  115. sk_nid_triple_pop_free(sig_app, sid_free);
  116. sig_app = NULL;
  117. sk_nid_triple_free(sigx_app);
  118. sigx_app = NULL;
  119. }