最近一直在研究C++访问java写的https的网站的程序,由于本人对证书的知识知道比较少,进展的特别慢,其中也查了不少资料关于证书的。证书类型这里写从别人的blog里摘的地址没记住转一点了。
PKCS 全称是 Public-Key Cryptography Standards ,是由 RSA 实验室与其它安全系统开发商为促进公钥密码的发展而制订的一系列标准,PKCS 目前共发布过 15 个标准。 常用的有:
PKCS#7 Cryptographic Message Syntax Standard
PKCS#10 Certification Request Standard
PKCS#12 Personal Information Exchange Syntax Standard
X.509是常见通用的证书格式。所有的证书都符合为Public Key Infrastructure (PKI) 制定的 ITU-T X509 国际标准。
PKCS#7 常用的后缀是: .P7B .P7C .SPC
PKCS#12 常用的后缀有: .P12 .PFX
X.509 DER 编码(ASCII)的后缀是: .DER .CER .CRT
X.509 PAM 编码(Base64)的后缀是: .PEM .CER .CRT
.cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。
.pem跟crt/cer的区别是它以Ascii来表示。
pfx/p12用于存放个人证书/私钥,他通常包含保护密码,2进制方式
p10是证书请求
p7r是CA对证书请求的回复,只用于导入
p7b以树状展示证书链(certificate chain),同时也支持单个证书,不含私钥。
由于我们是分开来做的,我做c++的,同事做java的。java服务给我们一个keystore类型的证书和一个crt的证书。
我开始测试的代码如下,简单的说就是使用wininet进行实现。
代码是在codeproject上下载的地址http://u.115.com/file/f3f67596a8#SSLConnection.rar我存到网盘上了
源地址
*SSL/TLS客户端程序WIN32版(以demos/cli.cpp为基础)
*需要用到动态连接库libeay32.dll,ssleay.dll,
*同时在setting中加入ws2_32.lib libeay32.lib ssleay32.lib,
*以上库文件在编译openssl后可在out32dll目录下找到,
*所需证书文件请参照文章自行生成*/
//*****************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include "openssl/crypto.h"
#include "openssl/x509.h"
#include "openssl/pem.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/rand.h"
#pragma comment(lib, "ssleay32.lib")
#pragma comment(lib, "ws2_32.lib")
//ws2_32.lib libeay32.lib ssleay32.lib
/*所有需要的参数信息都在此处以#define的形式提供*/
#define CERTF "client.csr" /*客户端的证书(需经CA签名).crt*/
#define KEYF "client.key" /*客户端的私钥(建议加密存储)*/
#define CACERT "ca.crt" /*CA 的证书*/
#define PORT 9002 /*服务端的端口*/
#define SERVER_ADDR "127.0.0.1" /*服务段的IP地址*/
#define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(-2); }
#define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(-3); }
{
int err;
int sd;
struct sockaddr_in sa;
SSL_CTX* ctx;
SSL* ssl;
X509* server_cert;
char* str;
char buf [4096];
SSL_METHOD *meth;
int seed_int[100]; /*存放随机序列*/
{
printf("WSAStartup()fail:%d/n",GetLastError());
return -1;
}
SSL_load_error_strings(); /*为打印调试信息作准备*/
ctx = SSL_CTX_new (meth);
CHK_NULL(ctx);
//SSL_CTX_load_verify_locations(ctx,CACERT,NULL); /*若验证,则放置CA证书*/
//if (SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM) <= 0)
//{
//ERR_print_errors_fp(stderr);
//exit(-2);
//}
//if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0)
//{
//ERR_print_errors_fp(stderr);
//exit(-3);
//}
//{
//printf("Private key does not match the certificate public key/n");
//exit(-4);
//}
srand( (unsigned)time( NULL ) );
for( int i = 0; i < 100;i++ )
seed_int[i] = rand();
RAND_seed(seed_int, sizeof(seed_int));
printf("Begin tcp socket.../n");
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr (SERVER_ADDR); /* Server IP */
sa.sin_port = htons (PORT); /* Server Port number */
sizeof(sa));
CHK_ERR(err, "connect");
printf("Begin SSL negotiation /n");
//system("pasue");
ssl = SSL_new (ctx);
CHK_NULL(ssl);
err = SSL_connect (ssl);
CHK_SSL(err);
printf ("SSL connection using %s/n", SSL_get_cipher (ssl));
server_cert = SSL_get_peer_certificate (ssl);
CHK_NULL(server_cert);
printf ("Server certificate:/n");
CHK_NULL(str);
printf ("/t subject: %s/n", str);
//free (str);
CHK_NULL(str);
printf ("/t issuer: %s/n", str);
//free (str);
printf("Begin SSL data exchange/n");
CHK_SSL(err);
CHK_SSL(err);
printf ("Got %d chars:'%s'/n", err, buf);
SSL_shutdown (ssl); /* send SSL/TLS close_notify */
shutdown (sd,2);
SSL_free (ssl);
SSL_CTX_free (ctx);
}
/*****************************************************************
* EOF - cli.cpp
*****************************************************************/