OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [crypto/] [x509/] [x509_test.go] - Blame information for rev 833

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package x509
6
 
7
import (
8
        "bytes"
9
        "crypto/dsa"
10
        "crypto/rand"
11
        "crypto/rsa"
12
        "crypto/x509/pkix"
13
        "encoding/asn1"
14
        "encoding/base64"
15
        "encoding/hex"
16
        "encoding/pem"
17
        "math/big"
18
        "testing"
19
        "time"
20
)
21
 
22
func TestParsePKCS1PrivateKey(t *testing.T) {
23
        block, _ := pem.Decode([]byte(pemPrivateKey))
24
        priv, err := ParsePKCS1PrivateKey(block.Bytes)
25
        if err != nil {
26
                t.Errorf("Failed to parse private key: %s", err)
27
                return
28
        }
29
        if priv.PublicKey.N.Cmp(rsaPrivateKey.PublicKey.N) != 0 ||
30
                priv.PublicKey.E != rsaPrivateKey.PublicKey.E ||
31
                priv.D.Cmp(rsaPrivateKey.D) != 0 ||
32
                priv.Primes[0].Cmp(rsaPrivateKey.Primes[0]) != 0 ||
33
                priv.Primes[1].Cmp(rsaPrivateKey.Primes[1]) != 0 {
34
                t.Errorf("got:%+v want:%+v", priv, rsaPrivateKey)
35
        }
36
}
37
 
38
func TestParsePKIXPublicKey(t *testing.T) {
39
        block, _ := pem.Decode([]byte(pemPublicKey))
40
        pub, err := ParsePKIXPublicKey(block.Bytes)
41
        if err != nil {
42
                t.Errorf("Failed to parse RSA public key: %s", err)
43
                return
44
        }
45
        rsaPub, ok := pub.(*rsa.PublicKey)
46
        if !ok {
47
                t.Errorf("Value returned from ParsePKIXPublicKey was not an RSA public key")
48
                return
49
        }
50
 
51
        pubBytes2, err := MarshalPKIXPublicKey(rsaPub)
52
        if err != nil {
53
                t.Errorf("Failed to marshal RSA public key for the second time: %s", err)
54
                return
55
        }
56
        if !bytes.Equal(pubBytes2, block.Bytes) {
57
                t.Errorf("Reserialization of public key didn't match. got %x, want %x", pubBytes2, block.Bytes)
58
        }
59
}
60
 
61
var pemPublicKey = `-----BEGIN PUBLIC KEY-----
62
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3VoPN9PKUjKFLMwOge6+
63
wnDi8sbETGIx2FKXGgqtAKpzmem53kRGEQg8WeqRmp12wgp74TGpkEXsGae7RS1k
64
enJCnma4fii+noGH7R0qKgHvPrI2Bwa9hzsH8tHxpyM3qrXslOmD45EH9SxIDUBJ
65
FehNdaPbLP1gFyahKMsdfxFJLUvbUycuZSJ2ZnIgeVxwm4qbSvZInL9Iu4FzuPtg
66
fINKcbbovy1qq4KvPIrXzhbY3PWDc6btxCf3SE0JdE1MCPThntB62/bLMSQ7xdDR
67
FF53oIpvxe/SCOymfWq/LW849Ytv3Xwod0+wzAP8STXG4HSELS4UedPYeHJJJYcZ
68
+QIDAQAB
69
-----END PUBLIC KEY-----
70
`
71
 
