Projekt

Obecné

Profil

« Předchozí | Další » 

Revize cc51ca2c

Přidáno uživatelem Stanislav Král před asi 4 roky(ů)

Re #8472 - Finished implementation of parse_cert_pem method that parses a subject out of a PEM formatted certificate

Added few unit tests testing this method.

Zobrazit rozdíly:

src/services/cryptography.py
2 2
import re
3 3

  
4 4
# encryption method to be used when generating private keys
5
from src.model.subject import Subject
5 6
from src.utils.temporary_file import TemporaryFile
6 7

  
7 8
PRIVATE_KEY_ENCRYPTION_METHOD = "-aes256"
......
227 228
            raise CryptographyException(OPENSSL_EXECUTABLE, args, err.decode())
228 229

  
229 230
    def parse_cert_pem(self, cert_pem):
231
        """
232
        Parses the given certificate in PEM format and returns the subject of the certificate
233
        :param cert_pem: a certificated in a PEM format to be parsed
234
        :return: a subject stored within the supplied certificate
235
        """
236
        # run openssl x509 to view certificate content
230 237
        args = ["x509", "-noout", "-text", "-in", "-"]
231

  
232 238
        result = self.__run_for_output(args, proc_input=bytes(cert_pem, encoding="utf-8")).decode()
239

  
240
        # find the line containing the subject
233 241
        match = re.search(r"Subject:\s(.*)", result)
234
        pass
235
        # TODO use logger
236 242
        if match is None:
243
            # TODO use logger
237 244
            print(f"Could not find subject to parse: {result}")
245
            return None
238 246
        else:
239
            found = re.findall(r"\s?([^=\s]+)\s?=\s?([^,\n]+)", match)
240
            print(found)
241
            for pair in found:
242
                print(pair)
243

  
247
            # find all attributes (key = value
248
            found = re.findall(r"\s?([^c=\s]+)\s?=\s?([^,\n]+)", match.group())
249
            subj = Subject()
250
            for key, value in found:
251
                if key == "C":
252
                    subj.country = value
253
                elif key == "ST":
254
                    subj.state = value
255
                elif key == "L":
256
                    subj.locality = value
257
                elif key == "O":
258
                    subj.organization = value
259
                elif key == "OU":
260
                    subj.organization_unit = value
261
                elif key == "CN":
262
                    subj.common_name = value
263
                elif key == "emailAddress":
264
                    subj.email_address = value
265
            return subj
244 266

  
245 267

  
246 268
class CryptographyException(Exception):

Také k dispozici: Unified diff