func (self *X509Name) Print() ([]byte, error) { bio := C.BIO_new(C.BIO_s_mem()) defer C.BIO_free(bio) //TODO check for error here C.X509_NAME_print_ex(bio, self.Name, 0, C.XN_FLAG_MULTILINE) var temp *C.char buf_len := C.BIO_ctrl(bio, C.BIO_CTRL_INFO, 0, unsafe.Pointer(&temp)) return C.GoBytes(unsafe.Pointer(temp), C.int(buf_len)), nil }
//Export an OpenSSL X509 to a DER buffer func (self *Certificate) DumpDERCertificate() ([]byte, error) { bio := C.BIO_new(C.BIO_s_mem()) defer C.BIO_free(bio) ret := C.i2d_X509_bio(bio, self.x509) if ret == 0 { return nil, errors.New("problem dumping certificate") } var temp *C.char buf_len := C.BIO_ctrl(bio, C.BIO_CTRL_INFO, 0, unsafe.Pointer(&temp)) return C.GoBytes(unsafe.Pointer(temp), C.int(buf_len)), nil }
// MarshalPEM converts the X509 certificate to PEM-encoded format func (c *Certificate) MarshalPEM() (pem_block []byte, err error) { bio := C.BIO_new(C.BIO_s_mem()) if bio == nil { return nil, errors.New("failed to allocate memory BIO") } defer C.BIO_free(bio) if int(C.PEM_write_bio_X509(bio, c.x)) != 1 { return nil, errors.New("failed dumping certificate") } return ioutil.ReadAll(asAnyBio(bio)) }
func (c *Certificate) X509NamePrintEx() (out []byte, err error) { bio := C.BIO_new(C.BIO_s_mem()) if bio == nil { return nil, errors.New("failed to allocate memory BIO") } defer C.BIO_free(bio) name := C.X509_get_subject_name(c.x) // TODO, pass in flags instead of using this hardcoded one if int(C.X509_NAME_print_ex(bio, name, 0, C.XN_FLAG_RFC2253)) < 0 { return nil, errors.New("failed formatting subject") } return ioutil.ReadAll(asAnyBio(bio)) }
func (self *PKey) DumpPEM() ([]byte, error) { bio := C.BIO_new(C.BIO_s_mem()) defer C.BIO_free(bio) if bio == nil { return nil, errors.New("problem converting pem key to openssl key") } ret := C.PEM_write_bio_PrivateKey(bio, self.PKey, nil, nil, 0, nil, nil) if int(ret) == 0 { return nil, errors.New(sslerr.SSLErrorMessage()) } var temp *C.char buf_len := C.BIO_ctrl(bio, C.BIO_CTRL_INFO, C.long(0), unsafe.Pointer(&temp)) buffer := C.GoBytes(unsafe.Pointer(temp), C.int(buf_len)) return buffer, nil }
func (key *pKey) MarshalPKIXPublicKeyDER() (der_block []byte, err error) { bio := C.BIO_new(C.BIO_s_mem()) if bio == nil { return nil, errors.New("failed to allocate memory BIO") } defer C.BIO_free(bio) rsa := (*C.RSA)(C.EVP_PKEY_get1_RSA(key.key)) if rsa == nil { return nil, errors.New("failed getting rsa key") } defer C.RSA_free(rsa) if int(C.i2d_RSA_PUBKEY_bio(bio, rsa)) != 1 { return nil, errors.New("failed dumping public key der") } return ioutil.ReadAll(asAnyBio(bio)) }
func (key *pKey) MarshalPKCS1PrivateKeyPEM() (pem_block []byte, err error) { bio := C.BIO_new(C.BIO_s_mem()) if bio == nil { return nil, errors.New("failed to allocate memory BIO") } defer C.BIO_free(bio) rsa := (*C.RSA)(C.EVP_PKEY_get1_RSA(key.key)) if rsa == nil { return nil, errors.New("failed getting rsa key") } defer C.RSA_free(rsa) if int(C.PEM_write_bio_RSAPrivateKey(bio, rsa, nil, nil, C.int(0), nil, nil)) != 1 { return nil, errors.New("failed dumping private key") } return ioutil.ReadAll(asAnyBio(bio)) }
func NewBIO(method *BIOMethod) *BIO { return &BIO{BIO: C.BIO_new(method.BIOMethod)} }
func (b *readBio) MakeCBIO() *C.BIO { b.buf = bufPool.Get() rv := C.BIO_new(C.BIO_s_readBio()) rv.ptr = unsafe.Pointer(b) return rv }
func (b *writeBio) MakeCBIO() *C.BIO { rv := C.BIO_new(C.BIO_s_writeBio()) rv.ptr = unsafe.Pointer(b) return rv }
func (b *readBio) MakeCBIO() *C.BIO { rv := C.BIO_new(C.BIO_s_readBio()) token := readBioMapping.Add(unsafe.Pointer(b)) rv.ptr = unsafe.Pointer(token) return rv }
func NewBIO(method *BIOMethod) *BIO { return newBIO(C.BIO_new(method.BIOMethod)) }