Skip to content

Commit 688f589

Browse files
gdamsqmuntalCopilot
authored
openssl: implement cgoless wrappers for C functions (#288)
* openssl: implement cgoless wrappers for C functions * fix aes test * remove darwin pins * unskip md4 hash test * enable more tests * re enable symcrypt tests * add boiletplate linux syscall file * auto generate variadic functions * add checkheader if statement back * fix windows * implement windows support * fix iSAvailable functions * fix renamed function in nocgo * support dynload mode * panic if non-optional functions can't be loaded * deduplicate code * fix unix * fix windows * fix windows * fix windows * deduplicate variadic handling * simplify code a bit * fix nocgo error handling * fix typedefs * fix dlsym * fix pointer tests * several improvements and fixes * fix cTypeSize * fix type map * fix type map * fix data types * use dlclose on nocgo * fix newMkcgoErr * unhardcode dlopen and friends * fix RTLD_LOCAL * use keepalive * support cgoless linux and darwin * add cgoless test to CI * gate syscallN assembly * deduplicate some code * revert msgo bump * mkcgo cleanup * fix static trmapolines * fix static trmapolines * remove point_conversion_form_t special case * remove point_conversion_form_t special case * remove stale comment * don't hardcode zdl.go * don't hardcode zdl.go * fix needsAssembly * cleanup * deduplicate fakecgo * support 32-bit arches * fix fakecgo * gate ocgo changes behind a build flag * readd cgoless without build tag test * add eof * revert changes in the root package * add missing build tags * add cgo suffix to cgo files * put common nocgo and cgo code in same file * add missing build tags * don't generate 1.24 file if not needed * generalize checkheader * add copyright flag to mkcgo * Update internal/ossl/errors_nocgo.go Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: qmuntal <[email protected]> Co-authored-by: qmuntal <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent dab968e commit 688f589

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+7881
-1522
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Check headers
2727
working-directory: ./cmd/checkheader
2828
run: |
29-
go run . --ossl-include /usr/local/src/openssl-${{ matrix.openssl-version }}/include -shim ../../internal/ossl/shims.h
29+
go run . -include /usr/local/src/openssl-${{ matrix.openssl-version }}/include -shim ../../internal/ossl/shims.h
3030
- name: Set OpenSSL config and prove FIPS
3131
run: |
3232
sudo cp ./scripts/openssl-3.cnf /usr/local/ssl/openssl.cnf

cmd/checkheader/main.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/golang-fips/openssl/v2/internal/mkcgo"
1212
)
1313

