a_verify.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 1995-2016 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 NO_ASN1_OLD
  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. ASN1err(ASN1_F_ASN1_VERIFY, 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. ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  36. goto err;
  37. }
  38. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  39. ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  40. goto err;
  41. }
  42. inl = i2d(data, NULL);
  43. if (inl <= 0) {
  44. ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_INTERNAL_ERROR);
  45. goto err;
  46. }
  47. buf_in = OPENSSL_malloc((unsigned int)inl);
  48. if (buf_in == NULL) {
  49. ASN1err(ASN1_F_ASN1_VERIFY, 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. ASN1err(ASN1_F_ASN1_VERIFY, 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. ASN1err(ASN1_F_ASN1_VERIFY, 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, X509_ALGOR *a,
  75. ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey)
  76. {
  77. EVP_MD_CTX *ctx = NULL;
  78. unsigned char *buf_in = NULL;
  79. int ret = -1, inl = 0;
  80. int mdnid, pknid;
  81. size_t inll = 0;
  82. if (pkey == NULL) {
  83. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_PASSED_NULL_PARAMETER);
  84. return -1;
  85. }
  86. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  87. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  88. return -1;
  89. }
  90. ctx = EVP_MD_CTX_new();
  91. if (ctx == NULL) {
  92. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
  93. goto err;
  94. }
  95. /* Convert signature OID into digest and public key OIDs */
  96. if (!OBJ_find_sigid_algs(OBJ_obj2nid(a->algorithm), &mdnid, &pknid)) {
  97. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  98. goto err;
  99. }
  100. if (mdnid == NID_undef) {
  101. if (pkey->ameth == NULL || pkey->ameth->item_verify == NULL) {
  102. ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
  103. ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  104. goto err;
  105. }
  106. ret = pkey->ameth->item_verify(ctx, it, asn, a, signature, pkey);
  107. /*
  108. * Return value of 2 means carry on, anything else means we exit
  109. * straight away: either a fatal error of the underlying verification
  110. * routine handles all verification.
  111. */
  112. if (ret != 2)
  113. goto err;
  114. ret = -1;
  115. } else {
  116. const EVP_MD *type = EVP_get_digestbynid(mdnid);
  117. if (type == NULL) {
  118. ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
  119. ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  120. goto err;
  121. }
  122. /* Check public key OID matches public key type */
  123. if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id) {
  124. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
  125. goto err;
  126. }
  127. if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
  128. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
  129. ret = 0;
  130. goto err;
  131. }
  132. }
  133. inl = ASN1_item_i2d(asn, &buf_in, it);
  134. if (inl <= 0) {
  135. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_INTERNAL_ERROR);
  136. goto err;
  137. }
  138. if (buf_in == NULL) {
  139. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
  140. goto err;
  141. }
  142. inll = inl;
  143. ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
  144. buf_in, inl);
  145. if (ret <= 0) {
  146. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
  147. goto err;
  148. }
  149. ret = 1;
  150. err:
  151. OPENSSL_clear_free(buf_in, inll);
  152. EVP_MD_CTX_free(ctx);
  153. return ret;
  154. }