//Helper function that calls encoding/pem to convert DER to PEM func ParseCertificatePEM(pemData []byte) (*Certificate, error) { length := C.int(len(pemData)) buffer := unsafe.Pointer(&pemData[0]) bio := C.BIO_new_mem_buf(buffer, length) cert := C.PEM_read_bio_X509(bio, nil, nil, nil) if cert == nil { return nil, errors.New("problem loading certificate" + sslerr.SSLErrorMessage().String()) } return &Certificate{x509: cert}, nil }
// LoadCertificateFromPEM loads an X509 certificate from a PEM-encoded block. func LoadCertificateFromPEM(pem_block []byte) (*Certificate, error) { if len(pem_block) == 0 { return nil, errors.New("empty pem block") } runtime.LockOSThread() defer runtime.UnlockOSThread() bio := C.BIO_new_mem_buf(unsafe.Pointer(&pem_block[0]), C.int(len(pem_block))) cert := C.PEM_read_bio_X509(bio, nil, nil, nil) C.BIO_free(bio) if cert == nil { return nil, errorFromErrorQueue() } x := &Certificate{x: cert} runtime.SetFinalizer(x, func(x *Certificate) { C.X509_free(x.x) }) return x, nil }