Skip to content

Commit

Permalink
Merge pull request #155 from rolandshoemaker/serialutils
Browse files Browse the repository at this point in the history
Serial conversion functions
  • Loading branch information
jsha committed May 7, 2015
2 parents b1e3c37 + ae62792 commit f6c7b49
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ca/certificate-authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestRevoke(t *testing.T) {
}
cert, err := x509.ParseCertificate(certObj.DER)
test.AssertNotError(t, err, "Certificate failed to parse")
serialString := fmt.Sprintf("%032x", cert.SerialNumber)
serialString := core.SerialToString(cert.SerialNumber)
err = ca.RevokeCertificate(serialString)
test.AssertNotError(t, err, "Revocation failed")

Expand Down Expand Up @@ -415,7 +415,7 @@ func TestIssueCertificate(t *testing.T) {
}

// Verify that the cert got stored in the DB
serialString := fmt.Sprintf("%032x", cert.SerialNumber)
serialString := core.SerialToString(cert.SerialNumber)
certBytes, err := storageAuthority.GetCertificate(serialString)
test.AssertNotError(t, err,
fmt.Sprintf("Certificate %s not found in database", serialString))
Expand Down
14 changes: 14 additions & 0 deletions core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
blog "github.com/letsencrypt/boulder/log"
"hash"
"io"
Expand Down Expand Up @@ -188,3 +189,16 @@ func VerifyCSR(csr *x509.CertificateRequest) error {

return errors.New("Unsupported CSR signing algorithm")
}

func SerialToString(serial *big.Int) string {
return fmt.Sprintf("%032x", serial)
}

func StringToSerial(serial string) (*big.Int, error) {
var serialNum big.Int
if len(serial) != 32 {
return &serialNum, errors.New("Serial number should be 32 characters long")
}
_, err := fmt.Sscanf(serial, "%032x", &serialNum)
return &serialNum, err
}
14 changes: 14 additions & 0 deletions core/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"github.com/letsencrypt/boulder/test"
"math"
"math/big"
)

// challenges.go
Expand All @@ -34,3 +35,16 @@ func TestRandString(t *testing.T) {
// This is covered by NewToken
return
}

func TestSerialUtils(t *testing.T) {
serial := SerialToString(big.NewInt(100000000000000000))
test.AssertEquals(t, serial, "0000000000000000016345785d8a0000")

serialNum, err := StringToSerial("0000000000000000016345785d8a0000")
test.AssertNotError(t, err, "Couldn't convert serial number to *big.Int")
test.AssertBigIntEquals(t, serialNum, big.NewInt(100000000000000000))

badSerial, err := StringToSerial("doop!!!!000")
test.AssertEquals(t, fmt.Sprintf("%v", err), "Serial number should be 32 characters long")
fmt.Println(badSerial)
}
2 changes: 1 addition & 1 deletion ra/registration-authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestNewCertificate(t *testing.T) {
test.AssertNotError(t, err, "Failed to parse certificate")

// Verify that cert shows up and is as expected
dbCert, err := sa.GetCertificate(fmt.Sprintf("%032x", parsedCert.SerialNumber))
dbCert, err := sa.GetCertificate(core.SerialToString(parsedCert.SerialNumber))
test.AssertNotError(t, err, fmt.Sprintf("Could not fetch certificate %032x from database",
parsedCert.SerialNumber))
test.Assert(t, bytes.Compare(cert.DER, dbCert) == 0, "Certificates differ")
Expand Down
2 changes: 1 addition & 1 deletion sa/storage-authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func (ssa *SQLStorageAuthority) AddCertificate(certDER []byte) (digest string, e
if err != nil {
return
}
serial := fmt.Sprintf("%032x", parsedCertificate.SerialNumber)
serial := core.SerialToString(parsedCertificate.SerialNumber)

tx, err := ssa.db.Begin()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions test/test-tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"encoding/base64"
"fmt"
"math/big"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -74,6 +75,12 @@ func AssertIntEquals(t *testing.T, one int, two int) {
}
}

func AssertBigIntEquals(t *testing.T, one *big.Int, two *big.Int) {
if one.Cmp(two) != 0 {
t.Errorf("%s Int [%d] != [%d]", caller(), one, two)
}
}

func AssertContains(t *testing.T, haystack string, needle string) {
if !strings.Contains(haystack, needle) {
t.Errorf("%s String [%s] does not contain [%s]", caller(), haystack, needle)
Expand Down

0 comments on commit f6c7b49

Please sign in to comment.