tomaswolf commented on code in PR #944: URL: https://github.com/apache/mina-sshd/pull/944#discussion_r4701602686
########## sshd-common/src/main/java/org/apache/sshd/common/util/security/MLDSAParameterSets.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.sshd.common.util.security; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +/** + * ML-DSA parameter sets from FIPS 204, table 2. SSH names follow draft-sfluhrer-ssh-mldsa-08. + * + * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> + */ +public enum MLDSAParameterSets { + /** ML-DSA-44 parameters. */ + ML_DSA_44("ML-DSA-44", "ssh-mldsa-44", 1312, 17), + /** ML-DSA-65 parameters. */ + ML_DSA_65("ML-DSA-65", "ssh-mldsa-65", 1952, 18), + /** ML-DSA-87 parameters. */ + ML_DSA_87("ML-DSA-87", "ssh-mldsa-87", 2592, 19); + + /** Maximum raw public key size in bytes. */ + public static final int MAX_PUBLIC_KEY_SIZE = 2592; + /** SSH key type names for all parameter sets. */ + public static final List<String> SSH_NAMES = Collections.unmodifiableList(Arrays.asList( + ML_DSA_44.getSshName(), ML_DSA_65.getSshName(), ML_DSA_87.getSshName())); + + private final String jceName; + private final String sshName; + private final int publicKeySize; + private final int oid; + + MLDSAParameterSets(String jceName, String sshName, int publicKeySize, int oid) { + this.jceName = jceName; + this.sshName = sshName; + this.publicKeySize = publicKeySize; + this.oid = oid; + } + + /** + * @return the JCA algorithm name + */ + public String getJceName() { + return jceName; + } + + /** + * @return the SSH key type name + */ + public String getSshName() { + return sshName; + } + + /** + * @return the raw public key size in bytes + */ + public int getPublicKeySize() { + return publicKeySize; + } + + /** + * @return the last arc of the RFC 9881 OID 2.16.840.1.101.3.4.3.x + * @see <a href="https://www.rfc-editor.org/rfc/rfc9881#section-3">RFC 9881 section 3</a> + */ + public int getOid() { + return oid; + } + + /** + * Finds a parameter set by its SSH key type name. + * + * @param name the SSH key type name + * @return the parameter set, or {@code null} if the name is unknown + */ + public static MLDSAParameterSets fromSshName(String name) { + for (MLDSAParameterSets params : values()) { + if (params.sshName.equals(name)) { + return params; + } + } + return null; + } + + /** + * Finds a parameter set by its JCA name, ignoring case and hyphens. + * Review Comment: Why ignoring case and hyphens? ########## sshd-common/src/main/java/org/apache/sshd/common/util/security/MLDSAParameterSets.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.sshd.common.util.security; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +/** + * ML-DSA parameter sets from FIPS 204, table 2. SSH names follow draft-sfluhrer-ssh-mldsa-08. + * + * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> + */ +public enum MLDSAParameterSets { + /** ML-DSA-44 parameters. */ + ML_DSA_44("ML-DSA-44", "ssh-mldsa-44", 1312, 17), + /** ML-DSA-65 parameters. */ + ML_DSA_65("ML-DSA-65", "ssh-mldsa-65", 1952, 18), + /** ML-DSA-87 parameters. */ + ML_DSA_87("ML-DSA-87", "ssh-mldsa-87", 2592, 19); + + /** Maximum raw public key size in bytes. */ + public static final int MAX_PUBLIC_KEY_SIZE = 2592; + /** SSH key type names for all parameter sets. */ + public static final List<String> SSH_NAMES = Collections.unmodifiableList(Arrays.asList( + ML_DSA_44.getSshName(), ML_DSA_65.getSshName(), ML_DSA_87.getSshName())); + + private final String jceName; + private final String sshName; + private final int publicKeySize; + private final int oid; + + MLDSAParameterSets(String jceName, String sshName, int publicKeySize, int oid) { + this.jceName = jceName; + this.sshName = sshName; + this.publicKeySize = publicKeySize; + this.oid = oid; + } + + /** + * @return the JCA algorithm name + */ + public String getJceName() { + return jceName; + } + + /** + * @return the SSH key type name + */ + public String getSshName() { + return sshName; + } + + /** + * @return the raw public key size in bytes + */ + public int getPublicKeySize() { + return publicKeySize; + } + + /** + * @return the last arc of the RFC 9881 OID 2.16.840.1.101.3.4.3.x + * @see <a href="https://www.rfc-editor.org/rfc/rfc9881#section-3">RFC 9881 section 3</a> + */ + public int getOid() { + return oid; + } + + /** + * Finds a parameter set by its SSH key type name. + * + * @param name the SSH key type name + * @return the parameter set, or {@code null} if the name is unknown + */ + public static MLDSAParameterSets fromSshName(String name) { + for (MLDSAParameterSets params : values()) { + if (params.sshName.equals(name)) { + return params; + } + } + return null; + } + + /** + * Finds a parameter set by its JCA name, ignoring case and hyphens. + * + * @param name the JCA algorithm name + * @return the parameter set, or {@code null} if the name is unknown + */ + public static MLDSAParameterSets fromJceName(String name) { + if (name != null) { + String normalized = name.replace("-", "").toUpperCase(Locale.ROOT); Review Comment: This means that "ML-DſA-44" will match. (That "ſ" is U+017F, when converted to upper case, it becomes "S".) If for some reason this needs to be case insensitive, convert both to lower case, which should not have any collisions for the characters in "ML-DSA". But as I wrote above: why compare case-insensitively at all? ########## sshd-common/src/main/java/org/apache/sshd/common/util/buffer/keys/MLDSABufferPublicKeyParser.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.sshd.common.util.buffer.keys; + +import java.security.GeneralSecurityException; +import java.security.PublicKey; +import java.security.spec.InvalidKeySpecException; + +import org.apache.sshd.common.util.buffer.Buffer; +import org.apache.sshd.common.util.security.MLDSAParameterSets; +import org.apache.sshd.common.util.security.SecurityUtils; +import org.apache.sshd.common.util.security.mldsa.MLDSAUtils; + +/** + * Parses the SSH public-key encoding for ML-DSA parameter sets. + * + * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> + */ +public final class MLDSABufferPublicKeyParser extends AbstractBufferPublicKeyParser<PublicKey> { + /** Shared parser for all ML-DSA parameter sets. */ + public static final MLDSABufferPublicKeyParser INSTANCE = new MLDSABufferPublicKeyParser(); + + private MLDSABufferPublicKeyParser() { + super(PublicKey.class, MLDSAParameterSets.SSH_NAMES); + } + + /** + * @param keyType the SSH key type name + * @return whether the key type has provider support + */ + @Override + public boolean isKeyTypeSupported(String keyType) { + MLDSAParameterSets params = MLDSAParameterSets.fromSshName(keyType); + return params != null && SecurityUtils.isMLDSASupported(params.getJceName()); + } + + /** + * @param keyType the SSH key type name + * @param buffer the buffer containing the public key string + * @return the public key + * @throws GeneralSecurityException if the key type is unsupported or the key data is invalid + */ + @Override + public PublicKey getRawPublicKey(String keyType, Buffer buffer) throws GeneralSecurityException { + MLDSAParameterSets params = MLDSAParameterSets.fromSshName(keyType); + if (params == null || !isKeyTypeSupported(keyType)) { Review Comment: This redundantly calls `MLDSAParameterSets.fromSshName()` again. I'd do ``` if (params == null || !SecurityUtils.isMLDSASupported(params.getJceName())) { ... ``` instead. ########## sshd-common/pom.xml: ########## @@ -140,6 +140,8 @@ <systemPropertyVariables> <org.apache.sshd.security.provider.SunECWrapper.enabled>false</org.apache.sshd.security.provider.SunECWrapper.enabled> <org.apache.sshd.security.provider.EdDSA.enabled>false</org.apache.sshd.security.provider.EdDSA.enabled> + <org.apache.sshd.security.provider.SunWrapper.enabled>false</org.apache.sshd.security.provider.SunWrapper.enabled> + <mldsa.test.provider>BC</mldsa.test.provider> Review Comment: What's the point with this system property? ########## sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyUtils.java: ########## @@ -902,10 +918,28 @@ public static boolean compareKeys(PrivateKey k1, PrivateKey k2) { return compareECKeys(ECPrivateKey.class.cast(k1), ECPrivateKey.class.cast(k2)); } else if (isEd25519(k1) && isEd25519(k2)) { return SecurityUtils.compareEDDSAPrivateKeys(k1, k2); + } else if ((k1 != null) && (k2 != null)) { + return compareMLDSAKeys(k1, k2); } return false; // either key is null or not of same class } + private static boolean compareMLDSAKeys(PrivateKey k1, PrivateKey k2) { + if (k1 == k2) { + return true; + } + try { + if (MLDSAUtils.getParameters(k1) != MLDSAUtils.getParameters(k2)) { + return false; + } + } catch (InvalidKeySpecException e) { + return false; + } + byte[] e1 = k1.getEncoded(); + byte[] e2 = k2.getEncoded(); + return (e1 != null) && (e2 != null) && MessageDigest.isEqual(e1, e2); + } Review Comment: Where's the comparison for ML-DSA _public_ keys? -- 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]
