blob: 80940dcd02c8979b920af57560e3ab6a5d95e5f4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.ca;
import com.yahoo.security.KeyAlgorithm;
import com.yahoo.security.KeyUtils;
import com.yahoo.security.SubjectAlternativeName;
import com.yahoo.test.ManualClock;
import org.junit.Test;
import java.security.KeyPair;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.List;
import static java.time.temporal.ChronoUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author mpolden
*/
public class CertificatesTest {
private final KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC, 256);
private final X509Certificate caCertificate = CertificateTester.createCertificate("CA", keyPair);
@Test
public void expiry() {
var clock = new ManualClock();
var certificates = new Certificates(clock);
var csr = CertificateTester.createCsr();
var certificate = certificates.create(csr, caCertificate, keyPair.getPrivate());
var now = clock.instant();
assertEquals(now.minus(Duration.ofHours(1)).truncatedTo(SECONDS), certificate.getNotBefore().toInstant());
assertEquals(now.plus(Duration.ofDays(30)).truncatedTo(SECONDS), certificate.getNotAfter().toInstant());
}
@Test
public void add_san_from_csr() throws Exception {
var certificates = new Certificates(new ManualClock());
var dnsName = "host.example.com";
var csr = CertificateTester.createCsr(dnsName);
var certificate = certificates.create(csr, caCertificate, keyPair.getPrivate());
assertNotNull(certificate.getSubjectAlternativeNames());
assertEquals(1, certificate.getSubjectAlternativeNames().size());
assertEquals(List.of(SubjectAlternativeName.Type.DNS_NAME.getTag(), dnsName),
certificate.getSubjectAlternativeNames().iterator().next());
}
}
|