简体   繁体   English

具有基于非文件密钥的SFTP

[英]SFTP with Non-File Based Keys

I'm planning to write some software using SFTP with public/private key authentication to upload files to a server. 我打算使用带有公共/专用密钥身份验证的SFTP编写一些软件,以将文件上传到服务器。 I am wondering how people recommend managing the keys (especially the private key). 我想知道人们如何建议管理密钥(尤其是私钥)。

My target platform is Windows with C# or C++. 我的目标平台是带有C#或C ++的Windows。 I've looked at a number of libraries: 我看了一些库:

C# C#
(free/open) (自由/开放)
SharpSSH SharpSSH
Granados 格拉纳多斯

(commercial) (商业)
Rebex Rebex的

C++ C ++
libcurl/OpenSSH 的libcurl / OpenSSH的

All of these appear to require the private key to be stored on the filesystem, which I would prefer to avoid for security reasons. 所有这些似乎都要求将私钥存储在文件系统上,出于安全考虑,我宁愿避免使用该私钥。 I would also prefer not to implement the authentication myself although I recognize that as an option. 我也不想自己实施身份验证,尽管我认为这是一种选择。 My questions: 我的问题:

Is there a way to feed any of these libraries (or any library/API I may have overlooked) the key values directly from memory instead of loading from a file? 有没有一种方法可以直接从内存中而不是从文件中加载任何这些库(或我可能忽略的任何库/ API)中的键值?

If not, what is the recommended way to manage these key files? 如果不是,建议使用什么方法来管理这些密钥文件? Beyond password encryption and tight access control, are there other things one should do to protect the key file? 除了密码加密和严格的访问控制之外,还应该采取其他措施来保护密钥文件吗?

All of these appear to require the private key to be stored on the filesystem, which I would prefer to avoid for security reasons. 所有这些似乎都要求将私钥存储在文件系统上,出于安全考虑,我宁愿避免使用该私钥。

I think you should embrace the filesystem. 我认为您应该拥抱文件系统。 Modern ones such as NTFS (which as a Windows developer you are almost surely using) have robust security mechanisms which are probably better thought out than most any alternative mechanism you (or I) might devise. NTFS(您几乎肯定会使用的Windows开发人员)等现代工具具有健壮的安全性机制,与您(或我)可能设计的大多数其他替代性机制相比,这些机制可能要深思熟虑。 NTFS has access control lists, encryption, and so on--if you can't trust your filesystem to keep your data safe, why do you even bother caring about network traffic? NTFS具有访问控制列表,加密等功能-如果您不信任文件系统来确保数据安全,为什么还要担心网络流量呢?

I'll note that there are some specific circumstances which could preclude doing what I've said above, but I don't see any of them mentioned in the original post. 我会注意到,在某些特定情况下可能无法执行我在上面所说的内容,但我在原始帖子中没有提到任何这些情况。

I've never used Granados, but that library's SSH2UserAuthKey class has a FromSECSHStyleStream method that loads a user's authentication key from an arbitrary Stream (which could be a MemoryStream if you want to load the key directly from memory). 我从未使用过Granados,但是该库的SSH2UserAuthKey类具有FromSECSHStyleStream方法,该方法从任意Stream (如果要直接从内存中加载密钥,则可以是MemoryStream加载用户的身份验证密钥。 It seems like this would let you bypass the filesystem for key storage. 似乎这样可以让您绕过文件系统进行密钥存储。

Rebex.Net has a SshPrivateKey class constructor that can load from a byte[] ; Rebex.Net有一个SshPrivateKey类构造函数 ,可以从byte[]加载; this should also let you avoid the filesystem. 这也应该使您避免使用文件系统。

While I haven't checked the other libraries, it seems likely that all of them would provide methods to load a key from a byte[] , MemoryStream , or unsigned char* , with convenience methods that load directly from a file. 虽然我没有检查其他库,但似乎所有这些库都提供了从byte[]MemoryStreamunsigned char*加载密钥的方法,而便捷方法直接从文件加载。

These APIs don't solve the problem of how the private key will actually be stored, though. 但是,这些API并不能解决私钥实际存储方式的问题。 You could password-protect it (this seems a fairly common approach with SSH private keys) or use the ProtectedData class to encrypt it with the current Windows user's credentials. 您可以用密码保护它(这似乎是使用SSH私钥的相当普遍的方法),也可以使用ProtectedData类使用当前Windows用户的凭据对其进行加密。

SFTP classes of SecureBlackbox (our product) let you load the key from any stream or byte array. SecureBlackbox(我们的产品)的SFTP类使您可以从任何流或字节数组中加载密钥。 Moreover, they also let you generate and convert SSH keys in various formats. 此外,它们还使您可以生成和转换各种格式的SSH密钥。

If you have questions, we provide support to evaluating users via Forum and HelpDesk 如果您有任何疑问,我们将通过ForumHelpDesk为评估用户提供支持

Following code shows how to authenticate using a private key using Rebex SFTP . 以下代码显示了如何使用Rebex SFTP使用私钥进行身份验证。

Code is taken from following forum post . 代码来自以下论坛帖子 For more info check SFTP tutorial . 有关更多信息,请查看SFTP教程

// create client and connect 
Sftp client = new Sftp();
client.Connect(hostname);

// instead of loading it from disk file load the private key from a byte array
//SshPrivateKey privateKey = new SshPrivateKey("key_rsa.pem", "password");

byte[] privateKeyData = YourMethodForObtainingPrivateKey();    
SshPrivateKey privateKey = new SshPrivateKey(privateKeyData, "password");

// authenticate 
client.Login(username, privateKey);

// ... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM