pmeth_check.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2006-2021 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 <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include "crypto/bn.h"
  15. #include "crypto/asn1.h"
  16. #include "crypto/evp.h"
  17. #include "evp_local.h"
  18. /*
  19. * Returns:
  20. * 1 True
  21. * 0 False
  22. * -1 Unsupported (use legacy path)
  23. */
  24. static int try_provided_check(EVP_PKEY_CTX *ctx, int selection, int checktype)
  25. {
  26. EVP_KEYMGMT *keymgmt;
  27. void *keydata;
  28. if (evp_pkey_ctx_is_legacy(ctx))
  29. return -1;
  30. keymgmt = ctx->keymgmt;
  31. keydata = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
  32. &keymgmt, ctx->propquery);
  33. if (keydata == NULL) {
  34. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  35. return 0;
  36. }
  37. return evp_keymgmt_validate(keymgmt, keydata, selection, checktype);
  38. }
  39. static int evp_pkey_public_check_combined(EVP_PKEY_CTX *ctx, int checktype)
  40. {
  41. EVP_PKEY *pkey = ctx->pkey;
  42. int ok;
  43. if (pkey == NULL) {
  44. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  45. return 0;
  46. }
  47. if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
  48. checktype)) != -1)
  49. return ok;
  50. if (pkey->type == EVP_PKEY_NONE)
  51. goto not_supported;
  52. #ifndef FIPS_MODULE
  53. /* legacy */
  54. /* call customized public key check function first */
  55. if (ctx->pmeth->public_check != NULL)
  56. return ctx->pmeth->public_check(pkey);
  57. /* use default public key check function in ameth */
  58. if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL)
  59. goto not_supported;
  60. return pkey->ameth->pkey_public_check(pkey);
  61. #endif
  62. not_supported:
  63. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  64. return -2;
  65. }
  66. int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
  67. {
  68. return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
  69. }
  70. int EVP_PKEY_public_check_quick(EVP_PKEY_CTX *ctx)
  71. {
  72. return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
  73. }
  74. static int evp_pkey_param_check_combined(EVP_PKEY_CTX *ctx, int checktype)
  75. {
  76. EVP_PKEY *pkey = ctx->pkey;
  77. int ok;
  78. if (pkey == NULL) {
  79. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  80. return 0;
  81. }
  82. if ((ok = try_provided_check(ctx,
  83. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
  84. checktype)) != -1)
  85. return ok;
  86. if (pkey->type == EVP_PKEY_NONE)
  87. goto not_supported;
  88. #ifndef FIPS_MODULE
  89. /* legacy */
  90. /* call customized param check function first */
  91. if (ctx->pmeth->param_check != NULL)
  92. return ctx->pmeth->param_check(pkey);
  93. /* use default param check function in ameth */
  94. if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL)
  95. goto not_supported;
  96. return pkey->ameth->pkey_param_check(pkey);
  97. #endif
  98. not_supported:
  99. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  100. return -2;
  101. }
  102. int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
  103. {
  104. return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
  105. }
  106. int EVP_PKEY_param_check_quick(EVP_PKEY_CTX *ctx)
  107. {
  108. return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
  109. }
  110. int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
  111. {
  112. EVP_PKEY *pkey = ctx->pkey;
  113. int ok;
  114. if (pkey == NULL) {
  115. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  116. return 0;
  117. }
  118. if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
  119. OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)
  120. return ok;
  121. /* not supported for legacy keys */
  122. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  123. return -2;
  124. }
  125. int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
  126. {
  127. EVP_PKEY *pkey = ctx->pkey;
  128. int ok;
  129. if (pkey == NULL) {
  130. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  131. return 0;
  132. }
  133. if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR,
  134. OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)
  135. return ok;
  136. /* not supported for legacy keys */
  137. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  138. return -2;
  139. }
  140. int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
  141. {
  142. EVP_PKEY *pkey = ctx->pkey;
  143. int ok;
  144. if (pkey == NULL) {
  145. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  146. return 0;
  147. }
  148. if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR,
  149. OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)
  150. return ok;
  151. if (pkey->type == EVP_PKEY_NONE)
  152. goto not_supported;
  153. #ifndef FIPS_MODULE
  154. /* legacy */
  155. /* call customized check function first */
  156. if (ctx->pmeth->check != NULL)
  157. return ctx->pmeth->check(pkey);
  158. /* use default check function in ameth */
  159. if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL)
  160. goto not_supported;
  161. return pkey->ameth->pkey_check(pkey);
  162. #endif
  163. not_supported:
  164. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  165. return -2;
  166. }