Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Parse C++ to Golang #35

Closed
roberto497 opened this issue Sep 28, 2022 · 1 comment
Closed

Parse C++ to Golang #35

roberto497 opened this issue Sep 28, 2022 · 1 comment

Comments

@roberto497
Copy link

I want to pass this code in c++ to golang

`#include
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/crypto.h>

usando namespace std;

int main()
{
OpenSSL_add_all_algorithms();
ENGINE_load_dynamic();

// Configura o mecanismo OpenSSL
ENGINE* engine = ENGINE_by_id("dynamic");
string enginePath = "/usr/local/lib/engines/pkcs11.so";
string modulePath = "/usr/local/lib/opensc-pkcs11.so";

ENGINE_ctrl_cmd_string(engine,"SO_PATH", enginePath.c_str(), 0);
ENGINE_ctrl_cmd_string(engine,"LIST_ADD","1", 0);
ENGINE_ctrl_cmd_string(engine,"CARREGAR", NULO, 0);
ENGINE_ctrl_cmd_string(engine,"MODULE_PATH", modulePath.c_str(), 0);

string pin ="123456";	
ENGINE_ctrl_cmd_string(engine,"PIN", pin.c_str(), 0);
ENGINE_ctrl_cmd_string(engine,"VERBOSE", NULL , 0);
ENGINE_init(engine);
ENGINE_set_default(engine, ENGINE_METHOD_ALL);

string keyName ="id_9352";
EVP_PKEY *evp = ENGINE_load_private_key(engine, keyName.c_str(), NULL, NULL);

// Lê o arquivo criptografado
long unsigned int comprimento = 128;
caractere unsigned buf[comprimento];
FILE* f = fopen("encrypted.bin","r");
fread(buf, 1, comprimento, f);
fclose(f);

// Tenta descriptografar
unsigned char output[comprimento];
unsigned char* p = output;
RSA *rsa = EVP_PKEY_get1_RSA(evp);
int outputLen = RSA_private_decrypt(comprimento, buf, p, rsa, RSA_PKCS1_PADDING);
if (outputLen == -1) {
    long err = ERR_get_error();
    cout < <"Erro ao descriptografar:" << ERR_error_string(err, NULL) << endl;
    return 1;
}

cout << output << endl;
return 0;

}`

Me and I started to do it but stop here

`
type Engine struct {
e *C.ENGINE
}

func EngineById(name string) (*Engine, error) {
C.init()
cname := C.CString(name)
log.Println(name)
defer C.free(unsafe.Pointer(cname))
e := &Engine{
e: C.ENGINE_by_id(cname),
}
if e.e == nil {
return nil, fmt.Errorf("engine %s missing", name)
}

cParamSoPath := C.CString("SO_PATH")
defer C.free(unsafe.Pointer(cParamSoPath))
cValueSoPath := C.CString("/usr/lib/i386-linux-gnu/engines-1.1/pkcs11.so")
defer C.free(unsafe.Pointer(cValueSoPath))

cParamListAdd := C.CString("LIST_ADD")
defer C.free(unsafe.Pointer(cParamListAdd))
cValueListAdd := C.CString("1")
defer C.free(unsafe.Pointer(cValueListAdd))

cParamLoad := C.CString("LOAD")
defer C.free(unsafe.Pointer(cParamLoad))

cParamModulePath := C.CString("MODULE_PATH")
defer C.free(unsafe.Pointer(cParamModulePath))
cValueModulePath := C.CString("/home/procondutor/libaetpkss.so.3.0.3930")
defer C.free(unsafe.Pointer(cValueModulePath))

cParamPin := C.CString("PIN")
defer C.free(unsafe.Pointer(cParamPin))
cValuePin := C.CString("2903")
defer C.free(unsafe.Pointer(cValuePin))

C.ENGINE_ctrl_cmd_string(e.e, cParamSoPath, cValueSoPath, 0);
C.ENGINE_ctrl_cmd_string(e.e, cParamListAdd, cValueListAdd, 0);
C.ENGINE_ctrl_cmd_string(e.e, cParamLoad, nil, 0);
C.ENGINE_ctrl_cmd_string(e.e, cParamModulePath, cValueModulePath, 0);
C.ENGINE_ctrl_cmd_string(e.e, cParamPin, cValuePin, 0);

if C.ENGINE_init(e.e) == 0 {
		C.ENGINE_free(e.e)
		return nil, fmt.Errorf("engine %s not initialized", name)
}
C.ENGINE_set_default(e.e, C.ENGINE_METHOD_ALL);

string keyName := "pkcs11:model=19C43A06010D0000;manufacturer=A.E.T.%20Europe%20B.V.;serial=022A00100020FA04;token=TOKEN;id=%19%00%00%00;object=Procondutor;type=private"
cKeyName := C.CString(keyName)
defer C.free(unsafe.Pointer(cKeyName))

evp := C.ENGINE_load_private_key(e.e, cKeyName, nil, nil)

runtime.SetFinalizer(e, func(e *Engine) {
				C.ENGINE_finish(e.e)
				C.ENGINE_free(e.e)
})

}

`

can someone help me complete it?

@p-shahi
Copy link
Member

p-shahi commented Oct 3, 2022

@roberto497
We're only maintaining this repo for bugs, etc. So it's not a really high likelihood that we will answer this

@p-shahi p-shahi closed this as completed Oct 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants