Search in sources :

Example 1 with ByteArrayBuffer

use of org.apache.sshd.common.util.buffer.ByteArrayBuffer in project gerrit by GerritCodeReview.

the class SshDaemon method computeHostKeys.

private List<HostKey> computeHostKeys() {
    if (listen.isEmpty()) {
        return Collections.emptyList();
    }
    List<HostKey> r = new ArrayList<>();
    List<PublicKey> keys = myHostKeys();
    for (PublicKey pub : keys) {
        Buffer buf = new ByteArrayBuffer();
        buf.putRawPublicKey(pub);
        byte[] keyBin = buf.getCompactData();
        for (String addr : advertised) {
            r.add(new HostKey(addr, keyBin));
        }
    }
    return Collections.unmodifiableList(r);
}
Also used : ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer) Buffer(org.apache.sshd.common.util.buffer.Buffer) HostKey(com.google.gerrit.server.ssh.HostKey) PublicKey(java.security.PublicKey) ArrayList(java.util.ArrayList) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer)

Example 2 with ByteArrayBuffer

use of org.apache.sshd.common.util.buffer.ByteArrayBuffer in project gerrit by GerritCodeReview.

the class SshUtil method toOpenSshPublicKey.

/**
 * Convert an RFC 4716 style key to an OpenSSH style key.
 *
 * @param keyStr the key string to convert.
 * @return {@code keyStr} if conversion failed; otherwise the converted key, in OpenSSH key
 *     format.
 */
public static String toOpenSshPublicKey(String keyStr) {
    try {
        final StringBuilder strBuf = new StringBuilder();
        final BufferedReader br = new BufferedReader(new StringReader(keyStr));
        // BEGIN SSH2 line...
        String line = br.readLine();
        if (line == null || !line.equals("---- BEGIN SSH2 PUBLIC KEY ----")) {
            return keyStr;
        }
        while ((line = br.readLine()) != null) {
            if (line.indexOf(':') == -1) {
                strBuf.append(line);
                break;
            }
        }
        while ((line = br.readLine()) != null) {
            if (line.startsWith("---- ")) {
                break;
            }
            strBuf.append(line);
        }
        final PublicKey key = new ByteArrayBuffer(BaseEncoding.base64().decode(strBuf.toString())).getRawPublicKey();
        if (key instanceof RSAPublicKey) {
            strBuf.insert(0, KeyPairProvider.SSH_RSA + " ");
        } else if (key instanceof DSAPublicKey) {
            strBuf.insert(0, KeyPairProvider.SSH_DSS + " ");
        } else {
            return keyStr;
        }
        strBuf.append(' ');
        strBuf.append("converted-key");
        return strBuf.toString();
    } catch (IOException | RuntimeException e) {
        return keyStr;
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 3 with ByteArrayBuffer

use of org.apache.sshd.common.util.buffer.ByteArrayBuffer in project gerrit by GerritCodeReview.

the class SshUtil method parse.

/**
 * Parse a public key into its Java type.
 *
 * @param key the account key to parse.
 * @return the valid public key object.
 * @throws InvalidKeySpecException the key supplied is not a valid SSH key.
 * @throws NoSuchAlgorithmException the JVM is missing the key algorithm.
 * @throws NoSuchProviderException the JVM is missing the provider.
 */
public static PublicKey parse(AccountSshKey key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    try {
        final String s = key.encodedKey();
        if (s == null) {
            throw new InvalidKeySpecException("No key string");
        }
        final byte[] bin = BaseEncoding.base64().decode(s);
        return new ByteArrayBuffer(bin).getRawPublicKey();
    } catch (RuntimeException | SshException e) {
        throw new InvalidKeySpecException("Cannot parse key", e);
    }
}
Also used : InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SshException(org.apache.sshd.common.SshException) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer)

Example 4 with ByteArrayBuffer

use of org.apache.sshd.common.util.buffer.ByteArrayBuffer in project gitblit by gitblit.

the class SshKey method getRawData.

public String getRawData() {
    if (rawData == null && publicKey != null) {
        // build the raw data manually from the public key
        Buffer buf = new ByteArrayBuffer();
        // 1: identify the algorithm
        buf.putRawPublicKey(publicKey);
        String alg = buf.getString();
        // 2: encode the key
        buf.clear();
        buf.putPublicKey(publicKey);
        String b64 = Base64.encodeBase64String(buf.getBytes());
        String c = getComment();
        rawData = alg + " " + b64 + (StringUtils.isEmpty(c) ? "" : (" " + c));
    }
    return rawData;
}
Also used : Buffer(org.apache.sshd.common.util.buffer.Buffer) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer)

Aggregations

ByteArrayBuffer (org.apache.sshd.common.util.buffer.ByteArrayBuffer)4 PublicKey (java.security.PublicKey)2 Buffer (org.apache.sshd.common.util.buffer.Buffer)2 HostKey (com.google.gerrit.server.ssh.HostKey)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 DSAPublicKey (java.security.interfaces.DSAPublicKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 ArrayList (java.util.ArrayList)1 SshException (org.apache.sshd.common.SshException)1