C++ OpenSSL 之四:CER转换为PEM
1.等同于使用: openssl x509 -in "cer_path" -inform DER -out "save_path" -outform PEM
2.代码如下:cerFilePath是cer文件;cer文件是csr文件请求后获得。
bool MakePemSSL(const char* cerFilePath, const char* savePemFilePath) {
int ret = 0;
X509 *x509 = NULL;
FILE *cerFile = NULL, *pemFile = NULL;
cerFile = fopen(cerFilePath, "rb");
if (cerFile == NULL) {
fprintf(stderr, "MakeLocalPemSSL fopen cerFilePath err \n");
goto free_all;
}
pemFile = fopen(savePemFilePath, "w+");
if (pemFile == NULL) {
fprintf(stderr, "MakeLocalPemSSL fopen savePemFilePath err \n");
goto free_all;
}
x509 = d2i_X509_fp(cerFile, NULL);
if (x509 == NULL) {
fprintf(stderr, "MakeLocalPemSSL failed to parse to X509 from cerFile \n");
goto free_all;
}
ret = PEM_write_X509(pemFile, x509);
if (ret != 1) {
fprintf(stderr, "MakeLocalPemSSL failed to PEM_write_X509 \n");
goto free_all;
}
free_all:
if (cerFile) fclose(cerFile);
if (pemFile) fclose(pemFile);
return (ret == 1);
}
以上。
《C++ OpenSSL 之一:编译和使用》
《C++ OpenSSL 之二:生成RSA文件》
《C++ OpenSSL 之三:生成CSR文件》
《C++ OpenSSL 之四:CER转换为PEM》
《C++ OpenSSL 之五:生成P12文件》