diff --git a/deps/wrapper/include/wrapper/utils/csp.h b/deps/wrapper/include/wrapper/utils/csp.h index ad85931..c4c7526 100644 --- a/deps/wrapper/include/wrapper/utils/csp.h +++ b/deps/wrapper/include/wrapper/utils/csp.h @@ -32,6 +32,7 @@ class Csp { Csp(){}; ~Csp(){}; +#ifdef CSP_ENABLE bool isGost2001CSPAvailable(); bool isGost2012_256CSPAvailable(); bool isGost2012_512CSPAvailable(); @@ -65,7 +66,6 @@ class Csp { Handle certToPkcs12(Handle cert, bool exportPrivateKey, Handle password); void importPkcs12(Handle p12, Handle password); -#ifdef CSP_ENABLE PCCERT_CONTEXT static createCertificateContext(Handle cert); bool static findExistingCertificate( diff --git a/deps/wrapper/src/utils/csp.cpp b/deps/wrapper/src/utils/csp.cpp index ff02f85..eb929d2 100644 --- a/deps/wrapper/src/utils/csp.cpp +++ b/deps/wrapper/src/utils/csp.cpp @@ -2,11 +2,12 @@ #include "wrapper/utils/csp.h" +#ifdef CSP_ENABLE + bool Csp::isGost2001CSPAvailable() { LOGGER_FN(); try { -#ifdef CSP_ENABLE static HCRYPTPROV hCryptProv = 0; bool res; @@ -26,9 +27,6 @@ bool Csp::isGost2001CSPAvailable() { hCryptProv = 0; return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ THROW_EXCEPTION(0, Csp, e, "Error check GOST 2001 provaider"); @@ -39,7 +37,6 @@ bool Csp::isGost2012_256CSPAvailable() { LOGGER_FN(); try { -#ifdef CSP_ENABLE static HCRYPTPROV hCryptProv = 0; bool res; @@ -59,9 +56,6 @@ bool Csp::isGost2012_256CSPAvailable() { hCryptProv = 0; return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ THROW_EXCEPTION(0, Csp, e, "Error check GOST 2001 provaider"); @@ -72,7 +66,6 @@ bool Csp::isGost2012_512CSPAvailable() { LOGGER_FN(); try { -#ifdef CSP_ENABLE static HCRYPTPROV hCryptProv = 0; bool res; @@ -92,9 +85,6 @@ bool Csp::isGost2012_512CSPAvailable() { hCryptProv = 0; return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ THROW_EXCEPTION(0, Csp, e, "Error check GOST 2001 provaider"); @@ -104,11 +94,11 @@ bool Csp::isGost2012_512CSPAvailable() { bool Csp::checkCPCSPLicense() { LOGGER_FN(); + static HCRYPTPROV hCryptProv = 0; + LPBYTE pbData; + try { -#ifdef CSP_ENABLE - static HCRYPTPROV hCryptProv = 0; DWORD cbData = 0; - LPBYTE pbData; bool res = false; if (!isGost2001CSPAvailable()) { @@ -157,11 +147,17 @@ bool Csp::checkCPCSPLicense() { } return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (hCryptProv) { + CryptReleaseContext(hCryptProv, 0); + hCryptProv = 0; + } + + if (pbData) { + free((BYTE*)pbData); + } + THROW_EXCEPTION(0, Csp, e, "Error check cpcsp license"); } } @@ -169,11 +165,11 @@ bool Csp::checkCPCSPLicense() { Handle Csp::getCPCSPLicense() { LOGGER_FN(); + static HCRYPTPROV hCryptProv = 0; + LPBYTE pbData; + try { -#ifdef CSP_ENABLE - static HCRYPTPROV hCryptProv = 0; DWORD cbData = 0; - LPBYTE pbData; Handle license; if (!isGost2001CSPAvailable()) { @@ -227,11 +223,17 @@ Handle Csp::getCPCSPLicense() { } return license; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (hCryptProv) { + CryptReleaseContext(hCryptProv, 0); + hCryptProv = 0; + } + + if (pbData) { + free((BYTE*)pbData); + } + THROW_EXCEPTION(0, Csp, e, "Error get cpcsp license"); } } @@ -239,9 +241,9 @@ Handle Csp::getCPCSPLicense() { Handle Csp::getCPCSPVersion() { LOGGER_FN(); + static HCRYPTPROV hCryptProv = 0; + try { -#ifdef CSP_ENABLE - static HCRYPTPROV hCryptProv = 0; DWORD pbData = 0; DWORD cbData = (DWORD)sizeof(pbData); @@ -272,11 +274,13 @@ Handle Csp::getCPCSPVersion() { hCryptProv = 0; return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (hCryptProv) { + CryptReleaseContext(hCryptProv, 0); + hCryptProv = 0; + } + THROW_EXCEPTION(0, Csp, e, "Error get cpcsp version"); } } @@ -284,12 +288,12 @@ Handle Csp::getCPCSPVersion() { Handle Csp::getCPCSPVersionPKZI() { LOGGER_FN(); + static HCRYPTPROV hCryptProv = 0; + LPBYTE pbData; + try { -#ifdef CSP_ENABLE - static HCRYPTPROV hCryptProv = 0; PROV_PP_VERSION_EX *exVersion = NULL; DWORD cbData = 0; - LPBYTE pbData; Handle res; if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_GOST_2001_DH, CRYPT_VERIFYCONTEXT)){ @@ -336,11 +340,17 @@ Handle Csp::getCPCSPVersionPKZI() { } return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (hCryptProv) { + CryptReleaseContext(hCryptProv, 0); + hCryptProv = 0; + } + + if (pbData) { + free((BYTE*)pbData); + } + THROW_EXCEPTION(0, Csp, e, "Error get cpcsp version"); } } @@ -348,12 +358,12 @@ Handle Csp::getCPCSPVersionPKZI() { Handle Csp::getCPCSPVersionSKZI() { LOGGER_FN(); + static HCRYPTPROV hCryptProv = 0; + LPBYTE pbData; + try { -#ifdef CSP_ENABLE - static HCRYPTPROV hCryptProv = 0; PROV_PP_VERSION_EX *exVersion = NULL; DWORD cbData = 0; - LPBYTE pbData; Handle res; if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_GOST_2001_DH, CRYPT_VERIFYCONTEXT)){ @@ -400,11 +410,17 @@ Handle Csp::getCPCSPVersionSKZI() { } return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (hCryptProv) { + CryptReleaseContext(hCryptProv, 0); + hCryptProv = 0; + } + + if (pbData) { + free((BYTE*)pbData); + } + THROW_EXCEPTION(0, Csp, e, "Error get cpcsp version"); } } @@ -412,10 +428,10 @@ Handle Csp::getCPCSPVersionSKZI() { Handle Csp::getCPCSPSecurityLvl() { LOGGER_FN(); + static HCRYPTPROV hCryptProv = 0; + try { -#ifdef CSP_ENABLE std::vector secureLvl = { { "KC1" }, { "KC2" }, { "KC3" }, { "KB1" }, { "KB2" }, { "KA1" } }; - static HCRYPTPROV hCryptProv = 0; Handle version; DWORD dwVersion[20]; DWORD dwDataLength = (DWORD)sizeof(dwVersion); @@ -443,11 +459,13 @@ Handle Csp::getCPCSPSecurityLvl() { hCryptProv = 0; return version; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (hCryptProv) { + CryptReleaseContext(hCryptProv, 0); + hCryptProv = 0; + } + THROW_EXCEPTION(0, Csp, e, "Error get cpcsp version"); } } @@ -455,13 +473,13 @@ Handle Csp::getCPCSPSecurityLvl() { std::vector Csp::enumProviders() { LOGGER_FN(); + LPTSTR pszName; + try { -#ifdef CSP_ENABLE std::vector res; DWORD dwIndex = 0; DWORD dwType; DWORD cbName; - LPTSTR pszName; while (CryptEnumProviders(dwIndex, NULL, 0, &dwType, NULL, &cbName)) { @@ -480,11 +498,12 @@ std::vector Csp::enumProviders() { } return res; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ + if (pszName) { + free(pszName); + } + THROW_EXCEPTION(0, Csp, e, "Error enum providers"); } } @@ -493,7 +512,6 @@ std::vector> Csp::enumContainers(int provType, Handle> res; std::vector providers; HCRYPTPROV hProv = 0; @@ -637,9 +655,6 @@ std::vector> Csp::enumContainers(int provType, Handle e){ THROW_EXCEPTION(0, Csp, e, "Error enum containers"); @@ -649,14 +664,11 @@ std::vector> Csp::enumContainers(int provType, Handle Csp::getCertificateFromContainer(Handle contName, int provType, Handle provName) { LOGGER_FN(); -#ifdef CSP_ENABLE HCRYPTPROV hProv = NULL; HCRYPTKEY hKey = NULL; BYTE* pbCertificate = NULL; -#endif try { -#ifdef CSP_ENABLE DWORD cbName; DWORD dwKeySpec; PCCERT_CONTEXT pCertContext; @@ -734,12 +746,8 @@ Handle Csp::getCertificateFromContainer(Handle contNam } return new Certificate(hcert); -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ -#ifdef CSP_ENABLE free(pbCertificate); if (hKey) { @@ -754,7 +762,6 @@ Handle Csp::getCertificateFromContainer(Handle contNam hProv = NULL; } -#endif THROW_EXCEPTION(0, Csp, e, "Error get certificate from container"); } @@ -763,14 +770,11 @@ Handle Csp::getCertificateFromContainer(Handle contNam void Csp::installCertificateFromContainer(Handle contName, int provType, Handle provName) { LOGGER_FN(); -#ifdef CSP_ENABLE HCRYPTPROV hProv = NULL; HCRYPTKEY hKey = NULL; BYTE* pbCertificate = NULL; -#endif try { -#ifdef CSP_ENABLE DWORD cbName; DWORD dwKeySpec, dwSize; PCCERT_CONTEXT pCertContext; @@ -930,12 +934,8 @@ void Csp::installCertificateFromContainer(Handle contName, int prov } return; -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e){ -#ifdef CSP_ENABLE free(pbCertificate); if (hKey) { @@ -950,7 +950,6 @@ void Csp::installCertificateFromContainer(Handle contName, int prov hProv = NULL; } -#endif THROW_EXCEPTION(0, Csp, e, "Error install certificate from container"); } @@ -959,13 +958,10 @@ void Csp::installCertificateFromContainer(Handle contName, int prov void Csp::installCertificateToContainer(Handle cert, Handle contName, int provType, Handle provName) { LOGGER_FN(); -#ifdef CSP_ENABLE HCRYPTPROV hProv = NULL; HCRYPTKEY hKey = NULL; -#endif try { -#ifdef CSP_ENABLE DWORD dwKeySpec; PCCERT_CONTEXT pCertContext; @@ -1029,12 +1025,8 @@ void Csp::installCertificateToContainer(Handle cert, Handle e){ -#ifdef CSP_ENABLE if (hKey) { CryptDestroyKey(hKey); hKey = NULL; @@ -1047,7 +1039,6 @@ void Csp::installCertificateToContainer(Handle cert, Handle cert, Handle contName, int provType, Handle provName) { LOGGER_FN(); -#ifdef CSP_ENABLE HCRYPTPROV hProv = NULL; -#endif try { -#ifdef CSP_ENABLE if (contName.isEmpty()) { THROW_EXCEPTION(0, Csp, NULL, "container name epmty"); } @@ -1083,9 +1071,6 @@ void Csp::deleteContainer(Handle contName, int provType, Handle e){ THROW_EXCEPTION(0, Csp, e, "Error install certificate from container"); @@ -1095,16 +1080,13 @@ void Csp::deleteContainer(Handle contName, int provType, Handle Csp::getContainerNameByCertificate(Handle cert, Handle category){ LOGGER_FN(); -#ifdef CSP_ENABLE PCCERT_CONTEXT pCertContext = HCRYPT_NULL; HCRYPTPROV hCryptProv = HCRYPT_NULL; HCRYPTKEY hPublicKey = HCRYPT_NULL; LPBYTE pbContainerName = HCRYPT_NULL; LPBYTE pbFPCert = HCRYPT_NULL; -#endif try { -#ifdef CSP_ENABLE DWORD cbFPCert; DWORD cbContainerName; DWORD dwFlags; @@ -1197,13 +1179,8 @@ Handle Csp::getContainerNameByCertificate(Handle cert, } return res; - -#else - THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE"); -#endif } catch (Handle e) { -#ifdef CSP_ENABLE if (pCertContext) { CertFreeCertificateContext(pCertContext); pCertContext = HCRYPT_NULL; @@ -1225,14 +1202,11 @@ Handle Csp::getContainerNameByCertificate(Handle cert, THROW_EXCEPTION(0, Csp, NULL, "CryptReleaseContext. Error: 0x%08x", GetLastError()); } } -#endif THROW_EXCEPTION(0, Csp, e, "Error get containerName by Certificate"); } } -#ifdef CSP_ENABLE - PCCERT_CONTEXT Csp::createCertificateContext(Handle cert) { LOGGER_FN(); @@ -2129,7 +2103,7 @@ static DWORD set_certificate_to_store_internal( return err; } -void Csp::installCertificateFromCloud ( +void Csp::installCertificateFromCloud( Handle hcert, const std::string & szAuthURL, const std::string & szRestURL, diff --git a/src/node/utils/wcsp.cpp b/src/node/utils/wcsp.cpp index 2d12c16..d377b04 100644 --- a/src/node/utils/wcsp.cpp +++ b/src/node/utils/wcsp.cpp @@ -69,12 +69,16 @@ NAN_METHOD(WCsp::IsGost2001CSPAvailable) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); bool res = _this->isGost2001CSPAvailable(); info.GetReturnValue().Set(Nan::New(res)); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -83,12 +87,16 @@ NAN_METHOD(WCsp::IsGost2012_256CSPAvailable) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); bool res = _this->isGost2012_256CSPAvailable(); info.GetReturnValue().Set(Nan::New(res)); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -97,12 +105,16 @@ NAN_METHOD(WCsp::IsGost2012_512CSPAvailable) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); bool res = _this->isGost2012_512CSPAvailable(); info.GetReturnValue().Set(Nan::New(res)); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -111,12 +123,16 @@ NAN_METHOD(WCsp::CheckCPCSPLicense) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); bool res = _this->checkCPCSPLicense(); info.GetReturnValue().Set(Nan::New(res)); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -125,6 +141,7 @@ NAN_METHOD(WCsp::GetCPCSPLicense) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); Handle lic = _this->getCPCSPLicense(); @@ -133,6 +150,9 @@ NAN_METHOD(WCsp::GetCPCSPLicense) { info.GetReturnValue().Set(v8Lic); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -141,6 +161,7 @@ NAN_METHOD(WCsp::GetCPCSPVersion) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); Handle ver = _this->getCPCSPVersion(); @@ -149,6 +170,9 @@ NAN_METHOD(WCsp::GetCPCSPVersion) { info.GetReturnValue().Set(v8Ver); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -157,6 +181,7 @@ NAN_METHOD(WCsp::GetCPCSPVersionPKZI) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); Handle ver = _this->getCPCSPVersionPKZI(); @@ -165,6 +190,9 @@ NAN_METHOD(WCsp::GetCPCSPVersionPKZI) { info.GetReturnValue().Set(v8Ver); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -173,6 +201,7 @@ NAN_METHOD(WCsp::GetCPCSPVersionSKZI) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); Handle ver = _this->getCPCSPVersionSKZI(); @@ -181,6 +210,9 @@ NAN_METHOD(WCsp::GetCPCSPVersionSKZI) { info.GetReturnValue().Set(v8Ver); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -189,6 +221,7 @@ NAN_METHOD(WCsp::GetCPCSPSecurityLvl) { METHOD_BEGIN(); try { +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); Handle lic = _this->getCPCSPSecurityLvl(); @@ -197,6 +230,9 @@ NAN_METHOD(WCsp::GetCPCSPSecurityLvl) { info.GetReturnValue().Set(v8Lic); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -206,6 +242,7 @@ NAN_METHOD(WCsp::EnumProviders) METHOD_BEGIN(); try{ +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); std::vector res = _this->enumProviders(); @@ -224,6 +261,9 @@ NAN_METHOD(WCsp::EnumProviders) info.GetReturnValue().Set(array8); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -233,6 +273,7 @@ NAN_METHOD(WCsp::EnumContainers) METHOD_BEGIN(); try{ +#ifdef CSP_ENABLE UNWRAP_DATA(Csp); int type = NULL; @@ -281,6 +322,9 @@ NAN_METHOD(WCsp::EnumContainers) info.GetReturnValue().Set(array8); return; +#else + Nan::ThrowError("Only if CSP_ENABLE"); +#endif // CSP_ENABLE } TRY_END(); } @@ -342,7 +386,7 @@ NAN_METHOD(WCsp::InstallCertificateFromCloud) info.GetReturnValue().Set(info.This()); return; #else - Nan::ThrowError("Only for CryptoPro CSP 5"); + Nan::ThrowError("Only if CSP_ENABLE and for CryptoPro CSP 5"); #endif // CSP_ENABLE } TRY_END();