Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for subjectAlternativeName from CSR #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder
import org.bouncycastle.pkcs.PKCS10CertificationRequest
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers
import org.bouncycastle.asn1.x509.Extensions
import org.bouncycastle.asn1.x509.GeneralNames
import org.nordix.simplepki.domain.model.PkiEntity
import org.nordix.simplepki.domain.model.PkiOperations
import org.nordix.simplepki.domain.model.RevocationEntry
Expand Down Expand Up @@ -64,6 +67,13 @@ internal class DefaultPkiOperations(private val clock: Clock) : PkiOperations {
.addExtension(Extension.basicConstraints, true, CertificateSettings.NON_CA_BASIC_CONSTRAINTS)
.addExtension(Extension.keyUsage, true, CertificateSettings.NON_CA_KEY_USAGES)
.addExtension(Extension.extendedKeyUsage, true, CertificateSettings.EXTENDED_KEY_USAGES)

// Add the Subject Alternative Name (SAN) extension if present in the CSR
val sanExtension = getSubjectAlternativeNames(csr)
if (sanExtension != null) {
certificateBuilder.addExtension(Extension.subjectAlternativeName, false, sanExtension)
}

val signer = JcaContentSignerBuilder(CertificateSettings.SIGNATURE_ALGORITHM)
.build(ca.privateKey)
return JcaX509CertificateConverter()
Expand All @@ -84,4 +94,18 @@ internal class DefaultPkiOperations(private val clock: Clock) : PkiOperations {
return JcaX509CRLConverter()
.getCRL(crlHolder)
}

private fun getSubjectAlternativeNames(csr: PKCS10CertificationRequest): GeneralNames? {
val attributes = csr.attributes
for (attribute in attributes) {
if (attribute.attrType == PKCSObjectIdentifiers.pkcs_9_at_extensionRequest) {
val extensions = Extensions.getInstance(attribute.attrValues.getObjectAt(0))
val sanExtension = extensions.getExtension(Extension.subjectAlternativeName)
if (sanExtension != null) {
return GeneralNames.getInstance(sanExtension.parsedValue)
}
}
}
return null
}
}