github.com/sentienttechnologies/studio-go-runner@v0.0.0-20201118202441-6d21f2ced8ee/internal/runner/ssh.go (about)

     1  // Copyright 2020 (c) Cognizant Digital Business, Evolutionary AI. All rights reserved. Issued under the Apache 2.0 License.
     2  
     3  package runner
     4  
     5  // This file contains functions to assist in wrangling SSH signatures
     6  // for encoding and decoding etc to achieve parity with Python Paramiko
     7  // clients etc
     8  
     9  import (
    10  	"encoding/binary"
    11  
    12  	"golang.org/x/crypto/ssh"
    13  
    14  	"github.com/go-stack/stack"
    15  	"github.com/jjeffery/kv"
    16  )
    17  
    18  func parseString(in []byte) (out, rest []byte, err kv.Error) {
    19  	if len(in) < 4 {
    20  		return out, rest, kv.NewError("bad length").With("stack", stack.Trace().TrimRuntime())
    21  	}
    22  	length := binary.BigEndian.Uint32(in)
    23  	in = in[4:]
    24  	if uint32(len(in)) < length {
    25  		return out, rest, kv.NewError("truncated data").With("stack", stack.Trace().TrimRuntime())
    26  	}
    27  	out = in[:length]
    28  	rest = in[length:]
    29  
    30  	return out, rest, nil
    31  }
    32  
    33  // ParseSSHSignature is used to extract a signature from a byte buffer encoded
    34  // formatted according to, https://tools.ietf.org/html/draft-ietf-curdle-ssh-ed25519-01.
    35  // A pair of Length,Value items.  The first the Format string for the signature
    36  // and the second the bytes of the key blob.
    37  //
    38  func ParseSSHSignature(in []byte) (out *ssh.Signature, err kv.Error) {
    39  	format, in, err := parseString(in)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	out = &ssh.Signature{
    45  		Format: string(format),
    46  	}
    47  
    48  	out.Blob, _, err = parseString(in)
    49  	return out, err
    50  }