From a2a39bfcbc4fc4567c26b18d4c533cae55dfccca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?= <grubba@grubba.org> Date: Sat, 11 Jan 2014 14:35:45 +0100 Subject: [PATCH] Crypto.DSA: Moved some functions from Standards.PKCS.DSA. To avoid circular dependencies between Crypto.DSA and Standards.PKCS.DSA the pkcs_*() functions are now inlined in Crypto.DSA. This also adds pkcs_algorithm_identifer() to Crypto.DSA. Fixes module dumping problems for Crypto.DSA. --- lib/modules/Crypto.pmod/DSA.pike | 45 ++++++++++++++++--- lib/modules/Standards.pmod/PKCS.pmod/DSA.pmod | 5 +-- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/modules/Crypto.pmod/DSA.pike b/lib/modules/Crypto.pmod/DSA.pike index 96a8185a99..9b99ac9fac 100644 --- a/lib/modules/Crypto.pmod/DSA.pike +++ b/lib/modules/Crypto.pmod/DSA.pike @@ -223,21 +223,54 @@ variant this_program generate_key() // #define Sequence Standards.ASN1.Types.Sequence +#define Integer Standards.ASN1.Types.Integer +#define BitString Standards.ASN1.Types.BitString -//! Calls @[Standards.PKCS.DSA.signatue_algorithm_id] with the -//! provided @[hash]. +//! Returns the AlgorithmIdentifier as defined in RFC5280 section +//! 4.1.1.2 including the DSA parameters. +Sequence pkcs_algorithm_identifier() +{ + return + Sequence( ({ Standards.PKCS.Identifiers.dsa_id, + Sequence( ({ Integer(get_p()), + Integer(get_q()), + Integer(get_g()) + }) ) + }) ); +} + + +//! Returns the PKCS-1 algorithm identifier for DSA and the provided +//! hash algorithm. Only @[SHA1] supported. Sequence pkcs_signature_algorithm_id(.Hash hash) { - return [object(Sequence)]Standards.PKCS.DSA->signature_algorithm_id(hash); + switch(hash->name()) + { + case "sha1": + return Sequence( ({ Standards.PKCS.Identifiers.dsa_sha_id }) ); + break; + case "sha224": + return Sequence( ({ Standards.PKCS.Identifiers.dsa_sha224_id }) ); + break; + case "sha256": + return Sequence( ({ Standards.PKCS.Identifiers.dsa_sha256_id }) ); + break; + } + return 0; } -//! Calls @[Standards.PKCS.DSA.build_public_key] with this object as -//! argument. +//! Creates a SubjectPublicKeyInfo ASN.1 sequence for the object. +//! See RFC 5280 section 4.1.2.7. Sequence pkcs_public_key() { - return [object(Sequence)]Standards.PKCS.DSA->build_public_key(this); + return Sequence(({ + pkcs_algorithm_identifier(), + BitString(Integer(get_y())->get_der()), + })); } +#undef BitString +#undef Integer #undef Sequence //! Signs the @[message] with a PKCS-1 signature using hash algorithm diff --git a/lib/modules/Standards.pmod/PKCS.pmod/DSA.pmod b/lib/modules/Standards.pmod/PKCS.pmod/DSA.pmod index b731638ef7..d2e962e1e5 100644 --- a/lib/modules/Standards.pmod/PKCS.pmod/DSA.pmod +++ b/lib/modules/Standards.pmod/PKCS.pmod/DSA.pmod @@ -16,10 +16,7 @@ import Standards.ASN1.Types; Sequence algorithm_identifier(Crypto.DSA|void dsa) { return - dsa ? Sequence( ({ .Identifiers.dsa_id, - Sequence( ({ Integer(dsa->get_p()), - Integer(dsa->get_q()), - Integer(dsa->get_g()) }) ) }) ) + dsa ? dsa->pkcs_algorithm_identifier() : Sequence( ({ .Identifiers.dsa_id }) ); // FIXME: Shouldn't there be a Null() here? } -- GitLab