x_algor.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stddef.h>
  10. #include <openssl/x509.h>
  11. #include <openssl/asn1.h>
  12. #include <openssl/asn1t.h>
  13. #include "crypto/evp.h"
  14. ASN1_SEQUENCE(X509_ALGOR) = {
  15. ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
  16. ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
  17. } ASN1_SEQUENCE_END(X509_ALGOR)
  18. ASN1_ITEM_TEMPLATE(X509_ALGORS) =
  19. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
  20. ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
  21. IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
  22. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
  23. IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
  24. int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
  25. {
  26. if (alg == NULL)
  27. return 0;
  28. if (ptype != V_ASN1_UNDEF) {
  29. if (alg->parameter == NULL)
  30. alg->parameter = ASN1_TYPE_new();
  31. if (alg->parameter == NULL)
  32. return 0;
  33. }
  34. ASN1_OBJECT_free(alg->algorithm);
  35. alg->algorithm = aobj;
  36. if (ptype == 0)
  37. return 1;
  38. if (ptype == V_ASN1_UNDEF) {
  39. ASN1_TYPE_free(alg->parameter);
  40. alg->parameter = NULL;
  41. } else
  42. ASN1_TYPE_set(alg->parameter, ptype, pval);
  43. return 1;
  44. }
  45. void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
  46. const void **ppval, const X509_ALGOR *algor)
  47. {
  48. if (paobj)
  49. *paobj = algor->algorithm;
  50. if (pptype) {
  51. if (algor->parameter == NULL) {
  52. *pptype = V_ASN1_UNDEF;
  53. return;
  54. } else
  55. *pptype = algor->parameter->type;
  56. if (ppval)
  57. *ppval = algor->parameter->value.ptr;
  58. }
  59. }
  60. /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
  61. void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
  62. {
  63. int param_type;
  64. if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
  65. param_type = V_ASN1_UNDEF;
  66. else
  67. param_type = V_ASN1_NULL;
  68. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
  69. }
  70. int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)
  71. {
  72. int rv;
  73. rv = OBJ_cmp(a->algorithm, b->algorithm);
  74. if (rv)
  75. return rv;
  76. if (!a->parameter && !b->parameter)
  77. return 0;
  78. return ASN1_TYPE_cmp(a->parameter, b->parameter);
  79. }
  80. int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src)
  81. {
  82. if (src == NULL || dest == NULL)
  83. return 0;
  84. if (dest->algorithm)
  85. ASN1_OBJECT_free(dest->algorithm);
  86. dest->algorithm = NULL;
  87. if (dest->parameter)
  88. ASN1_TYPE_free(dest->parameter);
  89. dest->parameter = NULL;
  90. if (src->algorithm)
  91. if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL)
  92. return 0;
  93. if (src->parameter) {
  94. dest->parameter = ASN1_TYPE_new();
  95. if (dest->parameter == NULL)
  96. return 0;
  97. /* Assuming this is also correct for a BOOL.
  98. * set does copy as a side effect.
  99. */
  100. if (ASN1_TYPE_set1(dest->parameter,
  101. src->parameter->type, src->parameter->value.ptr) == 0)
  102. return 0;
  103. }
  104. return 1;
  105. }