Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from Ragarr/working
Browse files Browse the repository at this point in the history
Working
  • Loading branch information
Ragarr authored Nov 27, 2023
2 parents 6778eec + dc60dce commit ce1d27c
Show file tree
Hide file tree
Showing 12 changed files with 600 additions and 78 deletions.
4 changes: 4 additions & 0 deletions src/packages/authorities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .elPapa import ElPapa
from .pedroSanchez import PedroSanchez
from .ursula import Ursula
from .certificate import Certificate
26 changes: 26 additions & 0 deletions src/packages/authorities/certificate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from cryptography import x509

class Certificate:
def __init__(self, certificate: x509.Certificate, issuer_certificate = None) -> None:

self.__issuer_certificate = issuer_certificate

if issuer_certificate is None:
self.__issuer_certificate = certificate

self.__certificate = certificate


@property
def issuer_certificate(self) -> x509.Certificate:
return self.__issuer_certificate

@property
def certificate(self) -> x509.Certificate:
return self.__certificate

def __str__(self) -> str:
return f"\nCertificate: {str(self.__certificate)} - Issuer: {str(self.issuer_certificate)}"

def __repr__(self) -> str:
return self.__str__()
80 changes: 80 additions & 0 deletions src/packages/authorities/elPapa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import datetime
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from .certificate import Certificate
from .singleton import singleton

@singleton
class ElPapa:
def __init__(self):
self.__private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)

self.__subject = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, "VA"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Vaticano"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Roma"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Cristianismo"),
x509.NameAttribute(NameOID.COMMON_NAME, "Dios.com"),
])

x509certificate = x509.CertificateBuilder().subject_name(
self.__subject
).issuer_name(
self.__subject
).public_key(
self.__private_key.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.now(datetime.timezone.utc)
).not_valid_after(
# Our certificate will be valid for 10 days
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=10)
).add_extension(
x509.SubjectAlternativeName([x509.DNSName("localhost")]),
critical=False,
# Sign our certificate with our private key
).sign(self.__private_key, hashes.SHA256())

self.__certificate = Certificate(x509certificate)

@property
def certificate(self):
return self.__certificate

@property
def trusted_certs(self):
return [self.__certificate]

def info(self):
return self.__subject


def issueCertificate(self, csr) -> x509.Certificate:
x509certificate = x509.CertificateBuilder().subject_name(
csr.subject
).issuer_name(
self.__subject
).public_key(
csr.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.now(datetime.timezone.utc)
).not_valid_after(
# Our certificate will be valid for 10 days
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=10)
).sign(self.__private_key, hashes.SHA256())

certificate = Certificate(x509certificate, self.__certificate)

return certificate

if __name__ == '__main__':
a = ElPapa()
print(a.certificate)
64 changes: 64 additions & 0 deletions src/packages/authorities/pedroSanchez.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import datetime
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from .elPapa import ElPapa
from .certificate import Certificate
from .singleton import singleton

@singleton
class PedroSanchez:
def __init__(self) -> None:
self.__private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
self.__subject = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, "ES"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Madrid"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Madrid"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "PSOE"),
x509.NameAttribute(NameOID.COMMON_NAME, "Presidente de españa"),
])

csr = x509.CertificateSigningRequestBuilder().subject_name(
self.__subject
).sign(self.__private_key, hashes.SHA256())

elpapa = ElPapa()

self.__certificate = elpapa.issueCertificate(csr)

self.__trusted_certs = [self.__certificate] + elpapa.trusted_certs

@property
def trusted_certs(self):
return self.__trusted_certs

@property
def certificate(self):
return self.__certificate

def issueCertificate(self, csr) -> x509.Certificate:
certificate = x509.CertificateBuilder().subject_name(
csr.subject
).issuer_name(
self.__subject
).public_key(
csr.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.now(datetime.timezone.utc)
).not_valid_after(
# Our certificate will be valid for 10 days
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=10)
).sign(self.__private_key, hashes.SHA256())


return Certificate(certificate, self.__certificate)




7 changes: 7 additions & 0 deletions src/packages/authorities/singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
64 changes: 64 additions & 0 deletions src/packages/authorities/ursula.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import datetime
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from .elPapa import ElPapa

from .certificate import Certificate
from .singleton import singleton

@singleton
class Ursula:
def __init__(self) -> None:
self.__private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)

self.__subject = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, "DE"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Berlin"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Berlin"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Mercedes"),
x509.NameAttribute(NameOID.COMMON_NAME, "doishnewneitz.com"),
])

csr = x509.CertificateSigningRequestBuilder().subject_name(
self.__subject
).sign(self.__private_key, hashes.SHA256())

elpapa = ElPapa()

self.__certificate = elpapa.issueCertificate(csr)
self.__trusted_certs = [self.__certificate] + elpapa.trusted_certs

@property
def trusted_certs(self):
return self.__trusted_certs

@property
def certificate(self):
return self.__certificate

def issueCertificate(self, csr):
certificate = x509.CertificateBuilder().subject_name(
csr.subject
).issuer_name(
self.__subject
).public_key(
csr.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.now(datetime.timezone.utc)
).not_valid_after(
# Our certificate will be valid for 10 days
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=10)
).sign(self.__private_key, hashes.SHA256())

return Certificate(certificate, self.__certificate)




Loading

0 comments on commit ce1d27c

Please sign in to comment.