Skip to content

Commit

Permalink
Fix list typing for older python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
bitkeeper committed Jul 19, 2023
1 parent 697c8a8 commit 119d6d7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
16 changes: 8 additions & 8 deletions asyncua/crypto/cert_gen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
crypothelper contains helper functions to isolate the lower level cryto stuff from the GDS client.
"""
from typing import Dict
from typing import Dict, List
import datetime

from cryptography import x509
Expand All @@ -20,7 +20,7 @@
""" Create lookup table for x509.ObjectIdentifier based on textual name, by swapping key<>value of the available mapping"""


def _names_to_nameattributes(names: Dict[str, str]) -> list[x509.NameAttribute]:
def _names_to_nameattributes(names: Dict[str, str]) -> List[x509.NameAttribute]:
"""Convert a dict with key/value of an x509.NameAttribute list
Args:
Expand Down Expand Up @@ -60,8 +60,8 @@ def dump_private_key_as_pem(private_key: rsa.RSAPrivateKey) -> bytes:
def generate_self_signed_app_certificate(private_key: rsa.RSAPrivateKey,
common_name: str,
names: Dict[str, str],
subject_alt_names: list[x509.GeneralName],
extended: list[x509.ObjectIdentifier],
subject_alt_names: List[x509.GeneralName],
extended: List[x509.ObjectIdentifier],
days: int = 365) -> x509.Certificate:
"""Generate a self signed certificate for OPC UA client/server application that is according to OPC 10000-4 6.1 / OPC 10000-6 6.2.2
Expand All @@ -78,7 +78,7 @@ def generate_self_signed_app_certificate(private_key: rsa.RSAPrivateKey,
x509.Certificate: The generated certificate.
"""
generate_ca = len(extended) == 0
name_attributes: list[x509.NameAttribute] = _names_to_nameattributes(names)
name_attributes: List[x509.NameAttribute] = _names_to_nameattributes(names)
name_attributes.insert(0, x509.NameAttribute(NameOID.COMMON_NAME, common_name))

public_key = private_key.public_key()
Expand Down Expand Up @@ -138,8 +138,8 @@ def generate_self_signed_app_certificate(private_key: rsa.RSAPrivateKey,
def generate_app_certificate_signing_request(private_key: rsa.RSAPrivateKey,
common_name: str,
names: Dict[str, str],
subject_alt_names: list[x509.GeneralName],
extended: list[x509.ObjectIdentifier]
subject_alt_names: List[x509.GeneralName],
extended: List[x509.ObjectIdentifier]
) -> x509.CertificateSigningRequest:
"""Generate a certificate signing request for a OPC UA client/server application that is according to OPC 10000-4 6.1 / OPC 10000-6 6.2.2
Expand All @@ -154,7 +154,7 @@ def generate_app_certificate_signing_request(private_key: rsa.RSAPrivateKey,
x509.CertificateSigningRequest: The generated certificate signing request
"""

name_attributes: list[x509.NameAttribute] = _names_to_nameattributes(names)
name_attributes: List[x509.NameAttribute] = _names_to_nameattributes(names)
name_attributes.insert(0, x509.NameAttribute(NameOID.COMMON_NAME, common_name))

builder = x509.CertificateSigningRequestBuilder()
Expand Down
14 changes: 4 additions & 10 deletions examples/generate_certificates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Example of several certficate creation helpers"""
from typing import Dict
from typing import Dict, List
import asyncio
from pathlib import Path
import socket
Expand Down Expand Up @@ -41,7 +41,7 @@ def generate_private_key_for_myserver():


async def generate_self_signed_certificate():
subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myselfsignedserver"),
subject_alt_names: List[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myselfsignedserver"),
x509.DNSName(f"{HOSTNAME}")]

# key: RSAPrivateKey = generate_private_key()
Expand All @@ -58,7 +58,7 @@ async def generate_self_signed_certificate():


def generate_applicationgroup_ca():
subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myserver"),
subject_alt_names: List[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myserver"),
x509.DNSName(f"{HOSTNAME}")]

key: RSAPrivateKey = generate_private_key()
Expand All @@ -76,7 +76,7 @@ def generate_applicationgroup_ca():


async def generate_csr():
subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myserver"),
subject_alt_names: List[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myserver"),
x509.DNSName(f"{HOSTNAME}")]

key: RSAPrivateKey = generate_private_key()
Expand All @@ -95,12 +95,6 @@ async def generate_csr():


async def sign_csr():

# setup some common data (normally this differs for the CA and the CSR)

subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{HOSTNAME}:foobar:myserver"),
x509.DNSName(f"{HOSTNAME}")]

issuer = await load_certificate(base_certs / 'ca_application.der')
key_ca = await load_private_key(base_private / 'ca_application.pem')
csr_file: Path = base_csr / 'myserver.csr'
Expand Down
7 changes: 4 additions & 3 deletions tests/test_gen_certificates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Several tests for certificate /signing request generation"""
from typing import List
from datetime import datetime, timedelta
import socket
from cryptography import x509
Expand All @@ -19,7 +20,7 @@ async def test_create_self_signed_app_certificate():
'localityName': 'Foo',
'organizationName': "Bar Ltd",
}
subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{hostname}:foobar:myserver"),
subject_alt_names: List[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{hostname}:foobar:myserver"),
x509.DNSName(f"{hostname}")]

extended = [ExtendedKeyUsageOID.CLIENT_AUTH,
Expand Down Expand Up @@ -99,7 +100,7 @@ async def test_app_create_certificate_signing_request():
'localityName': 'Foo',
'organizationName': "Bar Ltd",
}
subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{hostname}:foobar:myserver"),
subject_alt_names: List[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{hostname}:foobar:myserver"),
x509.DNSName(f"{hostname}")]

extended = [ExtendedKeyUsageOID.CLIENT_AUTH,
Expand Down Expand Up @@ -148,7 +149,7 @@ async def test_app_sign_certificate_request():
'localityName': 'Foo',
'organizationName': "Bar Ltd",
}
subject_alt_names: list[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{hostname}:foobar:myserver"),
subject_alt_names: List[x509.GeneralName] = [x509.UniformResourceIdentifier(f"urn:{hostname}:foobar:myserver"),
x509.DNSName(f"{hostname}")]

extended = [ExtendedKeyUsageOID.CLIENT_AUTH,
Expand Down

0 comments on commit 119d6d7

Please sign in to comment.