Older/ToolKit/Util/SSLUtil.h

122 lines
4.4 KiB
C
Raw Normal View History

2024-09-28 23:55:00 +08:00
/*
* Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
*
* This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef ZLTOOLKIT_SSLUTIL_H
#define ZLTOOLKIT_SSLUTIL_H
#include <memory>
#include <string>
#include <vector>
typedef struct x509_st X509;
typedef struct evp_pkey_st EVP_PKEY;
typedef struct ssl_ctx_st SSL_CTX;
typedef struct ssl_st SSL;
typedef struct bio_st BIO;
namespace toolkit {
/**
* ssl证书后缀一般分为以下几种
* pem:base64的字符编码串
* cer:pem的私钥配合使用
* p12:
*/
class SSLUtil {
public:
static std::string getLastError();
/**
* pem,p12,cer后缀
* openssl加载p12证书时会校验公钥和私钥是否匹对p12的公钥时可能需要传入证书密码
* @param file_path_or_data
* @param isFile
* @return
*/
static std::vector<std::shared_ptr<X509> > loadPublicKey(const std::string &file_path_or_data, const std::string &passwd = "", bool isFile = true);
/**
* pem,p12后缀
* @param file_path_or_data
* @param passwd
* @param isFile
* @return
*/
static std::shared_ptr<EVP_PKEY> loadPrivateKey(const std::string &file_path_or_data, const std::string &passwd = "", bool isFile = true);
/**
* SSL_CTX对象
* @param cer
* @param key
* @param serverMode
* @return SSL_CTX对象
*/
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);
/**
* ssl对象
* @param ctx SSL_CTX对象
*/
static std::shared_ptr<SSL> makeSSL(SSL_CTX *ctx);
/**
* specifies that the default locations from which CA certificates are loaded should be used.
* There is one default directory and one default file.
* The default CA certificates directory is called "certs" in the default OpenSSL directory.
* Alternatively the SSL_CERT_DIR environment variable can be defined to override this location.
* The default CA certificates file is called "cert.pem" in the default OpenSSL directory.
* Alternatively the SSL_CERT_FILE environment variable can be defined to override this location.
* /usr/local/ssl/certs//usr/local/ssl/cert.pem的证书
* SSL_CERT_FILE将替换/usr/local/ssl/cert.pem的路径
*/
static bool loadDefaultCAs(SSL_CTX *ctx);
/**
*
*/
static bool trustCertificate(SSL_CTX *ctx, X509 *cer);
/**
*
* @param cer
* @param ... CA根证书X509类型nullptr结尾
* @return
*/
static bool verifyX509(X509 *cer, ...);
/**
* 使
* @param cer ras的公钥
* @param in_str 245256
* @param enc_or_dec true:,false:
* @return
*/
static std::string cryptWithRsaPublicKey(X509 *cer, const std::string &in_str, bool enc_or_dec);
/**
* 使
* @param private_key ras的私钥
* @param in_str 245256
* @param enc_or_dec true:,false:
* @return
*/
static std::string cryptWithRsaPrivateKey(EVP_PKEY *private_key, const std::string &in_str, bool enc_or_dec);
/**
*
* @param cer
* @return
*/
static std::string getServerName(X509 *cer);
};
}//namespace toolkit
#endif //ZLTOOLKIT_SSLUTIL_H