Coverage for jbank/management/commands/make_x509.py: 0%

# Coverage for jbank/management/commands/make_x509.py : 0%

## 29 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import logging

2 from datetime import datetime, timedelta

3 from cryptography import x509

4 from cryptography.hazmat.backends import default_backend

5 from cryptography.hazmat.primitives import serialization, hashes

6 from cryptography.hazmat.primitives.asymmetric import rsa

7 from django.core.management.base import CommandParser

8 from jutil.command import SafeCommand

9

10

11 logger = logging.getLogger(__name__)

12

13

14 class Command(SafeCommand):

15 help = """

16 Generates RSA private key and x509 certificate in .pem format (for testing)

17 """

18

19 def add_arguments(self, parser: CommandParser):

20 parser.add_argument('--key-file', type=str, default='key.pem')

21 parser.add_argument('--cert-file', type=str, default='cert.pem')

22 parser.add_argument('--country', type=str, default='US')

23 parser.add_argument('--state', type=str, default='TX')

24 parser.add_argument('--locality', type=str, default='Dallas')

25 parser.add_argument('--org-name', type=str, default='Kajala Group')

26 parser.add_argument('--common-name', type=str, default='kajala.com')

27

28 def do(self, *args, **options):

29 # Generate our key

30 key = rsa.generate_private_key(

31 public_exponent=65537,

32 key_size=2048,

33 backend=default_backend()

34 )

35

36 # Write to disk unencrypted

37 with open(options['key_file'], "wb") as f:

38 f.write(key.private_bytes(

39 encoding=serialization.Encoding.PEM,

40 format=serialization.PrivateFormat.TraditionalOpenSSL,

41 encryption_algorithm=serialization.NoEncryption(),

42 ))

43 print('{} written'.format(f.name))

44

45 # Various details about who we are. For a self-signed certificate the

46 # subject and issuer are always the same.

47 subject = issuer = x509.Name([

48 x509.NameAttribute(x509.NameOID.COUNTRY_NAME, options['country']),

49 x509.NameAttribute(x509.NameOID.STATE_OR_PROVINCE_NAME, options['state']),

50 x509.NameAttribute(x509.NameOID.LOCALITY_NAME, options['locality']),

51 x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, options['org_name']),

52 x509.NameAttribute(x509.NameOID.COMMON_NAME, options['common_name']),

53 ])

54 cert = x509.CertificateBuilder().subject_name(

55 subject

56 ).issuer_name(

57 issuer

58 ).public_key(

59 key.public_key()

60 ).serial_number(

61 x509.random_serial_number()

62 ).not_valid_before(

63 datetime.utcnow()

64 ).not_valid_after(

65 # Our certificate will be valid for 10 days

66 datetime.utcnow() + timedelta(days=10)

67 ).add_extension(

68 x509.SubjectAlternativeName([x509.DNSName("localhost")]),

69 critical=False,

70 ).sign(key, hashes.SHA256(), default_backend())

71

72 # Write our certificate out to disk.

73 with open(options['cert_file'], "wb") as f:

74 f.write(cert.public_bytes(serialization.Encoding.PEM))

75 print('{} written'.format(f.name))