Add a PKCS11 provider supporting signing and verifing sigatures.

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@22435 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Love Hörnquist Åstrand
2008-01-14 20:53:56 +00:00
parent 9a0cc41169
commit c9f443454e
9 changed files with 2396 additions and 70 deletions

View File

@@ -1,5 +1,12 @@
2008-01-14 Love H<>rnquist <20>strand <lha@it.su.se>
* Add a PKCS11 provider supporting signing and verifing sigatures.
2008-01-13 Love H<>rnquist <20>strand <lha@it.su.se>
* version-script.map: Replace hx509_name_to_der_name with
hx509_name_binary.
* print.c: make print_func static
2007-12-26 Love H<>rnquist <20>strand <lha@it.su.se>

View File

@@ -76,6 +76,7 @@ dist_libhx509_la_SOURCES = \
name.c \
peer.c \
print.c \
softp11.c \
ref/pkcs11.h \
req.c \
revoke.c
@@ -117,7 +118,7 @@ crmf_asn1_files: $(asn1_compile) $(srcdir)/crmf.asn1
$(libhx509_la_OBJECTS): $(srcdir)/hx509-protos.h $(srcdir)/hx509-private.h
$(srcdir)/hx509-protos.h:
cd $(srcdir) && perl ../../cf/make-proto.pl -E HX509_LIB_FUNCTION -q -P comment -o hx509-protos.h $(dist_libhx509_la_SOURCES) || rm -f hx509-protos.h
cd $(srcdir) && perl ../../cf/make-proto.pl -R '^(_|^C)' -E HX509_LIB_FUNCTION -q -P comment -o hx509-protos.h $(dist_libhx509_la_SOURCES) || rm -f hx509-protos.h
$(srcdir)/hx509-private.h:
cd $(srcdir) && perl ../../cf/make-proto.pl -q -P comment -p hx509-private.h $(dist_libhx509_la_SOURCES) || rm -f hx509-private.h
@@ -161,7 +162,8 @@ CLEANFILES = $(BUILT_SOURCES) \
cert-proxy.der cert-ca.der cert-ee.der pkcs10-request.der \
wca.pem wuser.pem wdc.pem wcrl.crl \
random-data statfile crl.crl \
test
test \
test-rc-file.rc
clean-local:
@echo "cleaning PKITS" ; rm -rf PKITS_data
@@ -171,10 +173,13 @@ clean-local:
#
check_SCRIPTS = $(SCRIPT_TESTS)
check_PROGRAMS = $(PROGRAM_TESTS)
check_PROGRAMS = $(PROGRAM_TESTS) test_soft_pkcs11
LDADD = libhx509.la
test_soft_pkcs11_LDADD = libhx509.la
test_soft_pkcs11_CPPFLAGS = -I$(srcdir)/ref
TESTS = $(SCRIPT_TESTS) $(PROGRAM_TESTS)
PROGRAM_TESTS = \
@@ -188,6 +193,7 @@ SCRIPT_TESTS = \
test_crypto \
test_nist \
test_nist2 \
test_pkcs11 \
test_nist_cert \
test_nist_pkcs12 \
test_req \
@@ -232,6 +238,11 @@ test_nist2: test_nist2.in Makefile
chmod +x test_nist2.tmp
mv test_nist2.tmp test_nist2
test_pkcs11: test_pkcs11.in Makefile
$(do_subst) < $(srcdir)/test_pkcs11.in > test_pkcs11.tmp
chmod +x test_pkcs11.tmp
mv test_pkcs11.tmp test_pkcs11
test_nist_cert: test_nist_cert.in Makefile
$(do_subst) < $(srcdir)/test_nist_cert.in > test_nist_cert.tmp
chmod +x test_nist_cert.tmp
@@ -274,6 +285,7 @@ EXTRA_DIST = \
test_nist2.in \
test_nist_cert.in \
test_nist_pkcs12.in \
test_pkcs11.in \
test_query.in \
test_req.in \
test_windows.in \

View File

