protected SSHPacket putPubKey(SSHPacket reqBuf) throws UserAuthException { PublicKey key; try { key = kProv.getPublic(); } catch (IOException ioe) { throw new UserAuthException("Problem getting public key from " + kProv, ioe); } // public key as 2 strings: [ key type | key blob ] reqBuf.putString(KeyType.fromKey(key).toString()) .putString(new Buffer.PlainBuffer().putPublicKey(key).getCompactData()); return reqBuf; }
private PublicKey readPublicKey(final PlainBuffer plainBuffer) throws Buffer.BufferException, GeneralSecurityException { return KeyType.fromString(plainBuffer.readString()).readPubKeyFromBuffer(plainBuffer); }
@SuppressWarnings("unchecked") public T putPublicKey(PublicKey key) { KeyType.fromKey(key).putPubKeyIntoBuffer(key, this); return (T) this; }
public KeyPairWrapper(KeyPair kp) { this.kp = kp; type = KeyType.fromKey(kp.getPublic()); }
@Override protected boolean isUnknownKeyAccepted(final String hostname, final PublicKey key) throws ConnectionCanceledException, ChecksumException { final String message = String.format("%s. %s %s?", LocaleFactory.localizedString("Unknown fingerprint", "Sftp"), MessageFormat.format(LocaleFactory.localizedString("The fingerprint for the {1} key sent by the server is {0}.", "Sftp"), new SSHFingerprintGenerator().fingerprint(key), KeyType.fromKey(key).name()), LocaleFactory.localizedString("Continue", "Credentials")); if(!prompt.prompt(message)) { throw new ConnectionCanceledException(); } this.allow(hostname, key, true); return true; }
@Override public String getName() { return KeyType.ED25519.toString(); }
/** * Key type. Either "ssh-rsa" for RSA key, or "ssh-dss" for DSA key. */ @Override public KeyType getType() throws IOException { return KeyType.fromString(headers.get("PuTTY-User-Key-File-2")); }
private PrivateKey createECDSAPrivateKey(KeyType kt, PlainBuffer buffer, String name) throws GeneralSecurityException, Buffer.BufferException { kt.readPubKeyFromBuffer(buffer); // Public key BigInteger s = new BigInteger(1, buffer.readBytes()); X9ECParameters ecParams = NISTNamedCurves.getByName(name); ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(), ecParams.getN()); ECPrivateKeySpec pks = new ECPrivateKeySpec(s, ecCurveSpec); return SecurityUtils.getKeyFactory("ECDSA").generatePrivate(pks); } }
public static KeyType fromString(String sType) { for (KeyType kt : values()) if (kt.sType.equals(sType)) return kt; return UNKNOWN; }
static boolean isCertificateOfType(Key key, KeyType innerKeyType) { if (!(key instanceof Certificate)) { return false; } @SuppressWarnings("unchecked") Key innerKey = ((Certificate<PublicKey>) key).getKey(); return innerKeyType.isMyType(innerKey); }
@Override public KeyType getType() throws IOException { return type != null ? type : (type = KeyType.fromKey(getPublic())); }
@Override protected boolean isChangedKeyAccepted(final String hostname, final PublicKey key) throws ConnectionCanceledException, ChecksumException { final String message = String.format("%s. %s %s?", LocaleFactory.localizedString("Changed fingerprint", "Sftp"), MessageFormat.format(LocaleFactory.localizedString("The fingerprint for the {1} key sent by the server is {0}.", "Sftp"), new SSHFingerprintGenerator().fingerprint(key), KeyType.fromKey(key).name()), LocaleFactory.localizedString("Continue", "Credentials")); if(!prompt.prompt(message)) { throw new ConnectionCanceledException(); } this.allow(hostname, key, true); return true; } }
@Override public String getName() { return KeyType.ECDSA256.toString(); }
@SuppressWarnings("unchecked") public T putPublicKey(PublicKey key) { KeyType.fromKey(key).putPubKeyIntoBuffer(key, this); return (T) this; }
/** * Read and store the separate public key provided alongside the private key * * @param publicKey Public key accessible through a {@code Reader} */ private void initPubKey(Reader publicKey) throws IOException { final BufferedReader br = new BufferedReader(publicKey); try { final String keydata = br.readLine(); if (keydata != null) { String[] parts = keydata.trim().split(" "); assert parts.length >= 2; type = KeyType.fromString(parts[0]); pubKey = new Buffer.PlainBuffer(Base64.decode(parts[1])).readPublicKey(); } } finally { br.close(); } } }
@SuppressWarnings("unchecked") static <T extends PublicKey> Certificate<T> readPubKey(Buffer<?> buf, KeyType innerKeyType) throws GeneralSecurityException { Certificate.Builder<T> builder = Certificate.getBuilder(); try { builder.nonce(buf.readBytes()); builder.publicKey((T) innerKeyType.readPubKeyFromBuffer(buf)); builder.serial(buf.readUInt64AsBigInteger()); builder.type(buf.readUInt32()); builder.id(buf.readString()); builder.validPrincipals(unpackList(buf.readBytes())); builder.validAfter(dateFromEpoch(buf.readUInt64())); builder.validBefore(dateFromEpoch(buf.readUInt64())); builder.critOptions(unpackMap(buf.readBytes())); builder.extensions(unpackMap(buf.readBytes())); buf.readString(); // reserved builder.signatureKey(buf.readBytes()); builder.signature(buf.readBytes()); } catch (Buffer.BufferException be) { throw new GeneralSecurityException(be); } return builder.build(); }
public static KeyType fromString(String sType) { for (KeyType kt : values()) if (kt.sType.equals(sType)) return kt; return UNKNOWN; }