72
var pemPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
73
MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0
74
fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu
75
/ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu
76
RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/
77
EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A
78
IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS
79
tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V
80
-----END RSA PRIVATE KEY-----
81
`
82
 
83
func bigFromString(s string) *big.Int {
84
        ret := new(big.Int)
85
        ret.SetString(s, 10)
86
        return ret
87
}
88
 
89
func fromBase10(base10 string) *big.Int {
90
        i := new(big.Int)
91
        i.SetString(base10, 10)
92
        return i
93
}
94
 
95
func bigFromHexString(s string) *big.Int {
96
        ret := new(big.Int)
97
        ret.SetString(s, 16)
98
        return ret
99
}
100
 
101
var rsaPrivateKey = &rsa.PrivateKey{
102
        PublicKey: rsa.PublicKey{
103
                N: bigFromString("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077"),
104
                E: 65537,
105
        },
106
        D: bigFromString("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861"),
107
        Primes: []*big.Int{
108
                bigFromString("98920366548084643601728869055592650835572950932266967461790948584315647051443"),
109
                bigFromString("94560208308847015747498523884063394671606671904944666360068158221458669711639"),
110
        },
111
}
112
 
113
func TestMarshalRSAPrivateKey(t *testing.T) {
114
        priv := &rsa.PrivateKey{
115
                PublicKey: rsa.PublicKey{
116
                        N: fromBase10("16346378922382193400538269749936049106320265317511766357599732575277382844051791096569333808598921852351577762718529818072849191122419410612033592401403764925096136759934497687765453905884149505175426053037420486697072448609022753683683718057795566811401938833367954642951433473337066311978821180526439641496973296037000052546108507805269279414789035461158073156772151892452251106173507240488993608650881929629163465099476849643165682709047462010581308719577053905787496296934240246311806555924593059995202856826239801816771116902778517096212527979497399966526283516447337775509777558018145573127308919204297111496233"),
117
                        E: 3,
118
                },
119
                D: fromBase10("10897585948254795600358846499957366070880176878341177571733155050184921896034527397712889205732614568234385175145686545381899460748279607074689061600935843283397424506622998458510302603922766336783617368686090042765718290914099334449154829375179958369993407724946186243249568928237086215759259909861748642124071874879861299389874230489928271621259294894142840428407196932444474088857746123104978617098858619445675532587787023228852383149557470077802718705420275739737958953794088728369933811184572620857678792001136676902250566845618813972833750098806496641114644760255910789397593428910198080271317419213080834885003"),
120
                Primes: []*big.Int{
121
                        fromBase10("1025363189502892836833747188838978207017355117492483312747347695538428729137306368764177201532277413433182799108299960196606011786562992097313508180436744488171474690412562218914213688661311117337381958560443"),
122
                        fromBase10("3467903426626310123395340254094941045497208049900750380025518552334536945536837294961497712862519984786362199788654739924501424784631315081391467293694361474867825728031147665777546570788493758372218019373"),
123
                        fromBase10("4597024781409332673052708605078359346966325141767460991205742124888960305710298765592730135879076084498363772408626791576005136245060321874472727132746643162385746062759369754202494417496879741537284589047"),
124
                },
125
        }
126
 
127
        derBytes := MarshalPKCS1PrivateKey(priv)
128
 
129
        priv2, err := ParsePKCS1PrivateKey(derBytes)
130
        if err != nil {
131
                t.Errorf("error parsing serialized key: %s", err)
132
                return
133
        }
134
        if priv.PublicKey.N.Cmp(priv2.PublicKey.N) != 0 ||
135
                priv.PublicKey.E != priv2.PublicKey.E ||
136
                priv.D.Cmp(priv2.D) != 0 ||
137
                len(priv2.Primes) != 3 ||
138
                priv.Primes[0].Cmp(priv2.Primes[0]) != 0 ||
139
                priv.Primes[1].Cmp(priv2.Primes[1]) != 0 ||
140
                priv.Primes[2].Cmp(priv2.Primes[2]) != 0 {
141
                t.Errorf("got:%+v want:%+v", priv, priv2)
142
        }
143
}
144
 
145
type matchHostnamesTest struct {
146
        pattern, host string
147
        ok            bool
148
}
149
 
150
var matchHostnamesTests = []matchHostnamesTest{
151
        {"a.b.c", "a.b.c", true},
152
        {"a.b.c", "b.b.c", false},
153
        {"", "b.b.c", false},
154
        {"a.b.c", "", false},
155
        {"example.com", "example.com", true},
156
        {"example.com", "www.example.com", false},
157
        {"*.example.com", "www.example.com", true},
158
        {"*.example.com", "xyz.www.example.com", false},
159
        {"*.*.example.com", "xyz.www.example.com", true},
160
        {"*.www.*.com", "xyz.www.example.com", true},
161
}
162
 
163
func TestMatchHostnames(t *testing.T) {
164
        for i, test := range matchHostnamesTests {
165
                r := matchHostnames(test.pattern, test.host)
166
                if r != test.ok {
167
                        t.Errorf("#%d mismatch got: %t want: %t", i, r, test.ok)
168
                }
169
        }
170
}
171
 
172
func TestCertificateParse(t *testing.T) {
173
        s, _ := hex.DecodeString(certBytes)
174
        certs, err := ParseCertificates(s)
175
        if err != nil {
176
                t.Error(err)
177
        }
178
        if len(certs) != 2 {
179
                t.Errorf("Wrong number of certs: got %d want 2", len(certs))
180
                return
181
        }
182
 
183
        err = certs[0].CheckSignatureFrom(certs[1])
184
        if err != nil {
185
                t.Error(err)
186
        }
187
 
188
        if err := certs[0].VerifyHostname("mail.google.com"); err != nil {
189
                t.Error(err)
190
        }
191
}
192
 
193
var certBytes = "308203223082028ba00302010202106edf0d9499fd4533dd1297fc42a93be1300d06092a864886" +
194
        "f70d0101050500304c310b3009060355040613025a4131253023060355040a131c546861777465" +
195
        "20436f6e73756c74696e67202850747929204c74642e311630140603550403130d546861777465" +
196
        "20534743204341301e170d3039303332353136343932395a170d3130303332353136343932395a" +
197
        "3069310b3009060355040613025553311330110603550408130a43616c69666f726e6961311630" +
198
        "140603550407130d4d6f756e7461696e205669657731133011060355040a130a476f6f676c6520" +
199
        "496e63311830160603550403130f6d61696c2e676f6f676c652e636f6d30819f300d06092a8648" +
200
        "86f70d010101050003818d0030818902818100c5d6f892fccaf5614b064149e80a2c9581a218ef" +
201
        "41ec35bd7a58125ae76f9ea54ddc893abbeb029f6b73616bf0ffd868791fba7af9c4aebf3706ba" +
202
        "3eeaeed27435b4ddcfb157c05f351d66aa87fee0de072d66d773affbd36ab78bef090e0cc861a9" +
203
        "03ac90dd98b51c9c41566c017f0beec3bff391051ffba0f5cc6850ad2a590203010001a381e730" +
204
        "81e430280603551d250421301f06082b0601050507030106082b06010505070302060960864801" +
205
        "86f842040130360603551d1f042f302d302ba029a0278625687474703a2f2f63726c2e74686177" +
206
        "74652e636f6d2f54686177746553474343412e63726c307206082b060105050701010466306430" +
207
        "2206082b060105050730018616687474703a2f2f6f6373702e7468617774652e636f6d303e0608" +
208
        "2b060105050730028632687474703a2f2f7777772e7468617774652e636f6d2f7265706f736974" +
209
        "6f72792f5468617774655f5347435f43412e637274300c0603551d130101ff04023000300d0609" +
210
        "2a864886f70d01010505000381810062f1f3050ebc105e497c7aedf87e24d2f4a986bb3b837bd1" +
211
        "9b91ebcad98b065992f6bd2b49b7d6d3cb2e427a99d606c7b1d46352527fac39e6a8b6726de5bf" +
212
        "70212a52cba07634a5e332011bd1868e78eb5e3c93cf03072276786f207494feaa0ed9d53b2110" +
213
        "a76571f90209cdae884385c882587030ee15f33d761e2e45a6bc308203233082028ca003020102" +
214
        "020430000002300d06092a864886f70d0101050500305f310b3009060355040613025553311730" +
215
        "15060355040a130e566572695369676e2c20496e632e31373035060355040b132e436c61737320" +
216
        "33205075626c6963205072696d6172792043657274696669636174696f6e20417574686f726974" +
217
        "79301e170d3034303531333030303030305a170d3134303531323233353935395a304c310b3009" +
218
        "060355040613025a4131253023060355040a131c54686177746520436f6e73756c74696e672028" +
219
        "50747929204c74642e311630140603550403130d5468617774652053474320434130819f300d06" +
220
        "092a864886f70d010101050003818d0030818902818100d4d367d08d157faecd31fe7d1d91a13f" +
221
        "0b713cacccc864fb63fc324b0794bd6f80ba2fe10493c033fc093323e90b742b71c403c6d2cde2" +
222
        "2ff50963cdff48a500bfe0e7f388b72d32de9836e60aad007bc4644a3b847503f270927d0e62f5" +
223
        "21ab693684317590f8bfc76c881b06957cc9e5a8de75a12c7a68dfd5ca1c875860190203010001" +
224
        "a381fe3081fb30120603551d130101ff040830060101ff020100300b0603551d0f040403020106" +
225
        "301106096086480186f842010104040302010630280603551d110421301fa41d301b3119301706" +
226
        "035504031310507269766174654c6162656c332d313530310603551d1f042a30283026a024a022" +
227
        "8620687474703a2f2f63726c2e766572697369676e2e636f6d2f706361332e63726c303206082b" +
228
        "0601050507010104263024302206082b060105050730018616687474703a2f2f6f6373702e7468" +
229
        "617774652e636f6d30340603551d25042d302b06082b0601050507030106082b06010505070302" +
230
        "06096086480186f8420401060a6086480186f845010801300d06092a864886f70d010105050003" +
231
        "81810055ac63eadea1ddd2905f9f0bce76be13518f93d9052bc81b774bad6950a1eededcfddb07" +
232
        "e9e83994dcab72792f06bfab8170c4a8edea5334edef1e53d906c7562bd15cf4d18a8eb42bb137" +
233
        "9048084225c53e8acb7feb6f04d16dc574a2f7a27c7b603c77cd0ece48027f012fb69b37e02a2a" +
234
        "36dcd585d6ace53f546f961e05af"
235
 
236
func TestCreateSelfSignedCertificate(t *testing.T) {
237
        random := rand.Reader
238
 
239
        block, _ := pem.Decode([]byte(pemPrivateKey))
240
        priv, err := ParsePKCS1PrivateKey(block.Bytes)
241
        if err != nil {
242
                t.Errorf("Failed to parse private key: %s", err)
243
                return
244
        }
245
 
246
        commonName := "test.example.com"
247
        template := Certificate{
248
                SerialNumber: big.NewInt(1),
249
                Subject: pkix.Name{
250
                        CommonName:   commonName,
251
                        Organization: []string{"Acme Co"},
252
                },
253
                NotBefore: time.Unix(1000, 0),
254
                NotAfter:  time.Unix(100000, 0),
255
 
256
                SubjectKeyId: []byte{1, 2, 3, 4},
257
                KeyUsage:     KeyUsageCertSign,
258
 
259
                BasicConstraintsValid: true,
260
                IsCA:                  true,
261
                DNSNames:              []string{"test.example.com"},
262
 
263
                PolicyIdentifiers:   []asn1.ObjectIdentifier{[]int{1, 2, 3}},
264
                PermittedDNSDomains: []string{".example.com", "example.com"},
265
        }
266
 
267
        derBytes, err := CreateCertificate(random, &template, &template, &priv.PublicKey, priv)
268
        if err != nil {
269
                t.Errorf("Failed to create certificate: %s", err)
270
                return
271
        }
272
 
273
        cert, err := ParseCertificate(derBytes)
274
        if err != nil {
275
                t.Errorf("Failed to parse certificate: %s", err)
276
                return
277
        }
278
 
279
        if len(cert.PolicyIdentifiers) != 1 || !cert.PolicyIdentifiers[0].Equal(template.PolicyIdentifiers[0]) {
280
                t.Errorf("Failed to parse policy identifiers: got:%#v want:%#v", cert.PolicyIdentifiers, template.PolicyIdentifiers)
281
        }
282
 
283
        if len(cert.PermittedDNSDomains) != 2 || cert.PermittedDNSDomains[0] != ".example.com" || cert.PermittedDNSDomains[1] != "example.com" {
284
                t.Errorf("Failed to parse name constraints: %#v", cert.PermittedDNSDomains)
285
        }
286
 
287
        if cert.Subject.CommonName != commonName {
288
                t.Errorf("Subject wasn't correctly copied from the template. Got %s, want %s", cert.Subject.CommonName, commonName)
289
        }
290
 
291
        if cert.Issuer.CommonName != commonName {
292
                t.Errorf("Issuer wasn't correctly copied from the template. Got %s, want %s", cert.Issuer.CommonName, commonName)
293
        }
294
 
295
        err = cert.CheckSignatureFrom(cert)
296
        if err != nil {
297
                t.Errorf("Signature verification failed: %s", err)
298
                return
299
        }
300
}
301
 
302
// Self-signed certificate using DSA with SHA1
303
var dsaCertPem = `-----BEGIN CERTIFICATE-----
304
MIIEDTCCA82gAwIBAgIJALHPghaoxeDhMAkGByqGSM44BAMweTELMAkGA1UEBhMC
305
VVMxCzAJBgNVBAgTAk5DMQ8wDQYDVQQHEwZOZXd0b24xFDASBgNVBAoTC0dvb2ds
306
ZSwgSW5jMRIwEAYDVQQDEwlKb24gQWxsaWUxIjAgBgkqhkiG9w0BCQEWE2pvbmFs
307
bGllQGdvb2dsZS5jb20wHhcNMTEwNTE0MDMwMTQ1WhcNMTEwNjEzMDMwMTQ1WjB5
308
MQswCQYDVQQGEwJVUzELMAkGA1UECBMCTkMxDzANBgNVBAcTBk5ld3RvbjEUMBIG
309
A1UEChMLR29vZ2xlLCBJbmMxEjAQBgNVBAMTCUpvbiBBbGxpZTEiMCAGCSqGSIb3
310
DQEJARYTam9uYWxsaWVAZ29vZ2xlLmNvbTCCAbcwggEsBgcqhkjOOAQBMIIBHwKB
311
gQC8hLUnQ7FpFYu4WXTj6DKvXvz8QrJkNJCVMTpKAT7uBpobk32S5RrPKXocd4gN
312
8lyGB9ggS03EVlEwXvSmO0DH2MQtke2jl9j1HLydClMf4sbx5V6TV9IFw505U1iW
313
jL7awRMgxge+FsudtJK254FjMFo03ZnOQ8ZJJ9E6AEDrlwIVAJpnBn9moyP11Ox5
314
Asc/5dnjb6dPAoGBAJFHd4KVv1iTVCvEG6gGiYop5DJh28hUQcN9kul+2A0yPUSC
315
X93oN00P8Vh3eYgSaCWZsha7zDG53MrVJ0Zf6v/X/CoZNhLldeNOepivTRAzn+Rz
316
kKUYy5l1sxYLHQKF0UGNCXfFKZT0PCmgU+PWhYNBBMn6/cIh44vp85ideo5CA4GE
317
AAKBgFmifCafzeRaohYKXJgMGSEaggCVCRq5xdyDCat+wbOkjC4mfG01/um3G8u5
318
LxasjlWRKTR/tcAL7t0QuokVyQaYdVypZXNaMtx1db7YBuHjj3aP+8JOQRI9xz8c
319
bp5NDJ5pISiFOv4p3GZfqZPcqckDt78AtkQrmnal2txhhjF6o4HeMIHbMB0GA1Ud
320
DgQWBBQVyyr7hO11ZFFpWX50298Sa3V+rzCBqwYDVR0jBIGjMIGggBQVyyr7hO11
321
ZFFpWX50298Sa3V+r6F9pHsweTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAk5DMQ8w
322
DQYDVQQHEwZOZXd0b24xFDASBgNVBAoTC0dvb2dsZSwgSW5jMRIwEAYDVQQDEwlK
323
b24gQWxsaWUxIjAgBgkqhkiG9w0BCQEWE2pvbmFsbGllQGdvb2dsZS5jb22CCQCx
324
z4IWqMXg4TAMBgNVHRMEBTADAQH/MAkGByqGSM44BAMDLwAwLAIUPtn/5j8Q1jJI
325
7ggOIsgrhgUdjGQCFCsmDq1H11q9+9Wp9IMeGrTSKHIM
326
-----END CERTIFICATE-----
327
`
328
 
329
func TestParseCertificateWithDsaPublicKey(t *testing.T) {
330
        expectedKey := &dsa.PublicKey{
331
                Parameters: dsa.Parameters{
332
                        P: bigFromHexString("00BC84B52743B169158BB85974E3E832AF5EFCFC42B264349095313A4A013EEE069A1B937D92E51ACF297A1C77880DF25C8607D8204B4DC45651305EF4A63B40C7D8C42D91EDA397D8F51CBC9D0A531FE2C6F1E55E9357D205C39D395358968CBEDAC11320C607BE16CB9DB492B6E78163305A34DD99CE43C64927D13A0040EB97"),
333
                        Q: bigFromHexString("009A67067F66A323F5D4EC7902C73FE5D9E36FA74F"),
334
                        G: bigFromHexString("009147778295BF5893542BC41BA806898A29E43261DBC85441C37D92E97ED80D323D44825FDDE8374D0FF15877798812682599B216BBCC31B9DCCAD527465FEAFFD7FC2A193612E575E34E7A98AF4D10339FE47390A518CB9975B3160B1D0285D1418D0977C52994F43C29A053E3D685834104C9FAFDC221E38BE9F3989D7A8E42"),
335
                },
336
                Y: bigFromHexString("59A27C269FCDE45AA2160A5C980C19211A820095091AB9C5DC8309AB7EC1B3A48C2E267C6D35FEE9B71BCBB92F16AC8E559129347FB5C00BEEDD10BA8915C90698755CA965735A32DC7575BED806E1E38F768FFBC24E41123DC73F1C6E9E4D0C9E692128853AFE29DC665FA993DCA9C903B7BF00B6442B9A76A5DADC6186317A"),
337
        }
338
        pemBlock, _ := pem.Decode([]byte(dsaCertPem))
339
        cert, err := ParseCertificate(pemBlock.Bytes)
340
        if err != nil {
341
                t.Fatalf("Failed to parse certificate: %s", err)
342
        }
343
        if cert.PublicKeyAlgorithm != DSA {
344
                t.Errorf("Parsed key algorithm was not DSA")
345
        }
346
        parsedKey, ok := cert.PublicKey.(*dsa.PublicKey)
347
        if !ok {
348
                t.Fatalf("Parsed key was not a DSA key: %s", err)
349
        }
350
        if expectedKey.Y.Cmp(parsedKey.Y) != 0 ||
351
                expectedKey.P.Cmp(parsedKey.P) != 0 ||
352
                expectedKey.Q.Cmp(parsedKey.Q) != 0 ||
353
                expectedKey.G.Cmp(parsedKey.G) != 0 {
354
                t.Fatal("Parsed key differs from expected key")
355
        }
356
}
357
 
358
func TestParseCertificateWithDSASignatureAlgorithm(t *testing.T) {
359
        pemBlock, _ := pem.Decode([]byte(dsaCertPem))
360
        cert, err := ParseCertificate(pemBlock.Bytes)
361
        if err != nil {
362
                t.Fatalf("Failed to parse certificate: %s", err)
363
        }
364
        if cert.SignatureAlgorithm != DSAWithSHA1 {
365
                t.Errorf("Parsed signature algorithm was not DSAWithSHA1")
366
        }
367
}
368
 
369
func TestVerifyCertificateWithDSASignature(t *testing.T) {
370
        pemBlock, _ := pem.Decode([]byte(dsaCertPem))
371
        cert, err := ParseCertificate(pemBlock.Bytes)
372
        if err != nil {
373
                t.Fatalf("Failed to parse certificate: %s", err)
374
        }
375
        // test cert is self-signed
376
        if err = cert.CheckSignatureFrom(cert); err != nil {
377
                t.Fatalf("DSA Certificate verfication failed: %s", err)
378
        }
379
}
380
 
381
const pemCertificate = `-----BEGIN CERTIFICATE-----
382
MIIB5DCCAZCgAwIBAgIBATALBgkqhkiG9w0BAQUwLTEQMA4GA1UEChMHQWNtZSBDbzEZMBcGA1UE
383
AxMQdGVzdC5leGFtcGxlLmNvbTAeFw03MDAxMDEwMDE2NDBaFw03MDAxMDIwMzQ2NDBaMC0xEDAO
384
BgNVBAoTB0FjbWUgQ28xGTAXBgNVBAMTEHRlc3QuZXhhbXBsZS5jb20wWjALBgkqhkiG9w0BAQED
385
SwAwSAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0fd7Ai2KW5ToIwzFo
386
fvJcS/STa6HA5gQenRUCAwEAAaOBnjCBmzAOBgNVHQ8BAf8EBAMCAAQwDwYDVR0TAQH/BAUwAwEB
387
/zANBgNVHQ4EBgQEAQIDBDAPBgNVHSMECDAGgAQBAgMEMBsGA1UdEQQUMBKCEHRlc3QuZXhhbXBs
388
ZS5jb20wDwYDVR0gBAgwBjAEBgIqAzAqBgNVHR4EIzAhoB8wDoIMLmV4YW1wbGUuY29tMA2CC2V4
389
YW1wbGUuY29tMAsGCSqGSIb3DQEBBQNBAHKZKoS1wEQOGhgklx4+/yFYQlnqwKXvar/ZecQvJwui
390
0seMQnwBhwdBkHfVIU2Fu5VUMRyxlf0ZNaDXcpU581k=
391
-----END CERTIFICATE-----`
392
 
393
func TestCRLCreation(t *testing.T) {
394
        block, _ := pem.Decode([]byte(pemPrivateKey))
395
        priv, _ := ParsePKCS1PrivateKey(block.Bytes)
396
        block, _ = pem.Decode([]byte(pemCertificate))
397
        cert, _ := ParseCertificate(block.Bytes)
398
 
399
        now := time.Unix(1000, 0)
400
        expiry := time.Unix(10000, 0)
401
 
402
        revokedCerts := []pkix.RevokedCertificate{
403
                {
404
                        SerialNumber:   big.NewInt(1),
405
                        RevocationTime: now,
406
                },
407
                {
408
                        SerialNumber:   big.NewInt(42),
409
                        RevocationTime: now,
410
                },
411
        }
412
 
413
        crlBytes, err := cert.CreateCRL(rand.Reader, priv, revokedCerts, now, expiry)
414
        if err != nil {
415
                t.Errorf("error creating CRL: %s", err)
416
        }
417
 
418
        _, err = ParseDERCRL(crlBytes)
419
        if err != nil {
420
                t.Errorf("error reparsing CRL: %s", err)
421
        }
422
}
423
 
424
func fromBase64(in string) []byte {
425
        out := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
426
        _, err := base64.StdEncoding.Decode(out, []byte(in))
427
        if err != nil {
428
                panic("failed to base64 decode")
429
        }
430
        return out
431
}
432
 
433
func TestParseDERCRL(t *testing.T) {
434
        derBytes := fromBase64(derCRLBase64)
435
        certList, err := ParseDERCRL(derBytes)
436
        if err != nil {
437
                t.Errorf("error parsing: %s", err)
438
                return
439
        }
440
        numCerts := len(certList.TBSCertList.RevokedCertificates)
441
        expected := 88
442
        if numCerts != expected {
443
                t.Errorf("bad number of revoked certificates. got: %d want: %d", numCerts, expected)
444
        }
445
 
446
        if certList.HasExpired(time.Unix(1302517272, 0)) {
447
                t.Errorf("CRL has expired (but shouldn't have)")
448
        }
449
 
450
        // Can't check the signature here without a package cycle.
451
}
452
 
453
func TestParsePEMCRL(t *testing.T) {
454
        pemBytes := fromBase64(pemCRLBase64)
455
        certList, err := ParseCRL(pemBytes)
456
        if err != nil {
457
                t.Errorf("error parsing: %s", err)
458
                return
459
        }
460
        numCerts := len(certList.TBSCertList.RevokedCertificates)
461
        expected := 2
462
        if numCerts != expected {
463
                t.Errorf("bad number of revoked certificates. got: %d want: %d", numCerts, expected)
464
        }
465
 
466
        if certList.HasExpired(time.Unix(1302517272, 0)) {
467
                t.Errorf("CRL has expired (but shouldn't have)")
468
        }
469
 
470
        // Can't check the signature here without a package cycle.
471
}
472
 
473
const derCRLBase64 = "MIINqzCCDJMCAQEwDQYJKoZIhvcNAQEFBQAwVjEZMBcGA1UEAxMQUEtJIEZJTk1FQ0NBTklDQTEVMBMGA1UEChMMRklOTUVDQ0FOSUNBMRUwEwYDVQQLEwxGSU5NRUNDQU5JQ0ExCzAJBgNVBAYTAklUFw0xMTA1MDQxNjU3NDJaFw0xMTA1MDQyMDU3NDJaMIIMBzAhAg4Ze1od49Lt1qIXBydAzhcNMDkwNzE2MDg0MzIyWjAAMCECDl0HSL9bcZ1Ci/UHJ0DPFw0wOTA3MTYwODQzMTNaMAAwIQIOESB9tVAmX3cY7QcnQNAXDTA5MDcxNjA4NDUyMlowADAhAg4S1tGAQ3mHt8uVBydA1RcNMDkwODA0MTUyNTIyWjAAMCECDlQ249Y7vtC25ScHJ0DWFw0wOTA4MDQxNTI1MzdaMAAwIQIOISMop3NkA4PfYwcnQNkXDTA5MDgwNDExMDAzNFowADAhAg56/BMoS29KEShTBydA2hcNMDkwODA0MTEwMTAzWjAAMCECDnBp/22HPH5CSWoHJ0DbFw0wOTA4MDQxMDU0NDlaMAAwIQIOV9IP+8CD8bK+XAcnQNwXDTA5MDgwNDEwNTcxN1owADAhAg4v5aRz0IxWqYiXBydA3RcNMDkwODA0MTA1NzQ1WjAAMCECDlOU34VzvZAybQwHJ0DeFw0wOTA4MDQxMDU4MjFaMAAwIAINO4CD9lluIxcwBydBAxcNMDkwNzIyMTUzMTU5WjAAMCECDgOllfO8Y1QA7/wHJ0ExFw0wOTA3MjQxMTQxNDNaMAAwIQIOJBX7jbiCdRdyjgcnQUQXDTA5MDkxNjA5MzAwOFowADAhAg5iYSAgmDrlH/RZBydBRRcNMDkwOTE2MDkzMDE3WjAAMCECDmu6k6srP3jcMaQHJ0FRFw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VhEVEF5TVRBeU16RTBOVEF4TkZvd0dqQVlCZ05WDQpIUmdFRVJnUE1qQXdNakV3TWpNeE5EVXdNVFJhb0RBd0xqQWZCZ05WSFNNRUdEQVdnQlQxVERGNlVRTS9MTmVMDQpsNWx2cUhHUXEzZzltekFMQmdOVkhSUUVCQUlDQUlRd0RRWUpLb1pJaHZjTkFRRUZCUUFEZ1lFQUZVNUFzNk16DQpxNVBSc2lmYW9iUVBHaDFhSkx5QytNczVBZ2MwYld5QTNHQWR4dXI1U3BQWmVSV0NCamlQL01FSEJXSkNsQkhQDQpHUmNxNXlJZDNFakRrYUV5eFJhK2k2N0x6dmhJNmMyOUVlNks5cFNZd2ppLzdSVWhtbW5Qclh0VHhsTDBsckxyDQptUVFKNnhoRFJhNUczUUE0Q21VZHNITnZicnpnbUNZcHZWRT0NCi0tLS0tRU5EIFg1MDkgQ1JMLS0tLS0NCg0K"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.