SSLUtil.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef ZLTOOLKIT_SSLUTIL_H
  11. #define ZLTOOLKIT_SSLUTIL_H
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. typedef struct x509_st X509;
  16. typedef struct evp_pkey_st EVP_PKEY;
  17. typedef struct ssl_ctx_st SSL_CTX;
  18. typedef struct ssl_st SSL;
  19. typedef struct bio_st BIO;
  20. namespace toolkit {
  21. /**
  22. * ssl证书后缀一般分为以下几种
  23. * pem:这个是base64的字符编码串,可能存在公钥、私钥或者两者都存在
  24. * cer:只且只能是公钥,可以与pem的私钥配合使用
  25. * p12:必须包括私钥和公钥
  26. */
  27. class SSLUtil {
  28. public:
  29. static std::string getLastError();
  30. /**
  31. * 加载公钥证书,支持pem,p12,cer后缀
  32. * 由于openssl加载p12证书时会校验公钥和私钥是否匹对,所以加载p12的公钥时可能需要传入证书密码
  33. * @param file_path_or_data 文件路径或文件内容
  34. * @param isFile 是否为文件
  35. * @return 公钥证书列表
  36. */
  37. static std::vector<std::shared_ptr<X509> > loadPublicKey(const std::string &file_path_or_data, const std::string &passwd = "", bool isFile = true);
  38. /**
  39. * 加载私钥证书,支持pem,p12后缀
  40. * @param file_path_or_data 文件路径或文件内容
  41. * @param passwd 密码
  42. * @param isFile 是否为文件
  43. * @return 私钥证书
  44. */
  45. static std::shared_ptr<EVP_PKEY> loadPrivateKey(const std::string &file_path_or_data, const std::string &passwd = "", bool isFile = true);
  46. /**
  47. * 创建SSL_CTX对象
  48. * @param cer 公钥数组
  49. * @param key 私钥
  50. * @param serverMode 是否为服务器模式或客户端模式
  51. * @return SSL_CTX对象
  52. */
  53. static std::shared_ptr<SSL_CTX> makeSSLContext(const std::vector<std::shared_ptr<X509> > &cers, const std::shared_ptr<EVP_PKEY> &key, bool serverMode = true, bool checkKey = false);
  54. /**
  55. * 创建ssl对象
  56. * @param ctx SSL_CTX对象
  57. */
  58. static std::shared_ptr<SSL> makeSSL(SSL_CTX *ctx);
  59. /**
  60. * specifies that the default locations from which CA certificates are loaded should be used.
  61. * There is one default directory and one default file.
  62. * The default CA certificates directory is called "certs" in the default OpenSSL directory.
  63. * Alternatively the SSL_CERT_DIR environment variable can be defined to override this location.
  64. * The default CA certificates file is called "cert.pem" in the default OpenSSL directory.
  65. * Alternatively the SSL_CERT_FILE environment variable can be defined to override this location.
  66. * 信任/usr/local/ssl/certs/目录下的所有证书/usr/local/ssl/cert.pem的证书
  67. * 环境变量SSL_CERT_FILE将替换/usr/local/ssl/cert.pem的路径
  68. */
  69. static bool loadDefaultCAs(SSL_CTX *ctx);
  70. /**
  71. * 信任某公钥
  72. */
  73. static bool trustCertificate(SSL_CTX *ctx, X509 *cer);
  74. /**
  75. * 验证证书合法性
  76. * @param cer 待验证的证书
  77. * @param ... 信任的CA根证书,X509类型,以nullptr结尾
  78. * @return 是否合法
  79. */
  80. static bool verifyX509(X509 *cer, ...);
  81. /**
  82. * 使用公钥加解密数据
  83. * @param cer 公钥,必须为ras的公钥
  84. * @param in_str 加密或解密的原始数据,实测加密最大支持245个字节,加密后数据长度固定为256个字节
  85. * @param enc_or_dec true:加密,false:解密
  86. * @return 加密或解密后的数据
  87. */
  88. static std::string cryptWithRsaPublicKey(X509 *cer, const std::string &in_str, bool enc_or_dec);
  89. /**
  90. * 使用私钥加解密数据
  91. * @param private_key 私钥,必须为ras的私钥
  92. * @param in_str 加密或解密的原始数据,实测加密最大支持245个字节,加密后数据长度固定为256个字节
  93. * @param enc_or_dec true:加密,false:解密
  94. * @return 加密或解密后的数据
  95. */
  96. static std::string cryptWithRsaPrivateKey(EVP_PKEY *private_key, const std::string &in_str, bool enc_or_dec);
  97. /**
  98. * 获取证书域名
  99. * @param cer 证书公钥
  100. * @return 证书域名
  101. */
  102. static std::string getServerName(X509 *cer);
  103. };
  104. }//namespace toolkit
  105. #endif //ZLTOOLKIT_SSLUTIL_H