Files
@ 988ac40d5cec
Branch filter:
Location: gimmecert/gimmecert/storage.py
988ac40d5cec
3.7 KiB
text/x-python
GC-15: Implemented scenario for server certificate issuance where user has not initialised the CA hierarchy:
- Added functional test that tests if correct error is shown to user
in case he/she has not initialised the CA hierarchy.
- Introduced new function to check if storage is initialised.
- Added initial simplified server command implementation.
- Added functional test that tests if correct error is shown to user
in case he/she has not initialised the CA hierarchy.
- Introduced new function to check if storage is initialised.
- Added initial simplified server command implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2018 Branko Majic
#
# This file is part of Gimmecert.
#
# Gimmecert is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Gimmecert is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Gimmecert. If not, see <http://www.gnu.org/licenses/>.
#
import os
import cryptography.hazmat.primitives.serialization
import gimmecert.utils
def initialise_storage(project_directory):
"""
Initialises certificate storage in the given project directory.
Storage initialisation consists of creating the necessary
directory structure. Directories created under the passed-in
project directory are:
- .gimmcert/
- .gimmcert/ca/
:param project_directory: Path to directory under which the storage should be initialised.
:type project_directory: str
"""
os.mkdir(os.path.join(project_directory, '.gimmecert'))
os.mkdir(os.path.join(project_directory, '.gimmecert', 'ca'))
def write_private_key(private_key, path):
"""
Writes the passed-in private key to designated path in
OpenSSL-style PEM format.
The private key is written without any encryption.
:param private_key: Private key that should be written.
:type private_key: cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey
:param path: File path where the key should be written.
:type path: str
"""
private_key_pem = private_key.private_bytes(
encoding=cryptography.hazmat.primitives.serialization.Encoding.PEM,
format=cryptography.hazmat.primitives.serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=cryptography.hazmat.primitives.serialization.NoEncryption()
)
with open(path, 'wb') as key_file:
key_file.write(private_key_pem)
def write_certificate(certificate, path):
"""
Writes the passed-in certificate to designated path in
OpenSSL-style PEM format.
:param certificate: Certificate that should be writtent-out.
:type certificate: cryptography.x509.Certificate
:param path: File path where the certificate should be written.
:type path: str
"""
certificate_pem = certificate.public_bytes(encoding=cryptography.hazmat.primitives.serialization.Encoding.PEM)
with open(path, 'wb') as certificate_file:
certificate_file.write(certificate_pem)
def write_certificate_chain(certificate_chain, path):
"""
Writes the passed-in certificate chain to designated path in
OpenSSL-style PEM format. Certificates are separated with
newlines.
:param certificate_chain: List of certificates to output to the file.
:type certificate_chain: list[cryptography.x509.Certificate]
:param path: File path where the chain should be written.
:type path: str
"""
chain_pem = b"\n".join(
[gimmecert.utils.certificate_to_pem(certificate) for certificate in certificate_chain]
)
with open(path, 'wb') as certificate_chain_file:
certificate_chain_file.write(chain_pem)
def is_initialised(project_directory):
"""
Checks if Gimmecert has been initialised in designated project
directory.
:param project_directory: Path to project directory to check.
:type project_directory: str
"""
if os.path.exists(os.path.join(project_directory, '.gimmecert')):
return True
return False
|