From bd351261ec4c1be9f7da2a7d7488ea3e3c072738 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Mon, 20 Feb 2017 11:09:20 +0100 Subject: [PATCH 1/9] Initial idea of openssl-1.1.0 compatibility (still missing some magic around certificates) --- tool/Makefile.am | 2 +- tool/openssl-compat.c | 53 ++++++++++++++++++++++++++++++++++++++++++ tool/openssl-compat.h | 27 +++++++++++++++++++++ tool/util.c | 9 ++++--- tool/yubico-piv-tool.c | 4 ++-- 5 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 tool/openssl-compat.c create mode 100644 tool/openssl-compat.h diff --git a/tool/Makefile.am b/tool/Makefile.am index 4a1657b..aa024a0 100644 --- a/tool/Makefile.am +++ b/tool/Makefile.am @@ -40,7 +40,7 @@ noinst_LTLIBRARIES = libpiv_cmd.la libpiv_util.la libpiv_cmd_la_SOURCES = cmdline.ggo cmdline.c cmdline.h libpiv_cmd_la_CFLAGS = -libpiv_util_la_SOURCES = util.c util.h +libpiv_util_la_SOURCES = util.c util.h openssl-compat.c libpiv_util_la_LIBADD = $(top_builddir)/lib/libykpiv.la $(OPENSSL_LIBS) cmdline.c cmdline.h: cmdline.ggo Makefile.am $(top_srcdir)/configure.ac diff --git a/tool/openssl-compat.c b/tool/openssl-compat.c new file mode 100644 index 0000000..b8b274b --- /dev/null +++ b/tool/openssl-compat.c @@ -0,0 +1,53 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#if OPENSSL_VERSION_NUMBER < 0x10100000L + +#include +#include + + +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) +{ + /* If the fields n and e in r are NULL, the corresponding input + * parameters MUST be non-NULL for n and e. d may be + * left NULL (in case only the public key is used). + */ + if ((r->n == NULL && n == NULL) + || (r->e == NULL && e == NULL)) + return 0; + + if (n != NULL) { + BN_free(r->n); + r->n = n; + } + if (e != NULL) { + BN_free(r->e); + r->e = e; + } + if (d != NULL) { + BN_free(r->d); + r->d = d; + } + + return 1; +} + +void RSA_get0_key(const RSA *r, + const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) +{ + if (n != NULL) + *n = r->n; + if (e != NULL) + *e = r->e; + if (d != NULL) + *d = r->d; +} + +#endif /* OPENSSL_VERSION_NUMBER */ diff --git a/tool/openssl-compat.h b/tool/openssl-compat.h new file mode 100644 index 0000000..d5e9f7a --- /dev/null +++ b/tool/openssl-compat.h @@ -0,0 +1,27 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef LIBCRYPTO_COMPAT_H +#define LIBCRYPTO_COMPAT_H + +#if OPENSSL_VERSION_NUMBER < 0x10100000L + +#include +#include +#include +#include +#include + +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d); +void RSA_get0_key(const RSA *r, + const BIGNUM **n, const BIGNUM **e, const BIGNUM **d); + +#endif /* OPENSSL_VERSION_NUMBER */ +#endif /* LIBCRYPTO_COMPAT_H */ + diff --git a/tool/util.c b/tool/util.c index baea3f1..678de85 100644 --- a/tool/util.c +++ b/tool/util.c @@ -37,6 +37,7 @@ #include #endif +#include "openssl-compat.h" #include #include #include @@ -61,7 +62,7 @@ FILE *open_file(const char *file_name, int mode) { } unsigned char get_algorithm(EVP_PKEY *key) { - int type = EVP_PKEY_type(key->type); + int type = EVP_PKEY_type(EVP_PKEY_id(key)); switch(type) { case EVP_PKEY_RSA: { @@ -641,15 +642,17 @@ int SSH_write_X509(FILE *fp, X509 *x) { case EVP_PKEY_RSA2: { RSA *rsa; unsigned char n[256]; + const BIGNUM *bn_n; char rsa_id[] = "\x00\x00\x00\x07ssh-rsa"; char rsa_f4[] = "\x00\x00\x00\x03\x01\x00\x01"; rsa = EVP_PKEY_get1_RSA(pkey); + RSA_get0_key(rsa, &bn_n, NULL, NULL); - set_component(n, rsa->n, RSA_size(rsa)); + set_component(n, bn_n, RSA_size(rsa)); - uint32_t bytes = BN_num_bytes(rsa->n); + uint32_t bytes = BN_num_bytes(bn_n); char len_buf[5]; int len = 4; diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index 757e1c4..5bf5df4 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -42,6 +42,7 @@ #include #endif +#include "openssl-compat.h" #include #include #include @@ -234,8 +235,7 @@ static bool generate_key(ykpiv_state *state, const char *slot, goto generate_out; } - rsa->n = bignum_n; - rsa->e = bignum_e; + RSA_set0_key(rsa, bignum_n, bignum_e, NULL); EVP_PKEY_set1_RSA(public_key, rsa); } else if(algorithm == algorithm_arg_ECCP256 || algorithm == algorithm_arg_ECCP384) { EC_GROUP *group; From ad4e93a462c4effbb0f48c0a75f594570308f938 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Thu, 23 Feb 2017 13:23:45 +0100 Subject: [PATCH 2/9] Few more OpenSSL 1.1.0 incompatibilities --- tool/Makefile.am | 2 +- tool/openssl-compat.c | 30 ++++++++++++++++++++++++++++++ tool/openssl-compat.h | 7 +++++++ tool/util.c | 26 +++++++++++++------------- tool/yubico-piv-tool.c | 16 ++++++++++------ ykcs11/openssl_utils.c | 20 ++++++++++++-------- 6 files changed, 73 insertions(+), 28 deletions(-) diff --git a/tool/Makefile.am b/tool/Makefile.am index aa024a0..45206e3 100644 --- a/tool/Makefile.am +++ b/tool/Makefile.am @@ -40,7 +40,7 @@ noinst_LTLIBRARIES = libpiv_cmd.la libpiv_util.la libpiv_cmd_la_SOURCES = cmdline.ggo cmdline.c cmdline.h libpiv_cmd_la_CFLAGS = -libpiv_util_la_SOURCES = util.c util.h openssl-compat.c +libpiv_util_la_SOURCES = util.c util.h openssl-compat.c openssl-compat.h libpiv_util_la_LIBADD = $(top_builddir)/lib/libykpiv.la $(OPENSSL_LIBS) cmdline.c cmdline.h: cmdline.ggo Makefile.am $(top_srcdir)/configure.ac diff --git a/tool/openssl-compat.c b/tool/openssl-compat.c index b8b274b..229c6bf 100644 --- a/tool/openssl-compat.c +++ b/tool/openssl-compat.c @@ -7,6 +7,7 @@ * https://www.openssl.org/source/license.html */ +#include #if OPENSSL_VERSION_NUMBER < 0x10100000L #include @@ -50,4 +51,33 @@ void RSA_get0_key(const RSA *r, *d = r->d; } +void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) +{ + if (p != NULL) + *p = r->p; + if (q != NULL) + *q = r->q; +} + +void RSA_get0_crt_params(const RSA *r, + const BIGNUM **dmp1, const BIGNUM **dmq1, + const BIGNUM **iqmp) +{ + if (dmp1 != NULL) + *dmp1 = r->dmp1; + if (dmq1 != NULL) + *dmq1 = r->dmq1; + if (iqmp != NULL) + *iqmp = r->iqmp; +} + +void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **palg, + ASN1_OCTET_STRING **pdigest) +{ + if (palg) + *palg = sig->algor; + if (pdigest) + *pdigest = sig->digest; +} + #endif /* OPENSSL_VERSION_NUMBER */ diff --git a/tool/openssl-compat.h b/tool/openssl-compat.h index d5e9f7a..95bdd84 100644 --- a/tool/openssl-compat.h +++ b/tool/openssl-compat.h @@ -17,10 +17,17 @@ #include #include #include +#include int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d); void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d); +void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q); +void RSA_get0_crt_params(const RSA *r, + const BIGNUM **dmp1, const BIGNUM **dmq1, + const BIGNUM **iqmp); +void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **palg, + ASN1_OCTET_STRING **pdigest); #endif /* OPENSSL_VERSION_NUMBER */ #endif /* LIBCRYPTO_COMPAT_H */ diff --git a/tool/util.c b/tool/util.c index 678de85..63e2716 100644 --- a/tool/util.c +++ b/tool/util.c @@ -438,23 +438,23 @@ bool set_component(unsigned char *in_ptr, const BIGNUM *bn, int element_len) { } bool prepare_rsa_signature(const unsigned char *in, unsigned int in_len, unsigned char *out, unsigned int *out_len, int nid) { - X509_SIG digestInfo; - X509_ALGOR algor; + X509_SIG *digestInfo; + X509_ALGOR *algor; ASN1_TYPE parameter; - ASN1_OCTET_STRING digest; + ASN1_OCTET_STRING *digest; unsigned char data[1024]; memcpy(data, in, in_len); - digestInfo.algor = &algor; - digestInfo.algor->algorithm = OBJ_nid2obj(nid); - digestInfo.algor->parameter = ¶meter; - digestInfo.algor->parameter->type = V_ASN1_NULL; - digestInfo.algor->parameter->value.ptr = NULL; - digestInfo.digest = &digest; - digestInfo.digest->data = data; - digestInfo.digest->length = (int)in_len; - *out_len = (unsigned int)i2d_X509_SIG(&digestInfo, &out); + digestInfo = X509_SIG_new(); + X509_SIG_getm(digestInfo, &algor, &digest); + algor = X509_ALGOR_new(); + X509_ALGOR_set0(algor, OBJ_nid2obj(nid), V_ASN1_NULL, ¶meter); + parameter.type = V_ASN1_NULL; + parameter.value.ptr = NULL; + digest->data = data; + digest->length = (int)in_len; + *out_len = (unsigned int)i2d_X509_SIG(digestInfo, &out); return true; } @@ -637,7 +637,7 @@ int SSH_write_X509(FILE *fp, X509 *x) { return ret; } - switch (pkey->type) { + switch (EVP_PKEY_id(pkey)) { case EVP_PKEY_RSA: case EVP_PKEY_RSA2: { RSA *rsa; diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index 5bf5df4..a980a29 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -411,39 +411,43 @@ static bool import_key(ykpiv_state *state, enum enum_key_format key_format, unsigned char dmp1[128]; unsigned char dmq1[128]; unsigned char iqmp[128]; + const BIGNUM *bn_e, *bn_p, *bn_q, *bn_dmp1, *bn_dmq1, *bn_iqmp; int element_len = 128; if(algorithm == YKPIV_ALGO_RSA1024) { element_len = 64; } - if((set_component(e, rsa_private_key->e, 3) == false) || + RSA_get0_key(rsa_private_key, NULL, &bn_e, NULL); + RSA_get0_factors(rsa_private_key, &bn_p, &bn_q); + RSA_get0_crt_params(rsa_private_key, &bn_dmp1, &bn_dmq1, &bn_iqmp); + if((set_component(e, bn_e, 3) == false) || !(e[0] == 0x01 && e[1] == 0x00 && e[2] == 0x01)) { fprintf(stderr, "Invalid public exponent for import (only 0x10001 supported)\n"); goto import_out; } - if(set_component(p, rsa_private_key->p, element_len) == false) { + if(set_component(p, bn_p, element_len) == false) { fprintf(stderr, "Failed setting p component.\n"); goto import_out; } - if(set_component(q, rsa_private_key->q, element_len) == false) { + if(set_component(q, bn_q, element_len) == false) { fprintf(stderr, "Failed setting q component.\n"); goto import_out; } - if(set_component(dmp1, rsa_private_key->dmp1, element_len) == false) { + if(set_component(dmp1, bn_dmp1, element_len) == false) { fprintf(stderr, "Failed setting dmp1 component.\n"); goto import_out; } - if(set_component(dmq1, rsa_private_key->dmq1, element_len) == false) { + if(set_component(dmq1, bn_dmq1, element_len) == false) { fprintf(stderr, "Failed setting dmq1 component.\n"); goto import_out; } - if(set_component(iqmp, rsa_private_key->iqmp, element_len) == false) { + if(set_component(iqmp, bn_iqmp, element_len) == false) { fprintf(stderr, "Failed setting iqmp component.\n"); goto import_out; } diff --git a/ykcs11/openssl_utils.c b/ykcs11/openssl_utils.c index 4ceb704..b1899df 100644 --- a/ykcs11/openssl_utils.c +++ b/ykcs11/openssl_utils.c @@ -31,6 +31,7 @@ #include "openssl_utils.h" #include #include "../tool/util.h" // TODO: share this better? +#include "../tool/openssl-compat.h" // TODO: share this better? #include "debug.h" #include @@ -115,8 +116,7 @@ CK_RV do_create_empty_cert(CK_BYTE_PTR in, CK_ULONG in_len, CK_BBOOL is_rsa, if(bignum_e == NULL) goto create_empty_cert_cleanup; - rsa->n = bignum_n; - rsa->e = bignum_e; + RSA_set0_key(rsa, bignum_n, bignum_e, NULL); if (EVP_PKEY_set1_RSA(key, rsa) == 0) goto create_empty_cert_cleanup; @@ -316,7 +316,7 @@ CK_RV do_store_pubk(X509 *cert, EVP_PKEY **key) { CK_KEY_TYPE do_get_key_type(EVP_PKEY *key) { - switch (key->type) { + switch (EVP_PKEY_id(key)) { case EVP_PKEY_RSA: case EVP_PKEY_RSA2: return CKK_RSA; @@ -349,18 +349,20 @@ CK_ULONG do_get_rsa_modulus_length(EVP_PKEY *key) { CK_RV do_get_modulus(EVP_PKEY *key, CK_BYTE_PTR data, CK_ULONG_PTR len) { RSA *rsa; + const BIGNUM *n; rsa = EVP_PKEY_get1_RSA(key); if (rsa == NULL) return CKR_FUNCTION_FAILED; - if ((CK_ULONG)BN_num_bytes(rsa->n) > *len) { + RSA_get0_key(rsa, &n, NULL, NULL); + if ((CK_ULONG)BN_num_bytes(n) > *len) { RSA_free(rsa); rsa = NULL; return CKR_BUFFER_TOO_SMALL; } - *len = (CK_ULONG)BN_bn2bin(rsa->n, data); + *len = (CK_ULONG)BN_bn2bin(n, data); RSA_free(rsa); rsa = NULL; @@ -372,18 +374,20 @@ CK_RV do_get_public_exponent(EVP_PKEY *key, CK_BYTE_PTR data, CK_ULONG_PTR len) CK_ULONG e = 0; RSA *rsa; + const BIGNUM *bn_e; rsa = EVP_PKEY_get1_RSA(key); if (rsa == NULL) return CKR_FUNCTION_FAILED; - if ((CK_ULONG)BN_num_bytes(rsa->e) > *len) { + RSA_get0_key(rsa, NULL, &bn_e, NULL); + if ((CK_ULONG)BN_num_bytes(bn_e) > *len) { RSA_free(rsa); rsa = NULL; return CKR_BUFFER_TOO_SMALL; } - *len = (CK_ULONG)BN_bn2bin(rsa->e, data); + *len = (CK_ULONG)BN_bn2bin(bn_e, data); RSA_free(rsa); rsa = NULL; @@ -406,7 +410,7 @@ CK_RV do_get_public_key(EVP_PKEY *key, CK_BYTE_PTR data, CK_ULONG_PTR len) { const EC_POINT *ecp; point_conversion_form_t pcf = POINT_CONVERSION_UNCOMPRESSED; - switch(key->type) { + switch(EVP_PKEY_id(key)) { case EVP_PKEY_RSA: case EVP_PKEY_RSA2: From d2ffc41a6c63c9a0e221ea593c21b8fad3b628df Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Fri, 20 Oct 2017 11:16:18 +0200 Subject: [PATCH 3/9] RAND_pseudo_bytes is deprecated in OpenSSL 1.1.0 --- lib/ykpiv.c | 2 +- tool/yubico-piv-tool.c | 6 +++--- ykcs11/tests/ykcs11_tests.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ykpiv.c b/lib/ykpiv.c index da4fa44..2aff46c 100644 --- a/lib/ykpiv.c +++ b/lib/ykpiv.c @@ -478,7 +478,7 @@ ykpiv_rc ykpiv_authenticate(ykpiv_state *state, unsigned const char *key) { dataptr += 8; *dataptr++ = 0x81; *dataptr++ = 8; - if(RAND_pseudo_bytes(dataptr, 8) == -1) { + if(RAND_bytes(dataptr, 8) == -1) { if(state->verbose) { fprintf(stderr, "Failed getting randomness for authentication.\n"); } diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index a980a29..ca50fa7 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -645,7 +645,7 @@ static bool set_dataobject(ykpiv_state *state, int verbose, int type) { id = YKPIV_OBJ_CAPABILITY; } memcpy(obj, tmpl, len); - if(RAND_pseudo_bytes(obj + offs, rand_len) == -1) { + if(RAND_bytes(obj + offs, rand_len) == -1) { fprintf(stderr, "error: no randomness.\n"); return false; } @@ -1457,7 +1457,7 @@ static bool test_signature(ykpiv_state *state, enum enum_slot slot, { unsigned char rand[128]; EVP_MD_CTX *mdctx; - if(RAND_pseudo_bytes(rand, 128) == -1) { + if(RAND_bytes(rand, 128) == -1) { fprintf(stderr, "error: no randomness.\n"); return false; } @@ -1604,7 +1604,7 @@ static bool test_decipher(ykpiv_state *state, enum enum_slot slot, size_t len2 = sizeof(data); RSA *rsa = EVP_PKEY_get1_RSA(pubkey); - if(RAND_pseudo_bytes(secret, sizeof(secret)) == -1) { + if(RAND_bytes(secret, sizeof(secret)) == -1) { fprintf(stderr, "error: no randomness.\n"); ret = false; goto decipher_out; diff --git a/ykcs11/tests/ykcs11_tests.c b/ykcs11/tests/ykcs11_tests.c index 027ff51..df17239 100644 --- a/ykcs11/tests/ykcs11_tests.c +++ b/ykcs11/tests/ykcs11_tests.c @@ -371,7 +371,7 @@ static void test_import_and_sign_all_10() { for (i = 0; i < 24; i++) { for (j = 0; j < 10; j++) { - if(RAND_pseudo_bytes(some_data, sizeof(some_data)) == -1) + if(RAND_bytes(some_data, sizeof(some_data)) == -1) exit(EXIT_FAILURE); asrt(funcs->C_Login(session, CKU_USER, "123456", 6), CKR_OK, "Login USER"); @@ -562,7 +562,7 @@ static void test_import_and_sign_all_10_RSA() { for (i = 0; i < 24; i++) { for (j = 0; j < 10; j++) { - if(RAND_pseudo_bytes(some_data, sizeof(some_data)) == -1) + if(RAND_bytes(some_data, sizeof(some_data)) == -1) exit(EXIT_FAILURE); asrt(funcs->C_Login(session, CKU_USER, "123456", 6), CKR_OK, "Login USER"); From 4a847677cc3bf81988d046673bf0612d6a0eb228 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Fri, 20 Oct 2017 16:35:09 +0200 Subject: [PATCH 4/9] WIP:Use RSA/EC_KEY METHOD to provide X509 signatures using high-level OpenSSL API --- tool/yubico-piv-tool.c | 77 ++++++++++++++++++++++++++++++++++++++++++ ykcs11/openssl_utils.c | 2 ++ 2 files changed, 79 insertions(+) diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index ca50fa7..313b9e5 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -116,6 +116,62 @@ static bool sign_data(ykpiv_state *state, const unsigned char *in, size_t len, u return false; } +static int ec_key_ex_data_idx = -1; + +struct internal_key { + ykpiv_state *state; + int algorithm; + int key; +}; + +int +yk_rsa_meth_sign(int dtype, const unsigned char *m, unsigned int m_length, + unsigned char *sigret, unsigned int *siglen, const RSA *rsa) +{ + const RSA_METHOD *meth = RSA_get_method(rsa); + const struct internal_key *key = RSA_meth_get0_app_data(meth); + if (sign_data(key->state, m, m_length, sigret, (size_t *)siglen, key->algorithm, key->key)) + return 0; + + return 1; +} + +int +yk_ec_meth_sign(int type, const unsigned char *dgst, int dlen, + unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv, + const BIGNUM *r, EC_KEY *ec) +{ + const struct internal_key *key = EC_KEY_get_ex_data(ec, ec_key_ex_data_idx); + if (sign_data(key->state, dgst, dlen, sig, (size_t *)siglen, key->algorithm, key->key)) + return 0; + + return 1; +} + +static int +wrap_public_key(ykpiv_state *state, int algorithm, EVP_PKEY *public_key, + int key) +{ + if(YKPIV_IS_RSA(algorithm)) { + RSA_METHOD *meth = RSA_meth_dup(RSA_get_default_method()); + RSA *rsa = EVP_PKEY_get0_RSA(public_key); + struct internal_key int_key = {state, algorithm, key}; + RSA_meth_set0_app_data(meth, &int_key); + RSA_meth_set_sign(meth, yk_rsa_meth_sign); + RSA_set_method(rsa, meth); + } else { + EC_KEY *ec = EVP_PKEY_get0_EC_KEY(public_key); + EC_KEY_METHOD *meth = EC_KEY_METHOD_new(EC_KEY_get_method(ec)); + struct internal_key int_key = {state, algorithm, key}; + if (ec_key_ex_data_idx == -1) + ec_key_ex_data_idx = EC_KEY_get_ex_new_index(0, NULL, NULL, NULL, 0); + EC_KEY_set_ex_data(ec, ec_key_ex_data_idx, &int_key); + EC_KEY_METHOD_set_sign(meth, yk_ec_meth_sign, NULL, NULL); /* XXX ?? */ + EC_KEY_set_method(ec, meth); + } + return 0; +} + static bool generate_key(ykpiv_state *state, const char *slot, enum enum_algorithm algorithm, const char *output_file_name, enum enum_key_format key_format, enum enum_pin_policy pin_policy, @@ -743,6 +799,7 @@ static bool request_certificate(ykpiv_state *state, enum enum_key_format key_for goto request_out; } +#if OPENSSL_VERSION_NUMBER < 10100000L memcpy(digest, oid, oid_len); /* XXX: this should probably use X509_REQ_digest() but that's buggy */ if(!ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ_INFO), md, req->req_info, @@ -756,6 +813,7 @@ static bool request_certificate(ykpiv_state *state, enum enum_key_format key_for fprintf(stderr, "Unsupported algorithm %x or hash %x\n", algorithm, hash); goto request_out; } + if(YKPIV_IS_RSA(algorithm)) { signinput = digest; len = oid_len + digest_len; @@ -778,6 +836,13 @@ static bool request_certificate(ykpiv_state *state, enum enum_key_format key_for /* mark that all bits should be used. */ req->signature->flags = ASN1_STRING_FLAG_BITS_LEFT; } +#else + /* With opaque structures we can not touch whatever we want, but we need + * to embed the sign_data function in the RSA/EC key structures */ + wrap_public_key(state, algorithm, public_key, key); + + X509_REQ_sign(req, public_key, md); +#endif if(key_format == key_format_arg_PEM) { PEM_write_X509_REQ(output_file, req); @@ -797,9 +862,11 @@ request_out: EVP_PKEY_free(public_key); } if(req) { +#if OPENSSL_VERSION_NUMBER < 10100000L if(req->sig_alg->parameter) { req->sig_alg->parameter = NULL; } +#endif X509_REQ_free(req); } if(name) { @@ -928,6 +995,7 @@ static bool selfsign_certificate(ykpiv_state *state, enum enum_key_format key_fo if(nid == 0) { goto selfsign_out; } +#if OPENSSL_VERSION_NUMBER < 10100000L if(YKPIV_IS_RSA(algorithm)) { signinput = digest; len = oid_len + md_len; @@ -961,6 +1029,13 @@ static bool selfsign_certificate(ykpiv_state *state, enum enum_key_format key_fo * certificate can be validated. */ x509->signature->flags = ASN1_STRING_FLAG_BITS_LEFT; } +#else + /* With opaque structures we can not touch whatever we want, but we need + * to embed the sign_data function in the RSA/EC key structures */ + wrap_public_key(state, algorithm, public_key, key); + + X509_sign(x509, public_key, md); +#endif if(key_format == key_format_arg_PEM) { PEM_write_X509(output_file, x509); @@ -977,10 +1052,12 @@ selfsign_out: fclose(output_file); } if(x509) { +#if OPENSSL_VERSION_NUMBER < 10100000L if(x509->sig_alg->parameter) { x509->sig_alg->parameter = NULL; x509->cert_info->signature->parameter = NULL; } +#endif X509_free(x509); } if(public_key) { diff --git a/ykcs11/openssl_utils.c b/ykcs11/openssl_utils.c index b1899df..ee9e229 100644 --- a/ykcs11/openssl_utils.c +++ b/ykcs11/openssl_utils.c @@ -165,6 +165,7 @@ CK_RV do_create_empty_cert(CK_BYTE_PTR in, CK_ULONG in_len, CK_BBOOL is_rsa, X509_set_notBefore(cert, tm); X509_set_notAfter(cert, tm); +#if OPENSSL_VERSION_NUMBER < 10100000L // Manually set the signature algorithms. // OpenSSL 1.0.1i complains about empty DER fields // 8 => md5WithRsaEncryption @@ -174,6 +175,7 @@ CK_RV do_create_empty_cert(CK_BYTE_PTR in, CK_ULONG in_len, CK_BBOOL is_rsa, // Manually set a signature (same reason as before) ASN1_BIT_STRING_set_bit(cert->signature, 8, 1); ASN1_BIT_STRING_set(cert->signature, "\x00", 1); +#endif len = i2d_X509(cert, NULL); if (len < 0) From a2715f0a4a68325dec8bb831eee92e30af91a593 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Mon, 13 Nov 2017 17:43:06 +0100 Subject: [PATCH 5/9] Use OpenSSL 1.1.0 API --- ykcs11/openssl_utils.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ykcs11/openssl_utils.c b/ykcs11/openssl_utils.c index ee9e229..4182706 100644 --- a/ykcs11/openssl_utils.c +++ b/ykcs11/openssl_utils.c @@ -476,16 +476,20 @@ CK_RV do_get_public_key(EVP_PKEY *key, CK_BYTE_PTR data, CK_ULONG_PTR len) { CK_RV do_encode_rsa_public_key(ykcs11_rsa_key_t **key, CK_BYTE_PTR modulus, CK_ULONG mlen, CK_BYTE_PTR exponent, CK_ULONG elen) { ykcs11_rsa_key_t *k; + BIGNUM *k_n = NULL, *k_e = NULL; if (modulus == NULL || exponent == NULL) return CKR_ARGUMENTS_BAD; if ((k = RSA_new()) == NULL) return CKR_HOST_MEMORY; - if ((k->n = BN_bin2bn(modulus, mlen, NULL)) == NULL) + if ((k_n = BN_bin2bn(modulus, mlen, NULL)) == NULL) return CKR_FUNCTION_FAILED; - if ((k->e = BN_bin2bn(exponent, elen, NULL)) == NULL) + if ((k_e = BN_bin2bn(exponent, elen, NULL)) == NULL) + return CKR_FUNCTION_FAILED; + + if (RSA_set0_key(k, k_n, k_e, NULL) == 0) return CKR_FUNCTION_FAILED; *key = k; From 13f542c1f8fc6a8f0f1dafafb948f09c7b0e7c45 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 14 Nov 2017 10:29:34 +0100 Subject: [PATCH 6/9] Use the new OpenSSL 1.1.0 API also in the HW tests --- ykcs11/tests/ykcs11_tests.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ykcs11/tests/ykcs11_tests.c b/ykcs11/tests/ykcs11_tests.c index df17239..34054b4 100644 --- a/ykcs11/tests/ykcs11_tests.c +++ b/ykcs11/tests/ykcs11_tests.c @@ -466,6 +466,7 @@ static void test_import_and_sign_all_10_RSA() { CK_BYTE_PTR s_ptr; CK_ULONG r_len; CK_ULONG s_len; + const BIGNUM *bp, *bq, *biqmp, *bdmp1, *bdmq1; unsigned char *px; @@ -508,11 +509,13 @@ static void test_import_and_sign_all_10_RSA() { asrt(RSA_generate_key_ex(rsak, 1024, e_bn, NULL), 1, "GENERATE RSAK"); - asrt(BN_bn2bin(rsak->p, p), 64, "GET P"); - asrt(BN_bn2bin(rsak->q, q), 64, "GET Q"); - asrt(BN_bn2bin(rsak->dmp1, dp), 64, "GET DP"); - asrt(BN_bn2bin(rsak->dmq1, dp), 64, "GET DQ"); - asrt(BN_bn2bin(rsak->iqmp, qinv), 64, "GET QINV"); + RSA_get0_factors(rsak, &bp, &bq); + RSA_get0_crt_params(rsak, &bdmp1, &bdmq1, &biqmp); + asrt(BN_bn2bin(bp, p), 64, "GET P"); + asrt(BN_bn2bin(bq, q), 64, "GET Q"); + asrt(BN_bn2bin(bdmp1, dp), 64, "GET DP"); + asrt(BN_bn2bin(bdmq1, dp), 64, "GET DQ"); + asrt(BN_bn2bin(biqmp, qinv), 64, "GET QINV"); From eda075fa57353c316b7da109c3a5784f6335c75c Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 14 Nov 2017 10:30:04 +0100 Subject: [PATCH 7/9] Provide the bogus signature with OpenSSL 1.1.0 API --- ykcs11/tests/ykcs11_tests.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ykcs11/tests/ykcs11_tests.c b/ykcs11/tests/ykcs11_tests.c index 34054b4..59ce950 100644 --- a/ykcs11/tests/ykcs11_tests.c +++ b/ykcs11/tests/ykcs11_tests.c @@ -259,6 +259,32 @@ static void test_login() { } +#if OPENSSL_VERSION_NUMBER >= 10100000L +static int bogus_sign(int dtype, const unsigned char *m, unsigned int m_length, + unsigned char *sigret, unsigned int *siglen, const RSA *rsa) { + sigret = malloc(1); + sigret = ""; + *siglen = 1; + return 0; +} + +static void bogus_sign_cert(X509 *cert) { + EVP_PKEY *pkey = EVP_PKEY_new(); + RSA *rsa = RSA_new(); + RSA_METHOD *meth = RSA_meth_dup(RSA_get_default_method()); + BIGNUM *e = BN_new(); + + BN_set_word(e, 65537); + RSA_generate_key_ex(rsa, 1024, e, NULL); + RSA_meth_set_sign(meth, bogus_sign); + RSA_set_method(rsa, meth); + EVP_PKEY_set1_RSA(pkey, rsa); + X509_sign(cert, pkey, EVP_md5()); + EVP_PKEY_free(pkey); +} +#endif + + // Import a newly generated P256 pvt key and a certificate // to every slot and use the key to sign some data static void test_import_and_sign_all_10() { @@ -344,11 +370,15 @@ static void test_import_and_sign_all_10() { X509_set_notBefore(cert, tm); X509_set_notAfter(cert, tm); +#if OPENSSL_VERSION_NUMBER < 10100000L cert->sig_alg->algorithm = OBJ_nid2obj(8); cert->cert_info->signature->algorithm = OBJ_nid2obj(8); ASN1_BIT_STRING_set_bit(cert->signature, 8, 1); ASN1_BIT_STRING_set(cert->signature, "\x00", 1); +#else + bogus_sign_cert(cert); +#endif p = value_c; if ((cert_len = (CK_ULONG) i2d_X509(cert, &p)) == 0 || cert_len > sizeof(value_c)) @@ -538,11 +568,16 @@ static void test_import_and_sign_all_10_RSA() { X509_set_notBefore(cert, tm); X509_set_notAfter(cert, tm); +#if OPENSSL_VERSION_NUMBER < 10100000L + /* putting bogus data to signature to make some checks happy */ cert->sig_alg->algorithm = OBJ_nid2obj(8); cert->cert_info->signature->algorithm = OBJ_nid2obj(8); ASN1_BIT_STRING_set_bit(cert->signature, 8, 1); ASN1_BIT_STRING_set(cert->signature, "\x00", 1); +#else + bogus_sign_cert(cert); +#endif px = value_c; if ((cert_len = (CK_ULONG) i2d_X509(cert, &px)) == 0 || cert_len > sizeof(value_c)) From 0a131a053d6aad4b0d2f0c7670c1dfa889a43004 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 14 Nov 2017 10:34:32 +0100 Subject: [PATCH 8/9] Do not use the new API with the old OpenSSL --- tool/yubico-piv-tool.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index 313b9e5..c1e4d7b 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -148,6 +148,7 @@ yk_ec_meth_sign(int type, const unsigned char *dgst, int dlen, return 1; } +#if OPENSSL_VERSION_NUMBER >= 10100000L static int wrap_public_key(ykpiv_state *state, int algorithm, EVP_PKEY *public_key, int key) @@ -171,6 +172,7 @@ wrap_public_key(ykpiv_state *state, int algorithm, EVP_PKEY *public_key, } return 0; } +#endif static bool generate_key(ykpiv_state *state, const char *slot, enum enum_algorithm algorithm, const char *output_file_name, From 77c51a7317d4e0bf7439505191e894a720137890 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 14 Nov 2017 12:32:23 +0100 Subject: [PATCH 9/9] Properly apply the OpenSSL version checks --- tool/openssl-compat.c | 2 +- tool/openssl-compat.h | 1 + tool/yubico-piv-tool.c | 10 +++++----- ykcs11/tests/ykcs11_tests.c | 6 +++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tool/openssl-compat.c b/tool/openssl-compat.c index 229c6bf..a51af90 100644 --- a/tool/openssl-compat.c +++ b/tool/openssl-compat.c @@ -7,7 +7,7 @@ * https://www.openssl.org/source/license.html */ -#include +#include "openssl-compat.h" #if OPENSSL_VERSION_NUMBER < 0x10100000L #include diff --git a/tool/openssl-compat.h b/tool/openssl-compat.h index 95bdd84..b606f5f 100644 --- a/tool/openssl-compat.h +++ b/tool/openssl-compat.h @@ -10,6 +10,7 @@ #ifndef LIBCRYPTO_COMPAT_H #define LIBCRYPTO_COMPAT_H +#include #if OPENSSL_VERSION_NUMBER < 0x10100000L #include diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index c1e4d7b..399d89f 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -116,6 +116,7 @@ static bool sign_data(ykpiv_state *state, const unsigned char *in, size_t len, u return false; } +#if OPENSSL_VERSION_NUMBER >= 0x10100000L static int ec_key_ex_data_idx = -1; struct internal_key { @@ -148,7 +149,6 @@ yk_ec_meth_sign(int type, const unsigned char *dgst, int dlen, return 1; } -#if OPENSSL_VERSION_NUMBER >= 10100000L static int wrap_public_key(ykpiv_state *state, int algorithm, EVP_PKEY *public_key, int key) @@ -801,7 +801,7 @@ static bool request_certificate(ykpiv_state *state, enum enum_key_format key_for goto request_out; } -#if OPENSSL_VERSION_NUMBER < 10100000L +#if OPENSSL_VERSION_NUMBER < 0x10100000L memcpy(digest, oid, oid_len); /* XXX: this should probably use X509_REQ_digest() but that's buggy */ if(!ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ_INFO), md, req->req_info, @@ -864,7 +864,7 @@ request_out: EVP_PKEY_free(public_key); } if(req) { -#if OPENSSL_VERSION_NUMBER < 10100000L +#if OPENSSL_VERSION_NUMBER < 0x10100000L if(req->sig_alg->parameter) { req->sig_alg->parameter = NULL; } @@ -997,7 +997,7 @@ static bool selfsign_certificate(ykpiv_state *state, enum enum_key_format key_fo if(nid == 0) { goto selfsign_out; } -#if OPENSSL_VERSION_NUMBER < 10100000L +#if OPENSSL_VERSION_NUMBER < 0x10100000L if(YKPIV_IS_RSA(algorithm)) { signinput = digest; len = oid_len + md_len; @@ -1054,7 +1054,7 @@ selfsign_out: fclose(output_file); } if(x509) { -#if OPENSSL_VERSION_NUMBER < 10100000L +#if OPENSSL_VERSION_NUMBER < 0x10100000L if(x509->sig_alg->parameter) { x509->sig_alg->parameter = NULL; x509->cert_info->signature->parameter = NULL; diff --git a/ykcs11/tests/ykcs11_tests.c b/ykcs11/tests/ykcs11_tests.c index 59ce950..aaedbde 100644 --- a/ykcs11/tests/ykcs11_tests.c +++ b/ykcs11/tests/ykcs11_tests.c @@ -259,7 +259,7 @@ static void test_login() { } -#if OPENSSL_VERSION_NUMBER >= 10100000L +#if OPENSSL_VERSION_NUMBER >= 0x10100000L static int bogus_sign(int dtype, const unsigned char *m, unsigned int m_length, unsigned char *sigret, unsigned int *siglen, const RSA *rsa) { sigret = malloc(1); @@ -370,7 +370,7 @@ static void test_import_and_sign_all_10() { X509_set_notBefore(cert, tm); X509_set_notAfter(cert, tm); -#if OPENSSL_VERSION_NUMBER < 10100000L +#if OPENSSL_VERSION_NUMBER < 0x10100000L cert->sig_alg->algorithm = OBJ_nid2obj(8); cert->cert_info->signature->algorithm = OBJ_nid2obj(8); @@ -568,7 +568,7 @@ static void test_import_and_sign_all_10_RSA() { X509_set_notBefore(cert, tm); X509_set_notAfter(cert, tm); -#if OPENSSL_VERSION_NUMBER < 10100000L +#if OPENSSL_VERSION_NUMBER < 0x10100000L /* putting bogus data to signature to make some checks happy */ cert->sig_alg->algorithm = OBJ_nid2obj(8); cert->cert_info->signature->algorithm = OBJ_nid2obj(8);