Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Certificate

Entity: certificate.key

Compute Method: _compute_pem_key

Inputs Method Outputs
handlers = (
            lambda c, p: (serialization.load_der_private_key(c, p), False),
            lambda c, p: (serialization.load_pem_private_key(c, p), False),
            lambda c, _: (serialization.load_der_public_key(c), True),
            lambda c, _: (serialization.load_pem_public_key(c), True),
        )
for key in self:

            key.pem_key = None
            key.public = None
            key.loading_error = ""

            if not (content := key.content.content):
                continue

            pwd = key.password.encode('utf-8') if key.password else None
            loading_error = self.env._("This key could not be loaded. Either its content or its password is erroneous.")

            for handler in handlers:
                try:
                    pkey, key.public = handler(content, pwd)
                    break
                except (TypeError, ValueError):
                    pass
            else:
                key.loading_error = loading_error
                continue

            if key.public:
                key.pem_key = BinaryBytes(pkey.public_bytes(
                    encoding=Encoding.PEM,
                    format=serialization.PublicFormat.SubjectPublicKeyInfo,
                ))
            else:
                encryption = serialization.BestAvailableEncryption(pwd) if pwd else serialization.NoEncryption()
                key.pem_key = BinaryBytes(pkey.private_bytes(
                    encoding=Encoding.PEM,
                    format=serialization.PrivateFormat.PKCS8,
                    encryption_algorithm=encryption,
                ))

Back to entity