x_algor.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 1998-2021 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 <stddef.h>
  10. #include <openssl/x509.h>
  11. #include <openssl/asn1.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/err.h>
  14. #include "crypto/asn1.h"
  15. #include "crypto/evp.h"
  16. ASN1_SEQUENCE(X509_ALGOR) = {
  17. ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
  18. ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
  19. } ASN1_SEQUENCE_END(X509_ALGOR)
  20. ASN1_ITEM_TEMPLATE(X509_ALGORS) =
  21. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
  22. ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
  23. IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
  24. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
  25. IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
  26. int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
  27. {
  28. if (alg == NULL)
  29. return 0;
  30. if (ptype != V_ASN1_UNDEF) {
  31. if (alg->parameter == NULL)
  32. alg->parameter = ASN1_TYPE_new();
  33. if (alg->parameter == NULL)
  34. return 0;
  35. }
  36. ASN1_OBJECT_free(alg->algorithm);
  37. alg->algorithm = aobj;
  38. if (ptype == 0)
  39. return 1;
  40. if (ptype == V_ASN1_UNDEF) {
  41. ASN1_TYPE_free(alg->parameter);
  42. alg->parameter = NULL;
  43. } else
  44. ASN1_TYPE_set(alg->parameter, ptype, pval);
  45. return 1;
  46. }
  47. void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
  48. const void **ppval, const X509_ALGOR *algor)
  49. {
  50. if (paobj)
  51. *paobj = algor->algorithm;
  52. if (pptype) {
  53. if (algor->parameter == NULL) {
  54. *pptype = V_ASN1_UNDEF;
  55. return;
  56. } else
  57. *pptype = algor->parameter->type;
  58. if (ppval)
  59. *ppval = algor->parameter->value.ptr;
  60. }
  61. }
  62. /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
  63. void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
  64. {
  65. int param_type;
  66. if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
  67. param_type = V_ASN1_UNDEF;
  68. else
  69. param_type = V_ASN1_NULL;
  70. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_get_type(md)), param_type, NULL);
  71. }
  72. int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)
  73. {
  74. int rv;
  75. rv = OBJ_cmp(a->algorithm, b->algorithm);
  76. if (rv)
  77. return rv;
  78. if (!a->parameter && !b->parameter)
  79. return 0;
  80. return ASN1_TYPE_cmp(a->parameter, b->parameter);
  81. }
  82. int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src)
  83. {
  84. if (src == NULL || dest == NULL)
  85. return 0;
  86. if (dest->algorithm)
  87. ASN1_OBJECT_free(dest->algorithm);
  88. dest->algorithm = NULL;
  89. if (dest->parameter)
  90. ASN1_TYPE_free(dest->parameter);
  91. dest->parameter = NULL;
  92. if (src->algorithm)
  93. if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL)
  94. return 0;
  95. if (src->parameter != NULL) {
  96. dest->parameter = ASN1_TYPE_new();
  97. if (dest->parameter == NULL)
  98. return 0;
  99. /* Assuming this is also correct for a BOOL.
  100. * set does copy as a side effect.
  101. */
  102. if (ASN1_TYPE_set1(dest->parameter,
  103. src->parameter->type, src->parameter->value.ptr) == 0)
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  109. int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md)
  110. {
  111. /* Default is SHA1 so no need to create it - still success */
  112. if (md == NULL || EVP_MD_is_a(md, "SHA1"))
  113. return 1;
  114. *palg = X509_ALGOR_new();
  115. if (*palg == NULL)
  116. return 0;
  117. X509_ALGOR_set_md(*palg, md);
  118. return 1;
  119. }
  120. /* convert algorithm ID to EVP_MD, default SHA1 */
  121. const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg)
  122. {
  123. const EVP_MD *md;
  124. if (alg == NULL)
  125. return EVP_sha1();
  126. md = EVP_get_digestbyobj(alg->algorithm);
  127. if (md == NULL)
  128. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_DIGEST);
  129. return md;
  130. }
  131. X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg)
  132. {
  133. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
  134. return NULL;
  135. return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
  136. alg->parameter);
  137. }
  138. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  139. int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
  140. {
  141. X509_ALGOR *algtmp = NULL;
  142. ASN1_STRING *stmp = NULL;
  143. *palg = NULL;
  144. if (mgf1md == NULL || EVP_MD_is_a(mgf1md, "SHA1"))
  145. return 1;
  146. /* need to embed algorithm ID inside another */
  147. if (!ossl_x509_algor_new_from_md(&algtmp, mgf1md))
  148. goto err;
  149. if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
  150. goto err;
  151. *palg = X509_ALGOR_new();
  152. if (*palg == NULL)
  153. goto err;
  154. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  155. stmp = NULL;
  156. err:
  157. ASN1_STRING_free(stmp);
  158. X509_ALGOR_free(algtmp);
  159. if (*palg != NULL)
  160. return 1;
  161. return 0;
  162. }