com.sshtools.common.ssh.x509.SshX509EcdsaSha2NistPublicKeyRfc6187 Maven / Gradle / Ivy
package com.sshtools.common.ssh.x509;
/*-
* #%L
* X509 Certificate Support
* %%
* Copyright (C) 2002 - 2024 JADAPTIVE Limited
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.ECPublicKey;
import com.sshtools.common.logger.Log;
import com.sshtools.common.ssh.SshException;
import com.sshtools.common.ssh.components.SshPublicKey;
import com.sshtools.common.ssh.components.SshX509PublicKey;
import com.sshtools.common.ssh.components.jce.Ssh2EcdsaSha2NistPublicKey;
import com.sshtools.common.util.ByteArrayReader;
import com.sshtools.common.util.ByteArrayWriter;
public abstract class SshX509EcdsaSha2NistPublicKeyRfc6187 extends Ssh2EcdsaSha2NistPublicKey implements SshX509PublicKey {
Certificate[] certs;
public SshX509EcdsaSha2NistPublicKeyRfc6187(String name, String spec, String curve, String nistpCurve) {
super(name, spec, curve, nistpCurve);
}
public SshX509EcdsaSha2NistPublicKeyRfc6187(ECPublicKey pk, String curve) throws IOException {
super(pk, curve);
}
public SshX509EcdsaSha2NistPublicKeyRfc6187(Certificate[] chain, String curve) throws IOException {
super((ECPublicKey)chain[0].getPublicKey(), curve);
this.certs = chain;
}
public SshPublicKey init(byte[] blob, int start, int len) throws SshException {
ByteArrayReader reader = new ByteArrayReader(blob, start, len);
try {
String alg = reader.readString();
if (!alg.equals(getAlgorithm())) {
throw new SshException("Public key blob is not a "
+ getAlgorithm() + " formatted key [" + alg + "]",
SshException.BAD_API_USAGE);
}
int certificateCount = (int) reader.readInt();
if(Log.isDebugEnabled()) {
Log.debug("Expecting chain of " + certificateCount);
}
if(certificateCount <= 0) {
throw new SshException(
"There are no certificats present in the public key blob",
SshException.POSSIBLE_CORRUPT_FILE);
}
this.certs = new Certificate[certificateCount];
for(int i=0;i