Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial conversion functions #155

Merged
merged 2 commits into from
May 7, 2015
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
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