Package com.sshtools.j2ssh.io

Examples of com.sshtools.j2ssh.io.ByteArrayWriter


        try {
            Signature sig = Signature.getInstance("SHA1withRSA");
            sig.initSign(prvKey);
            sig.update(data);

            ByteArrayWriter baw = new ByteArrayWriter();
            baw.writeString(getAlgorithmName());
            baw.writeBinaryString(sig.sign());

            return baw.toByteArray();
        } catch (Exception e) {
            return null;
        }
    }
View Full Code Here


            byte[] payload = pem.decryptPayload(passphrase);
            SimpleASNReader asn = new SimpleASNReader(payload);

            if (PEM.DSA_PRIVATE_KEY.equals(pem.getType())) {
                DSAKeyInfo keyInfo = DSAKeyInfo.getDSAKeyInfo(asn);
                ByteArrayWriter baw = new ByteArrayWriter();
                baw.writeString("ssh-dss");
                baw.writeBigInteger(keyInfo.getP());
                baw.writeBigInteger(keyInfo.getQ());
                baw.writeBigInteger(keyInfo.getG());
                baw.writeBigInteger(keyInfo.getX());

                return baw.toByteArray();
            } else if (PEM.RSA_PRIVATE_KEY.equals(pem.getType())) {
                RSAKeyInfo keyInfo = RSAKeyInfo.getRSAKeyInfo(asn);
                ByteArrayWriter baw = new ByteArrayWriter();
                baw.writeString("ssh-rsa");
                baw.writeBigInteger(keyInfo.getPublicExponent());
                baw.writeBigInteger(keyInfo.getModulus());
                baw.writeBigInteger(keyInfo.getPrivateExponent());

                return baw.toByteArray();
            } else {
                throw new InvalidSshKeyException("Unsupported type: " +
                    pem.getType());
            }
        } catch (GeneralSecurityException e) {
View Full Code Here

     * @return
     *
     * @throws IOException
     */
    public byte[] toByteArray() throws IOException {
        ByteArrayWriter baw = new ByteArrayWriter();
        baw.write(SSH_AGENT_CONSTRAINT_TIMEOUT);
        baw.writeUINT32(timeout);
        baw.write(SSH_AGENT_CONSTRAINT_USE_LIMIT);
        baw.writeUINT32(uselimit);
        baw.write(SSH_AGENT_CONSTRAINT_FORWARDING_STEPS);
        baw.writeUINT32(maxsteps);
        baw.write(SSH_AGENT_CONSTRAINT_FORWARDING_PATH);
        baw.writeString(forwardingpath);
        baw.write(SSH_AGENT_CONSTRAINT_SSH1_COMPAT);
        baw.write(compat ? 0 : 1);
        baw.write(SSH_AGENT_CONSTRAINT_NEED_USER_VERIFICATION);
        baw.write(userverify ? 0 : 1);

        return baw.toByteArray();
    }
View Full Code Here

    }

    private byte[] makeSshKey(char chr) throws IOException {
        try {
            // Create the first 20 bytes of key data
            ByteArrayWriter keydata = new ByteArrayWriter();
            byte[] data = new byte[20];
            Hash hash = new Hash("SHA");

            // Put the dh k value
            hash.putBigInteger(k);

            // Put in the exchange hash
            hash.putBytes(exchangeHash);

            // Put in the character
            hash.putByte((byte) chr);

            // Put the exchange hash in again
            hash.putBytes(sessionIdentifier);

            // Create the fist 20 bytes
            data = hash.doFinal();
            keydata.write(data);

            // Now do the next 20
            hash.reset();

            // Put the dh k value in again
            hash.putBigInteger(k);

            // And the exchange hash
            hash.putBytes(exchangeHash);

            // Finally the first 20 bytes of data we created
            hash.putBytes(data);
            data = hash.doFinal();

            // Put it all together
            keydata.write(data);

            // Return it
            return keydata.toByteArray();
        } catch (NoSuchAlgorithmException nsae) {
            sendDisconnect(SshMsgDisconnect.KEY_EXCHANGE_FAILED,
                "Application error");
            throw new TransportProtocolException("SHA algorithm not supported");
        } catch (IOException ioe) {
View Full Code Here

     *
     * @since 0.2.0
     */
    public final byte[] toByteArray() throws InvalidMessageException {
        // Create a writer object to construct the array
        ByteArrayWriter baw = new ByteArrayWriter();

        // Write the message id
        baw.write(messageId);

        // Call the abstract method so subclasses classes can add their data
        constructByteArray(baw);

        // Return the array
        return baw.toByteArray();
    }
View Full Code Here

            SshCipher cipher = algorithms.getCipher();
            SshHmac hmac = algorithms.getHmac();
            SshCompression compression = algorithms.getCompression();

            // Write the data into a byte array
            ByteArrayWriter message = new ByteArrayWriter();

            // Get the message payload data
            byte[] msgdata = msg.toByteArray();

            //int payload = msgdata.length;
            int padding = 4;
            int cipherlen = 8;

            // Determine the cipher length
            if (cipher != null) {
                cipherlen = cipher.getBlockSize();
            }

            // Compress the payload if necersary
            if (compression != null) {
                msgdata = compression.compress(msgdata, 0, msgdata.length);
            }

            //Determine the padding length
            padding += ((cipherlen -
            ((msgdata.length + 5 + padding) % cipherlen)) % cipherlen);

            // Write the packet length field
            message.writeInt(msgdata.length + 1 + padding);

            // Write the padding length
            message.write(padding);

            // Write the message payload
            message.write(msgdata, 0, msgdata.length);

            // Create some random data for the padding
            byte[] pad = new byte[padding];
            rnd.nextBytes(pad);

            // Write the padding
            message.write(pad);

            // Get the unencrypted packet data
            byte[] packet = message.toByteArray();
            byte[] mac = null;

            // Generate the MAC
            if (hmac != null) {
                mac = hmac.generate(sequenceNo, packet, 0, packet.length);
            }

            // Perfrom encrpytion
            if (cipher != null) {
                packet = cipher.transform(packet);
            }

            // Reset the message
            message.reset();

            // Write the packet data
            message.write(packet);

            // Combine the packet and MAC
            if (mac != null) {
                message.write(mac);
            }

            bytesTransfered += message.size();

            // Send!
            out.write(message.toByteArray());

            out.flush();

            // Increment the sequence no
            if (sequenceNo < sequenceWrapLimit) {
View Full Code Here

                // Were forwarding so insert the forwarding notice before any other data
                SshAgentForwardingNotice msg = new SshAgentForwardingNotice(InetAddress.getLocalHost()
                                                                                       .getHostName(),
                        InetAddress.getLocalHost().getHostAddress(),
                        socket.getPort());
                ByteArrayWriter baw = new ByteArrayWriter();
                baw.writeBinaryString(msg.toByteArray());
                sendChannelData(baw.toByteArray());
            }

            super.onChannelOpen();

            // Now bind the socket to the channel
View Full Code Here

    }

    class SocketReader implements Runnable {
        public void run() {
            byte[] buffer = new byte[getMaximumPacketSize()];
            ByteArrayWriter baw = new ByteArrayWriter();

            try {
                socket.setSoTimeout(2000);
            } catch (SocketException ex2) {
            }

            try {
                int read = 0;

                while ((read >= 0) && !isClosed()) {
                    try {
                        read = socket.getInputStream().read(buffer);
                    } catch (InterruptedIOException ex1) {
                        read = ex1.bytesTransferred;
                    }

                    synchronized (state) {
                        if (isClosed() || isLocalEOF()) {
                            break;
                        }

                        if (read > 0) {
                            baw.write(buffer, 0, read);
                            sendChannelData(baw.toByteArray());
                            baw.reset();
                        }
                    }
                }
            } catch (IOException ex) {
                // Break out of the while loop
View Full Code Here

        authentication.registerMessage(SshMsgUserAuthPKOK.class,
            SshMsgUserAuthPKOK.SSH_MSG_USERAUTH_PK_OK);
        log.info(
            "Determining if server can accept public key for authentication");

        ByteArrayWriter baw = new ByteArrayWriter();

        // Now prepare and send the message
        baw.write(0);
        baw.writeString(key.getAlgorithmName());
        baw.writeBinaryString(key.getEncoded());

        SshMessage msg = new SshMsgUserAuthRequest(username, serviceToStart,
                getMethodName(), baw.toByteArray());
        authentication.sendMessage(msg);

        try {
            msg = authentication.readMessage(SshMsgUserAuthPKOK.SSH_MSG_USERAUTH_PK_OK);
View Full Code Here

            log.info("Agent authentication with key " + key.getFingerprint() +
                " [" + description + "] is " +
                (acceptable ? " acceptable" : " not acceptable"));

            if (acceptable) {
                ByteArrayWriter baw = new ByteArrayWriter();
                log.info("Generating data to sign");
                log.info("Preparing public key authentication request");

                // Now prepare and send the message
                baw.write(1);
                baw.writeString(key.getAlgorithmName());
                baw.writeBinaryString(key.getEncoded());

                // Create the signature data
                ByteArrayWriter data = new ByteArrayWriter();
                data.writeBinaryString(authentication.getSessionIdentifier());
                data.write(SshMsgUserAuthRequest.SSH_MSG_USERAUTH_REQUEST);
                data.writeString(getUsername());
                data.writeString(serviceToStart);
                data.writeString(getMethodName());
                data.write(1);
                data.writeString(key.getAlgorithmName());
                data.writeBinaryString(key.getEncoded());

                // Generate the signature
                baw.writeBinaryString(agent.hashAndSign(key, data.toByteArray()));

                SshMsgUserAuthRequest msg = new SshMsgUserAuthRequest(getUsername(),
                        serviceToStart, getMethodName(), baw.toByteArray());
                authentication.sendMessage(msg);
View Full Code Here

TOP

Related Classes of com.sshtools.j2ssh.io.ByteArrayWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.