From b524161802d261f7de7a5a3feb252b69f746679b Mon Sep 17 00:00:00 2001 From: Jeannie Kidd Date: Fri, 17 Apr 2020 09:44:23 -0700 Subject: [PATCH] implement asn1 parsing --- asn1.go | 33 +++++++++++++++++++++++++++++++++ shim.h | 1 + 2 files changed, 34 insertions(+) create mode 100644 asn1.go diff --git a/asn1.go b/asn1.go new file mode 100644 index 00000000..fdcff435 --- /dev/null +++ b/asn1.go @@ -0,0 +1,33 @@ +package openssl + +// #include "shim.h" +import "C" + +import ( + "errors" + "io/ioutil" +) + +// ASN1Parse parses and extracts ASN.1 structure and returns the data in text format +func ASN1Parse(asn1 []byte) (string, error) { + if len(asn1) == 0 { + return "", errors.New("empty asn1 structure") + } + + out := C.BIO_new(C.BIO_s_mem()) + if out == nil { + return "", errors.New("failed allocating output buffer") + } + defer C.BIO_free(out) + + if int(C.ASN1_parse_dump(out, (*C.uchar)(&asn1[0]), C.long(len(asn1)), 1, 0)) == 0 { + return "", errors.New("failed to parse asn1 data") + } + + parsed, err := ioutil.ReadAll(asAnyBio(out)) + if err != nil { + return "", errors.New("failed to read bio data as bytes") + } + + return string(parsed), nil +} diff --git a/shim.h b/shim.h index c77a0a7d..2837d6d7 100644 --- a/shim.h +++ b/shim.h @@ -32,6 +32,7 @@ #include #include #include +#include #ifndef SSL_MODE_RELEASE_BUFFERS #define SSL_MODE_RELEASE_BUFFERS 0