This repository has been archived by the owner on May 31, 2024. It is now read-only.
forked from seniorbeto/Criptografia_2023-24
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Ragarr/working
Working
- Loading branch information
Showing
12 changed files
with
600 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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,4 @@ | ||
from .elPapa import ElPapa | ||
from .pedroSanchez import PedroSanchez | ||
from .ursula import Ursula | ||
from .certificate import Certificate |
This file contains 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,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__() |
This file contains 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,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) |
This file contains 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,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) | ||
|
||
|
||
|
||
|
This file contains 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,7 @@ | ||
def singleton(class_): | ||
instances = {} | ||
def getinstance(*args, **kwargs): | ||
if class_ not in instances: | ||
instances[class_] = class_(*args, **kwargs) | ||
return instances[class_] | ||
return getinstance |
This file contains 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,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) | ||
|
||
|
||
|
||
|
Oops, something went wrong.