dakirily commented on code in PR #465:
URL: https://github.com/apache/qpid-broker-j/pull/465#discussion_r4754433291


##########
broker-core/src/main/java/org/apache/qpid/server/security/certificate/GeneratedCertificate.java:
##########
@@ -0,0 +1,54 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.security.certificate;
+
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+import java.util.Objects;
+
+/**
+ * Private key and self-signed certificate produced by a {@link 
CertificateGenerator}.
+ *
+ * @param privateKey private key matching the public key of the certificate
+ * @param certificate self-signed certificate
+ */
+public record GeneratedCertificate(PrivateKey privateKey, X509Certificate 
certificate)

Review Comment:
   `GeneratedCertificate` accepts any non-null `PrivateKey`. Java permits a 
signing key to return null from `getEncoded()`, but the existing auto-generated 
keystore saves those bytes and reloads them as PKCS#8.
   A local generator returning signing-capable keys without an encoding passed 
the reusable contract tests in the integration experiment.
   
   Please require a key with a usable PKCS#8 encoding that the broker can load 
again for the requested algorithm. Add a test that saves the bytes, reloads the 
key through `KeyFactory`, signs with the reloaded key and verifies with the 
certificate's public key. The consumer-side validation belongs in #464. 
Supporting keys that cannot be exported would need a different storage approach.



