mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 22:03:48 +08:00
first step of v2 final release
This commit is contained in:
@@ -2,85 +2,119 @@
|
||||
package gmssl
|
||||
|
||||
/*
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/engine.h>
|
||||
|
||||
char *get_errors() {
|
||||
char *ret;
|
||||
BIO *bio;
|
||||
char *data;
|
||||
long len;
|
||||
if (!(bio = BIO_new(BIO_s_mem()))) {
|
||||
return (char *)NULL;
|
||||
}
|
||||
ERR_print_errors(bio);
|
||||
len = BIO_get_mem_data(bio, &data);
|
||||
ret = OPENSSL_strdup(data);
|
||||
BIO_free(bio);
|
||||
return ret;
|
||||
}
|
||||
|
||||
EVP_PKEY *load_private_key_from_engine(ENGINE *e, const char *key_id,
|
||||
const char *pass)
|
||||
{
|
||||
return (EVP_PKEY *)NULL;
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"errors"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func GetEngineNames() []string {
|
||||
return []string{"skf", "sdf"}
|
||||
engines := []string{}
|
||||
C.ENGINE_load_builtin_engines()
|
||||
eng := C.ENGINE_get_first()
|
||||
for {
|
||||
if eng == nil {
|
||||
break
|
||||
}
|
||||
engines = append(engines, C.GoString(C.ENGINE_get_id(eng)))
|
||||
eng = C.ENGINE_get_next(eng)
|
||||
}
|
||||
C.ENGINE_free(eng)
|
||||
return engines
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
engine *C.ENGINE
|
||||
engine *C.ENGINE;
|
||||
}
|
||||
|
||||
func OpenEngine(name string, args map[string]string) (*Engine, error) {
|
||||
func GetEngineByName(name string) (*Engine, error) {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
|
||||
engine := C.ENGINE_by_id(cname)
|
||||
if engine == nil {
|
||||
return nil, fmt.Errorf("shit")
|
||||
eng := C.ENGINE_by_id(cname)
|
||||
if eng == nil {
|
||||
cerrors := C.get_errors()
|
||||
return nil, errors.New(C.GoString(cerrors))
|
||||
}
|
||||
|
||||
ret := &Engine{engine}
|
||||
ret := &Engine{eng}
|
||||
runtime.SetFinalizer(ret, func(ret *Engine) {
|
||||
C.ENGINE_finish(ret.engine)
|
||||
C.ENGINE_free(ret.engine)
|
||||
})
|
||||
if 1 != C.ENGINE_init(eng) {
|
||||
cerrors := C.get_errors()
|
||||
return nil, errors.New(C.GoString(cerrors))
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (eng *Engine) ExecuteCommand(cmd_name string, arg string, optinal bool) error {
|
||||
ccmd := C.CString(cmd_name)
|
||||
defer C.free(unsafe.Pointer(ccmd))
|
||||
func (e *Engine) RunCommand(name, arg string) error {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
carg := C.CString(arg)
|
||||
defer C.free(unsafe.Pointer(carg))
|
||||
|
||||
if 1 != C.ENGINE_ctrl_cmd_string(eng.engine, ccmd, carg, 0) {
|
||||
return errors.New("Not implemented")
|
||||
if 1 != C.ENGINE_ctrl_cmd_string(e.engine, cname, carg, 0) {
|
||||
cerrors := C.get_errors()
|
||||
return errors.New(C.GoString(cerrors))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (eng *Engine) LoadPrivateKey(key_id string, args map[string]string) (*PrivateKey, error) {
|
||||
cid := C.CString(key_id)
|
||||
func (e *Engine) LoadConfigFile(path string) error {
|
||||
return errors.New("Engine.LoadConfigFile() not implemented")
|
||||
}
|
||||
|
||||
func (e *Engine) GetPrivateKey(id string, pass string) (*PrivateKey, error) {
|
||||
cid := C.CString(id)
|
||||
defer C.free(unsafe.Pointer(cid))
|
||||
|
||||
pkey := C.ENGINE_load_private_key(eng.engine, cid, C.NULL, C.NULL)
|
||||
if pkey == nil {
|
||||
return nil, fmt.Errorf("shit")
|
||||
cpass := C.CString(pass)
|
||||
defer C.free(unsafe.Pointer(cpass))
|
||||
sk := C.load_private_key_from_engine(e.engine, cid, cpass)
|
||||
if sk == nil {
|
||||
cerrors := C.get_errors()
|
||||
return nil, errors.New(C.GoString(cerrors))
|
||||
}
|
||||
|
||||
ret := &PrivateKey{pkey}
|
||||
runtime.SetFinalizer(ret, func(ret *PrivateKey) {
|
||||
C.EVP_PKEY_free(ret.pkey)
|
||||
})
|
||||
return ret, nil
|
||||
return &PrivateKey{sk}, nil
|
||||
}
|
||||
|
||||
func (eng *Engine) LoadPublicKey(key_id string, args map[string]string) (*PublicKey, error) {
|
||||
cid := C.CString(key_id)
|
||||
func (e *Engine) GetPublicKey(id string) (
|
||||
*PublicKey, error) {
|
||||
cid := C.CString(id)
|
||||
defer C.free(unsafe.Pointer(cid))
|
||||
|
||||
pkey := C.ENGINE_load_public_key(eng.engine, cid, C.NULL, C.NULL)
|
||||
if pkey == nil {
|
||||
return nil, fmt.Errorf("shit")
|
||||
}
|
||||
|
||||
ret := &PublicKey{pkey}
|
||||
runtime.SetFinalizer(ret, func(ret *PublicKey) {
|
||||
C.EVP_PKEY_free(ret.pkey)
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (eng *Engine) LoadCertificate(ca_dn []string, args map[string]string) (string, error) {
|
||||
return "", errors.New("Not implemented")
|
||||
//pk := C.ENGINE_load_public_key(e.engine, cid, C.NULL, C.NULL)
|
||||
//if pk == nil {
|
||||
// cerrors := C.get_errors()
|
||||
// return nil, errors.New(C.GoString(cerrors))
|
||||
//}
|
||||
//return &PublicKey{pk}, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user