Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions asn1.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <openssl/objects.h>
#include <openssl/obj_mac.h>
#include <openssl/cms.h>
#include <openssl/asn1.h>

#ifndef SSL_MODE_RELEASE_BUFFERS
#define SSL_MODE_RELEASE_BUFFERS 0
Expand Down