All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sshtools.common.ssh.x509.SshX509RsaPublicKeyRfc6187 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.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPublicKey;

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.SshPublicKeyFactory;
import com.sshtools.common.ssh.components.SshX509PublicKey;
import com.sshtools.common.ssh.components.jce.Ssh2RsaPublicKey;
import com.sshtools.common.util.ByteArrayReader;
import com.sshtools.common.util.ByteArrayWriter;

public class SshX509RsaPublicKeyRfc6187 extends Ssh2RsaPublicKey implements SshX509PublicKey {

	 public static final String X509V3_SSH_RSA = "x509v3-ssh-rsa";

	public static class SshX509RsaPublicKeyRfc6187Factory implements SshPublicKeyFactory {

		@Override
		public SshX509RsaPublicKeyRfc6187 create() throws NoSuchAlgorithmException, IOException {
			return new SshX509RsaPublicKeyRfc6187();
		}

		@Override
		public String[] getKeys() {
			return new String[] { X509V3_SSH_RSA };
		}
	}
	
	Certificate[] certs;

	public SshX509RsaPublicKeyRfc6187() {
		
	}
	
	public SshX509RsaPublicKeyRfc6187(Certificate[] chain) {
		 super((RSAPublicKey)chain[0].getPublicKey());
		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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy