Skip to content

Support custom key pair - #5

Open
sodre wants to merge 2 commits into
wbond:masterfrom
sodre:f/custom-keypair
Open

Support custom key pair#5
sodre wants to merge 2 commits into
wbond:masterfrom
sodre:f/custom-keypair

Conversation

@sodre

@sodre sodre commented Mar 30, 2021

Copy link
Copy Markdown

@wbond

This PR uses a different approach to break the dependency from oscrypto. The user just needs to pass a class to build that has the algorithm property and the sign callable.

I added a UnitTest to show how one can use it the construct.

  A different approach where the user can pass a CustomKeyPair class to the build callable.
Comment thread csrbuilder/__init__.py
from functools import partial

from asn1crypto import x509, keys, csr, pem
from oscrypto import asymmetric

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We delay loading oscrypto until absolutely necessary.

  1. In case the public key is not asn1crypto.keys.PublicKeyInfo
  2. In case the private key does not conform to the duck typing

Comment thread csrbuilder/__init__.py
Comment on lines +165 to +177
if not isinstance(value, keys.PublicKeyInfo):
from oscrypto import asymmetric
if isinstance(value, asymmetric.PublicKey):
value = value.asn1
else:
raise TypeError(_pretty_message(
'''
subject_public_key must be an instance of
asn1crypto.keys.PublicKeyInfo or oscrypto.asymmetric.PublicKey,
not %s
''',
_type_name(value)
))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is simply inverting the logic, to first look for an asn1crypto.PublicKeyInfo, only if that fails should we import oscrypto.asymmetric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant