Browse Source

z/OS support: Encryption

Encryption now works so far, adapting the the solution for decryption.
I'm seeing the light at the end of the tunnel, let's hope it's not
a train.
master
David Lanzendörfer 2 months ago
parent
commit
90bd14fda8
17 changed files with 1872 additions and 1351 deletions
  1. +90
    -0
      ax_check_pkcs.m4
  2. +2
    -0
      configure.ac
  3. +29
    -23
      include/netpgp/create.h
  4. +17
    -0
      include/netpgp/crypto.h
  5. +98
    -27
      include/netpgp/packet.h
  6. +10
    -0
      include/netpgp/writer.h
  7. +1
    -1
      src/Makefile.am
  8. +595
    -581
      src/create.c
  9. +7
    -1
      src/crypto.c
  10. +202
    -20
      src/gskssl_crypto.c
  11. +11
    -10
      src/keyring.c
  12. +2
    -1
      src/main.c
  13. +28
    -1
      src/misc.c
  14. +46
    -5
      src/packet-parse.c
  15. +44
    -4
      src/signature.c
  16. +671
    -669
      src/validate.c
  17. +19
    -8
      src/writer.c

+ 90
- 0
ax_check_pkcs.m4 View File

@ -0,0 +1,90 @@
AU_ALIAS([CHECK_PKCS], [AX_CHECK_PKCS])
AC_DEFUN([AX_CHECK_PKCS], [
found=false
AC_ARG_WITH(gskssl,
AS_HELP_STRING([--with-gskssl=DIR],
[root of the GSK PKCS directory]),
[
case "$withval" in
"" | y | ye | yes | n | no)
AC_MSG_ERROR([Invalid --with-gskssl value])
;;
*) pkcsdirs="$withval"
;;
esac
], [
# if pkg-config is installed and gskssl has installed a .pc file,
# then use that information and don't search pkcsdirs
AC_PATH_PROG(PKG_CONFIG, pkg-config)
if test x"$PKG_CONFIG" != x""; then
PKCS_LDFLAGS=`$PKG_CONFIG gskssl --libs-only-L 2>/dev/null`
if test $? = 0; then
PKCS_LIBS=`$PKG_CONFIG gskssl --libs-only-l 2>/dev/null`
PKCS_INCLUDES=`$PKG_CONFIG gskssl --cflags-only-I 2>/dev/null`
found=true
fi
fi
# no such luck; use some default pkcsdirs
if ! $found; then
pkcsdirs="/usr/lpp/pkcs11 /usr"
fi
]
)
# note that we #include <gskssl/foo.h>, so the GSK PKCS headers have to be in
# an 'gskssl' subdirectory
if ! $found; then
PKCS_INCLUDES=
for pkcsdir in $pkcsdirs; do
AC_MSG_CHECKING([for include/gskssl.h in $pkcsdir])
if test -f "$pkcsdir/include/csnpdefs.h"; then
if test -f "$pkcsdir/lib/CSNPCA64.x"; then
PKCS_INCLUDES="-I$pkcsdir/include"
PKCS_LDFLAGS="-L$pkcsdir/lib"
PKCS_LIBS="$pkcsdir/lib/CSNPCA64.x"
found=true
AC_MSG_RESULT([yes])
fi
break
else
AC_MSG_RESULT([no])
fi
done
# if the file wasn't found, well, go ahead and try the link anyway -- maybe
# it will just work!
fi
# try the preprocessor and linker with our new flags,
# being careful not to pollute the global LIBS, LDFLAGS, and CPPFLAGS
AC_MSG_CHECKING([whether compiling and linking against GSK PKCS works])
echo "Trying link with PKCS_LDFLAGS=$PKCS_LDFLAGS;" \
"PKCS_LIBS=$PKCS_LIBS; PKCS_INCLUDES=$PKCS_INCLUDES" >&AS_MESSAGE_LOG_FD
save_LIBS="$LIBS"
save_LDFLAGS="$LDFLAGS"
save_CPPFLAGS="$CPPFLAGS"
LDFLAGS="$LDFLAGS $PKCS_LDFLAGS"
LIBS="$PKCS_LIBS $LIBS"
CPPFLAGS="$PKCS_INCLUDES $CPPFLAGS"
AC_LINK_IFELSE(
AC_LANG_PROGRAM([#include <gskssl.h>], [gsk_handle env_handle;]),
[
AC_MSG_RESULT([yes])
$1
], [
AC_MSG_RESULT([no])
$2
])
CPPFLAGS="$save_CPPFLAGS"
LDFLAGS="$save_LDFLAGS"
LIBS="$save_LIBS"
AC_SUBST([PKCS_INCLUDES])
AC_SUBST([PKCS_LIBS])
AC_SUBST([PKCS_LDFLAGS])
])

+ 2
- 0
configure.ac View File

@ -86,9 +86,11 @@ AC_TYPE_UINT8_T
# check for openssl
m4_include([ax_check_openssl.m4])
m4_include([ax_check_gskssl.m4])
m4_include([ax_check_pkcs.m4])
AX_CHECK_OPENSSL([LIBS="$LIBS $OPENSSL_LDFLAGS $OPENSSL_LIBS" INCLUDES="$INCLUDES $OPENSSL_INCLUDES" found_openssl="yes"],[found_openssl="no"])
AX_CHECK_GSKSSL([LIBS="$LIBS $GSKSSL_LDFLAGS $GSKSSL_LIBS" INCLUDES="$INCLUDES $GSKSSL_INCLUDES" found_gskssl="yes"],[found_gskssl="no"])
AX_CHECK_PKCS([LIBS="$LIBS $PKSC_LDFLAGS $PKCS_LIBS" INCLUDES="$INCLUDES $PKCS_INCLUDES" found_pkcs="yes"],[found_pkcs="no"])
AS_IF([test "$found_openssl" == "no"],[AS_IF([test "$found_gskssl" == "no"],[AC_MSG_FAILURE([No suitable SSL library found])])])


+ 29
- 23
include/netpgp/create.h View File

