SChannel
1
#define _WIN32_WINNT 0x0400
2
#define SECURITY_WIN32
3
#include <windows.h>
4
#include <wincrypt.h>
5
#include<stdio.h>
6
#include<stdlib.h>
7
8
//header for TLS/SSL
9
#include<SChannel.h>
10
#include<Security.h>
11
#include <Schnlsp.h>
12
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
13
#define KEYLENGTH 0x00800000
14
#define MACHINE_NAME "shipf"
15
16
PCCERT_CONTEXT getServerCertificate ()
17
{
18
HCERTSTORE hMyCertStore = NULL;
19
PCCERT_CONTEXT aCertContext;
20
SCHANNEL_CRED sslCredentials;
21
CredHandle hCredentials;
22
TimeStamp tsExpires;
23
SECURITY_STATUS ss = {0};
24
25
26
//-------------------------------------------------------
27
// Open the My store, also called the personal store.
28
// This call to CertOpenStore opens the Local_Machine My
29
// store as opposed to the Current_User's My store.
30
31
hMyCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM,
32
X509_ASN_ENCODING,
33
0,
34
CERT_SYSTEM_STORE_LOCAL_MACHINE,
35
L"MY");
36
37
if (hMyCertStore == NULL)
38
{
39
printf("Error opening MY store for server.\n");
40
goto cleanup;
41
}
42
else
43
printf("Opening My store for server OK.\n");
44
//-------------------------------------------------------
45
// Search for a certificate with some specified
46
// string in it. This example attempts to find
47
// a certificate with the string "example server" in
48
// its subject string. Substitute an appropriate string
49
// to find a certificate for a specific user.
50
51
aCertContext = CertFindCertificateInStore(hMyCertStore,
52
X509_ASN_ENCODING,
53
0,
54
CERT_FIND_SUBJECT_STR_A,
55
MACHINE_NAME, // use appropriate subject name
56
NULL
57
);
58
59
if (aCertContext == NULL)
60
{
61
printf("Error retrieving server certificate.");
62
goto cleanup;
63
}
64
else
65
printf("Retrieving server certificate OK.\n");
66
67
sslCredentials.dwVersion = SCHANNEL_CRED_VERSION;
68
sslCredentials.cCreds = 1;
69
sslCredentials.paCred = &aCertContext;
70
sslCredentials.grbitEnabledProtocols = SP_PROT_TLS1_SERVER;
71
72
ss = AcquireCredentialsHandle(NULL,
73
UNISP_NAME,
74
SECPKG_CRED_INBOUND,
75
NULL,
76
&sslCredentials,
77
NULL,
78
NULL,
79
&hCredentials,
80
&tsExpires
81
);
82
83
if(ss != SEC_E_OK)
84
{
85
if(ss == SEC_E_NO_CREDENTIALS)
86
printf("SEC_E_NO_CREDENTIALS \n");
87
printf("Error Call AcquireCredentialsHandle.\n");
88
goto cleanup;
89
}
90
else{
91
printf("Call AcquireCredentialsHandle OK.\n");
92
}
93
94
cleanup:
95
if(hMyCertStore)
96
{
97
CertCloseStore(hMyCertStore,0);
98
}
99
return aCertContext;
100
}
101
102
int main()
103
{
104
printf("hello world\n");
105
getServerCertificate();
106
}
#define _WIN32_WINNT 0x04002
#define SECURITY_WIN32 3
#include <windows.h>4
#include <wincrypt.h>5
#include<stdio.h>6
#include<stdlib.h>7

8
//header for TLS/SSL9
#include<SChannel.h>10
#include<Security.h> 11
#include <Schnlsp.h>12
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)13
#define KEYLENGTH 0x0080000014
#define MACHINE_NAME "shipf"15

