文章目录
- openssl3.2 - exp - buffer to BIO
- 概述
- 笔记
- END
openssl3.2 - exp - buffer to BIO
概述
openssl的资料看的差不多了, 准备将工程中用到的知识点整理一下.
openssl中很多API是以操作文件作为输入的, 也有很多API是以BIO作为输入的.
不管文件是不是受保护的, 如果有可能都在内存操作, 不落地.
为了防止要操作的内容落地, 最好是操作BIO.
为了操作BIO, 就需要将要操作的数据放到BIO中, 供openssl的API操作.
做了一个实验, 将工程中资源中包含的数据, 放到BIO中.
如果要操作的数据就是内存数据, 那么就先放到BIO中, 再用openssl的API来操作BIO.
笔记
/*!
* \file main.cpp
* \note openssl3.2 - exp - buffer to BIO
* vs2019 + vc++ + console
*/#include "my_openSSL_lib.h"
#include "openssl/bio.h"#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstdint> // for uint8_t
#include "resource.h" // for IDR_X
#include <assert.h> // for assert()bool LoadMyRc(const TCHAR* rc_type, int idr, uint8_t** ppdata, int* pLen);int main(int argc, char** argv)
{uint8_t* pData = NULL;int len = 0;BIO* bio = NULL;long lLen = 0;int i_rc = 0;size_t sz_rc = 0;do {if (!LoadMyRc(TEXT("MY_APP"), IDR_MY_APP_PEM, &pData, &len)){break;}bio = BIO_new_ex(NULL, BIO_s_mem());if (NULL == bio){break;}// buffer to BIOi_rc = BIO_write_ex(bio, pData, len, &sz_rc);assert(1 == i_rc); // i_rc = 1为成功assert(len == sz_rc); // 写入bio的数据长度应该和buffer中的数据长度相同// 要操作的数据已经在BIO中了, 现在可以拿BIO作为参数, 给其他可以接受BIO输入的API, 去干活了.} while (false);if (NULL != bio){BIO_free(bio);}if (NULL != pData){delete[] pData;}return 0;
}bool LoadMyRc(const TCHAR* rc_type, int idr, uint8_t** ppdata, int* pLen)
{bool b_rc = false;HRSRC hrSrc = NULL;LPCWSTR lpcwstr = NULL;HGLOBAL hg = NULL;int nResSize = 0;LPVOID pResValue = NULL;do {if ((NULL == rc_type) ||(NULL == ppdata) || (NULL == pLen)){break;}// 参数1为NULL, 是找自己进程的资源// 参数2只能用MAKEINTRESOURCE来转ID到LPCWSTR, 这个指针既不是名称字符串, 也不是直接的内容hrSrc = ::FindResource(NULL, MAKEINTRESOURCE(idr), rc_type);if (NULL == hrSrc){break;}hg = ::LoadResource(NULL, hrSrc);if (NULL == hg){break;}LPVOID pResValue = LockResource(hg);if (NULL == pResValue){break;} nResSize = SizeofResource(NULL, hrSrc);if (0 == nResSize){break;}// 如果是新建buffer, 传给openssl用, 必须用OPENSSL_malloc分配内存空间*ppdata = (uint8_t *)OPENSSL_malloc(nResSize);if (NULL == *ppdata){break;}memcpy(*ppdata, pResValue, nResSize);*pLen = nResSize;b_rc = true;} while (false);if (NULL != hrSrc){::FreeResource(hrSrc);}return b_rc;
}
/*!
\file my_openSSL_lib.h
*/#ifndef __MY_OPENSSL_LIB_H__
#define __MY_OPENSSL_LIB_H__#ifdef _WIN32
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib") // for select()#include <windows.h>#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")#endif /* #ifdef _WIN32 */// --------------------------------------------------------------------------------
// 开关宏 - begin
// --------------------------------------------------------------------------------#define MY_USE_APPLINK// --------------------------------------------------------------------------------
// 开关宏 - END
// --------------------------------------------------------------------------------#endif /* #ifndef __MY_OPENSSL_LIB_H__ */
/*!
* \file D:\my_dev\my_local_git_prj\study\openSSL\nmake_test\test_c\prj_005_afalgtest.c\my_openSSL_lib.c
*/#include "my_openSSL_lib.h"#ifdef MY_USE_APPLINK
#include <openssl/applink.c> /*! for OPENSSL_Uplink(00007FF8B7EF0FE8,08): no OPENSSL_Applink */
#endif // #ifdef MY_USE_APPLINK