Skip to content

Commit

Permalink
make CertificateInfo Serial type to *big.Int
Browse files Browse the repository at this point in the history
  • Loading branch information
Phus Lu committed Apr 4, 2015
1 parent 84b5df4 commit 175e155
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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

0 comments on commit 175e155

Please sign in to comment.