Skip to content

Rethink public_bytes/private_bytes APIs #4497

Closed

Description

@reaperhulk

Right now our public_bytes and private_bytes APIs look like:

public_bytes(encoding, format) -> bytes
private_bytes(encoding, format, encryption) -> bytes

Where encoding is the Encoding enum and format is either the PrivateFormat or PublicFormat enum depending on key type. This works, but when adding a new format (e.g. raw for x25519/ed25519 keys) it's problematic to update all call sites.

As discussed in IRC we're probably going to migrate to this API:

public_bytes(encode_format) -> bytes
private_bytes(encode_format, encryption) -> bytes

where encode_format is a composition of encoding and format. So where before you might do

private_key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())

in the new API this would become

private_key.private_bytes(PEM(PrivateFormat.PKCS8), NoEncryption())

Similarly for X25519 it would look like:

private_key.private_bytes(Raw(), NoEncryption())
# or
private_key.private_bytes(DER(PrivateFormat.PKCS8), BestAvailableEncryption())

Migration

Since we already have an existing API we need a migration and deprecation plan. For public_bytes we could do public_bytes(encoding=None, format=None), do an isinstance check on encoding to see if it's actually an instance of one of our new compositional classes and raise a deprecation warning if it's not.

If we want to use that approach for private_bytes we'd need to make it private_bytes(encoding=None, format=None, encryption=None), which would make composition style calls look pretty ugly during the transition with a superfluous None in the middle:

private_key.private_bytes(PEM(PrivateFormat.PKCS8), None, NoEncryption())

We could play games with args such that we check to see if the second arg is actually an instance of an encryption provider, which would solve the extra None, but will give us quite a bit of complexity in these methods while the deprecation period is in effect.

Finally, these are widely used methods and a deprecation window for this is likely no less than ~3 years, maybe longer? Thoughts?

Activity

  1. reaperhulk commented on Jan 5, 2019

    @reaperhulk
    MemberAuthor

    The existing API is not ideal but blowing it up doesn't give us enough advantages to be worth the pain. API stability is also a feature, so we'll choose stability here.

  2. locked as resolved and limited conversation to collaborators on Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions