summaryrefslogtreecommitdiffstats
blob: fcdb4c22b4b94cf1b7461382799813de8563a812 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (c) 2012 Isode Limited, London, England.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#pragma once

#include <Swiften/Base/SafeByteArray.h>
#include <Swiften/TLS/CertificateWithKey.h>

#include <boost/algorithm/string/predicate.hpp>

#define SECURITY_WIN32
#include <WinCrypt.h>

namespace Swift {
	class CAPICertificate : public Swift::CertificateWithKey {
		public:
			CAPICertificate(const std::string& capiUri)
			    : valid_(false), uri_(capiUri), cert_store_handle_(0), cert_store_(NULL), cert_name_(NULL) {
				setUri(capiUri);
			}

			virtual ~CAPICertificate() {
				if (cert_store_handle_ != NULL)
				{
					CertCloseStore(cert_store_handle_, 0);
				}
			}

			virtual bool isNull() const {
				return uri_.empty() || !valid_;
			}

			virtual bool isPrivateKeyExportable() const {
				/* We can check with CAPI, but for now the answer is "no" */
				return false;
			}

			virtual const std::string& getCertStoreName() const {
			    return cert_store_;
			}

			virtual const std::string& getCertName() const {
			    return cert_name_;
			}

			const ByteArray& getData() const {
////Might need to throw an exception here, or really generate PKCS12 blob from CAPI data?
				assert(0);
			}

			void setData(const ByteArray& data) {
				assert(0);
			}

			const SafeByteArray& getPassword() const {
/////Can't pass NULL to createSafeByteArray!
/////Should this throw an exception instead?
				return createSafeByteArray("");
			}

		protected:
			void setUri (const std::string& capiUri) {

				valid_ = false;

				/* Syntax: "certstore:" [<cert_store> ":"] <cert_id> */

				if (!boost::iequals(capiUri.substr(0, 10), "certstore:")) {
					return;
				}

				/* Substring of subject: uses "storename" */
				std::string capi_identity = capiUri.substr(10);
				std::string new_cert_store_name;
				size_t pos = capi_identity.find_first_of (':');

				if (pos == std::string::npos) {
					/* Using the default certificate store */
					new_cert_store_name = "MY";
					cert_name_ = capi_identity;
				} else {
					new_cert_store_name = capi_identity.substr(0, pos);
					cert_name_ = capi_identity.substr(pos + 1);
				}

				PCCERT_CONTEXT pCertContext = NULL;

				if (cert_store_handle_ != NULL)
				{
					if (new_cert_store_name != cert_store_) {
						CertCloseStore(cert_store_handle_, 0);
						cert_store_handle_ = NULL;
					}
				}

				if (cert_store_handle_ == NULL)
				{
					cert_store_handle_ = CertOpenSystemStore(0, cert_store_.c_str());
					if (!cert_store_handle_)
					{
						return;
					}
				}

				cert_store_ = new_cert_store_name;

				/* NB: This might have to change, depending on how we locate certificates */

				// Find client certificate. Note that this sample just searches for a 
				// certificate that contains the user name somewhere in the subject name.
				pCertContext = CertFindCertificateInStore(cert_store_handle_,
					X509_ASN_ENCODING,
					0,				// dwFindFlags
					CERT_FIND_SUBJECT_STR_A,
					cert_name_.c_str(),		// *pvFindPara
					NULL );				// pPrevCertContext

				if (pCertContext == NULL)
				{
					return;
				}


				/* Now verify that we can have access to the corresponding private key */

				DWORD len;
				CRYPT_KEY_PROV_INFO *pinfo;
				HCRYPTPROV hprov;
				HCRYPTKEY key;

				if (!CertGetCertificateContextProperty(pCertContext,
								       CERT_KEY_PROV_INFO_PROP_ID,
								       NULL,
								       &len))
				{
					CertFreeCertificateContext(pCertContext);
					return;
				}

				pinfo = static_cast<CRYPT_KEY_PROV_INFO *>(malloc(len));
				if (!pinfo) {
					CertFreeCertificateContext(pCertContext);
					return;
				}

				if (!CertGetCertificateContextProperty(pCertContext,
								       CERT_KEY_PROV_INFO_PROP_ID,
								       pinfo,
								       &len))
				{
					CertFreeCertificateContext(pCertContext);
					free(pinfo);
					return;
				}

				CertFreeCertificateContext(pCertContext);

				// Now verify if we have access to the private key
				if (!CryptAcquireContextW(&hprov,
							  pinfo->pwszContainerName,
							  pinfo->pwszProvName,
							  pinfo->dwProvType,
							  0))
				{
					free(pinfo);
					return;
				}

				if (!CryptGetUserKey(hprov, pinfo->dwKeySpec, &key))
				{
					CryptReleaseContext(hprov, 0);
					free(pinfo);
					return;
				}

				CryptDestroyKey(key);
				CryptReleaseContext(hprov, 0);
				free(pinfo);

				valid_ = true;
			}

		private:
			bool valid_;
			std::string uri_;

			HCERTSTORE cert_store_handle_;

			/* Parsed components of the uri_ */
			std::string cert_store_;
			std::string cert_name_;
	};
}