@ -65,8 +65,8 @@
* This struct contains the required information about how to write this stream
*/
struct pgp_output_t {
pgp_writer_t writer;
pgp_error_t *errors; /* error stack */
pgp_writer_t writer;
pgp_error_t *errors; /* error stack */
};
pgp_output_t *pgp_output_new(void);
@ -77,26 +77,26 @@ int pgp_filewrite(const char *, const char *, const size_t, const unsigned);
void pgp_build_pubkey(pgp_memory_t *, const pgp_pubkey_t *, unsigned);
unsigned pgp_calc_sesskey_checksum(pgp_pk_sesskey_t *, uint8_t *);
unsigned pgp_write_struct_userid(pgp_output_t *, const uint8_t *);
unsigned pgp_write_struct_userid(pgp_output_t *, const char *);
unsigned pgp_write_ss_header(pgp_output_t *, size_t, pgp_content_enum);
unsigned pgp_write_struct_seckey_ptag(const pgp_seckey_t *key,
const uint8_t *passphrase,
const size_t pplen,
pgp_output_t *output,
pgp_content_enum ptag);
const uint8_t *passphrase,
const size_t pplen,
pgp_output_t *output,
pgp_content_enum ptag);
unsigned pgp_write_struct_seckey(const pgp_seckey_t *,
const uint8_t *,
const size_t,
pgp_output_t *);
const uint8_t *,
const size_t,
pgp_output_t *);
unsigned pgp_write_struct_pubkey(pgp_output_t *, const pgp_pubkey_t *);
unsigned pgp_write_one_pass_sig(pgp_output_t *,
const pgp_seckey_t *,
const pgp_hash_alg_t,
const pgp_sig_type_t);
const pgp_seckey_t *,
const pgp_hash_alg_t,
const pgp_sig_type_t);
unsigned pgp_write_litdata(pgp_output_t *,
const uint8_t *,
const int,
const pgp_litdata_enum);
const uint8_t *,
const int,
const pgp_litdata_enum);
pgp_pk_sesskey_t *pgp_create_pk_sesskey(pgp_key_t *, const char *, pgp_pk_sesskey_t *);
unsigned pgp_write_pk_sesskey(pgp_output_t *, pgp_pk_sesskey_t *);
unsigned pgp_write_xfer_key(pgp_output_t *output,
@ -104,18 +104,24 @@ unsigned pgp_write_xfer_key(pgp_output_t *output,
const unsigned armoured);
void pgp_fast_create_userid(uint8_t **, uint8_t *);
unsigned pgp_write_userid(const uint8_t *, pgp_output_t *);
unsigned pgp_write_userid(const char *, pgp_output_t *);
#ifdef HAVE_OPENSSL
void pgp_fast_create_rsa_pubkey(pgp_pubkey_t *, time_t, BIGNUM *, BIGNUM *);
unsigned pgp_write_rsa_pubkey(time_t, const BIGNUM *, const BIGNUM *,
pgp_output_t *);
pgp_output_t *);
void pgp_fast_create_rsa_seckey(pgp_seckey_t *, time_t, BIGNUM *,
BIGNUM *, BIGNUM *, BIGNUM *,
BIGNUM *, BIGNUM *);
BIGNUM *, BIGNUM *, BIGNUM *,
BIGNUM *, BIGNUM *);
#endif
unsigned encode_m_buf(const uint8_t *, size_t, const pgp_pubkey_t *,
uint8_t *);
uint8_t *);
unsigned pgp_fileread_litdata(const char *, const pgp_litdata_enum,
pgp_output_t *);
pgp_output_t *);
unsigned pgp_write_symm_enc_data(const uint8_t *, const int,
pgp_output_t *);
pgp_output_t *);
#endif /* CREATE_H_ */

+ 17
- 0
include/netpgp/crypto.h View File

