-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgo-certlint.go
90 lines (77 loc) · 2.52 KB
/
go-certlint.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* go-certlint - Go wrapper for github.com/awslabs/certlint
* Written by Rob Stradling
* Copyright (C) 2018-2020 Sectigo Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package certlint
import (
"unsafe"
)
/*
#cgo pkg-config: ruby-?.?
#include <ruby.h>
typedef struct ruby_shared_data {
VALUE obj;
ID method_id;
int nargs;
VALUE args[4];
} ruby_shared_data;
static VALUE ruby_callback(VALUE ptr)
{
ruby_shared_data* data = (ruby_shared_data*)ptr;
return rb_funcall2(data->obj, data->method_id, data->nargs, data->args);
}
VALUE rescue_require(VALUE data, VALUE err)
{
fprintf(stderr, "Error: [[[%s]]]\n", RSTRING(rb_obj_as_string(err))->as.heap.ptr);
return Qnil;
}
void init_certlint(char* lib_dir, char* ext_dir)
{
ruby_init();
ruby_script("go-certlint");
ruby_init_loadpath();
ruby_incpush(lib_dir);
ruby_incpush(ext_dir);
rb_require("enc/encdb");
rb_require("enc/trans/transdb");
rb_require("certlint");
rb_eval_string("def runcablint(raw) return CertLint::CABLint.lint(raw).join(\"\n\") end");
rb_eval_string("def runcertlint(raw) return CertLint.lint(raw).join(\"\n\") end");
}
char* lint(char* linter, unsigned char* cert_buffer, size_t cert_len)
{
ruby_shared_data rbdata;
rbdata.obj = 0;
rbdata.method_id = rb_intern(linter);
rbdata.nargs = 1;
rbdata.args[0] = rb_str_new((char*)cert_buffer, cert_len);
VALUE t_result = rb_rescue(&ruby_callback, (VALUE)&rbdata, &rescue_require, Qnil);
return RSTRING_PTR(t_result);
}
*/
import "C"
func Init(base_dir string) {
C.init_certlint(C.CString(base_dir + "/lib"), C.CString(base_dir + "/ext"))
}
func Cablint(cert_der []byte) string {
return C.GoString(C.lint(C.CString("runcablint"), (*C.uchar)(unsafe.Pointer(&cert_der[0])), (C.ulong)(len(cert_der)))) + "\n"
}
func Certlint(cert_der []byte) string {
return C.GoString(C.lint(C.CString("runcertlint"), (*C.uchar)(unsafe.Pointer(&cert_der[0])), (C.ulong)(len(cert_der)))) + "\n"
}
func Finish() {
C.ruby_cleanup(0)
}