diff --git a/lib/modules/Crypto.pmod/DSA.pike b/lib/modules/Crypto.pmod/DSA.pike index 96a8185a99c0d0275ce75fb30793d0629351c2b5..9b99ac9facffde1cb9d1f33196c8b6d74102f9d4 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 b731638ef7cdf33b450bc5698ca8b5b2b0622e3a..d2e962e1e5f68a08122ddc0cffa79cb66202f459 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? }