9ff56c9570
From-SVN: r173931
248 lines
11 KiB
Go
248 lines
11 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package x509
|
|
|
|
import (
|
|
"asn1"
|
|
"big"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"encoding/hex"
|
|
"encoding/pem"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParsePKCS1PrivateKey(t *testing.T) {
|
|
block, _ := pem.Decode([]byte(pemPrivateKey))
|
|
priv, err := ParsePKCS1PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
t.Errorf("Failed to parse private key: %s", err)
|
|
return
|
|
}
|
|
if priv.PublicKey.N.Cmp(rsaPrivateKey.PublicKey.N) != 0 ||
|
|
priv.PublicKey.E != rsaPrivateKey.PublicKey.E ||
|
|
priv.D.Cmp(rsaPrivateKey.D) != 0 ||
|
|
priv.Primes[0].Cmp(rsaPrivateKey.Primes[0]) != 0 ||
|
|
priv.Primes[1].Cmp(rsaPrivateKey.Primes[1]) != 0 {
|
|
t.Errorf("got:%+v want:%+v", priv, rsaPrivateKey)
|
|
}
|
|
}
|
|
|
|
var pemPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
|
|
MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0
|
|
fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu
|
|
/ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu
|
|
RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/
|
|
EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A
|
|
IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS
|
|
tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V
|
|
-----END RSA PRIVATE KEY-----
|
|
`
|
|
|
|
func bigFromString(s string) *big.Int {
|
|
ret := new(big.Int)
|
|
ret.SetString(s, 10)
|
|
return ret
|
|
}
|
|
|
|
func fromBase10(base10 string) *big.Int {
|
|
i := new(big.Int)
|
|
i.SetString(base10, 10)
|
|
return i
|
|
}
|
|
|
|
var rsaPrivateKey = &rsa.PrivateKey{
|
|
PublicKey: rsa.PublicKey{
|
|
N: bigFromString("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077"),
|
|
E: 65537,
|
|
},
|
|
D: bigFromString("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861"),
|
|
Primes: []*big.Int{
|
|
bigFromString("98920366548084643601728869055592650835572950932266967461790948584315647051443"),
|
|
bigFromString("94560208308847015747498523884063394671606671904944666360068158221458669711639"),
|
|
},
|
|
}
|
|
|
|
func TestMarshalRSAPrivateKey(t *testing.T) {
|
|
priv := &rsa.PrivateKey{
|
|
PublicKey: rsa.PublicKey{
|
|
N: fromBase10("16346378922382193400538269749936049106320265317511766357599732575277382844051791096569333808598921852351577762718529818072849191122419410612033592401403764925096136759934497687765453905884149505175426053037420486697072448609022753683683718057795566811401938833367954642951433473337066311978821180526439641496973296037000052546108507805269279414789035461158073156772151892452251106173507240488993608650881929629163465099476849643165682709047462010581308719577053905787496296934240246311806555924593059995202856826239801816771116902778517096212527979497399966526283516447337775509777558018145573127308919204297111496233"),
|
|
E: 3,
|
|
},
|
|
D: fromBase10("10897585948254795600358846499957366070880176878341177571733155050184921896034527397712889205732614568234385175145686545381899460748279607074689061600935843283397424506622998458510302603922766336783617368686090042765718290914099334449154829375179958369993407724946186243249568928237086215759259909861748642124071874879861299389874230489928271621259294894142840428407196932444474088857746123104978617098858619445675532587787023228852383149557470077802718705420275739737958953794088728369933811184572620857678792001136676902250566845618813972833750098806496641114644760255910789397593428910198080271317419213080834885003"),
|
|
Primes: []*big.Int{
|
|
fromBase10("1025363189502892836833747188838978207017355117492483312747347695538428729137306368764177201532277413433182799108299960196606011786562992097313508180436744488171474690412562218914213688661311117337381958560443"),
|
|
fromBase10("3467903426626310123395340254094941045497208049900750380025518552334536945536837294961497712862519984786362199788654739924501424784631315081391467293694361474867825728031147665777546570788493758372218019373"),
|
|
fromBase10("4597024781409332673052708605078359346966325141767460991205742124888960305710298765592730135879076084498363772408626791576005136245060321874472727132746643162385746062759369754202494417496879741537284589047"),
|
|
},
|
|
}
|
|
|
|
derBytes := MarshalPKCS1PrivateKey(priv)
|
|
|
|
priv2, err := ParsePKCS1PrivateKey(derBytes)
|
|
if err != nil {
|
|
t.Errorf("error parsing serialized key: %s", err)
|
|
return
|
|
}
|
|
if priv.PublicKey.N.Cmp(priv2.PublicKey.N) != 0 ||
|
|
priv.PublicKey.E != priv2.PublicKey.E ||
|
|
priv.D.Cmp(priv2.D) != 0 ||
|
|
len(priv2.Primes) != 3 ||
|
|
priv.Primes[0].Cmp(priv2.Primes[0]) != 0 ||
|
|
priv.Primes[1].Cmp(priv2.Primes[1]) != 0 ||
|
|
priv.Primes[2].Cmp(priv2.Primes[2]) != 0 {
|
|
t.Errorf("got:%+v want:%+v", priv, priv2)
|
|
}
|
|
}
|
|
|
|
type matchHostnamesTest struct {
|
|
pattern, host string
|
|
ok bool
|
|
}
|
|
|
|
var matchHostnamesTests = []matchHostnamesTest{
|
|
{"a.b.c", "a.b.c", true},
|
|
{"a.b.c", "b.b.c", false},
|
|
{"", "b.b.c", false},
|
|
{"a.b.c", "", false},
|
|
{"example.com", "example.com", true},
|
|
{"example.com", "www.example.com", false},
|
|
{"*.example.com", "www.example.com", true},
|
|
{"*.example.com", "xyz.www.example.com", false},
|
|
{"*.*.example.com", "xyz.www.example.com", true},
|
|
{"*.www.*.com", "xyz.www.example.com", true},
|
|
}
|
|
|
|
func TestMatchHostnames(t *testing.T) {
|
|
for i, test := range matchHostnamesTests {
|
|
r := matchHostnames(test.pattern, test.host)
|
|
if r != test.ok {
|
|
t.Errorf("#%d mismatch got: %t want: %t", i, r, test.ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCertificateParse(t *testing.T) {
|
|
s, _ := hex.DecodeString(certBytes)
|
|
certs, err := ParseCertificates(s)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if len(certs) != 2 {
|
|
t.Errorf("Wrong number of certs: got %d want 2", len(certs))
|
|
return
|
|
}
|
|
|
|
err = certs[0].CheckSignatureFrom(certs[1])
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if err := certs[0].VerifyHostname("mail.google.com"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
var certBytes = "308203223082028ba00302010202106edf0d9499fd4533dd1297fc42a93be1300d06092a864886" +
|
|
"f70d0101050500304c310b3009060355040613025a4131253023060355040a131c546861777465" +
|
|
"20436f6e73756c74696e67202850747929204c74642e311630140603550403130d546861777465" +
|
|
"20534743204341301e170d3039303332353136343932395a170d3130303332353136343932395a" +
|
|
"3069310b3009060355040613025553311330110603550408130a43616c69666f726e6961311630" +
|
|
"140603550407130d4d6f756e7461696e205669657731133011060355040a130a476f6f676c6520" +
|
|
"496e63311830160603550403130f6d61696c2e676f6f676c652e636f6d30819f300d06092a8648" +
|
|
"86f70d010101050003818d0030818902818100c5d6f892fccaf5614b064149e80a2c9581a218ef" +
|
|
"41ec35bd7a58125ae76f9ea54ddc893abbeb029f6b73616bf0ffd868791fba7af9c4aebf3706ba" +
|
|
"3eeaeed27435b4ddcfb157c05f351d66aa87fee0de072d66d773affbd36ab78bef090e0cc861a9" +
|
|
"03ac90dd98b51c9c41566c017f0beec3bff391051ffba0f5cc6850ad2a590203010001a381e730" +
|
|
"81e430280603551d250421301f06082b0601050507030106082b06010505070302060960864801" +
|
|
"86f842040130360603551d1f042f302d302ba029a0278625687474703a2f2f63726c2e74686177" +
|
|
"74652e636f6d2f54686177746553474343412e63726c307206082b060105050701010466306430" +
|
|
"2206082b060105050730018616687474703a2f2f6f6373702e7468617774652e636f6d303e0608" +
|
|
"2b060105050730028632687474703a2f2f7777772e7468617774652e636f6d2f7265706f736974" +
|
|
"6f72792f5468617774655f5347435f43412e637274300c0603551d130101ff04023000300d0609" +
|
|
"2a864886f70d01010505000381810062f1f3050ebc105e497c7aedf87e24d2f4a986bb3b837bd1" +
|
|
"9b91ebcad98b065992f6bd2b49b7d6d3cb2e427a99d606c7b1d46352527fac39e6a8b6726de5bf" +
|
|
"70212a52cba07634a5e332011bd1868e78eb5e3c93cf03072276786f207494feaa0ed9d53b2110" +
|
|
"a76571f90209cdae884385c882587030ee15f33d761e2e45a6bc308203233082028ca003020102" +
|
|
"020430000002300d06092a864886f70d0101050500305f310b3009060355040613025553311730" +
|
|
"15060355040a130e566572695369676e2c20496e632e31373035060355040b132e436c61737320" +
|
|
"33205075626c6963205072696d6172792043657274696669636174696f6e20417574686f726974" +
|
|
"79301e170d3034303531333030303030305a170d3134303531323233353935395a304c310b3009" +
|
|
"060355040613025a4131253023060355040a131c54686177746520436f6e73756c74696e672028" +
|
|
"50747929204c74642e311630140603550403130d5468617774652053474320434130819f300d06" +
|
|
"092a864886f70d010101050003818d0030818902818100d4d367d08d157faecd31fe7d1d91a13f" +
|
|
"0b713cacccc864fb63fc324b0794bd6f80ba2fe10493c033fc093323e90b742b71c403c6d2cde2" +
|
|
"2ff50963cdff48a500bfe0e7f388b72d32de9836e60aad007bc4644a3b847503f270927d0e62f5" +
|
|
"21ab693684317590f8bfc76c881b06957cc9e5a8de75a12c7a68dfd5ca1c875860190203010001" +
|
|
"a381fe3081fb30120603551d130101ff040830060101ff020100300b0603551d0f040403020106" +
|
|
"301106096086480186f842010104040302010630280603551d110421301fa41d301b3119301706" +
|
|
"035504031310507269766174654c6162656c332d313530310603551d1f042a30283026a024a022" +
|
|
"8620687474703a2f2f63726c2e766572697369676e2e636f6d2f706361332e63726c303206082b" +
|
|
"0601050507010104263024302206082b060105050730018616687474703a2f2f6f6373702e7468" +
|
|
"617774652e636f6d30340603551d25042d302b06082b0601050507030106082b06010505070302" +
|
|
"06096086480186f8420401060a6086480186f845010801300d06092a864886f70d010105050003" +
|
|
"81810055ac63eadea1ddd2905f9f0bce76be13518f93d9052bc81b774bad6950a1eededcfddb07" +
|
|
"e9e83994dcab72792f06bfab8170c4a8edea5334edef1e53d906c7562bd15cf4d18a8eb42bb137" +
|
|
"9048084225c53e8acb7feb6f04d16dc574a2f7a27c7b603c77cd0ece48027f012fb69b37e02a2a" +
|
|
"36dcd585d6ace53f546f961e05af"
|
|
|
|
func TestCreateSelfSignedCertificate(t *testing.T) {
|
|
random := rand.Reader
|
|
|
|
block, _ := pem.Decode([]byte(pemPrivateKey))
|
|
priv, err := ParsePKCS1PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
t.Errorf("Failed to parse private key: %s", err)
|
|
return
|
|
}
|
|
|
|
template := Certificate{
|
|
SerialNumber: []byte{1},
|
|
Subject: Name{
|
|
CommonName: "test.example.com",
|
|
Organization: []string{"Acme Co"},
|
|
},
|
|
NotBefore: time.SecondsToUTC(1000),
|
|
NotAfter: time.SecondsToUTC(100000),
|
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4},
|
|
KeyUsage: KeyUsageCertSign,
|
|
|
|
BasicConstraintsValid: true,
|
|
IsCA: true,
|
|
DNSNames: []string{"test.example.com"},
|
|
|
|
PolicyIdentifiers: []asn1.ObjectIdentifier{[]int{1, 2, 3}},
|
|
PermittedDNSDomains: []string{".example.com", "example.com"},
|
|
}
|
|
|
|
derBytes, err := CreateCertificate(random, &template, &template, &priv.PublicKey, priv)
|
|
if err != nil {
|
|
t.Errorf("Failed to create certificate: %s", err)
|
|
return
|
|
}
|
|
|
|
cert, err := ParseCertificate(derBytes)
|
|
if err != nil {
|
|
t.Errorf("Failed to parse certificate: %s", err)
|
|
return
|
|
}
|
|
|
|
if len(cert.PolicyIdentifiers) != 1 || !cert.PolicyIdentifiers[0].Equal(template.PolicyIdentifiers[0]) {
|
|
t.Errorf("Failed to parse policy identifiers: got:%#v want:%#v", cert.PolicyIdentifiers, template.PolicyIdentifiers)
|
|
}
|
|
|
|
if len(cert.PermittedDNSDomains) != 2 || cert.PermittedDNSDomains[0] != ".example.com" || cert.PermittedDNSDomains[1] != "example.com" {
|
|
t.Errorf("Failed to parse name constraints: %#v", cert.PermittedDNSDomains)
|
|
}
|
|
|
|
err = cert.CheckSignatureFrom(cert)
|
|
if err != nil {
|
|
t.Errorf("Signature verification failed: %s", err)
|
|
return
|
|
}
|
|
}
|