-
Notifications
You must be signed in to change notification settings - Fork 1
Implement client side 1.2.1 spec #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from abc import abstractmethod | ||
| from typing import Any | ||
|
|
||
| from cbltest.api.jsonserializable import JSONSerializable | ||
| from cbltest.api.x509_certificate import CertKeyPair | ||
|
|
||
|
|
||
| class MultipeerReplicatorAuthenticator(JSONSerializable): | ||
| """ | ||
| The base class for replicator authenticators | ||
| """ | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Gets the type of authenticator (required for all authenticators)""" | ||
| return self.__name | ||
|
|
||
| def __init__(self, name: str) -> None: | ||
| self.__name = name | ||
|
|
||
| @abstractmethod | ||
| def to_json(self) -> Any: | ||
| pass | ||
|
|
||
|
|
||
| class MultipeerReplicatorCAAuthenticator(MultipeerReplicatorAuthenticator): | ||
| """ | ||
| Represents an authenticator based on a CA certificate. Use the | ||
| :class:`cbltest.api.x509_certificate.X509Generator` if you need to generate a CA certificate. | ||
| """ | ||
|
|
||
| def __init__(self, ca_data: CertKeyPair) -> None: | ||
| super().__init__("CA-CERT") | ||
| self.__ca_data = ca_data | ||
|
|
||
| def to_json(self) -> dict[str, Any]: | ||
| return { | ||
| "type": self.name, | ||
| "params": {"certificate": self.__ca_data.pem_bytes().decode("utf-8")}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from cryptography.hazmat.primitives import hashes | ||
| from cryptography.hazmat.primitives.asymmetric import ec | ||
| from cryptography.hazmat.primitives.serialization import Encoding, NoEncryption, pkcs12 | ||
| from cryptography.x509 import ( | ||
| BasicConstraints, | ||
| Certificate, | ||
| CertificateBuilder, | ||
| ExtendedKeyUsage, | ||
| ExtendedKeyUsageOID, | ||
| Name, | ||
| NameAttribute, | ||
| NameOID, | ||
| random_serial_number, | ||
| ) | ||
|
|
||
|
|
||
| class CertKeyPair: | ||
| """ | ||
| A class representing a certificate and its associated private key. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, certificate: Certificate, private_key: ec.EllipticCurvePrivateKey | ||
| ): | ||
| self.certificate = certificate | ||
| self.private_key = private_key | ||
|
|
||
| def pfx_bytes(self) -> bytes: | ||
| """ | ||
| Returns the certificate and private key in PFX format. | ||
| """ | ||
| ret_val = pkcs12.serialize_key_and_certificates( | ||
| name=b"cbltest", | ||
| key=self.private_key, | ||
| cert=self.certificate, | ||
| cas=None, | ||
| encryption_algorithm=NoEncryption(), | ||
| ) | ||
|
|
||
| return ret_val | ||
|
|
||
| def pem_bytes(self) -> bytes: | ||
| """ | ||
| Returns the certificate in PEM format. | ||
| """ | ||
| return self.certificate.public_bytes(encoding=Encoding.PEM) | ||
|
|
||
|
|
||
| def create_ca_certificate(CN: str) -> CertKeyPair: | ||
| private_key = ec.generate_private_key(ec.SECP256R1()) | ||
| cn_attribute = Name([NameAttribute(NameOID.COMMON_NAME, CN)]) | ||
| not_valid_before = datetime.now(timezone.utc) | ||
| not_valid_after = not_valid_before + timedelta(days=1) | ||
|
|
||
| ca_certificate: Certificate = ( | ||
| CertificateBuilder() | ||
| .subject_name(cn_attribute) | ||
| .issuer_name(cn_attribute) | ||
| .public_key(private_key.public_key()) | ||
| .serial_number(random_serial_number()) | ||
| .not_valid_before(not_valid_before) | ||
| .not_valid_after(not_valid_after) | ||
| .add_extension(BasicConstraints(ca=True, path_length=None), critical=True) | ||
| .sign(private_key, hashes.SHA256()) | ||
| ) | ||
|
|
||
| return CertKeyPair(ca_certificate, private_key) | ||
|
|
||
|
|
||
| def create_leaf_certificate( | ||
| CN: str, *, issuer_data: CertKeyPair | None = None | ||
| ) -> CertKeyPair: | ||
| private_key = ec.generate_private_key(ec.SECP256R1()) | ||
| cn_attribute = Name([NameAttribute(NameOID.COMMON_NAME, CN)]) | ||
| not_valid_before = datetime.now(timezone.utc) | ||
| not_valid_after = not_valid_before + timedelta(days=1) | ||
| issuer_name = issuer_data.certificate.subject if issuer_data else cn_attribute | ||
| signing_key = issuer_data.private_key if issuer_data else private_key | ||
|
|
||
| leaf_certificate = ( | ||
| CertificateBuilder() | ||
| .subject_name(cn_attribute) | ||
| .issuer_name(issuer_name) | ||
| .public_key(private_key.public_key()) | ||
| .serial_number(random_serial_number()) | ||
| .not_valid_before(not_valid_before) | ||
| .not_valid_after(not_valid_after) | ||
| .add_extension( | ||
| ExtendedKeyUsage( | ||
| [ExtendedKeyUsageOID.CLIENT_AUTH, ExtendedKeyUsageOID.SERVER_AUTH] | ||
| ), | ||
| critical=False, | ||
| ) | ||
| .sign(signing_key, hashes.SHA256()) | ||
| ) | ||
|
|
||
| return CertKeyPair(leaf_certificate, private_key) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.