a_verify.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 1995-2023 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 <stdio.h>
  10. #include <time.h>
  11. #include <sys/types.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/bn.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/buffer.h>
  17. #include <openssl/evp.h>
  18. #include "crypto/asn1.h"
  19. #include "crypto/evp.h"
  20. #include "crypto/rsa.h"
  21. #ifndef OPENSSL_NO_DEPRECATED_3_0
  22. int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
  23. char *data, EVP_PKEY *pkey)
  24. {
  25. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  26. const EVP_MD *type;
  27. unsigned char *p, *buf_in = NULL;
  28. int ret = -1, i, inl;
  29. if (ctx == NULL) {
  30. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  31. goto err;
  32. }
  33. i = OBJ_obj2nid(a->algorithm);
  34. type = EVP_get_digestbyname(OBJ_nid2sn(i));
  35. if (type == NULL) {
  36. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  37. goto err;
  38. }
  39. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  40. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  41. goto err;
  42. }
  43. inl = i2d(data, NULL);
  44. if (inl <= 0) {
  45. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  46. goto err;
  47. }
  48. buf_in = OPENSSL_malloc((unsigned int)inl);
  49. if (buf_in == NULL)
  50. goto err;
  51. p = buf_in;
  52. i2d(data, &p);
  53. ret = EVP_VerifyInit_ex(ctx, type, NULL)
  54. && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
  55. OPENSSL_clear_free(buf_in, (unsigned int)inl);
  56. if (!ret) {
  57. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  58. goto err;
  59. }
  60. ret = -1;
  61. if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
  62. (unsigned int)signature->length, pkey) <= 0) {
  63. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  64. ret = 0;
  65. goto err;
  66. }
  67. ret = 1;
  68. err:
  69. EVP_MD_CTX_free(ctx);
  70. return ret;
  71. }
  72. #endif
  73. int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
  74. const ASN1_BIT_STRING *signature, const void *data,
  75. EVP_PKEY *pkey)
  76. {
  77. return ASN1_item_verify_ex(it, alg, signature, data, NULL, pkey, NULL, NULL);
  78. }
  79. int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
  80. const ASN1_BIT_STRING *signature, const void *data,
  81. const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
  82. OSSL_LIB_CTX *libctx, const char *propq)
  83. {
  84. EVP_MD_CTX *ctx;
  85. int rv = -1;
  86. if ((ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq)) != NULL) {
  87. rv = ASN1_item_verify_ctx(it, alg, signature, data, ctx);
  88. EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
  89. EVP_MD_CTX_free(ctx);
  90. }
  91. return rv;
  92. }
  93. int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
  94. const ASN1_BIT_STRING *signature, const void *data,
  95. EVP_MD_CTX *ctx)
  96. {
  97. EVP_PKEY *pkey;
  98. unsigned char *buf_in = NULL;
  99. int ret = -1, inl = 0;
  100. int mdnid, pknid;
  101. size_t inll = 0;
  102. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
  103. if (pkey == NULL) {
  104. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  105. return -1;
  106. }
  107. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  108. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  109. return -1;
  110. }
  111. /* Convert signature OID into digest and public key OIDs */
  112. if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)) {
  113. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  114. goto err;
  115. }
  116. if (mdnid == NID_undef && evp_pkey_is_legacy(pkey)) {
  117. if (pkey->ameth == NULL || pkey->ameth->item_verify == NULL) {
  118. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  119. goto err;
  120. }
  121. ret = pkey->ameth->item_verify(ctx, it, data, alg, signature, pkey);
  122. /*
  123. * Return values meaning:
  124. * <=0: error.
  125. * 1: method does everything.
  126. * 2: carry on as normal, method has called EVP_DigestVerifyInit()
  127. */
  128. if (ret <= 0)
  129. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  130. if (ret <= 1)
  131. goto err;
  132. } else {
  133. const EVP_MD *type = NULL;
  134. /*
  135. * We don't yet have the ability for providers to be able to handle
  136. * X509_ALGOR style parameters. Fortunately the only one that needs this
  137. * so far is RSA-PSS, so we just special case this for now. In some
  138. * future version of OpenSSL we should push this to the provider.
  139. */
  140. if (mdnid == NID_undef && pknid == EVP_PKEY_RSA_PSS) {
  141. if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) {
  142. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
  143. goto err;
  144. }
  145. /* This function also calls EVP_DigestVerifyInit */
  146. if (ossl_rsa_pss_to_ctx(ctx, NULL, alg, pkey) <= 0) {
  147. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  148. goto err;
  149. }
  150. } else {
  151. /* Check public key OID matches public key type */
  152. if (!EVP_PKEY_is_a(pkey, OBJ_nid2sn(pknid))) {
  153. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
  154. goto err;
  155. }
  156. if (mdnid != NID_undef) {
  157. type = EVP_get_digestbynid(mdnid);
  158. if (type == NULL) {
  159. ERR_raise_data(ERR_LIB_ASN1,
  160. ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM,
  161. "nid=0x%x", mdnid);
  162. goto err;
  163. }
  164. }
  165. /*
  166. * Note that some algorithms (notably Ed25519 and Ed448) may allow
  167. * a NULL digest value.
  168. */
  169. if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
  170. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  171. ret = 0;
  172. goto err;
  173. }
  174. }
  175. }
  176. inl = ASN1_item_i2d(data, &buf_in, it);
  177. if (inl <= 0) {
  178. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  179. goto err;
  180. }
  181. if (buf_in == NULL) {
  182. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  183. goto err;
  184. }
  185. inll = inl;
  186. ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
  187. buf_in, inl);
  188. if (ret <= 0) {
  189. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  190. goto err;
  191. }
  192. ret = 1;
  193. err:
  194. OPENSSL_clear_free(buf_in, inll);
  195. return ret;
  196. }