a_verify.c 6.8 KB

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