pydantic_cryptography.x509

Name models

class pydantic_cryptography.x509.NameAttributeModel(*, oid, value)[source]

Pydantic model wrapping NameAttribute.

Normal model construction is straight forward:

>>> NameAttributeModel(oid="2.5.4.3", value="example.com")
NameAttributeModel(oid='2.5.4.3', value='example.com')

The constructor will also accept ObjectIdentifier objects for oid:

>>> NameAttributeModel(oid=NameOID.COMMON_NAME, value="example.com")
NameAttributeModel(oid='2.5.4.3', value='example.com')

For x509UniqueIdentifier attributes you have to base64-encode the value:

>>> import base64
>>> value = base64.b64encode(b"example.com")
>>> NameAttributeModel(oid=NameOID.X500_UNIQUE_IDENTIFIER, value=value)
NameAttributeModel(oid='2.5.4.45', value='ZXhhbXBsZS5jb20=')
Parameters:
  • oid (str | ObjectIdentifier) – The dotted string value of the OID (e.g. “2.5.4.3”).

  • value (str) – The value of the attribute.

model_validate(obj, *, **kwargs) NameAttributeModel

As usual, this function will parse a cryptography object:

>>> from cryptography import x509
>>> from cryptography.x509.oid import NameOID
>>> from pydantic_cryptography.x509 import NameAttributeModel
>>> attr = x509.NameAttribute(oid=NameOID.COMMON_NAME, value="example.com")
>>> model = NameAttributeModel.model_validate(attr)
>>> attr == model.cryptography
True

For **kwargs, please see model_validate() for more information.

property cryptography: NameAttribute[str | bytes]

The NameAttribute instance for this model.

class pydantic_cryptography.x509.NameModel(root=PydanticUndefined)[source]

Pydantic model wrapping Name.

This model is a Pydantic RootModel that takes a list of NameAttributeModel instances:

>>> NameModel([
...     NameAttributeModel(oid="2.5.4.3", value="example.com"),
... ])
NameModel(root=[NameAttributeModel(oid='2.5.4.3', value='example.com')])
Parameters:

root (list[NameAttributeModel]) – The name described by this model.

model_validate(obj, *, **kwargs) NameModel

As usual, this function will parse a cryptography object:

>>> from cryptography import x509
>>> from cryptography.x509.oid import NameOID
>>> from pydantic_cryptography.x509 import NameModel
>>> name_attr = x509.NameAttribute(oid=NameOID.COMMON_NAME, value="example.com")
>>> name = x509.Name([name_attr])
>>> name_model = NameModel.model_validate(name)
>>> name_model.cryptography == name
True

The function also accepts an RFC 4514 string as input:

>>> NameModel.model_validate("CN=example.com")
NameModel(root=[NameAttributeModel(oid='2.5.4.3', value='example.com')])

… and you can pass attribute name overrides as validation context:

>>> NameModel.model_validate(
...     "E=user@example.com",
...     context={'attr_name_overrides': {'E': NameOID.EMAIL_ADDRESS}}
... )
NameModel(root=[NameAttributeModel(oid='1.2.840.113549.1.9.1', value='user@example.com')])

For **kwargs, please see model_validate() for more information.

Parameters:

obj (str | Name | list[NameAttributeModel | NameAttribute])

Return type:

NameModel

property cryptography: Name

The Name instance for this model.