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
19 changes: 16 additions & 3 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import "C"
import (
"errors"
"io/ioutil"
"math/big"
"runtime"
"time"
"unsafe"
Expand Down Expand Up @@ -57,7 +58,7 @@ type Certificate struct {
}

type CertificateInfo struct {
Serial int
Serial *big.Int
Issued time.Duration
Expires time.Duration
Country string
Expand Down Expand Up @@ -193,8 +194,20 @@ func (c *Certificate) SetIssuerName(name *Name) error {
}

// SetSerial sets the serial of a certificate.
func (c *Certificate) SetSerial(serial int) error {
if C.ASN1_INTEGER_set(C.X509_get_serialNumber(c.x), C.long(serial)) != 1 {
func (c *Certificate) SetSerial(serial *big.Int) error {
sno := C.ASN1_INTEGER_new()
defer C.ASN1_INTEGER_free(sno)
bn := C.BN_new()
defer C.BN_free(bn)

serialBytes := serial.Bytes()
if bn = C.BN_bin2bn((*C.uchar)(unsafe.Pointer(&serialBytes[0])), C.int(len(serialBytes)), bn); bn == nil {
return errors.New("failed to set serial")
}
if sno = C.BN_to_ASN1_INTEGER(bn, sno); sno == nil {
return errors.New("failed to set serial")
}
if C.X509_set_serialNumber(c.x, sno) != 1 {
return errors.New("failed to set serial")
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package openssl

import (
"math/big"
"testing"
"time"
)
Expand All @@ -25,7 +26,7 @@ func TestCertGenerate(t *testing.T) {
t.Fatal(err)
}
info := &CertificateInfo{
Serial: 1,
Serial: big.NewInt(int64(1)),
Issued: 0,
Expires: 24 * time.Hour,
Country: "US",
Expand All @@ -47,7 +48,7 @@ func TestCAGenerate(t *testing.T) {
t.Fatal(err)
}
info := &CertificateInfo{
Serial: 1,
Serial: big.NewInt(int64(1)),
Issued: 0,
Expires: 24 * time.Hour,
Country: "US",
Expand All @@ -74,7 +75,7 @@ func TestCAGenerate(t *testing.T) {
t.Fatal(err)
}
info = &CertificateInfo{
Serial: 1,
Serial: big.NewInt(int64(1)),
Issued: 0,
Expires: 24 * time.Hour,
Country: "US",
Expand Down