-
Notifications
You must be signed in to change notification settings - Fork 12
/
init.go
64 lines (58 loc) · 2.06 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build !cmd_go_bootstrap
package openssl
// #include "goopenssl.h"
import "C"
import (
"errors"
)
// opensslInit loads and initialize OpenSSL.
// If successful, it returns the major and minor OpenSSL version
// as reported by the OpenSSL API.
//
// See Init() for details about file.
func opensslInit(file string) (major, minor, patch uint, err error) {
// Load the OpenSSL shared library using dlopen.
handle, err := dlopen(file)
if err != nil {
return 0, 0, 0, err
}
// Retrieve the loaded OpenSSL version and check if it is supported.
// Notice that major and minor could not match with the version parameter
// in case the name of the shared library file differs from the OpenSSL
// version it contains.
imajor := int(C.go_openssl_version_major(handle))
iminor := int(C.go_openssl_version_minor(handle))
ipatch := int(C.go_openssl_version_patch(handle))
if imajor < 0 || iminor < 0 || ipatch < 0 {
return 0, 0, 0, errors.New("openssl: can't retrieve OpenSSL version")
}
major, minor, patch = uint(imajor), uint(iminor), uint(ipatch)
var supported bool
if major == 1 {
supported = minor == 0 || minor == 1
} else if major == 3 {
// OpenSSL guarantees API and ABI compatibility within the same major version since OpenSSL 3.
supported = true
}
if !supported {
return 0, 0, 0, errUnsupportedVersion()
}
// Load the OpenSSL functions.
// See shims.go for the complete list of supported functions.
C.go_openssl_load_functions(handle, C.uint(major), C.uint(minor), C.uint(patch))
// Initialize OpenSSL.
C.go_openssl_OPENSSL_init()
if major == 1 && minor == 0 {
if C.go_openssl_thread_setup() != 1 {
return 0, 0, 0, fail("openssl: thread setup")
}
C.go_openssl_OPENSSL_add_all_algorithms_conf()
C.go_openssl_ERR_load_crypto_strings()
} else {
flags := C.uint64_t(C.GO_OPENSSL_INIT_ADD_ALL_CIPHERS | C.GO_OPENSSL_INIT_ADD_ALL_DIGESTS | C.GO_OPENSSL_INIT_LOAD_CONFIG | C.GO_OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
if C.go_openssl_OPENSSL_init_crypto(flags, nil) != 1 {
return 0, 0, 0, fail("openssl: init crypto")
}
}
return major, minor, patch, nil
}