14-
// checkheader is a static analyzer that detects incompatibilities between wrapper definitions and OpenSSL headers.
15-
// It generates a C source file where for each wrapper, the file declares a new symbol that's assigned to the symbol it represents in the actual OpenSSL headers.
14+
// checkheader is a static analyzer that detects incompatibilities between wrapper definitions and system headers.
15+
// It generates a C source file where for each wrapper, the file declares a new symbol that's assigned to the symbol it represents in the actual system headers.
1616
// This C file is then compiled using GCC. The compilation will succeed if everything is compatible, else it will
1717
// report a meaningful error.
1818
//
@@ -21,20 +21,20 @@ import (
2121
// - Blank lines are discarded.
2222
// - Comments are discarded unless they contain a C directive, i.e #include, #if or #endif. The directive in the comment is included in the output.
2323
// - Typedefs following the pattern "typedef void* _%name%_PTR" are translated into "#define %name% _%name%_PTR".
24-
// - Go constants are validated against their definition in the OpenSSL headers. Example:
24+
// - Go constants are validated against their definition in the system headers. Example:
2525
// "const { _EVP_CTRL_GCM_SET_TAG = 0x11 }" => "_Static_assert(EVP_CTRL_GCM_SET_TAG == 0x11);"
26-
// - Function macros are validated against their definition in the OpenSSL headers. Example:
26+
// - Function macros are validated against their definition in the system headers. Example:
2727
// "int RAND_bytes(unsigned char *a0, int a1)" => "int(*__check_0)(unsigned char *, int) = RAND_bytes;"
2828

2929
const description = `
3030
Example: A check operation:
31-
go run ./cmd/checkheader --ossl-include /usr/local/src/openssl-1.1.1/include -shim ./internal/ossl/shims.h
31+
go run ./cmd/checkheader -include /usr/local/src/openssl-1.1.1/include -shim ./internal/ossl/shims.h
3232
Checkheader generates a C program and compiles it with gcc. The compilation verifies types and functions defined in the target
33-
header file match the definitions in --ossl-include.
33+
header file match the definitions in -include.
3434
`
3535

36-
var osslInclude = flag.String("ossl-include", "", "OpenSSL include directory. Required.")
37-
var osslShim = flag.String("shim", "", "C header containing the OpenSSL wrappers. Required.")
36+
var shim = flag.String("shim", "", "C header containing the OpenSSL wrappers. Required.")
37+
var include = flag.String("include", "", "include directory")
3838
var work = flag.Bool("work", false, "print the name of the temporary C program file and do not delete it when exiting.")
3939

4040
func main() {
@@ -44,20 +44,17 @@ func main() {
4444
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", description)
4545
}
4646
flag.Parse()
47-
if *osslInclude == "" {
48-
fmt.Fprintln(flag.CommandLine.Output(), "required flag not provided: --ossl-include")
49-
flag.Usage()
50-
os.Exit(1)
51-
}
52-
if *osslShim == "" {
47+
if *shim == "" {
5348
fmt.Fprintln(flag.CommandLine.Output(), "required flag not provided: -shim")
5449
flag.Usage()
5550
os.Exit(1)
5651
}
57-
if _, err := os.Stat(*osslInclude); err != nil {
58-
log.Fatalf("OpenSSL include directory not found: %v\n", err)
52+
if *include != "" {
53+
if _, err := os.Stat(*include); err != nil {
54+
log.Fatalf("OpenSSL include directory not found: %v\n", err)
55+
}
5956
}
60-
s, err := generate(*osslShim)
57+
s, err := generate(*shim)
6158
if err != nil {
6259
log.Fatal(err)
6360
}
@@ -91,7 +88,7 @@ func gccRun(program string) error {
9188
"-c", // skip linking
9289
"-Werror", // promote all warnings to errors
9390
"-DOPENSSL_NO_DEPRECATED", // hide deprecated functions
94-
"-isystem", *osslInclude, // OpenSSL include from --ossl-include must be preferred over system includes
91+
"-isystem", *include, // OpenSSL include from --isystem must be preferred over system includes
9592
"-o", os.DevNull, // discard output
9693
name)
9794
p.Stdout = os.Stdout
@@ -119,15 +116,17 @@ func generate(header string) (string, error) {
119116
}
120117

121118
for _, enum := range src.Enums {
122-
if enum.Name == "_EVP_PKEY_OP_DERIVE" {
123-
// This is defined differently in OpenSSL 3,
124-
// but in our code it is only used in OpenSSL 1.
125-
continue
119+
for _, enumValue := range enum.Values {
120+
if enumValue.Name == "_EVP_PKEY_OP_DERIVE" {
121+
// This is defined differently in OpenSSL 3,
122+
// but in our code it is only used in OpenSSL 1.
123+
continue
124+
}
125+
name := strings.TrimPrefix(enumValue.Name, "_")
126+
fmt.Fprintf(w, "#ifdef %s\n", name)
127+
fmt.Fprintf(w, "_Static_assert(%s == %s, \"%s\");\n", enumValue.Value, name, enumValue.Name)
128+
fmt.Fprintln(w, "#endif")
126129
}
127-
name := strings.TrimPrefix(enum.Name, "_")
128-
fmt.Fprintf(w, "#ifdef %s\n", name)
129-
fmt.Fprintf(w, "_Static_assert(%s == %s, \"%s\");\n", enum.Value, name, enum.Name)
130-
fmt.Fprintln(w, "#endif")
131130
}
132131

133132
for _, def := range src.TypeDefs {

0 commit comments

Comments
 (0)