Hello,
I'm trying to add support for SFTP and I'm using openssl crate for the purpose. I'm not an openssl professional and that is my first usage of it. Can anyone advice me how to properly establish secure TCP connection? I implemented something already but when I try to connect to a SFTP server I get error "An error in the OpenSSL library: unknown protocol". The error happens when I try to wrap
TcpStream
into SslStream
.
Here is the function which connects to SFTP.
pub fn secure_connect<A: ToSocketAddrs>(addr: A) -> Result<FtpStream> {
match TcpStream::connect(addr) {
Ok(stream) => {
// Initialize SSL instance
let context = match SslContext::new(SslMethod::Sslv23) {
Ok(ctx) => ctx,
Err(e) => return Err(Error::new(ErrorKind::Other, e))
};
let ssl = match Ssl::new(&context) {
Ok(ssl) => ssl,
Err(e) => return Err(Error::new(ErrorKind::Other, e))
};
// Make the opened stream secured
let stream = match SslStream::connect(ssl, stream) {
Ok(stream) => stream,
Err(e) => return Err(Error::new(ErrorKind::Other, e))
};
let mut ftp_stream = FtpStream {
reader: BufReader::new(DataStream::Ssl(stream)),
};
try!(ftp_stream.read_response(status::READY));
Ok(ftp_stream)
},
Err(e) => Err(e)
}
}
The complete code is on github, lines 50-79.
Thank you for any info.