pmeth_check.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2006-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 <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. int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
  19. {
  20. EVP_PKEY *pkey = ctx->pkey;
  21. void *key;
  22. EVP_KEYMGMT *keymgmt;
  23. if (pkey == NULL) {
  24. EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
  25. return 0;
  26. }
  27. keymgmt = pkey->keymgmt;
  28. key = pkey->keydata;
  29. if (key != NULL && keymgmt != NULL)
  30. return evp_keymgmt_validate(keymgmt, key,
  31. OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
  32. if (pkey->type == EVP_PKEY_NONE)
  33. goto not_supported;
  34. #ifndef FIPS_MODULE
  35. /* legacy */
  36. /* call customized public key check function first */
  37. if (ctx->pmeth->public_check != NULL)
  38. return ctx->pmeth->public_check(pkey);
  39. /* use default public key check function in ameth */
  40. if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL)
  41. goto not_supported;
  42. return pkey->ameth->pkey_public_check(pkey);
  43. #endif
  44. not_supported:
  45. EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  46. return -2;
  47. }
  48. int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
  49. {
  50. EVP_PKEY *pkey = ctx->pkey;
  51. void *key;
  52. EVP_KEYMGMT *keymgmt;
  53. if (pkey == NULL) {
  54. EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
  55. return 0;
  56. }
  57. keymgmt = pkey->keymgmt;
  58. key = pkey->keydata;
  59. if (key != NULL && keymgmt != NULL)
  60. return evp_keymgmt_validate(keymgmt, key,
  61. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
  62. if (pkey->type == EVP_PKEY_NONE)
  63. goto not_supported;
  64. #ifndef FIPS_MODULE
  65. /* legacy */
  66. /* call customized param check function first */
  67. if (ctx->pmeth->param_check != NULL)
  68. return ctx->pmeth->param_check(pkey);
  69. /* use default param check function in ameth */
  70. if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL)
  71. goto not_supported;
  72. return pkey->ameth->pkey_param_check(pkey);
  73. #endif
  74. not_supported:
  75. EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  76. return -2;
  77. }
  78. int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
  79. {
  80. EVP_PKEY *pkey = ctx->pkey;
  81. void *key;
  82. EVP_KEYMGMT *keymgmt;
  83. if (pkey == NULL) {
  84. EVPerr(0, EVP_R_NO_KEY_SET);
  85. return 0;
  86. }
  87. keymgmt = pkey->keymgmt;
  88. key = pkey->keydata;
  89. if (key != NULL && keymgmt != NULL)
  90. return evp_keymgmt_validate(keymgmt, key,
  91. OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
  92. /* not supported for legacy keys */
  93. EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  94. return -2;
  95. }
  96. int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
  97. {
  98. EVP_PKEY *pkey = ctx->pkey;
  99. void *key;
  100. EVP_KEYMGMT *keymgmt;
  101. if (pkey == NULL) {
  102. EVPerr(0, EVP_R_NO_KEY_SET);
  103. return 0;
  104. }
  105. keymgmt = pkey->keymgmt;
  106. key = pkey->keydata;
  107. if (key != NULL && keymgmt != NULL)
  108. return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_KEYPAIR);
  109. /* not supported for legacy keys */
  110. EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  111. return -2;
  112. }
  113. int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
  114. {
  115. EVP_PKEY *pkey = ctx->pkey;
  116. void *key;
  117. EVP_KEYMGMT *keymgmt;
  118. if (pkey == NULL) {
  119. EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
  120. return 0;
  121. }
  122. keymgmt = pkey->keymgmt;
  123. key = pkey->keydata;
  124. if (key != NULL && keymgmt != NULL)
  125. return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_ALL);
  126. if (pkey->type == EVP_PKEY_NONE)
  127. goto not_supported;
  128. #ifndef FIPS_MODULE
  129. /* legacy */
  130. /* call customized check function first */
  131. if (ctx->pmeth->check != NULL)
  132. return ctx->pmeth->check(pkey);
  133. /* use default check function in ameth */
  134. if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL)
  135. goto not_supported;
  136. return pkey->ameth->pkey_check(pkey);
  137. #endif
  138. not_supported:
  139. EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  140. return -2;
  141. }