make CertificateInfo Serial type to *big.Int

This commit is contained in:
Phus Lu 2015-04-04 20:22:19 +08:00
parent 84b5df4774
commit 175e155dd4
2 changed files with 20 additions and 6 deletions

19
cert.go
View File

@ -27,6 +27,7 @@ import "C"
import ( import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"math/big"
"runtime" "runtime"
"time" "time"
"unsafe" "unsafe"
@ -57,7 +58,7 @@ type Certificate struct {
} }
type CertificateInfo struct { type CertificateInfo struct {
Serial int Serial *big.Int
Issued time.Duration Issued time.Duration
Expires time.Duration Expires time.Duration
Country string Country string
@ -193,8 +194,20 @@ func (c *Certificate) SetIssuerName(name *Name) error {
} }
// SetSerial sets the serial of a certificate. // SetSerial sets the serial of a certificate.
func (c *Certificate) SetSerial(serial int) error { func (c *Certificate) SetSerial(serial *big.Int) error {
if C.ASN1_INTEGER_set(C.X509_get_serialNumber(c.x), C.long(serial)) != 1 { 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 errors.New("failed to set serial")
} }
return nil return nil

View File

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