#include <folly/Portability.h>
#include <folly/Range.h>
+#include <folly/String.h>
#include <folly/io/async/ssl/OpenSSLUtils.h>
#include <folly/portability/OpenSSL.h>
#include <folly/ssl/OpenSSLLockTypes.h>
*/
class SSLContext {
public:
-
enum SSLVersion {
- SSLv2,
- SSLv3,
- TLSv1
+ SSLv2,
+ SSLv3,
+ TLSv1, // support TLS 1.0+
+ TLSv1_2, // support for only TLS 1.2+
};
/**
*/
virtual void ciphers(const std::string& ciphers);
- /**
- * Set default ciphers to be used in SSL handshake process.
- *
- * @param ciphers A list of ciphers to use for TLS.
- */
- virtual void setCipherList(const std::vector<std::string>& ciphers);
-
/**
* Low-level method that attempts to set the provided ciphers on the
* SSL_CTX object, and throws if something goes wrong.
virtual void setCiphersOrThrow(const std::string& ciphers);
/**
- * Sets the signature algorithms to be used during SSL negotiation
- * for TLS1.2+
- *
- * @param sigalgs A list of signature algorithms, eg. RSA+SHA512
+ * Set default ciphers to be used in SSL handshake process.
*/
- void setSignatureAlgorithms(const std::vector<std::string>& sigalgs);
+
+ template <typename Iterator>
+ void setCipherList(Iterator ibegin, Iterator iend) {
+ if (ibegin != iend) {
+ std::string opensslCipherList;
+ folly::join(":", ibegin, iend, opensslCipherList);
+ setCiphersOrThrow(opensslCipherList);
+ }
+ }
+
+ template <typename Container>
+ void setCipherList(const Container& cipherList) {
+ using namespace std;
+ setCipherList(begin(cipherList), end(cipherList));
+ }
+
+ template <typename Value>
+ void setCipherList(const std::initializer_list<Value>& cipherList) {
+ setCipherList(cipherList.begin(), cipherList.end());
+ }
+
+ /**
+ * Sets the signature algorithms to be used during SSL negotiation
+ * for TLS1.2+.
+ */
+
+ template <typename Iterator>
+ void setSignatureAlgorithms(Iterator ibegin, Iterator iend) {
+ if (ibegin != iend) {
+#if OPENSSL_VERSION_NUMBER >= 0x1000200fL
+ std::string opensslSigAlgsList;
+ join(":", ibegin, iend, opensslSigAlgsList);
+ if (!SSL_CTX_set1_sigalgs_list(ctx_, opensslSigAlgsList.c_str())) {
+ throw std::runtime_error("SSL_CTX_set1_sigalgs_list " + getErrors());
+ }
+#endif
+ }
+ }
+
+ template <typename Container>
+ void setSignatureAlgorithms(const Container& sigalgs) {
+ using namespace std;
+ setSignatureAlgorithms(begin(sigalgs), end(sigalgs));
+ }
+
+ template <typename Value>
+ void setSignatureAlgorithms(const std::initializer_list<Value>& sigalgs) {
+ setSignatureAlgorithms(sigalgs.begin(), sigalgs.end());
+ }
/**
* Sets the list of EC curves supported by the client.
* @param cert A PEM formatted certificate
*/
virtual void loadCertificateFromBufferPEM(folly::StringPiece cert);
+
/**
* Load private key.
*
* @param pkey A PEM formatted key
*/
virtual void loadPrivateKeyFromBufferPEM(folly::StringPiece pkey);
+
+ /**
+ * Load cert and key from PEM buffers. Guaranteed to throw if cert and
+ * private key mismatch so no need to call isCertKeyPairValid.
+ *
+ * @param cert A PEM formatted certificate
+ * @param pkey A PEM formatted key
+ */
+ virtual void loadCertKeyPairFromBufferPEM(
+ folly::StringPiece cert,
+ folly::StringPiece pkey);
+
+ /**
+ * Load cert and key from files. Guaranteed to throw if cert and key mismatch.
+ * Equivalent to calling loadCertificate() and loadPrivateKey().
+ *
+ * @param certPath Path to the certificate file
+ * @param keyPath Path to the private key file
+ * @param certFormat Certificate file format
+ * @param keyFormat Private key file format
+ */
+ virtual void loadCertKeyPairFromFiles(
+ const char* certPath,
+ const char* keyPath,
+ const char* certFormat = "PEM",
+ const char* keyFormat = "PEM");
+
+ /**
+ * Call after both cert and key are loaded to check if cert matches key.
+ * Must call if private key is loaded before loading the cert.
+ * No need to call if cert is loaded first before private key.
+ * @return true if matches, or false if mismatch.
+ */
+ virtual bool isCertKeyPairValid() const;
+
/**
* Load trusted certificates from specified file.
*
return ctx_;
}
- /**
- * Set preferences for how to treat locks in OpenSSL. This must be
- * called before the instantiation of any SSLContext objects, otherwise
- * the defaults will be used.
- *
- * OpenSSL has a lock for each module rather than for each object or
- * data that needs locking. Some locks protect only refcounts, and
- * might be better as spinlocks rather than mutexes. Other locks
- * may be totally unnecessary if the objects being protected are not
- * shared between threads in the application.
- *
- * By default, all locks are initialized as mutexes. OpenSSL's lock usage
- * may change from version to version and you should know what you are doing
- * before disabling any locks entirely.
- *
- * Example: if you don't share SSL sessions between threads in your
- * application, you may be able to do this
- *
- * setSSLLockTypes({{CRYPTO_LOCK_SSL_SESSION, SSLContext::LOCK_NONE}})
- */
- FOLLY_DEPRECATED("Use folly::ssl::setLockTypes")
- static void setSSLLockTypes(std::map<int, ssl::LockType> lockTypes);
-
/**
* Examine OpenSSL's error stack, and return a string description of the
* errors.