x509type.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 1995-2018 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 "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_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_DSA:
  33. ret = EVP_PK_DSA | EVP_PKT_SIGN;
  34. break;
  35. case EVP_PKEY_EC:
  36. ret = EVP_PK_EC | EVP_PKT_SIGN | EVP_PKT_EXCH;
  37. break;
  38. case EVP_PKEY_ED448:
  39. case EVP_PKEY_ED25519:
  40. ret = EVP_PKT_SIGN;
  41. break;
  42. case EVP_PKEY_DH:
  43. ret = EVP_PK_DH | EVP_PKT_EXCH;
  44. break;
  45. case NID_id_GostR3410_2001:
  46. case NID_id_GostR3410_2012_256:
  47. case NID_id_GostR3410_2012_512:
  48. ret = EVP_PKT_EXCH | EVP_PKT_SIGN;
  49. break;
  50. default:
  51. break;
  52. }
  53. i = X509_get_signature_nid(x);
  54. if (i && OBJ_find_sigid_algs(i, NULL, &i)) {
  55. switch (i) {
  56. case NID_rsaEncryption:
  57. case NID_rsa:
  58. ret |= EVP_PKS_RSA;
  59. break;
  60. case NID_dsa:
  61. case NID_dsa_2:
  62. ret |= EVP_PKS_DSA;
  63. break;
  64. case NID_X9_62_id_ecPublicKey:
  65. ret |= EVP_PKS_EC;
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. return ret;
  72. }