a_verify.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #ifndef OPENSSL_NO_DEPRECATED_3_0
  21. int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
  22. char *data, EVP_PKEY *pkey)
  23. {
  24. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  25. const EVP_MD *type;
  26. unsigned char *p, *buf_in = NULL;
  27. int ret = -1, i, inl;
  28. if (ctx == NULL) {
  29. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  30. goto err;
  31. }
  32. i = OBJ_obj2nid(a->algorithm);
  33. type = EVP_get_digestbyname(OBJ_nid2sn(i));
  34. if (type == NULL) {
  35. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  36. goto err;
  37. }
  38. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  39. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  40. goto err;
  41. }
  42. inl = i2d(data, NULL);
  43. if (inl <= 0) {
  44. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  45. goto err;
  46. }
  47. buf_in = OPENSSL_malloc((unsigned int)inl);
  48. if (buf_in == NULL) {
  49. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  50. goto err;
  51. }
  52. p = buf_in;
  53. i2d(data, &p);
  54. ret = EVP_VerifyInit_ex(ctx, type, NULL)
  55. && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
  56. OPENSSL_clear_free(buf_in, (unsigned int)inl);
  57. if (!ret) {
  58. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  59. goto err;
  60. }
  61. ret = -1;
  62. if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
  63. (unsigned int)signature->length, pkey) <= 0) {
  64. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  65. ret = 0;
  66. goto err;
  67. }
  68. ret = 1;
  69. err:
  70. EVP_MD_CTX_free(ctx);
  71. return ret;
  72. }
  73. #endif
  74. int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
  75. const ASN1_BIT_STRING *signature, const void *data,
  76. EVP_PKEY *pkey)
  77. {
  78. return ASN1_item_verify_ex(it, alg, signature, data, NULL, pkey, NULL, NULL);
  79. }
  80. int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
  81. const ASN1_BIT_STRING *signature, const void *data,
  82. const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
  83. OSSL_LIB_CTX *libctx, const char *propq)
  84. {
  85. EVP_MD_CTX *ctx;
  86. int rv = -1;
  87. if ((ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq)) != NULL) {
  88. rv = ASN1_item_verify_ctx(it, alg, signature, data, ctx);
  89. EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
  90. EVP_MD_CTX_free(ctx);
  91. }
  92. return rv;
  93. }
  94. int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
  95. const ASN1_BIT_STRING *signature, const void *data,
  96. EVP_MD_CTX *ctx)
  97. {
  98. EVP_PKEY *pkey;
  99. unsigned char *buf_in = NULL;
  100. int ret = -1, inl = 0;
  101. int mdnid, pknid;
  102. size_t inll = 0;
  103. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
  104. if (pkey == NULL) {
  105. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  106. return -1;
  107. }
  108. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  109. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  110. return -1;
  111. }
  112. /* Convert signature OID into digest and public key OIDs */
  113. if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)) {
  114. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  115. goto err;
  116. }
  117. if (mdnid == NID_undef) {
  118. if (pkey->ameth == NULL || pkey->ameth->item_verify == NULL) {
  119. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  120. goto err;
  121. }
  122. ret = pkey->ameth->item_verify(ctx, it, data, alg, signature, pkey);
  123. /*
  124. * Return values meaning:
  125. * <=0: error.
  126. * 1: method does everything.
  127. * 2: carry on as normal, method has called EVP_DigestVerifyInit()
  128. */
  129. if (ret <= 0)
  130. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  131. if (ret <= 1)
  132. goto err;
  133. } else {
  134. const EVP_MD *type = EVP_get_digestbynid(mdnid);
  135. if (type == NULL) {
  136. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  137. goto err;
  138. }
  139. /* Check public key OID matches public key type */
  140. if (!EVP_PKEY_is_a(pkey, OBJ_nid2sn(pknid))) {
  141. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
  142. goto err;
  143. }
  144. if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
  145. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  146. ret = 0;
  147. goto err;
  148. }
  149. }
  150. inl = ASN1_item_i2d(data, &buf_in, it);
  151. if (inl <= 0) {
  152. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  153. goto err;
  154. }
  155. if (buf_in == NULL) {
  156. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  157. goto err;
  158. }
  159. inll = inl;
  160. ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
  161. buf_in, inl);
  162. if (ret <= 0) {
  163. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  164. goto err;
  165. }
  166. ret = 1;
  167. err:
  168. OPENSSL_clear_free(buf_in, inll);
  169. return ret;
  170. }