##########
broker-core/src/main/java/org/apache/qpid/server/security/certificate/CertificateRequest.java:
##########
@@ -0,0 +1,291 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.security.certificate;
+
+import java.net.InetAddress;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import javax.security.auth.x500.X500Principal;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Immutable, and therefore thread-safe, parameters of a self-signed 
certificate, created with {@link #builder()}. The
+ * certificate a generator makes from them is described by {@link 
CertificateGenerator#generate(CertificateRequest)}.
+ */
+public class CertificateRequest
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(CertificateRequest.class);
+
+    // a name is kept only if it starts with an ASCII letter or an underscore 
and continues with ASCII letters,
+    // digits, underscores, dots and hyphens, which leaves out textual IP 
addresses and names containing spaces
+    private static final Pattern DNS_NAME_PATTERN = 
Pattern.compile("[\\w&&[^\\d]][\\w\\d.-]*");

Review Comment:
   The regex already existed in `SSLUtil`. It drops the valid name 
`1broker.example`, but keeps `_broker.example`, `broker..example`, labels with 
a leading or trailing hyphen, and labels longer than 63 characters. Moving it 
into `CertificateRequest` makes that behavior apply to every generator.
   
   Please check DNS names one label at a time without doing a DNS lookup. Keep 
IP addresses in `addresses`, and document the handling of trailing dots, 
internationalized names and wildcards. It is reasonable to filter bad names 
discovered from the network. For names explicitly supplied by a caller, please 
reject them with a useful error message.
   
   Add cases for numeric-leading labels, empty labels, long labels, hyphens, 
underscores and IP literals. The request's filtering policy should match its 
promise to supply valid hostnames.



##########
broker-core/src/main/java/org/apache/qpid/server/security/certificate/CertificateRequest.java:
##########
@@ -0,0 +1,291 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.security.certificate;
+
+import java.net.InetAddress;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import javax.security.auth.x500.X500Principal;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Immutable, and therefore thread-safe, parameters of a self-signed 
certificate, created with {@link #builder()}. The
+ * certificate a generator makes from them is described by {@link 
CertificateGenerator#generate(CertificateRequest)}.
+ */
+public class CertificateRequest
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(CertificateRequest.class);
+
+    // a name is kept only if it starts with an ASCII letter or an underscore 
and continues with ASCII letters,
+    // digits, underscores, dots and hyphens, which leaves out textual IP 
addresses and names containing spaces
+    private static final Pattern DNS_NAME_PATTERN = 
Pattern.compile("[\\w&&[^\\d]][\\w\\d.-]*");
+
+    private final String _keyAlgorithm;
+    private final int _keyLength;
+    private final String _signatureAlgorithm;
+    private final X500Principal _subject;
+    private final Instant _notBefore;
+    private final Instant _notAfter;
+    private final Set<String> _dnsNames;
+    private final Set<InetAddress> _addresses;
+
+    // private, so that the class cannot be subclassed and stays immutable 
without being declared final
+    private CertificateRequest(final Builder builder)
+    {
+        _keyAlgorithm = requireNonBlank(builder._keyAlgorithm, "Key 
algorithm");
+        _signatureAlgorithm = requireNonBlank(builder._signatureAlgorithm, 
"Signature algorithm");
+        _subject = requireSet(builder._subject, "Subject");
+        // RFC 5280 does not allow an empty issuer, and the issuer of a 
self-signed certificate is its subject
+        if (_subject.getName().isEmpty())
+        {
+            throw new IllegalArgumentException("Subject must not be empty");
+        }
+        // X.509 stores validity with a precision of one second

Review Comment:
   The request accepts `Instant.MAX`. The #464 implementation then throws 
unchecked `IllegalArgumentException` when it calls `Date.from()`. The normal 
month-based keystore caller does not produce this value, so this is an API 
boundary issue.
   
   Please define the supported date range and reject invalid values in the 
builder or require generators to report unsupported dates through the checked 
error contract. Add boundary cases and keep the existing
   coverage of dates from 2050, where the certificate uses GeneralizedTime.



##########
broker-core/src/main/java/org/apache/qpid/server/security/certificate/CertificateGenerator.java:
##########
@@ -0,0 +1,66 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.security.certificate;
+
+import java.security.GeneralSecurityException;
+
+/**

Review Comment:
   Please also say that a plugin with stricter requirements may pass specific 
`Provider` instances to its cryptographic engines, while still leaving the 
JVM's provider registry unchanged.



##########
broker-core/src/main/java/org/apache/qpid/server/security/certificate/CertificateRequest.java:
##########
@@ -0,0 +1,291 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.security.certificate;
+
+import java.net.InetAddress;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import javax.security.auth.x500.X500Principal;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Immutable, and therefore thread-safe, parameters of a self-signed 
certificate, created with {@link #builder()}. The
+ * certificate a generator makes from them is described by {@link 
CertificateGenerator#generate(CertificateRequest)}.
+ */
+public class CertificateRequest
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(CertificateRequest.class);
+
+    // a name is kept only if it starts with an ASCII letter or an underscore 
and continues with ASCII letters,
+    // digits, underscores, dots and hyphens, which leaves out textual IP 
addresses and names containing spaces
+    private static final Pattern DNS_NAME_PATTERN = 
Pattern.compile("[\\w&&[^\\d]][\\w\\d.-]*");
+
+    private final String _keyAlgorithm;
+    private final int _keyLength;
+    private final String _signatureAlgorithm;
+    private final X500Principal _subject;
+    private final Instant _notBefore;
+    private final Instant _notAfter;
+    private final Set<String> _dnsNames;
+    private final Set<InetAddress> _addresses;
+
+    // private, so that the class cannot be subclassed and stays immutable 
without being declared final
+    private CertificateRequest(final Builder builder)
+    {
+        _keyAlgorithm = requireNonBlank(builder._keyAlgorithm, "Key 
algorithm");
+        _signatureAlgorithm = requireNonBlank(builder._signatureAlgorithm, 
"Signature algorithm");
+        _subject = requireSet(builder._subject, "Subject");
+        // RFC 5280 does not allow an empty issuer, and the issuer of a 
self-signed certificate is its subject
+        if (_subject.getName().isEmpty())
+        {
+            throw new IllegalArgumentException("Subject must not be empty");
+        }
+        // X.509 stores validity with a precision of one second
+        _notBefore = requireSet(builder._notBefore, "Start of the validity 
period").truncatedTo(ChronoUnit.SECONDS);
+        _notAfter = requireSet(builder._notAfter, "End of the validity 
period").truncatedTo(ChronoUnit.SECONDS);
+        _keyLength = builder._keyLength;
+        if (_keyLength <= 0)
+        {
+            throw new IllegalArgumentException("Key length must be positive: " 
+ _keyLength);
+        }
+        if (!_notBefore.isBefore(_notAfter))
+        {
+            throw new IllegalArgumentException(String.format(
+                    "Start of the validity period %s must be at least one 
whole second before its end %s",
+                    _notBefore, _notAfter));
+        }
+        _addresses = builder._addresses;
+        final Map<Boolean, Set<String>> dnsNames = builder._dnsNames.stream()
+                .collect(Collectors.partitioningBy(dnsName -> 
DNS_NAME_PATTERN.matcher(dnsName).matches(),
+                                                   
Collectors.toUnmodifiableSet()));
+        _dnsNames = dnsNames.get(true);
+        // logged at debug only, as a host name lookup returns a textual IP 
address when there is no reverse DNS
+        // entry, so leaving out names is normal
+        if (!dnsNames.get(false).isEmpty())
+        {
+            LOGGER.debug("Names left out of the certificate for '{}', as they 
do not match the DNS name pattern: {}",
+                         _subject.getName(), dnsNames.get(false));
+        }
+    }
+
+    /**
+     * @return new builder
+     */
+    public static Builder builder()
+    {
+        return new Builder();
+    }
+
+    /**
+     * @return JCA name of the key algorithm
+     */
+    public String getKeyAlgorithm()
+    {
+        return _keyAlgorithm;
+    }
+
+    /**
+     * @return key length in bits
+     */
+    public int getKeyLength()
+    {
+        return _keyLength;
+    }
+
+    /**

Review Comment:
   The request says generators must accept JCA standard signature names, while 
`generate()` allows `NoSuchAlgorithmException` for unsupported algorithms. The 
#464 generator rejects the standard name `RSASSA-PSS`. PSS needs parameters 
that this request cannot carry, and an EC key length alone does not choose a 
particular curve.
   
   Please list a small required set, using the existing RSA/SHA-2 and EC 
contract cases as a starting point. Other algorithms and provider-specific 
aliases can remain optional and fail with a checked exception.
   There is no need to add PSS or named-curve options just to extract today's 
implementation. Defining the supported set would make the documentation and 
tests agree.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to