@@ -1474,6 +1474,13 @@ _hx509_cert_private_key(hx509_cert p)
return p->private_key;
}
int
hx509_cert_have_private_key(hx509_cert p)
{
return p->private_key ? 1 : 0;
}
int
_hx509_cert_private_key_exportable(hx509_cert p)
{

View File

@@ -64,6 +64,7 @@ struct hx509_private_key_ops {
int (*generate_private_key)(hx509_context,
struct hx509_generate_private_context *,
hx509_private_key);
BIGNUM *(*get_internal)(hx509_context, hx509_private_key, const char *);
int (*handle_alg)(const hx509_private_key,
const AlgorithmIdentifier *,
enum crypto_op_type);
@@ -115,6 +116,9 @@ struct signature_alg {
#define SIG_PUBLIC_SIG 0x200
#define SIG_SECRET 0x400
#define RA_RSA_USES_DIGEST_INFO 0x1000000
int (*verify_signature)(hx509_context context,
const struct signature_alg *,
const Certificate *,
@@ -248,43 +252,57 @@ rsa_verify_signature(hx509_context context,
}
if (retsize > tosize)
_hx509_abort("internal rsa decryption failure: ret > tosize");
ret = decode_DigestInfo(to, retsize, &di, &size);
free(to);
if (ret) {
goto out;
if (sig_alg->flags & RA_RSA_USES_DIGEST_INFO) {
ret = decode_DigestInfo(to, retsize, &di, &size);
free(to);
if (ret) {
goto out;
}
/* Check for extra data inside the sigature */
if (size != retsize) {
ret = HX509_CRYPTO_SIG_INVALID_FORMAT;
hx509_set_error_string(context, 0, ret, "size from decryption mismatch");
goto out;
}
if (sig_alg->digest_oid &&
der_heim_oid_cmp(&di.digestAlgorithm.algorithm,
(*sig_alg->digest_oid)()) != 0)
{
ret = HX509_CRYPTO_OID_MISMATCH;
hx509_set_error_string(context, 0, ret, "object identifier in RSA sig mismatch");
goto out;
}
/* verify that the parameters are NULL or the NULL-type */
if (di.digestAlgorithm.parameters != NULL &&
(di.digestAlgorithm.parameters->length != 2 ||
memcmp(di.digestAlgorithm.parameters->data, "\x05\x00", 2) != 0))
{
ret = HX509_CRYPTO_SIG_INVALID_FORMAT;
hx509_set_error_string(context, 0, ret, "Extra parameters inside RSA signature");
goto out;
}
ret = _hx509_verify_signature(context,
NULL,
&di.digestAlgorithm,
data,
&di.digest);
} else {
if (retsize != data->length ||
memcmp(to, data->data, retsize) != 0)
{
ret = HX509_CRYPTO_SIG_INVALID_FORMAT;
hx509_set_error_string(context, 0, ret, "RSA Signature incorrect");
goto out;
}
free(to);
}
/* Check for extra data inside the sigature */
if (size != retsize) {
ret = HX509_CRYPTO_SIG_INVALID_FORMAT;
hx509_set_error_string(context, 0, ret, "size from decryption mismatch");
goto out;
}
if (sig_alg->digest_oid &&
der_heim_oid_cmp(&di.digestAlgorithm.algorithm,
(*sig_alg->digest_oid)()) != 0)
{
ret = HX509_CRYPTO_OID_MISMATCH;
hx509_set_error_string(context, 0, ret, "object identifier in RSA sig mismatch");
goto out;
}
/* verify that the parameters are NULL or the NULL-type */
if (di.digestAlgorithm.parameters != NULL &&
(di.digestAlgorithm.parameters->length != 2 ||
memcmp(di.digestAlgorithm.parameters->data, "\x05\x00", 2) != 0))
{
ret = HX509_CRYPTO_SIG_INVALID_FORMAT;
hx509_set_error_string(context, 0, ret, "Extra parameters inside RSA signature");
goto out;
}
ret = _hx509_verify_signature(context,
NULL,
&di.digestAlgorithm,
data,
&di.digest);
out:
free_DigestInfo(&di);
RSA_free(rsa);
@@ -303,7 +321,6 @@ rsa_create_signature(hx509_context context,
const AlgorithmIdentifier *digest_alg;
heim_octet_string indata;
const heim_oid *sig_oid;
DigestInfo di;
size_t size;
int ret;
@@ -324,6 +341,8 @@ rsa_create_signature(hx509_context context,
digest_alg = hx509_signature_sha1();
} else if (der_heim_oid_cmp(sig_oid, oid_id_pkcs1_rsaEncryption()) == 0) {
digest_alg = hx509_signature_sha1();
} else if (der_heim_oid_cmp(sig_oid, oid_id_heim_rsa_pkcs1_x509()) == 0) {
digest_alg = NULL;
} else
return HX509_ALG_NOT_SUPP;
@@ -335,29 +354,34 @@ rsa_create_signature(hx509_context context,
}
}
memset(&di, 0, sizeof(di));
if (digest_alg) {
DigestInfo di;
memset(&di, 0, sizeof(di));
ret = _hx509_create_signature(context,
NULL,
digest_alg,
data,
&di.digestAlgorithm,
&di.digest);
if (ret)
return ret;
ASN1_MALLOC_ENCODE(DigestInfo,
indata.data,
indata.length,
&di,
&size,
ret);
free_DigestInfo(&di);
if (ret) {
hx509_set_error_string(context, 0, ret, "out of memory");
return ret;
ret = _hx509_create_signature(context,
NULL,
digest_alg,
data,
&di.digestAlgorithm,
&di.digest);
if (ret)
return ret;
ASN1_MALLOC_ENCODE(DigestInfo,
indata.data,
indata.length,
&di,
&size,
ret);
free_DigestInfo(&di);
if (ret) {
hx509_set_error_string(context, 0, ret, "out of memory");
return ret;
}
if (indata.length != size)
_hx509_abort("internal ASN.1 encoder error");
} else {
indata = *data;
}
if (indata.length != size)
_hx509_abort("internal ASN.1 encoder error");
sig->length = RSA_size(signer->private_key.rsa);
sig->data = malloc(sig->length);
@@ -371,7 +395,8 @@ rsa_create_signature(hx509_context context,
sig->data,
signer->private_key.rsa,
RSA_PKCS1_PADDING);
der_free_octet_string(&indata);
if (indata.data != data->data)
der_free_octet_string(&indata);
if (ret <= 0) {
ret = HX509_CMS_FAILED_CREATE_SIGATURE;
hx509_set_error_string(context, 0, ret,
@@ -517,6 +542,18 @@ rsa_private_key_export(hx509_context context,
return 0;
}
static BIGNUM *
rsa_get_internal(hx509_context context, hx509_private_key key, const char *type)
{
if (strcasecmp(type, "rsa-modulus") == 0) {
return BN_dup(key->private_key.rsa->n);
} else if (strcasecmp(type, "rsa-exponent") == 0) {
return BN_dup(key->private_key.rsa->e);
} else
return NULL;
}
static hx509_private_key_ops rsa_private_key_ops = {
"RSA PRIVATE KEY",
@@ -524,7 +561,8 @@ static hx509_private_key_ops rsa_private_key_ops = {
rsa_private_key2SPKI,
rsa_private_key_export,
rsa_private_key_import,
rsa_generate_private_key
rsa_generate_private_key,
rsa_get_internal
};
@@ -833,13 +871,24 @@ md2_verify_signature(hx509_context context,
return 0;
}
static const struct signature_alg heim_rsa_pkcs1_x509 = {
"rsa-pkcs1-x509",
oid_id_heim_rsa_pkcs1_x509,
hx509_signature_rsa_pkcs1_x509,
oid_id_pkcs1_rsaEncryption,
NULL,
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG,
rsa_verify_signature,
rsa_create_signature
};
static const struct signature_alg pkcs1_rsa_sha1_alg = {
"rsa",
oid_id_pkcs1_rsaEncryption,
hx509_signature_rsa_with_sha1,
oid_id_pkcs1_rsaEncryption,
NULL,
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG,
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG,
rsa_verify_signature,
rsa_create_signature
};
@@ -850,7 +899,7 @@ static const struct signature_alg rsa_with_sha256_alg = {
hx509_signature_rsa_with_sha256,
oid_id_pkcs1_rsaEncryption,
oid_id_sha256,
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG,
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG,
rsa_verify_signature,
rsa_create_signature
};
@@ -861,7 +910,7 @@ static const struct signature_alg rsa_with_sha1_alg = {
hx509_signature_rsa_with_sha1,
oid_id_pkcs1_rsaEncryption,
oid_id_secsig_sha_1,
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG,
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG,
rsa_verify_signature,
rsa_create_signature
};
@@ -872,7 +921,7 @@ static const struct signature_alg rsa_with_md5_alg = {
hx509_signature_rsa_with_md5,
oid_id_pkcs1_rsaEncryption,
oid_id_rsa_digest_md5,
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG,
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG,
rsa_verify_signature,
rsa_create_signature
};
@@ -883,7 +932,7 @@ static const struct signature_alg rsa_with_md2_alg = {
hx509_signature_rsa_with_md2,
oid_id_pkcs1_rsaEncryption,
oid_id_rsa_digest_md2,
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG,
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG,
rsa_verify_signature,
rsa_create_signature
};
@@ -952,7 +1001,7 @@ static const struct signature_alg *sig_algs[] = {
&pkcs1_rsa_sha1_alg,
&rsa_with_md5_alg,
&rsa_with_md2_alg,
&pkcs1_rsa_sha1_alg,
&heim_rsa_pkcs1_x509,
&dsa_sha1_alg,
&sha256_alg,
&sha1_alg,
@@ -1423,6 +1472,11 @@ const AlgorithmIdentifier _hx509_signature_rsa_data = {
{ 7, rk_UNCONST(rsa_oid) }, NULL
};
static const unsigned rsa_pkcs1_x509_oid[] ={ 1, 2, 752, 43, 16, 1 };
const AlgorithmIdentifier _hx509_signature_rsa_pkcs1_x509_data = {
{ 6, rk_UNCONST(rsa_pkcs1_x509_oid) }, NULL
};
static const unsigned des_rsdi_ede3_cbc_oid[] ={ 1, 2, 840, 113549, 3, 7 };
const AlgorithmIdentifier _hx509_des_rsdi_ede3_cbc_oid = {
{ 6, rk_UNCONST(des_rsdi_ede3_cbc_oid) }, NULL
@@ -1490,6 +1544,10 @@ const AlgorithmIdentifier *
hx509_signature_rsa(void)
{ return &_hx509_signature_rsa_data; }
const AlgorithmIdentifier *
hx509_signature_rsa_pkcs1_x509(void)
{ return &_hx509_signature_rsa_pkcs1_x509_data; }
const AlgorithmIdentifier *
hx509_crypto_des_rsdi_ede3_cbc(void)
{ return &_hx509_des_rsdi_ede3_cbc_oid; }
@@ -1597,6 +1655,16 @@ _hx509_private_key_exportable(hx509_private_key key)
return 1;
}
BIGNUM *
_hx509_private_key_get_internal(hx509_context context,
hx509_private_key key,
const char *type)
{
if (key->ops->get_internal == NULL)
return NULL;
return (*key->ops->get_internal)(context, key, type);
}
int
_hx509_private_key_export(hx509_context context,
const hx509_private_key key,

2017
lib/hx509/softp11.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,213 @@
/*
* Copyright (c) 2006 - 2008 Kungliga Tekniska H<>gskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "hx_locl.h"
#include "pkcs11.h"
#include <err.h>
static CK_RV
find_object(CK_SESSION_HANDLE session,
char *id,
CK_OBJECT_CLASS key_class,
CK_OBJECT_HANDLE_PTR object)
{
CK_ULONG object_count;
CK_RV ret;
CK_ATTRIBUTE search_data[] = {
{CKA_ID, id, 0 },
{CKA_CLASS, &key_class, sizeof(key_class)}
};
CK_ULONG num_search_data = sizeof(search_data)/sizeof(search_data[0]);
search_data[0].ulValueLen = strlen(id);
ret = C_FindObjectsInit(session, search_data, num_search_data);
if (ret != CKR_OK)
return ret;
ret = C_FindObjects(session, object, 1, &object_count);
if (ret != CKR_OK)
return ret;
if (object_count == 0) {
printf("found no object\n");
return 1;
}
ret = C_FindObjectsFinal(session);
if (ret != CKR_OK)
return ret;
return CKR_OK;
}
static char *sighash = "hej";
static char signature[1024];
int
main(int argc, char **argv)
{
CK_SLOT_ID_PTR slot_ids;
CK_SLOT_ID slot;
CK_ULONG num_slots;
CK_RV ret;
CK_SLOT_INFO slot_info;
CK_TOKEN_INFO token_info;
CK_SESSION_HANDLE session;
CK_OBJECT_HANDLE public, private;
C_Initialize(NULL_PTR);
ret = C_GetSlotList(FALSE, NULL, &num_slots);
if (ret != CKR_OK)
errx(1, "C_GetSlotList1 failed: %d", (int)ret);
if (num_slots == 0)
errx(1, "no slots");
if ((slot_ids = calloc(1, num_slots * sizeof(*slot_ids))) == NULL)
err(1, "alloc slots failed");
ret = C_GetSlotList(FALSE, slot_ids, &num_slots);
if (ret != CKR_OK)
errx(1, "C_GetSlotList2 failed: %d", (int)ret);
slot = slot_ids[0];
free(slot_ids);
ret = C_GetSlotInfo(slot, &slot_info);
if (ret)
errx(1, "C_GetSlotInfo failed: %d", (int)ret);
if ((slot_info.flags & CKF_TOKEN_PRESENT) == 0)
errx(1, "no token present");
ret = C_OpenSession(slot, CKF_SERIAL_SESSION, NULL, NULL, &session);
if (ret != CKR_OK)
errx(1, "C_OpenSession failed: %d", (int)ret);
ret = C_Login(session, CKU_USER, (unsigned char*)"foobar", 6);
if (ret != CKR_OK)
errx(1, "C_Login failed: %d", (int)ret);
ret = C_GetTokenInfo(slot, &token_info);
if (ret)
errx(1, "C_GetTokenInfo failed: %d", (int)ret);
if (token_info.flags & CKF_LOGIN_REQUIRED)
errx(1, "login required, even after C_Login");
ret = find_object(session, "cert", CKO_PUBLIC_KEY, &public);
if (ret != CKR_OK)
errx(1, "find cert failed: %d", (int)ret);
ret = find_object(session, "cert", CKO_PRIVATE_KEY, &private);
if (ret != CKR_OK)
errx(1, "find private key failed: %d", (int)ret);
{
CK_ULONG ck_sigsize;
CK_MECHANISM mechanism;
memset(&mechanism, 0, sizeof(mechanism));
mechanism.mechanism = CKM_RSA_PKCS;
ret = C_SignInit(session, &mechanism, private);
if (ret != CKR_OK)
return 1;
ck_sigsize = sizeof(signature);
ret = C_Sign(session, (CK_BYTE *)sighash, strlen(sighash),
(CK_BYTE *)signature, &ck_sigsize);
if (ret != CKR_OK) {
printf("C_Sign failed with: %d\n", (int)ret);
return 1;
}
ret = C_VerifyInit(session, &mechanism, public);
if (ret != CKR_OK)
return 1;
ret = C_Verify(session, (CK_BYTE *)signature, ck_sigsize,
(CK_BYTE *)sighash, strlen(sighash));
if (ret != CKR_OK) {
printf("message: %d\n", (int)ret);
return 1;
}
}
#if 0
{
CK_ULONG ck_sigsize, outsize;
CK_MECHANISM mechanism;
char outdata[1024];
memset(&mechanism, 0, sizeof(mechanism));
mechanism.mechanism = CKM_RSA_PKCS;
ret = C_EncryptInit(session, &mechanism, public);
if (ret != CKR_OK)
return 1;
ck_sigsize = sizeof(signature);
ret = C_Encrypt(session, (CK_BYTE *)sighash, strlen(sighash),
(CK_BYTE *)signature, &ck_sigsize);
if (ret != CKR_OK) {
printf("message: %d\n", (int)ret);
return 1;
}
ret = C_DecryptInit(session, &mechanism, private);
if (ret != CKR_OK)
return 1;
outsize = sizeof(outdata);
ret = C_Decrypt(session, (CK_BYTE *)signature, ck_sigsize,
(CK_BYTE *)outdata, &outsize);
if (ret != CKR_OK) {
printf("message: %d\n", (int)ret);
return 1;
}
if (memcmp(sighash, outdata, strlen(sighash)) != 0)
return 1;
}
#endif
ret = C_CloseSession(session);
if (ret != CKR_OK)
return 1;
C_Finalize(NULL_PTR);
return 0;
}

View File

@@ -3,7 +3,7 @@
1.2.840.113549.1.1.5
1.2.840.113549.1.1.4
1.2.840.113549.1.1.2
1.2.840.113549.1.1.5
1.2.752.43.16.1
2.16.840.1.101.3.4.2.1
1.3.14.3.2.26
1.2.840.113549.2.5

View File

@@ -3,4 +3,4 @@
1.2.840.113549.1.1.5
1.2.840.113549.1.1.4
1.2.840.113549.1.1.2
1.2.840.113549.1.1.5
1.2.752.43.16.1

View File

@@ -218,6 +218,8 @@ HEIMDAL_X509_1.0 {
_hx509_cert_assign_key;
_hx509_cert_private_key;
_hx509_name_from_Name;
# pkcs11 symbols
C_*;
local:
*;
};