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);
}
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;
}
}
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);
}
}
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;
}
Aggregations