@ -53,6 +53,7 @@
#ifndef CRYPTO_H_
#define CRYPTO_H_
#include "config.h"
#include "keyring.h"
#include "packet.h"
#include "memory.h"
@ -62,6 +63,12 @@
#include <openssl/dsa.h>
#endif
#ifdef HAVE_GSKSSL
#include <gskcms.h>
#include <csfbext.h>
#include <csnpdefs.h>
#endif
#define PGP_MIN_HASH_SIZE 16
/** pgp_hash_t */
@ -73,6 +80,9 @@ struct pgp_hash_t {
void (*add)(pgp_hash_t *, const uint8_t *, unsigned);
unsigned (*finish)(pgp_hash_t *, uint8_t *);
void *data; /* blob for data */
#ifdef HAVE_GSKSSL
CK_SESSION_HANDLE session;
#endif
};
/** pgp_crypt_t */
@ -176,8 +186,15 @@ void pgp_reader_pop_decrypt(pgp_stream_t *);
void pgp_reader_push_hash(pgp_stream_t *, pgp_hash_t *);
void pgp_reader_pop_hash(pgp_stream_t *);
#if HAVE_OPENSSL
int pgp_decrypt_decode_mpi(uint8_t *, unsigned, const BIGNUM *,
const BIGNUM *, const pgp_seckey_t *);
#endif
#if HAVE_GSKSSL
int pgp_decrypt_decode_mpi(uint8_t *, unsigned, const gsk_buffer,
const gsk_buffer, const pgp_seckey_t *);
#endif
unsigned pgp_rsa_encrypt_mpi(const uint8_t *, const size_t,
const pgp_pubkey_t *,


+ 98
- 27
include/netpgp/packet.h View File

@ -66,9 +66,10 @@
#include "types.h"
#include "errors.h"
#include "config.h"
#ifdef HAVE_GSKSSL
#define BIGNUM unsigned long
#include <gsktypes.h>
#endif
/* structure to keep track of printing state variables */
@ -379,11 +380,20 @@ typedef enum {
* \see RFC4880 5.5.2
*/
typedef struct {
BIGNUM *p; /* DSA prime p */
BIGNUM *q; /* DSA group order q */
BIGNUM *g; /* DSA group generator g */
BIGNUM *y; /* DSA public key value y (= g^x mod p
* with x being the secret) */
#ifdef HAVE_GSKSSL
gsk_buffer p; /* DSA prime p */
gsk_buffer q; /* DSA group order q */
gsk_buffer g; /* DSA group generator g */
gsk_buffer y; /* DSA public key value y (= g^x mod p
* with x being the secret) */
#endif
#ifdef HAVE_OPENSSL
BIGNUM *p; /* DSA prime p */
BIGNUM *q; /* DSA group order q */
BIGNUM *g; /* DSA group generator g */
BIGNUM *y; /* DSA public key value y (= g^x mod p
* with x being the secret) */
#endif
} pgp_dsa_pubkey_t;
/** Structure to hold an RSA public key.
@ -391,8 +401,14 @@ typedef struct {
* \see RFC4880 5.5.2
*/
typedef struct {
BIGNUM *n; /* RSA public modulus n */
BIGNUM *e; /* RSA public encryption exponent e */
#ifdef HAVE_GSKSSL
gsk_buffer n; /* RSA public modulus n */
gsk_buffer e; /* RSA public encryption exponent e */
#endif
#ifdef HAVE_OPENSSL
BIGNUM *n; /* RSA public modulus n */
BIGNUM *e; /* RSA public encryption exponent e */
#endif
} pgp_rsa_pubkey_t;
/** Structure to hold an ElGamal public key params.
@ -400,10 +416,18 @@ typedef struct {
* \see RFC4880 5.5.2
*/
typedef struct {
BIGNUM *p; /* ElGamal prime p */
BIGNUM *g; /* ElGamal group generator g */
BIGNUM *y; /* ElGamal public key value y (= g^x mod p
* with x being the secret) */
#ifdef HAVE_GSKSSL
gsk_buffer p; /* ElGamal prime p */
gsk_buffer g; /* ElGamal group generator g */
gsk_buffer y; /* ElGamal public key value y (= g^x mod p
* with x being the secret) */
#endif
#ifdef HAVE_OPENSSL
BIGNUM *p; /* ElGamal prime p */
BIGNUM *g; /* ElGamal group generator g */
BIGNUM *y; /* ElGamal public key value y (= g^x mod p
* with x being the secret) */
#endif
} pgp_elgamal_pubkey_t;
/** Version.
@ -438,20 +462,38 @@ typedef struct {
/** Structure to hold data for one RSA secret key
*/
typedef struct {
BIGNUM *d;
BIGNUM *p;
BIGNUM *q;
BIGNUM *u;
#ifdef HAVE_GSKSSL
gsk_buffer d;
gsk_buffer p;
gsk_buffer q;
gsk_buffer u;
#endif
#ifdef HAVE_OPENSSL
BIGNUM *d;
BIGNUM *p;
BIGNUM *q;
BIGNUM *u;
#endif
} pgp_rsa_seckey_t;
/** pgp_dsa_seckey_t */
typedef struct {
BIGNUM *x;
#ifdef HAVE_GSKSSL
gsk_buffer x;
#endif
#ifdef HAVE_OPENSSL
BIGNUM *x;
#endif
} pgp_dsa_seckey_t;
/** pgp_elgamal_seckey_t */
typedef struct {
BIGNUM *x;
#ifdef HAVE_GSKSSL
gsk_buffer x;
#endif
#ifdef HAVE_OPENSSL
BIGNUM *x;
#endif
} pgp_elgamal_seckey_t;
/** s2k_usage_t
@ -598,19 +640,36 @@ typedef enum {
/** Struct to hold params of an RSA signature */
typedef struct pgp_rsa_sig_t {
BIGNUM *sig; /* the signature value (m^d % n) */
#ifdef HAVE_GSKSSL
gsk_buffer sig; /* the signature value (m^d % n) */
#endif
#ifdef HAVE_OPENSSL
BIGNUM *sig; /* the signature value (m^d % n) */
#endif
} pgp_rsa_sig_t;
/** Struct to hold params of a DSA signature */
typedef struct pgp_dsa_sig_t {
BIGNUM *r; /* DSA value r */
BIGNUM *s; /* DSA value s */
#ifdef HAVE_GSKSSL
gsk_buffer r; /* DSA value r */
gsk_buffer s; /* DSA value s */
#endif
#ifdef HAVE_OPENSSL
BIGNUM *r; /* DSA value r */
BIGNUM *s; /* DSA value s */
#endif
} pgp_dsa_sig_t;
/** pgp_elgamal_signature_t */
typedef struct pgp_elgamal_sig_t {
BIGNUM *r;
BIGNUM *s;
#ifdef HAVE_GSKSSL
gsk_buffer r;
gsk_buffer s;
#endif
#ifdef HAVE_OPENSSL
BIGNUM *r;
BIGNUM *s;
#endif
} pgp_elgamal_sig_t;
#define PGP_KEY_ID_SIZE 8
@ -793,14 +852,26 @@ enum {
/** pgp_pk_sesskey_params_rsa_t */
typedef struct {
BIGNUM *encrypted_m;
BIGNUM *m;
#ifdef HAVE_GSKSSL
gsk_buffer encrypted_m;
gsk_buffer m;
#endif
#ifdef HAVE_OPENSSL
BIGNUM *encrypted_m;
BIGNUM *m;
#endif
} pgp_pk_sesskey_params_rsa_t;
/** pgp_pk_sesskey_params_elgamal_t */
typedef struct {
BIGNUM *g_to_k;
BIGNUM *encrypted_m;
#ifdef HAVE_GSKSSL
gsk_buffer g_to_k;
gsk_buffer encrypted_m;
#endif
#ifdef HAVE_OPENSSL
BIGNUM *g_to_k;
BIGNUM *encrypted_m;
#endif
} pgp_pk_sesskey_params_elgamal_t;
/** pgp_pk_sesskey_params_t */


+ 10
- 0
include/netpgp/writer.h View File

@ -59,6 +59,10 @@
#include "errors.h"
#include "keyring.h"
#ifdef HAVE_GSKSSL
#include <gsktypes.h>
#endif
/**
* \ingroup Writer
* the writer function prototype
@ -109,7 +113,13 @@ unsigned pgp_write(pgp_output_t *, const void *, unsigned);
unsigned pgp_write_length(pgp_output_t *, unsigned);
unsigned pgp_write_ptag(pgp_output_t *, pgp_content_enum);
unsigned pgp_write_scalar(pgp_output_t *, unsigned, unsigned);
#ifdef HAVE_GSKSSL
unsigned pgp_write_mpi(pgp_output_t *output, gsk_buffer bn);
#endif
#if HAVE_OPENSSL
unsigned pgp_write_mpi(pgp_output_t *, const BIGNUM *);
#endif
void pgp_writer_info_delete(pgp_writer_t *);
unsigned pgp_writer_info_finalise(pgp_error_t **, pgp_writer_t *);


+ 1
- 1
src/Makefile.am View File

@ -24,7 +24,7 @@ libnetpgp_la_SOURCES = \
bin_PROGRAMS = netpgp
netpgp_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/include -I$(top_srcdir)/include/netpgp -D_GNU_SOURCE
LDADD = $(GSKSSL_LIBS) $(ZLIB_FLAG)
LDADD = $(PKCS_LIBS) $(GSKSSL_LIBS) $(ZLIB_FLAG)
netpgp_LDFLAGS =
netpgp_SOURCES = \
main.c \


+ 595
- 581
src/create.c
File diff suppressed because it is too large
View File


+ 7
- 1
src/crypto.c View File

@ -89,11 +89,16 @@ __RCSID("$NetBSD$");
int
pgp_decrypt_decode_mpi(uint8_t *buf,
unsigned buflen,
#ifdef HAVE_OPENSSL
const BIGNUM *g_to_k,
const BIGNUM *encmpi,
#endif
#ifdef HAVE_GSKSSL
const gsk_buffer g_to_k,
const gsk_buffer encmpi,
#endif
const pgp_seckey_t *seckey)
{
#ifdef HAVE_OPENSSL_BN_H
unsigned mpisize;
uint8_t encmpibuf[NETPGP_BUFSIZ];
uint8_t mpibuf[NETPGP_BUFSIZ];
@ -101,6 +106,7 @@ pgp_decrypt_decode_mpi(uint8_t *buf,
int i;
int n;
#ifdef HAVE_OPENSSL
mpisize = (unsigned)BN_num_bytes(encmpi);
/* MPI can't be more than 65,536 */
if (mpisize > sizeof(encmpibuf)) {


+ 202
- 20
src/gskssl_crypto.c View File

@ -8,12 +8,17 @@
#include <stdlib.h>
#include <string.h>
#include <csnpdefs.h>
#include <gskcms.h>
#include <csfbext.h>
#include <dlfcn.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <strings.h>
#include "crypto.h"
#include "keyring.h"
#include "readerwriter.h"
@ -28,6 +33,37 @@
#define SHA384_DIGEST_LENGTH 48
#define MD5_DIGEST_LENGTH 16
CK_FUNCTION_LIST *funcs;
CK_RV getFunctionList(void)
{
CK_RV rc;
CK_RV (*pFunc)();
void *d;
#ifdef _LP64
char e[]="CSNPCA64";
#elif __XPLINK__ /* @P1C */
char e[]="CSNPCA3X"; /* @D2A */
#else
char e[]="CSNPCAPI";
#endif
d = dlopen(e,RTLD_NOW);
if ( d == NULL ) {
fprintf(stderr, "%s not found in linklist or LIBPATH\n",e); // @D1A
return !CKR_OK;
}
pFunc = (CK_RV (*)())dlsym(d,"C_GetFunctionList");
if (pFunc == NULL ) {
fprintf(stderr, "C_GetFunctionList() not found in module %s\n",e); // @D1A
return !CKR_OK;
}
rc = pFunc(&funcs);
if (rc != CKR_OK) {
return !CKR_OK;
}
return CKR_OK;
}
static int
md5_init(pgp_hash_t *hash)
{
@ -66,21 +102,128 @@ pgp_hash_md5(pgp_hash_t *hash)
*hash = md5;
}
CK_RV getSession(CK_SESSION_HANDLE *session)
{
static CK_BBOOL true = TRUE, tokenPresent = TRUE;
CK_ULONG snum;
CK_SLOT_ID_PTR pSlotList;
CK_TOKEN_INFO info;
CK_SLOT_ID slot;
CK_FLAGS flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
CK_RV rv = CKR_OK;
CK_C_INITIALIZE_ARGS cinit_args;
if (!__isPosixOn()) {
fprintf(stderr, "Must be run with POSIX(ON)!\n");
return 0;
}
rv = getFunctionList();
if (rv != CKR_OK) {
fprintf(stderr, "getFunctionList failed!\n");
return rv;
}
rv = funcs->C_Initialize(NULL);
if( rv != CKR_OK ) {
if( rv != CKR_CRYPTOKI_ALREADY_INITIALIZED ) {
fprintf(stderr, "C_Initialize: rv = 0x%.8X\n", rv);
}
}
rv = funcs->C_GetSlotList(tokenPresent, NULL, &snum);
if( rv != CKR_OK ) {
fprintf(stderr, "C_GetSlotList: rv = 0x%.8X\n", rv);
return rv;
}
pSlotList = (CK_SLOT_ID *)malloc(snum * sizeof(CK_SLOT_ID));
rv = funcs->C_GetSlotList(tokenPresent, pSlotList, &snum);
if( rv != CKR_OK ) {
fprintf(stderr, "C_GetSlotList: rv = 0x%.8X\n", rv);
return rv;
}
rv = funcs->C_GetTokenInfo(pSlotList[0], &info);
if( rv != CKR_OK ) {
fprintf(stderr, "C_GetTokenInfo: rv = 0x%.8X\n", rv);
return rv;
}
rv = funcs->C_OpenSession(pSlotList[0], flags, NULL, NULL, session);
if( rv != CKR_OK ) {
fprintf(stderr, "C_OpenSession: rv = 0x%.8X\n", rv);
return rv;
}
free(pSlotList);
return rv;
}
static int
sha1_init(pgp_hash_t *hash)
{
CK_RV rv = CKR_OK;
CK_MECHANISM mechanism;
rv = getSession(&hash->session);
if( rv != CKR_OK) {
fprintf(stderr, "getSession: rv = 0x%.8X\n", rv);
}
mechanism.mechanism = CKM_SHA_1;
mechanism.pParameter = NULL_PTR;
mechanism.ulParameterLen = 0;
rv = funcs->C_DigestInit(hash->session, &mechanism);
if( rv != CKR_OK) {
fprintf(stderr, "C_DigestInit: rv = 0x%.8X\n", rv);
}
return 1;
}
static void
sha1_add(pgp_hash_t *hash, const uint8_t *data, unsigned length)
{
CK_RV rv;
if (pgp_get_debug_level(__FILE__)) {
hexdump(stderr, "sha1_add", data, length);
}
rv = funcs->C_DigestUpdate(hash->session, (CK_BYTE_PTR)data, length);
if( rv != CKR_OK) {
fprintf(stderr, "C_DigestUpdate: rv = 0x%.8X\n", rv);
}
}
static unsigned
sha1_finish(pgp_hash_t *hash, uint8_t *out)
{
return PGP_SHA1_HASH_SIZE;
CK_RV rv;
CK_ULONG len = hash->size;
rv = funcs->C_DigestFinal(hash->session, out, &len);
if( rv != CKR_OK) {
fprintf(stderr, "C_DigestFinal: rv = 0x%.8X\n", rv);
}
if (pgp_get_debug_level(__FILE__)) {
hexdump(stderr, "sha1_finish", out, len);
}
free(hash->data);
hash->data = NULL;
rv = funcs->C_CloseSession( hash->session );
if( rv != CKR_OK) {
fprintf(stderr, "C_CloseSession: rv = 0x%.8X\n", rv);
}
rv = funcs->C_Finalize(NULL);
if( rv != CKR_OK) {
fprintf(stderr, "C_Finalize: rv = 0x%.8X\n", rv);
}
return len;
}
static const pgp_hash_t sha1 = {
@ -291,7 +434,56 @@ pgp_rsa_private_encrypt(uint8_t *out,
const pgp_rsa_seckey_t *seckey,
const pgp_rsa_pubkey_t *pubkey)
{
int n;
int n = NETPGP_BUFSIZ;
CK_SESSION_HANDLE session;
CK_RV rv = CKR_OK;
CK_OBJECT_HANDLE key;
CK_ULONG len = NETPGP_BUFSIZ;
CK_KEY_TYPE keyType = CKK_RSA;
CK_OBJECT_CLASS keyClass = CKO_PUBLIC_KEY;
CK_MECHANISM mechanism = {
CKM_RSA_X_509, 0, 0
};
CK_ATTRIBUTE template[] = {
{CKA_CLASS, &keyClass, sizeof(keyClass) },
{CKA_KEY_TYPE, &keyType, sizeof(keyType) },
{CKA_MODULUS, pubkey->n.data, pubkey->n.length },
{CKA_PUBLIC_EXPONENT, pubkey->e.data, pubkey->e.length }
};
rv = getSession(&session);
if( rv != CKR_OK) {
fprintf(stderr, "getSession: rv = 0x%.8X\n", rv);
}
rv = funcs->C_CreateObject(session, &template, 4, &key);
if( rv != CKR_OK) {
fprintf(stderr, "C_CreateObject: rv = 0x%.8X\n", rv);
}
rv = funcs->C_EncryptInit(session, &mechanism, key);
if( rv != CKR_OK) {
fprintf(stderr, "C_EncryptInit: rv = 0x%.8X\n", rv);
}
rv = funcs->C_Encrypt(session,in,length,out,&len);
if( rv != CKR_OK) {
fprintf(stderr, "C_Encrypt: rv = 0x%.8X\n", rv);
}
rv = funcs->C_CloseSession( session );
if( rv != CKR_OK) {
fprintf(stderr, "C_CloseSession: rv = 0x%.8X\n", rv);
}
rv = funcs->C_Finalize(NULL);
if( rv != CKR_OK) {
fprintf(stderr, "C_Finalize: rv = 0x%.8X\n", rv);
}
return n;
}
@ -383,14 +575,6 @@ pgp_text_from_hash(pgp_hash_t *hash)
return hash->name;
}
unsigned long *gsk_buffer_dup(gsk_buffer buf)
{
unsigned long *ret;
ret=malloc(buf.length);
memcpy(ret,buf.data,buf.length);
return ret;
}
/**
\ingroup HighLevel_KeyGenerate
\brief Generates an RSA keypair
@ -416,7 +600,6 @@ pgp_rsa_generate_keypair(pgp_key_t *keydata,
x509_algorithm_type key_algorithm = x509_alg_rsaEncryption;
//int key_size = pplen;
int key_size = 2048;
gsk_buffer *key_params = NULL;
x509_public_key_info public_key;
pkcs_private_key_info private_key;
@ -431,7 +614,7 @@ pgp_rsa_generate_keypair(pgp_key_t *keydata,
//status = gsk_generate_key_pair(key_algorithm, key_size, key_params, public_key,
// private_key, key_identifier);
status = gsk_generate_key_pair(key_algorithm, key_size, NULL, &public_key, &private_key, key_identifier);
status = gsk_generate_key_pair(key_algorithm, numbits, NULL, &public_key, &private_key, key_identifier);
/* populate pgp key from ssl key */
@ -442,8 +625,8 @@ pgp_rsa_generate_keypair(pgp_key_t *keydata,
status = gsk_factor_private_key_rsa(&private_key, &_n, &_e, &_d, &_p, &_q, &_dmp, &_dmq, &_u);
seckey->pubkey.key.rsa.n = gsk_buffer_dup(_n);
seckey->pubkey.key.rsa.e = gsk_buffer_dup(_e);
gsk_copy_buffer(&_n,&seckey->pubkey.key.rsa.n);
gsk_copy_buffer(&_e,&seckey->pubkey.key.rsa.e);;
/* seckey->s2k_usage = PGP_S2KU_ENCRYPTED_AND_HASHED; */
seckey->s2k_usage = PGP_S2KU_NONE;
@ -456,11 +639,11 @@ pgp_rsa_generate_keypair(pgp_key_t *keydata,
seckey->octetc = 0;
seckey->checksum = 0;
seckey->key.rsa.d = gsk_buffer_dup(_d);
seckey->key.rsa.p = gsk_buffer_dup(_p);
seckey->key.rsa.q = gsk_buffer_dup(_q);
seckey->key.rsa.u = gsk_buffer_dup(_u);
if (seckey->key.rsa.u == NULL) {
gsk_copy_buffer(&_d,&seckey->key.rsa.d);
gsk_copy_buffer(&_p,&seckey->key.rsa.p);
gsk_copy_buffer(&_q,&seckey->key.rsa.q);
gsk_copy_buffer(&_u,&seckey->key.rsa.u);
if (seckey->key.rsa.u.length == 0) {
(void) fprintf(stderr, "seckey->key.rsa.u is NULL\n");
return 0;
}
@ -532,7 +715,6 @@ pgp_rsa_new_selfsign_key(const int numbits,
const char *cipher)
{
pgp_key_t *keydata;
keydata = pgp_keydata_new();
if (!pgp_rsa_generate_keypair(keydata, numbits, e, hashalg, cipher,
(const uint8_t *) "", (const size_t) 0) ||


+ 11
- 10
src/keyring.c View File

@ -870,6 +870,7 @@ pgp_add_selfsigned_userid(pgp_key_t *skey, pgp_key_t *pkey, const uint8_t *useri
sigpacket.raw = pgp_mem_data(mem_sig);
/* add user id and signature to key */
printf("%s...\n",__FUNCTION__);
pgp_update_userid(skey, userid, &sigpacket, &sig->sig.info);
if(pkey) pgp_update_userid(pkey, userid, &sigpacket, &sig->sig.info);
@ -1453,6 +1454,7 @@ unsigned pgp_add_directsig(
const pgp_subpacket_t *sigpkt,
pgp_sig_info_t *siginfo)
{
printf("%s...\n",__FUNCTION__);
pgp_directsig_t *directsigp;
unsigned directsigidx;
@ -1469,12 +1471,10 @@ unsigned pgp_add_directsig(
}
}
EXPAND_ARRAY(key, directsig);
directsigp = &key->directsigs[key->directsigc++];
copy_sig_info(&directsigp->siginfo,
siginfo);
copy_sig_info(&directsigp->siginfo, siginfo);
pgp_copy_packet(&directsigp->packet, sigpkt);
return 0;
@ -1486,16 +1486,17 @@ unsigned pgp_update_userid(
const pgp_subpacket_t *sigpkt,
pgp_sig_info_t *siginfo)
{
unsigned uididx = 0;
unsigned uidsigidx = 0;
uint8_t **uidp;
pgp_uidsig_t *uidsigp;
printf("%s...\n",__FUNCTION__);
unsigned uididx = 0;
unsigned uidsigidx = 0;
unsigned char **uidp;
pgp_uidsig_t *uidsigp;
/* Try to find identical userID */
uidp = key->uids;
for (uididx = 0 ; uididx < key->uidc; uididx++, uidp++)
{
if (strcmp((char *)*uidp, (char *)userid) == 0)
if (strcmp(*uidp, userid) == 0)
{
/* Found one. check for duplicate uidsig */
uidsigp = key->uidsigs;
@ -1542,6 +1543,7 @@ unsigned pgp_update_subkey(
const pgp_subpacket_t *sigpkt,
pgp_sig_info_t *siginfo)
{
printf("%s...\n",__FUNCTION__);
unsigned subkeyidx = 0;
unsigned subkeysigidx = 0;
pgp_subkey_t *subkeyp;
@ -1607,8 +1609,7 @@ unsigned pgp_update_subkey(
subkeysigp->subkey = subkeyidx;
/* store sig info and packet */
copy_sig_info(&subkeysigp->siginfo,
siginfo);
copy_sig_info(&subkeysigp->siginfo, siginfo);
pgp_copy_packet(&subkeysigp->packet, sigpkt);
return 0;


+ 2
- 1
src/main.c View File

@ -315,7 +315,7 @@ int pgp_generate_keypair(netpgp_t *netpgp, prog_t *p, char *f)
newseckey = pgp_keydata_new();
newpubkey = pgp_keydata_new();
if (!pgp_rsa_generate_keypair(newseckey, 4096, 65537UL, hashalg, cipher, (const uint8_t *) "foo", (const size_t) 3)) return 0;
if (!pgp_rsa_generate_keypair(newseckey, 1024, 65537UL, hashalg, cipher, (const uint8_t *) "foo", (const size_t) 3)) return 0;
/* make a public key out of generated secret key */
if((newpubkey = pgp_ensure_pubkey(netpgp->pubring, &newseckey->key.seckey.pubkey, newseckey->pubkeyid))==NULL) return 0;
if (!pgp_add_selfsigned_userid(newseckey, newpubkey, (uint8_t *)newid, 365*24*3600)) return 0;
@ -324,6 +324,7 @@ int pgp_generate_keypair(netpgp_t *netpgp, prog_t *p, char *f)
// save rings
if(!netpgp_save_pubring(netpgp))
fprintf(stderr, "Couln't store pubring\n");
if(!netpgp_save_secring(netpgp))
fprintf(stderr, "Couln't store secring\n");


+ 28
- 1
src/misc.c View File

@ -323,10 +323,10 @@ hash_string(pgp_hash_t *hash, const uint8_t *buf, uint32_t len)
}
/* hash a bignum, possibly padded - first length, then string itself */
#ifdef HAVE_OPENSSL
static int
hash_bignum(pgp_hash_t *hash, BIGNUM *bignum)
{
#ifdef HAVE_OPENSSL_BN_H
uint8_t *bn;
size_t len;
int padbyte;
@ -349,8 +349,31 @@ hash_bignum(pgp_hash_t *hash, BIGNUM *bignum)
hash_string(hash, bn + 1 - padbyte, (unsigned)(len + padbyte));
free(bn);
return (int)(sizeof(len) + len + padbyte);
}
#endif
#ifdef HAVE_GSKSSL
static int
hash_bignum(pgp_hash_t *hash, gsk_buffer buf)
{
unsigned char *bn;
size_t len;
int padbyte;
int i;
bn=malloc(buf.length+1);
// filling the data into the char array
for(i=0; i<buf.length; i++) {
bn[i+1]=((char*)buf.data)[i];
}
bn[0] = 0x0;
padbyte = (bn[1] & 0x80) ? 1 : 0;
hash_string(hash, bn + 1 - padbyte, (unsigned)(len + padbyte));
free(bn);
return (int)(sizeof(len) + len + padbyte);
}
#endif
/** \file
*/
@ -826,6 +849,7 @@ pgp_memory_clear(pgp_memory_t *mem)
void
pgp_memory_release(pgp_memory_t *mem)
{
#ifdef HAVE_OPENSSL
if (mem->mmapped) {
(void) munmap(mem->buf, mem->length);
} else {
@ -833,6 +857,7 @@ pgp_memory_release(pgp_memory_t *mem)
}
mem->buf = NULL;
mem->length = 0;
#endif
}
void
@ -888,7 +913,9 @@ void
pgp_memory_free(pgp_memory_t *mem)
{
pgp_memory_release(mem);
#ifdef HAVE_OPENSSL
free(mem);
#endif
}
/**


+ 46
- 5
src/packet-parse.c View File

@ -70,6 +70,10 @@ __RCSID("$NetBSD$");
#include <openssl/cast.h>
#endif
#ifdef HAVE_GSKSSL
#include <gskcms.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
@ -684,7 +688,12 @@ limited_read_time(time_t *dest, pgp_region_t *region,
* \see RFC4880 3.2
*/
static int
#ifdef HAVE_OPENSSL
limread_mpi(BIGNUM **pbn, pgp_region_t *region, pgp_stream_t *stream)
#endif
#ifdef HAVE_GSKSSL
limread_mpi(gsk_buffer *pbn, pgp_region_t *region, pgp_stream_t *stream)
#endif
{
uint8_t buf[NETPGP_BUFSIZ] = "";
/* an MPI has a 2 byte length part.
@ -733,12 +742,20 @@ limread_mpi(BIGNUM **pbn, pgp_region_t *region, pgp_stream_t *stream)
* draft says. -- peter */
return 0;
}
#ifdef HAVE_OPENSSL_BN_H
#ifdef HAVE_OPENSSL
*pbn = BN_bin2bn(buf, (int)length, NULL);
#endif
#ifdef HAVE_GSKSSL
pbn->length=length;
pbn->data=malloc(length);
memcpy(pbn->data,buf,length);
#endif
printf("%s reached end\n",__FUNCTION__);
return 1;
}
static unsigned read_new_length(unsigned *, pgp_stream_t *);
/* allocate space, read, and stash data away in a virtual pkt */
@ -969,28 +986,44 @@ cmd_get_passphrase_free(pgp_seckey_passphrase_t *skp)
\ingroup Core_Create
\brief Free allocated memory
*/
#ifdef HAVE_OPENSSL
static void
free_BN(BIGNUM **pp)
{
#ifdef HAVE_OPENSSL_BN_H
BN_free(*pp);
*pp = NULL;
}
#endif
#ifdef HAVE_GSKSSL
static void
free_BN(gsk_buffer *buf)
{
gsk_free_buffer(buf);
}
#endif
#ifdef HAVE_OPENSSL
static void
dup_BN(BIGNUM **dst, const BIGNUM *src)
{
#ifdef HAVE_OPENSSL_BN_H
*dst = BN_dup(src);
}
#endif
#ifdef HAVE_GSKSSL
static void
dup_BN(gsk_buffer *dst, gsk_buffer src)
{
gsk_copy_buffer(&src,dst);
}
#endif
void
copy_sig_info(pgp_sig_info_t *dst, const pgp_sig_info_t *src)
{
printf("%s...\n",__FUNCTION__);
(void) memcpy(dst, src, sizeof(*src));
printf("%s: src->v4_hashlen %d\n",__FUNCTION__,src->v4_hashlen);
if ((dst->v4_hashed = calloc(1, src->v4_hashlen)) == NULL) {
(void) fprintf(stderr, "copy_sig_info: bad alloc\n");
} else {
@ -2141,8 +2174,8 @@ parse_v4_sig(pgp_region_t *region, pgp_stream_t *stream)
return 0;
}
pkt.u.sig.info.v4_hashlen = stream->readinfo.alength
- pkt.u.sig.v4_hashstart;
pkt.u.sig.info.v4_hashlen = stream->readinfo.alength - pkt.u.sig.v4_hashstart;
printf("%s pkt.u.sig.info.v4_hashlen %d\n",__FUNCTION__,pkt.u.sig.info.v4_hashlen);
if (pgp_get_debug_level(__FILE__)) {
fprintf(stderr, "v4_hashlen=%zd\n", pkt.u.sig.info.v4_hashlen);
}
@ -2974,8 +3007,14 @@ parse_pk_sesskey(pgp_region_t *region,
uint8_t c = 0x0;
uint8_t cs[2];
unsigned k;
#ifdef HAVE_OPENSSL
BIGNUM *g_to_k;
BIGNUM *enc_m;
#endif
#ifdef HAVE_GSKSSL
gsk_buffer g_to_k;
gsk_buffer enc_m;
#endif
int n;
uint8_t unencoded_m_buf[1024];
@ -3008,7 +3047,9 @@ parse_pk_sesskey(pgp_region_t *region,
return 0;
}
enc_m = pkt.u.pk_sesskey.params.rsa.encrypted_m;
#ifdef HAVE_OPENSSL
g_to_k = NULL;
#endif
break;
case PGP_PKA_DSA:


+ 44
- 4
src/signature.c View File

@ -153,7 +153,6 @@ rsa_sign(pgp_hash_t *hash,
const pgp_rsa_seckey_t *secrsa,
pgp_output_t *out)
{
#ifdef HAVE_OPENSSL_BN_H
unsigned prefixsize;
unsigned expected;
unsigned hashsize;
@ -163,7 +162,12 @@ rsa_sign(pgp_hash_t *hash,
uint8_t hashbuf[NETPGP_BUFSIZ];
uint8_t sigbuf[NETPGP_BUFSIZ];
uint8_t *prefix;
BIGNUM *bn;
#ifdef HAVE_OPENSSL
BIGNUM *bn;
#endif
#ifdef HAVE_GSKSSL
gsk_buffer bn;
#endif
if (strcmp(hash->name, "SHA1") == 0) {
hashsize = PGP_SHA1_HASH_SIZE + sizeof(prefix_sha1);
@ -181,7 +185,13 @@ rsa_sign(pgp_hash_t *hash,
prefixsize = sizeof(prefix_sha512);
expected = PGP_SHA512_HASH_SIZE;
}
#ifdef HAVE_OPENSSL
keysize = (BN_num_bits(pubrsa->n) + 7) / 8;
#endif
#ifdef HAVE_GSKSSL
keysize = ((pubrsa->n.length*8) + 7) / 8;
#endif
if (keysize > sizeof(hashbuf)) {
(void) fprintf(stderr, "rsa_sign: keysize too big\n");
return 0;
@ -217,8 +227,16 @@ rsa_sign(pgp_hash_t *hash,
}
t = pgp_rsa_private_encrypt(sigbuf, hashbuf, keysize, secrsa, pubrsa);
#ifdef HAVE_OPENSSL
bn = BN_bin2bn(sigbuf, (int)t, NULL);
#endif
#ifdef HAVE_GSKSSL
bn.data=malloc(NETPGP_BUFSIZ);
bn.length=NETPGP_BUFSIZ;
memcpy(bn.data,sigbuf,NETPGP_BUFSIZ);
#endif
pgp_write_mpi(out, bn);
#ifdef HAVE_OPENSSL
BN_free(bn);
#endif
return 1;
@ -275,7 +293,7 @@ rsa_verify(pgp_hash_alg_t type,
const pgp_rsa_sig_t *sig,
const pgp_rsa_pubkey_t *pubrsa)
{
#ifdef HAVE_OPENSSL_BN_H
printf("%s\n",__FUNCTION__);
const uint8_t *prefix;
unsigned n;
unsigned keysize;
@ -284,12 +302,19 @@ rsa_verify(pgp_hash_alg_t type,
uint8_t sigbuf[NETPGP_BUFSIZ];
uint8_t hashbuf_from_sig[NETPGP_BUFSIZ];
#ifdef HAVE_OPENSSL
keysize = BN_num_bytes(pubrsa->n);
#endif
#ifdef HAVE_GSKSSL
keysize = pubrsa->n.length;
#endif
/* RSA key can't be bigger than 65535 bits, so... */
if (keysize > sizeof(hashbuf_from_sig)) {
(void) fprintf(stderr, "rsa_verify: keysize too big\n");
return 0;
}
#ifdef HAVE_OPENSSL
if ((unsigned) BN_num_bits(sig->sig) > 8 * sizeof(sigbuf)) {
(void) fprintf(stderr, "rsa_verify: BN_numbits too big\n");
return 0;
@ -298,6 +323,12 @@ rsa_verify(pgp_hash_alg_t type,
n = pgp_rsa_public_decrypt(hashbuf_from_sig, sigbuf,
(unsigned)(BN_num_bits(sig->sig) + 7) / 8, pubrsa);
#endif
#ifdef HAVE_GSKSSL
memcpy(sigbuf,sig->sig.data,sig->sig.length);
n = pgp_rsa_public_decrypt(hashbuf_from_sig, sigbuf,
(unsigned)((sig->sig.length*8) + 7) / 8, pubrsa);
#endif
debug_len_decrypted = n;
if (n != keysize) {
@ -355,7 +386,6 @@ rsa_verify(pgp_hash_alg_t type,
}
return (memcmp(&hashbuf_from_sig[n], prefix, plen) == 0 &&
memcmp(&hashbuf_from_sig[n + plen], hash, hash_length) == 0);
#endif
}
static void
@ -769,14 +799,24 @@ pgp_write_sig(pgp_output_t *output,
case PGP_PKA_RSA:
case PGP_PKA_RSA_ENCRYPT_ONLY:
case PGP_PKA_RSA_SIGN_ONLY:
#ifdef HAVE_OPENSSL
if (seckey->key.rsa.d == NULL) {
#endif
#ifdef HAVE_GSKSSL
if (seckey->key.rsa.d.length == 0) {
#endif
(void) fprintf(stderr, "pgp_write_sig: null rsa.d\n");
return 0;
}
break;
case PGP_PKA_DSA:
#ifdef HAVE_OPENSSL
if (seckey->key.dsa.x == NULL) {
#endif
#ifdef HAVE_GSKSSL
if (seckey->key.dsa.x.length == 0) {
#endif
(void) fprintf(stderr, "pgp_write_sig: null dsa.x\n");
return 0;
}


+ 671
- 669
src/validate.c
File diff suppressed because it is too large
View File


+ 19
- 8
src/writer.c View File

@ -86,7 +86,6 @@ __RCSID("$NetBSD$");
#include "version.h"
#include "netpgpdigest.h"
/*
* return 1 if OK, otherwise 0
*/
@ -139,23 +138,36 @@ pgp_write_scalar(pgp_output_t *output, unsigned n, unsigned len)
* \param output
* \return 1 if OK, otherwise 0
*/
unsigned
#ifdef HAVE_OPENSSL
pgp_write_mpi(pgp_output_t *output, const BIGNUM *bn)
#endif
#ifdef HAVE_GSKSSL
pgp_write_mpi(pgp_output_t *output, gsk_buffer bn)
#endif
{
#ifdef HAVE_OPENSSL_BN_H
unsigned bits;
uint8_t buf[NETPGP_BUFSIZ];
unsigned bits;
uint8_t buf[NETPGP_BUFSIZ];
memset(buf,0,NETPGP_BUFSIZ);
#ifdef HAVE_GSKSSL
bits = (bn.length*8)-1;
#endif
#ifdef HAVE_OPENSSL
bits = (unsigned)BN_num_bits(bn);
#endif
if (bits > 65535) {
(void) fprintf(stderr, "pgp_write_mpi: too large %u\n", bits);
return 0;
}
#ifdef HAVE_OPENSSL
BN_bn2bin(bn, buf);
#endif
#ifdef HAVE_GSKSSL
memcpy(buf,bn.data,bn.length);
#endif
return pgp_write_scalar(output, bits, 2) &&
pgp_write(output, buf, (bits + 7) / 8);
#endif
}
/**
@ -336,8 +348,7 @@ pgp_writer_pop(pgp_output_t *output)
unsigned
pgp_writer_close(pgp_output_t *output)
{
unsigned ret;
unsigned ret;
ret = pgp_writer_info_finalise(&output->errors, &output->writer);
pgp_writer_info_delete(&output->writer);
return ret;


Loading…
Cancel
Save