OpenSSLCertUtils.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <string>
  18. #include <vector>
  19. #include <folly/Optional.h>
  20. #include <folly/io/IOBuf.h>
  21. #include <folly/portability/OpenSSL.h>
  22. #include <folly/ssl/OpenSSLPtrTypes.h>
  23. namespace folly {
  24. namespace ssl {
  25. class OpenSSLCertUtils {
  26. public:
  27. // Note: non-const until OpenSSL 1.1.0
  28. static Optional<std::string> getCommonName(X509& x509);
  29. static std::vector<std::string> getSubjectAltNames(X509& x509);
  30. /*
  31. * Return the subject name, if any, from the cert
  32. * @param x509 Reference to an X509
  33. * @return a folly::Optional<std::string>, or folly::none
  34. */
  35. static Optional<std::string> getSubject(X509& x509);
  36. /*
  37. * Return the issuer name, if any, from the cert
  38. * @param x509 Reference to an X509
  39. * @return a folly::Optional<std::string>, or folly::none
  40. */
  41. static Optional<std::string> getIssuer(X509& x509);
  42. /*
  43. * Get a string representation of the not-before time on the certificate
  44. */
  45. static std::string getNotBeforeTime(X509& x509);
  46. /*
  47. * Get a string representation of the not-after (expiration) time
  48. */
  49. static std::string getNotAfterTime(X509& x509);
  50. /*
  51. * Summarize the CN, Subject, Issuer, Validity, and extensions as a string
  52. */
  53. static folly::Optional<std::string> toString(X509& x509);
  54. /**
  55. * Decodes the DER representation of an X509 certificate.
  56. *
  57. * Throws on error (if a valid certificate can't be decoded).
  58. */
  59. static X509UniquePtr derDecode(ByteRange);
  60. /**
  61. * DER encodes an X509 certificate.
  62. *
  63. * Throws on error.
  64. */
  65. static std::unique_ptr<IOBuf> derEncode(X509&);
  66. /**
  67. * Reads certificates from memory and returns them as a vector of X509
  68. * pointers.
  69. */
  70. static std::vector<X509UniquePtr> readCertsFromBuffer(ByteRange);
  71. /**
  72. * Return the output of the X509_digest for chosen message-digest algo
  73. * NOTE: The returned digest will be in binary, and may need to be
  74. * hex-encoded
  75. */
  76. static std::array<uint8_t, SHA_DIGEST_LENGTH> getDigestSha1(X509& x509);
  77. static std::array<uint8_t, SHA256_DIGEST_LENGTH> getDigestSha256(X509& x509);
  78. /**
  79. * Reads a store from a file (or buffer). Throws on error.
  80. */
  81. static X509StoreUniquePtr readStoreFromFile(std::string caFile);
  82. static X509StoreUniquePtr readStoreFromBuffer(ByteRange);
  83. private:
  84. static std::string getDateTimeStr(const ASN1_TIME* time);
  85. };
  86. } // namespace ssl
  87. } // namespace folly