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.certificate

Compute Method: _compute_private_key

View 19.0
Inputs Method Outputs
  • pem_certificate
  • pkcs12_password
  • content_format
  • company_id
  • company_id.id
  • subject_common_name
  • name
+ attachments = self.env['ir.attachment'].search([
+             ('res_model', '=', 'certificate.key'),
+             ('res_field', '=', 'content'),
+             ('res_id', 'in', self.ids)
+         ])
+ content_to_key_id = {(att.datas, att.company_id.id): att.res_id for att in attachments}
+ for certificate in self:
+             if not certificate.pem_certificate:
+                 certificate.private_key_id = None
+                 continue
+ 
+             content = certificate.with_context(bin_size=False).content
+             key_password = certificate.pkcs12_password.encode('utf-8') if certificate.pkcs12_password else None
+             key = None
+ 
+             # Create the private key if using PKCS12 or PEM files and no private key is set
+             if certificate.content_format == 'pkcs12':
+                 with suppress(ValueError, TypeError, UnsupportedAlgorithm):
+                     key, _cert, _additional_certs = pkcs12.load_key_and_certificates(base64.b64decode(content), key_password)
+             elif certificate.content_format == 'pem':
+                 with suppress(ValueError, TypeError, UnsupportedAlgorithm):
+                     key = serialization.load_pem_private_key(base64.b64decode(content), password=key_password)
+ 
+             if key:
+                 pem_key = base64.b64encode(key.private_bytes(
+                     encoding=Encoding.PEM,
+                     format=serialization.PrivateFormat.PKCS8,
+                     encryption_algorithm=serialization.NoEncryption()
+                 ))
+                 key_id = content_to_key_id.get((pem_key, certificate.company_id.id))
+                 if not key_id:
+                     key_id = self.env['certificate.key'].create({
+                         'name': (certificate.subject_common_name or certificate.name or "") + ".key",
+                         'content': pem_key,
+                         'company_id': certificate.company_id.id,
+                     })
+                 certificate.private_key_id = key_id
  • private_key_id

Back to entity