2
0

a_verify.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
  19. #include "internal/evp_int.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. buf_in = OPENSSL_malloc((unsigned int)inl);
  44. if (buf_in == NULL) {
  45. ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
  46. goto err;
  47. }
  48. p = buf_in;
  49. i2d(data, &p);
  50. ret = EVP_VerifyInit_ex(ctx, type, NULL)
  51. && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
  52. OPENSSL_clear_free(buf_in, (unsigned int)inl);
  53. if (!ret) {
  54. ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
  55. goto err;
  56. }
  57. ret = -1;
  58. if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
  59. (unsigned int)signature->length, pkey) <= 0) {
  60. ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
  61. ret = 0;
  62. goto err;
  63. }
  64. ret = 1;
  65. err:
  66. EVP_MD_CTX_free(ctx);
  67. return ret;
  68. }
  69. #endif
  70. int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a,
  71. ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey)
  72. {
  73. EVP_MD_CTX *ctx = NULL;
  74. unsigned char *buf_in = NULL;
  75. int ret = -1, inl = 0;
  76. int mdnid, pknid;
  77. if (!pkey) {
  78. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_PASSED_NULL_PARAMETER);
  79. return -1;
  80. }
  81. if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
  82. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
  83. return -1;
  84. }
  85. ctx = EVP_MD_CTX_new();
  86. if (ctx == NULL) {
  87. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
  88. goto err;
  89. }
  90. /* Convert signature OID into digest and public key OIDs */
  91. if (!OBJ_find_sigid_algs(OBJ_obj2nid(a->algorithm), &mdnid, &pknid)) {
  92. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  93. goto err;
  94. }
  95. if (mdnid == NID_undef) {
  96. if (!pkey->ameth || !pkey->ameth->item_verify) {
  97. ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
  98. ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
  99. goto err;
  100. }
  101. ret = pkey->ameth->item_verify(ctx, it, asn, a, signature, pkey);
  102. /*
  103. * Return value of 2 means carry on, anything else means we exit
  104. * straight away: either a fatal error of the underlying verification
  105. * routine handles all verification.
  106. */
  107. if (ret != 2)
  108. goto err;
  109. ret = -1;
  110. } else {
  111. const EVP_MD *type;
  112. type = EVP_get_digestbynid(mdnid);
  113. if (type == NULL) {
  114. ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
  115. ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  116. goto err;
  117. }
  118. /* Check public key OID matches public key type */
  119. if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id) {
  120. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
  121. goto err;
  122. }
  123. if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
  124. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
  125. ret = 0;
  126. goto err;
  127. }
  128. }
  129. inl = ASN1_item_i2d(asn, &buf_in, it);
  130. if (buf_in == NULL) {
  131. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_MALLOC_FAILURE);
  132. goto err;
  133. }
  134. ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
  135. buf_in, inl);
  136. if (ret <= 0) {
  137. ASN1err(ASN1_F_ASN1_ITEM_VERIFY, ERR_R_EVP_LIB);
  138. goto err;
  139. }
  140. ret = 1;
  141. err:
  142. OPENSSL_clear_free(buf_in, (unsigned int)inl);
  143. EVP_MD_CTX_free(ctx);
  144. return ret;
  145. }