aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustus Winter <justus@g10code.com>2016-03-29 13:06:59 +0200
committerAndreas Schneider <asn@cryptomilk.org>2016-05-02 11:55:38 +0200
commit735e34f93270262752a86573e81d2f1f915d25df (patch)
treecc3ea13f03b9c0e447cf007ca71e0811eba56489 /src
parentfea9e3050c99209a05a340e61e617be46547141f (diff)
downloadlibssh-735e34f93270262752a86573e81d2f1f915d25df.tar.gz
libssh-735e34f93270262752a86573e81d2f1f915d25df.tar.xz
libssh-735e34f93270262752a86573e81d2f1f915d25df.zip
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@g10code.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'src')
-rw-r--r--src/libgcrypt.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/libgcrypt.c b/src/libgcrypt.c
index 17de68b8..60f6536c 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,54 @@ 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 != 0) {
+ 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 != 0) {
+ ssh_string_burn(result);
+ ssh_string_free(result);
+ result = NULL;
+ goto fail;
+ }
+
+fail:
+ gcry_sexp_release(fragment);
+ gcry_mpi_release(mpi);
+ return result;
+}
+
#endif