diff --git a/include/libssh/libgcrypt.h b/include/libssh/libgcrypt.h index 7b97c7f8..7556acae 100644 --- a/include/libssh/libgcrypt.h +++ b/include/libssh/libgcrypt.h @@ -71,6 +71,16 @@ 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 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 @@ -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