-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x509: generate serial number for nil template SerialNumber #279
- Loading branch information
Showing
2 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1626,16 +1626,18 @@ func TestInsecureAlgorithmErrorString(t *testing.T) { | |
*/ | ||
|
||
// These CSR was generated with OpenSSL: | ||
// openssl req -out CSR.csr -new -sha256 -nodes -keyout privateKey.key -config openssl.cnf | ||
// | ||
// openssl req -out CSR.csr -new -sha256 -nodes -keyout privateKey.key -config openssl.cnf | ||
// | ||
// With openssl.cnf containing the following sections: | ||
// [ v3_req ] | ||
// basicConstraints = CA:FALSE | ||
// keyUsage = nonRepudiation, digitalSignature, keyEncipherment | ||
// subjectAltName = email:[email protected],DNS:test.example.com | ||
// [ req_attributes ] | ||
// challengePassword = ignored challenge | ||
// unstructuredName = ignored unstructured name | ||
// | ||
// [ v3_req ] | ||
// basicConstraints = CA:FALSE | ||
// keyUsage = nonRepudiation, digitalSignature, keyEncipherment | ||
// subjectAltName = email:[email protected],DNS:test.example.com | ||
// [ req_attributes ] | ||
// challengePassword = ignored challenge | ||
// unstructuredName = ignored unstructured name | ||
var csrBase64Array = [...]string{ | ||
// Just [ v3_req ] | ||
"MIIDHDCCAgQCAQAwfjELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEUMBIGA1UEAwwLQ29tbW9uIE5hbWUxITAfBgkqhkiG9w0BCQEWEnRlc3RAZW1haWwuYWRkcmVzczCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK1GY4YFx2ujlZEOJxQVYmsjUnLsd5nFVnNpLE4cV+77sgv9NPNlB8uhn3MXt5leD34rm/2BisCHOifPucYlSrszo2beuKhvwn4+2FxDmWtBEMu/QA16L5IvoOfYZm/gJTsPwKDqvaR0tTU67a9OtxwNTBMI56YKtmwd/o8d3hYv9cg+9ZGAZ/gKONcg/OWYx/XRh6bd0g8DMbCikpWgXKDsvvK1Nk+VtkDO1JxuBaj4Lz/p/MifTfnHoqHxWOWl4EaTs4Ychxsv34/rSj1KD1tJqorIv5Xv2aqv4sjxfbrYzX4kvS5SC1goIovLnhj5UjmQ3Qy8u65eow/LLWw+YFcCAwEAAaBZMFcGCSqGSIb3DQEJDjFKMEgwCQYDVR0TBAIwADALBgNVHQ8EBAMCBeAwLgYDVR0RBCcwJYERZ29waGVyQGdvbGFuZy5vcmeCEHRlc3QuZXhhbXBsZS5jb20wDQYJKoZIhvcNAQELBQADggEBAB6VPMRrchvNW61Tokyq3ZvO6/NoGIbuwUn54q6l5VZW0Ep5Nq8juhegSSnaJ0jrovmUgKDN9vEo2KxuAtwG6udS6Ami3zP+hRd4k9Q8djJPb78nrjzWiindLK5Fps9U5mMoi1ER8ViveyAOTfnZt/jsKUaRsscY2FzE9t9/o5moE6LTcHUS4Ap1eheR+J72WOnQYn3cifYaemsA9MJuLko+kQ6xseqttbh9zjqd9fiCSh/LNkzos9c+mg2yMADitaZinAh+HZi50ooEbjaT3erNq9O6RqwJlgD00g6MQdoz9bTAryCUhCQfkIaepmQ7BxS0pqWNW3MMwfDwx/Snz6g=", | ||
|
@@ -2126,6 +2128,33 @@ func TestAdditionFieldsInGeneralSubtree(t *testing.T) { | |
} | ||
} | ||
|
||
func TestEmptySerialNumber(t *testing.T) { | ||
template := Certificate{ | ||
DNSNames: []string{"example.com"}, | ||
} | ||
for i := 0; i < 100; i++ { | ||
derBytes, err := CreateCertificate(rand.Reader, &template, &template, &testPrivateKey.PublicKey, testPrivateKey) | ||
if err != nil { | ||
t.Fatalf("failed to create certificate: %s", err) | ||
} | ||
cert, err := ParseCertificate(derBytes) | ||
if err != nil { | ||
t.Fatalf("failed to parse certificate: %s", err) | ||
} | ||
if sign := cert.SerialNumber.Sign(); sign != 1 { | ||
t.Fatalf("generated a non positive serial, sign: %d", sign) | ||
} | ||
b, err := asn1.Marshal(cert.SerialNumber) | ||
if err != nil { | ||
t.Fatalf("failed to marshal generated serial number: %s", err) | ||
} | ||
// subtract 2 for tag and length | ||
if l := len(b) - 2; l > 20 { | ||
t.Fatalf("generated serial number larger than 20 octets when encoded: %d", l) | ||
} | ||
} | ||
} | ||
|
||
func TestEmptySubject(t *testing.T) { | ||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
|