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

Fix cname encoding in CertificateGenerator #20745

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog/unreleased/issue-20738.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type = "fixed"
message = "Fix encoding of CName in generated CA certificates"

issues = ["20738"]
pulls = ["20745"]
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.graylog.security.certutil;

import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.GeneralName;
Expand All @@ -41,7 +43,8 @@ public class CertificateGenerator {
public static KeyPair generate(CertRequest request) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_GENERATION_ALGORITHM);
java.security.KeyPair certKeyPair = keyGen.generateKeyPair();
X500Name name = new X500Name("CN=" + request.cnName());

final X500Name name = getX500Name(request.cnName());

// TODO: cert serial number?
BigInteger serialNumber = BigInteger.valueOf(System.currentTimeMillis());
Expand Down Expand Up @@ -84,4 +87,10 @@ public static KeyPair generate(CertRequest request) throws Exception {
X509Certificate cert = new JcaX509CertificateConverter().getCertificate(certHolder);
return new KeyPair(certKeyPair.getPrivate(), certKeyPair.getPublic(), cert);
}

private static X500Name getX500Name(String cname) {
X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);
builder.addRDN(BCStyle.CN, cname);
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.security.certutil;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.security.cert.X509Certificate;
import java.time.Duration;

class CertificateGeneratorTest {

@Test
void testDomainName() throws Exception {
final KeyPair pair = selfSigned("www.graylog.org");
final X509Certificate certificate = pair.certificate();
final String cn = certificate.getSubjectX500Principal().getName();
Assertions.assertThat(cn).isEqualTo("CN=www.graylog.org");
}

@Test
void testEscaping() throws Exception {
final KeyPair pair = selfSigned("Graylog, Inc.");
final X509Certificate certificate = pair.certificate();
final String cn = certificate.getSubjectX500Principal().getName();
Assertions.assertThat(cn).isEqualTo("CN=Graylog\\, Inc.");
}

private static KeyPair selfSigned(String cname) throws Exception {
final CertRequest req = CertRequest.selfSigned(cname).validity(Duration.ofDays(1));
return CertificateGenerator.generate(req);
}
}
Loading