mirror of
https://github.com/libp2p/go-openssl.git
synced 2024-12-27 23:40:18 +08:00
76a5e0584d
[katamari commit: 05c97fb8e733433a63dcedaa7408c63beedd286f]
40 lines
647 B
Go
40 lines
647 B
Go
// Copyright (C) 2014 Space Monkey, Inc.
|
|
// +build cgo
|
|
|
|
package openssl
|
|
|
|
/*
|
|
#include "openssl/engine.h"
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"unsafe"
|
|
)
|
|
|
|
type Engine struct {
|
|
e *C.ENGINE
|
|
}
|
|
|
|
func EngineById(name string) (*Engine, error) {
|
|
cname := C.CString(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)
|
|
}
|
|
if C.ENGINE_init(e.e) == 0 {
|
|
C.ENGINE_free(e.e)
|
|
return nil, fmt.Errorf("engine %s not initialized", name)
|
|
}
|
|
runtime.SetFinalizer(e, func(e *Engine) {
|
|
C.ENGINE_finish(e.e)
|
|
C.ENGINE_free(e.e)
|
|
})
|
|
return e, nil
|
|
}
|