[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/5] libgcrypt: Add helper to extract MPIs into ssh_strings


* include/libssh/libgcrypt.h (ssh_sexp_extract_mpi): New prototype.
* src/libgcrypt.c (ssh_sexp_extract_mpi): New function.

Signed-off-by: Justus Winter <justus@xxxxxxxxxxx>
---
 include/libssh/libgcrypt.h |  9 +++++++++
 src/libgcrypt.c            | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/include/libssh/libgcrypt.h b/include/libssh/libgcrypt.h
index 7b97c7f..736c667 100644
--- a/include/libssh/libgcrypt.h
+++ b/include/libssh/libgcrypt.h
@@ -71,6 +71,15 @@ char *ssh_gcry_bn2dec(bignum bn);
 #define bignum_bn2bin(num,datalen,data) gcry_mpi_print(GCRYMPI_FMT_USG,data,datalen,NULL,num)
 #define bignum_cmp(num1,num2) gcry_mpi_cmp(num1,num2)
 
+/* Helper functions for data conversions.  */
+
+/* Extract an MPI from the given s-expression SEXP named NAME which is
+   encoded using INFORMAT and store it in a newly allocated ssh_string
+   encoded using OUTFORMAT.  */
+ssh_string ssh_sexp_extract_mpi(const gcry_sexp_t sexp, const char *name,
+                                enum gcry_mpi_format informat,
+                                enum gcry_mpi_format outformat);
+
 #endif /* HAVE_LIBGCRYPT */
 
 struct ssh_cipher_struct *ssh_get_ciphertab(void);
diff --git a/src/libgcrypt.c b/src/libgcrypt.c
index 17de68b..60960a3 100644
--- a/src/libgcrypt.c
+++ b/src/libgcrypt.c
@@ -2,6 +2,7 @@
  * This file is part of the SSH Library
  *
  * Copyright (c) 2009 by Aris Adamantiadis
+ * Copyright (C) 2016 g10 Code GmbH
  *
  * The SSH Library is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -27,6 +28,7 @@
 #include "libssh/session.h"
 #include "libssh/crypto.h"
 #include "libssh/wrapper.h"
+#include "libssh/string.h"
 
 #ifdef HAVE_LIBGCRYPT
 #include <gcrypt.h>
@@ -598,4 +600,51 @@ struct ssh_cipher_struct *ssh_get_ciphertab(void)
   return ssh_ciphertab;
 }
 
+/* Extract an MPI from the given s-expression SEXP named NAME which is
+   encoded using INFORMAT and store it in a newly allocated ssh_string
+   encoded using OUTFORMAT.  */
+ssh_string ssh_sexp_extract_mpi(const gcry_sexp_t sexp, const char *name,
+                                enum gcry_mpi_format informat,
+                                enum gcry_mpi_format outformat)
+{
+  gpg_error_t err;
+  ssh_string result = NULL;
+  gcry_sexp_t fragment = NULL;
+  gcry_mpi_t mpi = NULL;
+  size_t size;
+
+  fragment = gcry_sexp_find_token(sexp, name, 0);
+  if (fragment == NULL) {
+    goto fail;
+  }
+
+  mpi = gcry_sexp_nth_mpi(fragment, 1, informat);
+  if (mpi == NULL) {
+    goto fail;
+  }
+
+  err = gcry_mpi_print(outformat, NULL, 0, &size, mpi);
+  if (err) {
+    goto fail;
+  }
+
+  result = ssh_string_new(size);
+  if (result == NULL) {
+    goto fail;
+  }
+
+  err = gcry_mpi_print(outformat, ssh_string_data(result), size, NULL, mpi);
+  if (err) {
+    ssh_string_burn(result);
+    ssh_string_free(result);
+    result = NULL;
+    goto fail;
+  }
+
+ fail:
+  gcry_sexp_release(fragment);
+  gcry_mpi_release(mpi);
+  return result;
+}
+
 #endif
-- 
2.1.4


References:
Re: [PATCH 1/5] options: Check if a port has been givenAndreas Schneider <asn@xxxxxxxxxxxxxx>
[PATCH 1/5] options: Check if a port has been givenJustus Winter <justus@xxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org