summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/TLS/CAPICertificate.cpp')
-rw-r--r--Swiften/TLS/CAPICertificate.cpp49
1 files changed, 20 insertions, 29 deletions
diff --git a/Swiften/TLS/CAPICertificate.cpp b/Swiften/TLS/CAPICertificate.cpp
index a46b9f6..f10ad47 100644
--- a/Swiften/TLS/CAPICertificate.cpp
+++ b/Swiften/TLS/CAPICertificate.cpp
@@ -16,2 +16,3 @@
#include <Swiften/StringCodecs/Hexify.h>
+#include <Swiften/TLS/Schannel/SchannelUtil.h>
@@ -45,2 +46,3 @@ CAPICertificate::CAPICertificate(const std::string& capiUri, TimerFactory* timer
CAPICertificate::~CAPICertificate() {
+ SWIFT_LOG(debug) << "Destroying the CAPICertificate" << std::endl;
if (smartCardTimer_) {
@@ -52,3 +54,5 @@ CAPICertificate::~CAPICertificate() {
if (certStoreHandle_) {
- CertCloseStore(certStoreHandle_, 0);
+ if (CertCloseStore(certStoreHandle_, 0) == FALSE) {
+ SWIFT_LOG(debug) << "Failed to close the certificate store handle" << std::endl;
+ }
}
@@ -82,3 +86,3 @@ const std::string& CAPICertificate::getSmartCardReaderName() const {
-PCCERT_CONTEXT findCertificateInStore (HCERTSTORE certStoreHandle, const std::string &certName) {
+PCCERT_CONTEXT findCertificateInStore(HCERTSTORE certStoreHandle, const std::string &certName) {
if (!boost::iequals(certName.substr(0, 5), "sha1:")) {
@@ -107,4 +111,3 @@ PCCERT_CONTEXT findCertificateInStore (HCERTSTORE certStoreHandle, const std::st
-
-void CAPICertificate::setUri (const std::string& capiUri) {
+void CAPICertificate::setUri(const std::string& capiUri) {
valid_ = false;
@@ -120,3 +123,3 @@ void CAPICertificate::setUri (const std::string& capiUri) {
std::string newCertStoreName;
- size_t pos = capiIdentity.find_first_of (':');
+ size_t pos = capiIdentity.find_first_of(':');
@@ -148,4 +151,3 @@ void CAPICertificate::setUri (const std::string& capiUri) {
- PCCERT_CONTEXT certContext = findCertificateInStore (certStoreHandle_, certName_);
-
+ ScopedCertContext certContext(findCertificateInStore(certStoreHandle_, certName_));
if (!certContext) {
@@ -154,3 +156,2 @@ void CAPICertificate::setUri (const std::string& capiUri) {
-
/* Now verify that we can have access to the corresponding private key */
@@ -158,6 +159,2 @@ void CAPICertificate::setUri (const std::string& capiUri) {
DWORD len;
- CRYPT_KEY_PROV_INFO *pinfo;
- HCRYPTPROV hprov;
- HCRYPTKEY key;
-
if (!CertGetCertificateContextProperty(certContext,
@@ -166,3 +163,3 @@ void CAPICertificate::setUri (const std::string& capiUri) {
&len)) {
- CertFreeCertificateContext(certContext);
+ SWIFT_LOG(error) << "Error while retrieving context properties" << std::endl;
return;
@@ -170,5 +167,4 @@ void CAPICertificate::setUri (const std::string& capiUri) {
- pinfo = static_cast<CRYPT_KEY_PROV_INFO *>(malloc(len));
+ std::shared_ptr<CRYPT_KEY_PROV_INFO> pinfo(static_cast<CRYPT_KEY_PROV_INFO *>(malloc(len)), free);
if (!pinfo) {
- CertFreeCertificateContext(certContext);
return;
@@ -176,13 +172,10 @@ void CAPICertificate::setUri (const std::string& capiUri) {
- if (!CertGetCertificateContextProperty(certContext, CERT_KEY_PROV_INFO_PROP_ID, pinfo, &len)) {
- CertFreeCertificateContext(certContext);
- free(pinfo);
+ if (!CertGetCertificateContextProperty(certContext, CERT_KEY_PROV_INFO_PROP_ID, pinfo.get(), &len)) {
return;
}
+ certContext.FreeContext();
- CertFreeCertificateContext(certContext);
-
+ HCRYPTPROV hprov;
// Now verify if we have access to the private key
if (!CryptAcquireContextW(&hprov, pinfo->pwszContainerName, pinfo->pwszProvName, pinfo->dwProvType, 0)) {
- free(pinfo);
return;
@@ -190,3 +183,2 @@ void CAPICertificate::setUri (const std::string& capiUri) {
-
char smartCardReader[1024];
@@ -207,3 +199,4 @@ void CAPICertificate::setUri (const std::string& capiUri) {
else {
- ///Need to handle an error here
+ CryptReleaseContext(hprov, 0);
+ return;
}
@@ -211,5 +204,5 @@ void CAPICertificate::setUri (const std::string& capiUri) {
+ HCRYPTKEY key;
if (!CryptGetUserKey(hprov, pinfo->dwKeySpec, &key)) {
CryptReleaseContext(hprov, 0);
- free(pinfo);
return;
@@ -219,3 +212,2 @@ void CAPICertificate::setUri (const std::string& capiUri) {
CryptReleaseContext(hprov, 0);
- free(pinfo);
@@ -229,3 +221,3 @@ void CAPICertificate::setUri (const std::string& capiUri) {
-static void smartcard_check_status (SCARDCONTEXT hContext,
+static void smartcard_check_status(SCARDCONTEXT hContext,
const char* pReader,
@@ -290,3 +282,3 @@ static void smartcard_check_status (SCARDCONTEXT hContext,
-bool CAPICertificate::checkIfSmartCardPresent () {
+bool CAPICertificate::checkIfSmartCardPresent() {
if (!smartCardReaderName_.empty()) {
@@ -319,4 +311,2 @@ bool CAPICertificate::checkIfSmartCardPresent () {
-
-
switch (dwState) {
@@ -344,2 +334,3 @@ void CAPICertificate::handleSmartCardTimerTick() {
if (lastPollingResult_ && !poll) {
+ SWIFT_LOG(debug) << "CAPI Certificate detected that the certificate card was removed" << std::endl;
onCertificateCardRemoved();