[Chinese-commits] [fqterm] 25/34: draft for the new SSH public key crypto code

Boyuan Yang hosiet-guest at moszumanska.debian.org
Tue Nov 1 06:06:07 UTC 2016


This is an automated email from the git hooks/post-receive script.

hosiet-guest pushed a commit to branch master
in repository fqterm.

commit 3db93e424e6ebbae1242ff94617d03837fab35ea
Author: Iru Cai <mytbk920423 at gmail.com>
Date:   Fri Oct 28 11:48:13 2016 +0800

    draft for the new SSH public key crypto code
    
    The original one is too complicated and is broken under OpenSSL 1.1.0
---
 src/protocol/internal/ssh_pubkey_crypto.c | 53 +++++++++++++++++++++++++++++++
 src/protocol/internal/ssh_pubkey_crypto.h | 34 ++++++++++++++++++++
 2 files changed, 87 insertions(+)

diff --git a/src/protocol/internal/ssh_pubkey_crypto.c b/src/protocol/internal/ssh_pubkey_crypto.c
new file mode 100644
index 0000000..299bbae
--- /dev/null
+++ b/src/protocol/internal/ssh_pubkey_crypto.c
@@ -0,0 +1,53 @@
+#include "ssh_pubkey_crypto.h"
+#include <stdlib.h>
+
+struct ssh_pubkey_t*
+ssh_pubkey_new(enum pubkey_type t)
+{
+	struct ssh_pubkey_t *k = (struct ssh_pubkey_t*)
+		malloc(sizeof(struct ssh_pubkey_t));
+	k->key_type = t;
+	switch (t) {
+	case SSH_RSA:
+		k->key.ssh_rsa = RSA_new();
+	}
+	return k;
+}
+
+int
+ssh_pubkey_free(struct ssh_pubkey_t *k)
+{
+	switch (k->key_type) {
+	case SSH_RSA:
+		RSA_free(k->key.ssh_rsa);
+	}
+	free(k);
+}
+
+static int
+ssh_pubkey_encrypt_rsa(RSA *k, BIGNUM *out, BIGNUM *in)
+{
+	size_t len, ilen, olen;
+
+	olen = RSA_size(k);
+	ilen = BN_num_bytes(in);
+
+	unsigned char outbuf[olen], inbuf[ilen];
+
+	BN_bn2bin(in, inbuf);
+	len = RSA_public_encrypt(ilen, inbuf, outbuf, k,
+				 RSA_PKCS1_PADDING);
+	if (len <= 0) {
+		return -1;
+	}
+	BN_bin2bn(outbuf, len, out);
+	return 0;
+}
+
+int ssh_pubkey_encrypt(struct ssh_pubkey_t *k, BIGNUM *out, BIGNUM *in)
+{
+	switch (k->key_type) {
+	case SSH_RSA:
+		return ssh_pubkey_encrypt_rsa(k->key.ssh_rsa, out, in);
+	}
+}
diff --git a/src/protocol/internal/ssh_pubkey_crypto.h b/src/protocol/internal/ssh_pubkey_crypto.h
new file mode 100644
index 0000000..809ec06
--- /dev/null
+++ b/src/protocol/internal/ssh_pubkey_crypto.h
@@ -0,0 +1,34 @@
+/* This file is part of FQTerm project
+ * written by Iru Cai <mytbk920423 at gmail.com>
+ */
+
+#ifndef SSH_PUBKEY_CRYPTO_H
+#define SSH_PUBKEY_CRYPTO_H
+
+#include <openssl/rsa.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	enum pubkey_type {
+		SSH_RSA
+	};
+
+	struct ssh_pubkey_t
+	{
+		enum pubkey_type key_type; /* now only RSA is supported */
+		union {
+			RSA *ssh_rsa;
+		} key;
+	};
+
+	struct ssh_pubkey_t *ssh_pubkey_new(enum pubkey_type);
+	int ssh_pubkey_free(struct ssh_pubkey_t*);
+	int ssh_pubkey_encrypt(struct ssh_pubkey_t *k, BIGNUM *out, BIGNUM *in);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/chinese/fqterm.git



More information about the Chinese-commits mailing list