x509type.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 1995-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 "internal/cryptlib.h"
  11. #include <openssl/evp.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/x509.h>
  14. int X509_certificate_type(const X509 *x, const EVP_PKEY *pkey)
  15. {
  16. const EVP_PKEY *pk;
  17. int ret = 0, i;
  18. if (x == NULL)
  19. return 0;
  20. if (pkey == NULL)
  21. pk = X509_get0_pubkey(x);
  22. else
  23. pk = pkey;
  24. if (pk == NULL)
  25. return 0;
  26. switch (EVP_PKEY_get_id(pk)) {
  27. case EVP_PKEY_RSA:
  28. ret = EVP_PK_RSA | EVP_PKT_SIGN;
  29. /* if (!sign only extension) */
  30. ret |= EVP_PKT_ENC;
  31. break;
  32. case EVP_PKEY_RSA_PSS:
  33. ret = EVP_PK_RSA | EVP_PKT_SIGN;
  34. break;
  35. case EVP_PKEY_DSA:
  36. ret = EVP_PK_DSA | EVP_PKT_SIGN;
  37. break;
  38. case EVP_PKEY_EC:
  39. ret = EVP_PK_EC | EVP_PKT_SIGN | EVP_PKT_EXCH;
  40. break;
  41. case EVP_PKEY_ED448:
  42. case EVP_PKEY_ED25519:
  43. ret = EVP_PKT_SIGN;
  44. break;
  45. case EVP_PKEY_DH:
  46. ret = EVP_PK_DH | EVP_PKT_EXCH;
  47. break;
  48. case NID_id_GostR3410_2001:
  49. case NID_id_GostR3410_2012_256:
  50. case NID_id_GostR3410_2012_512:
  51. ret = EVP_PKT_EXCH | EVP_PKT_SIGN;
  52. break;
  53. default:
  54. break;
  55. }
  56. i = X509_get_signature_nid(x);
  57. if (i && OBJ_find_sigid_algs(i, NULL, &i)) {
  58. switch (i) {
  59. case NID_rsaEncryption:
  60. case NID_rsa:
  61. ret |= EVP_PKS_RSA;
  62. break;
  63. case NID_dsa:
  64. case NID_dsa_2:
  65. ret |= EVP_PKS_DSA;
  66. break;
  67. case NID_X9_62_id_ecPublicKey:
  68. ret |= EVP_PKS_EC;
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. return ret;
  75. }