pyauth / pyauth/python-pkcs11

Change DSA encoding to SubjectPublicKeyInfo

Open
#124 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
170
Forks
79
PR merge metrics
No merged PRs in 30d

Description

Currently DSA public keys are encoded as integers because RFC 3279 defines DSA public key encoding in that way. Unfortunately most Python crypto libraries don't natively support this (ex. load_der_public_key).

My suggestion is to use the SubjectPublicKeyInfo ASN1 definition from RFC 5280 to encode the DSA public key in a format that is more universally implemented in other crypto libraries. The proposed change would look something like this:

from asn1crypto.keys import (
    DSAParams,
    PublicKeyAlgorithm,
    PublicKeyAlgorithmId,
    PublicKeyInfo,
)


def encode_dsa_public_key(key):
    """
    Encode DSA public key into RFC 5280 DER-encoded format.

    :param PublicKey key: public key
    :rtype: bytes
    """

    algorithm = PublicKeyAlgorithm()
    algorithm["algorithm"] = PublicKeyAlgorithmId("dsa")
    algorithm["parameters"] = DSAParams({
        'g': int.from_bytes(key[Attribute.BASE], byteorder='big'),
        'p': int.from_bytes(key[Attribute.PRIME], byteorder='big'),
        'q': int.from_bytes(key[Attribute.SUBPRIME], byteorder='big'),
    })

    asn1 = PublicKeyInfo()
    asn1["algorithm"] = algorithm
    asn1["public_key"] = Integer(int.from_bytes(key[Attribute.VALUE], byteorder='big'))

    return asn1.dump()

Let me know if you think this is the correct way to solve the problem, or if you have other ideas for how this should be handled. I'm open to creating a PR for this if you feel this solution is the correct approach.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the DSA public-key encoding entry point and any related tests; the issue names no repository files. Review the RFC 5280 SubjectPublicKeyInfo proposal and verify that the resulting DER encoding is accepted by the Python crypto libraries mentioned in the issue. Done means DSA public keys use the proposed interoperable format with coverage for the new encoding.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cryptography, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.