Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encrypt string #19

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions deps/wrapper/include/wrapper/pki/cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class Cipher{
/*Symetric or assymetric(default)*/
void setCryptoMethod(CryptoMethod::Crypto_Method method);

void encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_FORMAT format);
void decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FORMAT format);
Handle<Bio> encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_FORMAT format);
Handle<Bio> decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FORMAT format);

public:
Handle<std::string> getAlgorithm();
Expand Down
2 changes: 2 additions & 0 deletions deps/wrapper/include/wrapper/utils/csp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Csp {
bool checkCPCSPLicense();
Handle<std::string> getCPCSPLicense();

Handle<std::string> getCPCSPVersion();

std::vector<ProviderProps> enumProviders();
std::vector<Handle<std::string>> enumContainers(int provType, Handle<std::string> provName);
Handle<Certificate> getCertifiacteFromContainer(Handle<std::string> contName, int provType, Handle<std::string> provName);
Expand Down
29 changes: 17 additions & 12 deletions deps/wrapper/src/pki/cipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ void Cipher::setRecipientCert(Handle<Certificate> cert){
}
}

void Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_FORMAT format){
Handle<Bio> Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_FORMAT format){
LOGGER_FN();

try{
X509 *firstRecipientCertificate = NULL;
EVP_PKEY *pkey = NULL;
Expand All @@ -101,7 +101,7 @@ void Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_
case CryptoMethod::SYMMETRIC:
/*Check pass*/
if (hpass == NULL){

/*Check key*/
if (hkey == NULL){
THROW_EXCEPTION(0, Cipher, NULL, "key undefined");
Expand All @@ -120,16 +120,16 @@ void Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_
/*
* We use 'benc' how cipher BIO method.
* This is a filter BIO that encrypts any data written through it
*/
*/
LOGGER_OPENSSL(BIO_new);
if ((benc = BIO_new(BIO_f_cipher())) == NULL){
THROW_OPENSSL_EXCEPTION(0, Cipher, NULL, "BIO_new(BIO_f_cipher())");
}

/*Save internal BIO cipher context to 'ctx'*/
LOGGER_OPENSSL(BIO_get_cipher_ctx);
BIO_get_cipher_ctx(benc, &ctx);

/*Use param '1' for encrypt*/
LOGGER_OPENSSL(EVP_CipherInit_ex);
if (!EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1)) {
Expand All @@ -149,7 +149,7 @@ void Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_
THROW_OPENSSL_EXCEPTION(0, Cipher, NULL, "Error write bio");
}
}

if (benc != NULL){
LOGGER_OPENSSL(BIO_push);
wbio = BIO_push(benc, wbio);
Expand All @@ -173,6 +173,8 @@ void Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_
THROW_EXCEPTION(0, Cipher, NULL, "bad decrypt");
}

return new Bio(wbio);

break;

//****************************************************************************************
Expand Down Expand Up @@ -241,15 +243,16 @@ void Cipher::encrypt(Handle<Bio> inSource, Handle<Bio> outEnc, DataFormat::DATA_
default:
THROW_EXCEPTION(0, Cipher, NULL, "Unknown crypto method");
}
return NULL;



}
catch (Handle<Exception> e){
THROW_EXCEPTION(0, Cipher, e, "Error encrypt");
}
}

void Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FORMAT format){
Handle<Bio> Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FORMAT format){
LOGGER_FN();

try{
Expand Down Expand Up @@ -288,7 +291,7 @@ void Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FOR
else if (memcmp(mbuf, magic, sizeof magic - 1)) {
THROW_EXCEPTION(0, Cipher, NULL, "bad magic number");
}

LOGGER_OPENSSL(EVP_BytesToKey);
if (EVP_BytesToKey(cipher, dgst, salt, (unsigned char *)hpass, strlen(hpass), 1, key, iv) == 0){
THROW_OPENSSL_EXCEPTION(0, Cipher, NULL, "EVP_BytesToKey");
Expand Down Expand Up @@ -320,7 +323,7 @@ void Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FOR
inl = BIO_read(rbio, (char *)buff, bsize);
if (inl <= 0){
break;
}
}
LOGGER_OPENSSL(BIO_write);
if (BIO_write(wbio, (char *)buff, inl) != inl) {
THROW_EXCEPTION(0, Cipher, NULL, "Error writing output bio");
Expand All @@ -331,6 +334,7 @@ void Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FOR
if (!BIO_flush(wbio)){
THROW_EXCEPTION(0, Cipher, NULL, "bad decrypt");
}
return new Bio(wbio);

break;

Expand Down Expand Up @@ -366,7 +370,7 @@ void Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FOR
if (!CMS_decrypt_set1_pkey(cms, rkey, rcert)) {
THROW_OPENSSL_EXCEPTION(0, Cipher, NULL, "CMS_decrypt_set1_pkey 'Error set private key'");
}

LOGGER_OPENSSL(CMS_decrypt);
if (!CMS_decrypt(cms, NULL, NULL, NULL, outDec->internal(), flags)) {
THROW_OPENSSL_EXCEPTION(0, Cipher, NULL, "CMS_decrypt 'Error decrypt cms'");
Expand All @@ -381,6 +385,7 @@ void Cipher::decrypt(Handle<Bio> inEnc, Handle<Bio> outDec, DataFormat::DATA_FOR
default:
THROW_EXCEPTION(0, Cipher, NULL, "Unknown crypto method");
}
return NULL;


}
Expand Down
53 changes: 53 additions & 0 deletions deps/wrapper/src/utils/csp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../stdafx.h"

#include "wrapper/utils/csp.h"
#include <sstream>

bool Csp::isGost2001CSPAvailable() {
LOGGER_FN();
Expand Down Expand Up @@ -236,6 +237,58 @@ Handle<std::string> Csp::getCPCSPLicense() {
}
}

Handle<std::string> Csp::getCPCSPVersion() {
LOGGER_FN();

try {
#ifdef CSP_ENABLE
static HCRYPTPROV hCryptProv = 0;
Handle<std::string> version;
DWORD dwVersion[100];
DWORD dwDataLength = (DWORD)sizeof(dwVersion);

if (!isGost2001CSPAvailable()) {
THROW_EXCEPTION(0, Key, NULL, "GOST 2001 provaider not available");
}

if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_GOST_2001_DH, CRYPT_VERIFYCONTEXT)){
THROW_EXCEPTION(0, Csp, NULL, "CryptAcquireContext. Error: 0x%08x", GetLastError());
}

if (!CryptGetProvParam(hCryptProv, PP_VERSION_EX, (BYTE*)&dwVersion, &dwDataLength, 0)){
THROW_EXCEPTION(0, Key, NULL, "CryptGetProvParam. Error: 0x%08x", GetLastError());
}

std::string str = "";
for (int i = 3; i >= 0; i--){
if (i == 1) continue;
std::stringstream ss;
ss << dwVersion[i];
if (i == 0)
str = str + ss.str();
else
str = str + ss.str() + ".";
}
version = new std::string(str);

if (hCryptProv) {
if (!CryptReleaseContext(hCryptProv, 0)) {
THROW_EXCEPTION(0, Csp, NULL, "CryptReleaseContext. Error: 0x%08x", GetLastError());
}
}

hCryptProv = 0;

return version;
#else
THROW_EXCEPTION(0, Csp, NULL, "Only if defined CSP_ENABLE");
#endif
}
catch (Handle<Exception> e){
THROW_EXCEPTION(0, Csp, e, "Error get cpcsp version");
}
}

std::vector<ProviderProps> Csp::enumProviders() {
LOGGER_FN();

Expand Down
17 changes: 15 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,22 @@ declare namespace native {
save(filename: string, dataFormat: trusted.DataFormat): void;
getEncodedHEX(): Buffer;
}

export enum CipherContentType {
url,
buffer,
}

export interface ICipherContent {
type: CipherContentType;
data: string | Buffer;
}

class Cipher {
constructor();
setCryptoMethod(method: trusted.CryptoMethod): void;
encrypt(filenameSource: string, filenameEnc: string, format: trusted.DataFormat): void;
decrypt(filenameEnc: string, filenameDec: string, format?: trusted.DataFormat): void;
encrypt(source: ICipherContent, destinationEnc: ICipherContent, format: trusted.DataFormat): string;
decrypt(sourceEnc: ICipherContent, destinationDec: ICipherContent, format?: trusted.DataFormat): string;
addRecipientsCerts(certs: CertificateCollection): void;
setPrivKey(rkey: Key): void;
setRecipientCert(rcert: Certificate): void;
Expand Down Expand Up @@ -835,6 +846,8 @@ declare namespace trusted.utils {
* @memberof Csp
*/
static getCPCSPLicense(): string;

static getCPCSPVersion(): string;
/**
* Enumerate available CSP
*
Expand Down
15 changes: 13 additions & 2 deletions lib/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,21 @@ declare namespace native {
public getEncodedHEX(): Buffer;
}

export enum CipherContentType {
url,
buffer,
}

export interface ICipherContent {
type: CipherContentType;
data: string | Buffer;
}

class Cipher {
constructor();
public setCryptoMethod(method: trusted.CryptoMethod): void;
public encrypt(filenameSource: string, filenameEnc: string, format: trusted.DataFormat): void;
public decrypt(filenameEnc: string, filenameDec: string, format?: trusted.DataFormat): void;
public encrypt(source: ICipherContent, destinationEnc: ICipherContent, format: trusted.DataFormat): string;
public decrypt(sourceEnc: ICipherContent, destDec: ICipherContent, format?: trusted.DataFormat): string;
public addRecipientsCerts(certs: CertificateCollection): void;
public setPrivKey(rkey: Key): void;
public setRecipientCert(rcert: Certificate): void;
Expand Down Expand Up @@ -475,6 +485,7 @@ declare namespace native {
public isGost2012_512CSPAvailable(): boolean;
public checkCPCSPLicense(): boolean;
public getCPCSPLicense(): string;
public getCPCSPVersion(): string;
public enumProviders(): object[];
public enumContainers(type?: number, provName?: string): string[];
public getCertifiacteFromContainer(contName: string, provType: number, provName?: string): PKI.Certificate;
Expand Down
43 changes: 39 additions & 4 deletions lib/pki/cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
/// <reference path="../object.ts" />

namespace trusted.pki {

export enum CipherContentType {
url,
buffer,
}

export interface ICipherContent {
type: CipherContentType;
data: string | Buffer;
}
/**
* Encrypt and decrypt operations
*
Expand Down Expand Up @@ -42,8 +52,21 @@ namespace trusted.pki {
*
* @memberOf Cipher
*/
public encrypt(filenameSource: string, filenameEnc: string, format: DataFormat): void {
this.handle.encrypt(filenameSource, filenameEnc, format);
public encrypt(source: ICipherContent, destinationEnc: ICipherContent, format: DataFormat): string {
let sourceData: any;
let destinationData: any;

if (source.type === CipherContentType.url) {
sourceData = source.data.toString();
} else {
sourceData = new Buffer(source.data.valueOf() as any);
}
if (destinationEnc.type === CipherContentType.url) {
destinationData = destinationEnc.data.toString();
} else {
destinationData = new Buffer(destinationEnc.data.valueOf() as any);
}
return this.handle.encrypt(sourceData, destinationData, format);
}

/**
Expand All @@ -55,8 +78,20 @@ namespace trusted.pki {
*
* @memberOf Cipher
*/
public decrypt(filenameEnc: string, filenameDec: string, format?: DataFormat): void {
this.handle.decrypt(filenameEnc, filenameDec, format);
public decrypt(sourceEnc: ICipherContent, destDec: ICipherContent, format: DataFormat): string {
let sourceData: any;
let destinationData: any;
if (sourceEnc.type === CipherContentType.url) {
sourceData = sourceEnc.data.toString();
} else {
sourceData = new Buffer(sourceEnc.data.valueOf() as any);
}
if (destDec.type === CipherContentType.url) {
destinationData = destDec.data.toString();
} else {
destinationData = new Buffer(destDec.data.valueOf() as any);
}
return this.handle.decrypt(sourceData, destinationData, format);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ namespace trusted.utils {
return csp.getCPCSPLicense();
}

/**
* Return instaled correct license for CryptoPro CSP
* Throw exception if provaider not available
*
* @static
* @returns {boolean}
* @memberof Csp
*/
public static getCPCSPVersion(): string {
const csp = new native.UTILS.Csp();
return csp.getCPCSPVersion();
}

/**
* Enumerate available CSP
*
Expand Down
Loading