16
PCCERT_CONTEXT getServerCertificate ()17
{18
HCERTSTORE hMyCertStore = NULL;19
PCCERT_CONTEXT aCertContext;20
SCHANNEL_CRED sslCredentials;21
CredHandle hCredentials;22
TimeStamp tsExpires;23
SECURITY_STATUS ss = {0};24

25

26
//-------------------------------------------------------27
// Open the My store, also called the personal store.28
// This call to CertOpenStore opens the Local_Machine My 29
// store as opposed to the Current_User's My store.30

31
hMyCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM,32
X509_ASN_ENCODING,33
0,34
CERT_SYSTEM_STORE_LOCAL_MACHINE,35
L"MY");36

37
if (hMyCertStore == NULL) 38
{39
printf("Error opening MY store for server.\n");40
goto cleanup;41
}42
else43
printf("Opening My store for server OK.\n");44
//-------------------------------------------------------45
// Search for a certificate with some specified46
// string in it. This example attempts to find47
// a certificate with the string "example server" in48
// its subject string. Substitute an appropriate string49
// to find a certificate for a specific user.50

51
aCertContext = CertFindCertificateInStore(hMyCertStore, 52
X509_ASN_ENCODING, 53
0,54
CERT_FIND_SUBJECT_STR_A,55
MACHINE_NAME, // use appropriate subject name56
NULL57
);58

59
if (aCertContext == NULL)60
{61
printf("Error retrieving server certificate.");62
goto cleanup;63
}64
else65
printf("Retrieving server certificate OK.\n");66

67
sslCredentials.dwVersion = SCHANNEL_CRED_VERSION;68
sslCredentials.cCreds = 1;69
sslCredentials.paCred = &aCertContext;70
sslCredentials.grbitEnabledProtocols = SP_PROT_TLS1_SERVER;71

72
ss = AcquireCredentialsHandle(NULL,73
UNISP_NAME,74
SECPKG_CRED_INBOUND,75
NULL,76
&sslCredentials,77
NULL,78
NULL,79
&hCredentials,80
&tsExpires81
);82

83
if(ss != SEC_E_OK)84
{85
if(ss == SEC_E_NO_CREDENTIALS)86
printf("SEC_E_NO_CREDENTIALS \n");87
printf("Error Call AcquireCredentialsHandle.\n");88
goto cleanup;89
}90
else{91
printf("Call AcquireCredentialsHandle OK.\n");92
}93

94
cleanup:95
if(hMyCertStore)96
{97
CertCloseStore(hMyCertStore,0);98
}99
return aCertContext;100
}101

102
int main()103
{104
printf("hello world\n");105
getServerCertificate();106
}LPCTSTR CCertificate::GetCertSubName(PCCERT_CONTEXT pCertContext)
{
DWORD cbSize;
LPTSTR pszName;
if(!pCertContext && m_pCertContext)
pCertContext = m_pCertContext;
// Get and Display the Name of subject of Certificate
if(!(cbSize=CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,NULL,NULL,0
)))
{
std::cout<<"CertGetNameString 1 Get Failed."<<std::endl;
return NULL;
}
//alloc for pszName
if(!(pszName = (LPTSTR)malloc(cbSize*sizeof(TCHAR))))
std::cout<<"Memory Allocation Failed."<<std::endl;
if(CertGetNameString(pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
pszName,
cbSize))
{
return pszName;
}
else
{
std::cout<<"CertGetNameString 2 Failed."<<std::endl;
free(pszName);
return NULL;
}
}
{
DWORD cbSize;
LPTSTR pszName;
if(!pCertContext && m_pCertContext)
pCertContext = m_pCertContext;
// Get and Display the Name of subject of Certificate
if(!(cbSize=CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,NULL,NULL,0
)))
{
std::cout<<"CertGetNameString 1 Get Failed."<<std::endl;
return NULL;
}
//alloc for pszName
if(!(pszName = (LPTSTR)malloc(cbSize*sizeof(TCHAR))))
std::cout<<"Memory Allocation Failed."<<std::endl;
if(CertGetNameString(pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
pszName,
cbSize))
{
return pszName;
}
else
{
std::cout<<"CertGetNameString 2 Failed."<<std::endl;
free(pszName);
return NULL;
}
}


SECURITY_STATUS ss
浙公网安备 33010602011771号