Skip to content

Commit

Permalink
feat(Certificate): Expose fields for start and end dates
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Aug 7, 2020
1 parent cb63f1b commit def9bee
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
8 changes: 0 additions & 8 deletions src/main/kotlin/tech/relaycorp/relaynet/DateTime.kt

This file was deleted.

4 changes: 1 addition & 3 deletions src/main/kotlin/tech/relaycorp/relaynet/ramf/RAMFMessage.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.relaycorp.relaynet.ramf

import tech.relaycorp.relaynet.HashingAlgorithm
import tech.relaycorp.relaynet.dateToZonedDateTime
import tech.relaycorp.relaynet.messages.InvalidMessageException
import tech.relaycorp.relaynet.messages.payloads.Payload
import tech.relaycorp.relaynet.wrappers.x509.Certificate
Expand Down Expand Up @@ -158,8 +157,7 @@ abstract class RAMFMessage<P : Payload> internal constructor(
if (now < creationDate) {
throw RAMFException("Creation date is in the future")
}
if (creationDate < dateToZonedDateTime(senderCertificate.certificateHolder.notBefore)
) {
if (creationDate < senderCertificate.startDate) {
throw RAMFException("Message was created before sender certificate was valid")
}
if (expiryDate < now) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.bouncycastle.cert.X509v3CertificateBuilder
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder
import tech.relaycorp.relaynet.BC_PROVIDER
import tech.relaycorp.relaynet.dateToZonedDateTime
import tech.relaycorp.relaynet.getSHA256Digest
import tech.relaycorp.relaynet.getSHA256DigestHex
import tech.relaycorp.relaynet.wrappers.generateRandomBigInteger
Expand All @@ -32,6 +31,7 @@ import java.security.cert.PKIXParameters
import java.security.cert.TrustAnchor
import java.security.cert.X509CertSelector
import java.sql.Date
import java.time.ZoneId
import java.time.ZonedDateTime

/**
Expand Down Expand Up @@ -147,6 +147,18 @@ class Certificate constructor(val certificateHolder: X509CertificateHolder) {
val subjectPrivateAddress
get() = "0" + getSHA256DigestHex(certificateHolder.subjectPublicKeyInfo.encoded)

/**
* The start date of the certificate.
*/
val startDate: ZonedDateTime
get() = dateToZonedDateTime(certificateHolder.notBefore)

/**
* The expiry date of the certificate.
*/
val expiryDate: ZonedDateTime
get() = dateToZonedDateTime(certificateHolder.notAfter)

private val basicConstraints: BasicConstraints? by lazy {
BasicConstraints.fromExtensions(certificateHolder.extensions)
}
Expand Down Expand Up @@ -193,10 +205,10 @@ class Certificate constructor(val certificateHolder: X509CertificateHolder) {

private fun validateValidityPeriod() {
val now = ZonedDateTime.now()
if (now < dateToZonedDateTime(certificateHolder.notBefore)) {
if (now < startDate) {
throw CertificateException("Certificate is not yet valid")
}
if (dateToZonedDateTime(certificateHolder.notAfter) < now) {
if (expiryDate < now) {
throw CertificateException("Certificate already expired")
}
}
Expand Down Expand Up @@ -289,4 +301,8 @@ class Certificate constructor(val certificateHolder: X509CertificateHolder) {

private fun convertCertToJava(certificate: Certificate) =
bcToJavaCertificateConverter.getCertificate(certificate.certificateHolder)

private fun dateToZonedDateTime(date: java.util.Date) = date.toInstant().atZone(
ZoneId.systemDefault()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,32 @@ class CertificateTest {
val expectedAddress = "0${sha256Hex(stubSubjectKeyPair.public.encoded)}"
assertEquals(expectedAddress, certificate.subjectPrivateAddress)
}

@Test
fun startDate() {
val startDate = ZonedDateTime.now()
val certificate = Certificate.issue(
stubSubjectCommonName,
stubSubjectKeyPair.public,
stubSubjectKeyPair.private,
stubValidityEndDate,
validityStartDate = startDate
)

assertEquals(startDate.withNano(0), certificate.startDate)
}

@Test
fun expiryDate() {
val certificate = Certificate.issue(
stubSubjectCommonName,
stubSubjectKeyPair.public,
stubSubjectKeyPair.private,
stubValidityEndDate
)

assertEquals(stubValidityEndDate.withNano(0), certificate.expiryDate)
}
}

@Nested
Expand Down

0 comments on commit def9bee

Please sign in to comment.