| 1 |
import base64 |
| 2 |
|
| 3 |
from cryptography import x509 |
| 4 |
from cryptography.x509 import ObjectIdentifier |
| 5 |
from cryptography.x509.oid import NameOID |
| 6 |
from cryptography.hazmat.primitives import hashes, serialization |
| 7 |
|
| 8 |
from odoo import _, api, models, service |
| 9 |
from odoo.exceptions import UserError |
| 10 |
|
| 11 |
CERT_TEMPLATE_NAME = { |
| 12 |
'prod': b'\x0c\x12ZATCA-Code-Signing', |
| 13 |
'sandbox': b'\x13\x15PREZATCA-Code-Signing', |
| 14 |
'preprod': b'\x13\x15PREZATCA-Code-Signing', |
| 15 |
} |
| 16 |
|
| 17 |
MAX_ALLOWED_CSR_VALUE_LENGTH = 64 |
| 18 |
|
| 19 |
|
| 20 |
class CertificateCertificate(models.Model): |
| 21 |
_inherit = 'certificate.certificate' |
| 22 |
|
| 23 |
def _l10n_sa_get_issuer_name(self): |
| 24 |
self.ensure_one() |
| 25 |
cert = x509.load_pem_x509_certificate(base64.b64decode(self.pem_certificate)) |
| 26 |
return ', '.join([s.rfc4514_string() for s in cert.issuer.rdns[::-1]]) |
| 27 |
|
| 28 |
@api.model |
| 29 |
def _l10n_sa_get_csr_vals(self, journal): |
| 30 |
company_id = journal.company_id |
| 31 |
parent_company_id = journal.company_id.parent_id |
| 32 |
version_info = service.common.exp_version() |
| 33 |
return { |
| 34 |
"country_name": { |
| 35 |
"value": company_id.country_id.code, |
| 36 |
"name": _("Country Name"), |
| 37 |
}, |
| 38 |
"org_unit_name": { |
| 39 |
"value": company_id.name if parent_company_id else company_id.vat[:10], |
| 40 |
"name": _("Company Name"), |
| 41 |
}, |
| 42 |
"org_name": { |
| 43 |
"value": parent_company_id.name if parent_company_id else company_id.name, |
| 44 |
"name": _("Parent Company Name") if parent_company_id else _("Company Name"), |
| 45 |
}, |
| 46 |
"common_name": { |
| 47 |
"value": f"{journal.code}-{journal.name}-{company_id.name}", |
| 48 |
"name": _("Common Name"), |
| 49 |
}, |
| 50 |
"org_id": { |
| 51 |
"value": parent_company_id.vat if parent_company_id else company_id.vat, |
| 52 |
"name": _("Parent Company VAT") if parent_company_id else _("Company VAT"), |
| 53 |
}, |
| 54 |
"state_name": { |
| 55 |
"value": company_id.state_id.name, |
| 56 |
"name": _("State/Province Name"), |
| 57 |
}, |
| 58 |
"locality_name": { |
| 59 |
"value": company_id.city, |
| 60 |
"name": _("Locality Name"), |
| 61 |
}, |
| 62 |
"egs_serial": { |
| 63 |
"value": f"1-Odoo|2-{version_info['server_serie']}|3-{journal.id}", |
| 64 |
"name": _("Journal Serial Number"), |
| 65 |
}, |
| 66 |
"org_uid": { |
| 67 |
"value": company_id.vat, |
| 68 |
"name": _("Company VAT"), |
| 69 |
}, |
| 70 |
"invoice_type": { |
| 71 |
"value": company_id._l10n_sa_get_csr_invoice_type(), |
| 72 |
"name": _("Invoice Type"), |
| 73 |
}, |
| 74 |
"location": { |
| 75 |
"value": company_id.street, |
| 76 |
"name": _("Street"), |
| 77 |
}, |
| 78 |
"industry": { |
| 79 |
"value": company_id.partner_id.industry_id.name or _("Other"), |
| 80 |
"name": _("Partner Industry Name"), |
| 81 |
}, |
| 82 |
"cert_tmp": { |
| 83 |
"value": CERT_TEMPLATE_NAME[company_id.l10n_sa_api_mode], |
| 84 |
"name": _("Certificate Template Name"), |
| 85 |
}, |
| 86 |
} |
| 87 |
|
| 88 |
@api.model |
| 89 |
def _l10n_sa_validate_csr_vals(self, journal): |
| 90 |
error_fields = set() |
| 91 |
for data in self._l10n_sa_get_csr_vals(journal).values(): |
| 92 |
if len(str(data['value'])) > MAX_ALLOWED_CSR_VALUE_LENGTH: |
| 93 |
error_fields.add(data['name']) |
| 94 |
if error_fields: |
| 95 |
company_fields = [_("Company Name"), _("Parent Company Name")] |
| 96 |
company_msg = _("<br/><br/>Once the journal is onboarded, please update the company name to match the one listed on the VAT Registration Certificate.") if any(field in error_fields for field in company_fields) else "" |
| 97 |
raise UserError(_( |
| 98 |
"Please make sure the following fields are shorter than %(max_length)d characters: %(error_fields_msg)s", |
| 99 |
max_length=MAX_ALLOWED_CSR_VALUE_LENGTH, |
| 100 |
error_fields_msg=" <br/>- " + " <br/>- ".join(error_fields) + company_msg |
| 101 |
)) |
| 102 |
|
| 103 |
@api.model |
| 104 |
def _l10n_sa_get_csr_str(self, journal): |
| 105 |
""" |
| 106 |
Return a string representation of a ZATCA compliant CSR that will be sent to the Compliance API in order to get back |
| 107 |
a signed X509 certificate |
| 108 |
""" |
| 109 |
if not journal: |
| 110 |
return |
| 111 |
|
| 112 |
builder = x509.CertificateSigningRequestBuilder() |
| 113 |
self._l10n_sa_validate_csr_vals(journal) |
| 114 |
csr_vals = {key: data['value'] for key, data in self._l10n_sa_get_csr_vals(journal).items()} |
| 115 |
subject_names = ( |
| 116 |
# Country Name |
| 117 |
(NameOID.COUNTRY_NAME, csr_vals['country_name']), |
| 118 |
# Organization Unit Name |
| 119 |
(NameOID.ORGANIZATIONAL_UNIT_NAME, csr_vals['org_unit_name']), |
| 120 |
# Organization Name |
| 121 |
(NameOID.ORGANIZATION_NAME, csr_vals['org_name']), |
| 122 |
# Subject Common Name |
| 123 |
(NameOID.COMMON_NAME, csr_vals['common_name']), |
| 124 |
# Organization Identifier |
| 125 |
(ObjectIdentifier('2.5.4.97'), csr_vals['org_id']), |
| 126 |
# State/Province Name |
| 127 |
(NameOID.STATE_OR_PROVINCE_NAME, csr_vals['state_name']), |
| 128 |
# Locality Name |
| 129 |
(NameOID.LOCALITY_NAME, csr_vals['locality_name']), |
| 130 |
) |
| 131 |
# The CertificateSigningRequestBuilder instances are immutable, which is why everytime we modify one, |
| 132 |
# we have to assign it back to itself to keep track of the changes |
| 133 |
builder = builder.subject_name(x509.Name([ |
| 134 |
x509.NameAttribute(n[0], '%s' % n[1]) for n in subject_names |
| 135 |
])) |
| 136 |
|
| 137 |
x509_alt_names_extension = x509.SubjectAlternativeName([ |
| 138 |
x509.DirectoryName(x509.Name([ |
| 139 |
# EGS Serial Number. Manufacturer or Solution Provider Name, Model or Version and Serial Number. |
| 140 |
# To be written in the following format: "1-... |2-... |3-..." |
| 141 |
x509.NameAttribute(ObjectIdentifier('2.5.4.4'), csr_vals['egs_serial']), |
| 142 |
# Organisation Identifier (UID) |
| 143 |
x509.NameAttribute(NameOID.USER_ID, csr_vals['org_uid']), |
| 144 |
# Invoice Type. 4-digit numerical input using 0 & 1 |
| 145 |
x509.NameAttribute(NameOID.TITLE, csr_vals['invoice_type']), |
| 146 |
# Location |
| 147 |
x509.NameAttribute(ObjectIdentifier('2.5.4.26'), csr_vals['location']), |
| 148 |
# Industry |
| 149 |
x509.NameAttribute(ObjectIdentifier('2.5.4.15'), csr_vals['industry']), |
| 150 |
])) |
| 151 |
]) |
| 152 |
|
| 153 |
x509_extensions = ( |
| 154 |
# Add Certificate template name extension |
| 155 |
(x509.UnrecognizedExtension(ObjectIdentifier('1.3.6.1.4.1.311.20.2'), |
| 156 |
csr_vals['cert_tmp']), False), |
| 157 |
# Add alternative names extension |
| 158 |
(x509_alt_names_extension, False), |
| 159 |
) |
| 160 |
|
| 161 |
for ext in x509_extensions: |
| 162 |
builder = builder.add_extension(ext[0], critical=ext[1]) |
| 163 |
|
| 164 |
private_key = serialization.load_pem_private_key(base64.b64decode(journal.company_id.l10n_sa_private_key_id.pem_key), password=None) |
| 165 |
request = builder.sign(private_key, hashes.SHA256()) |
| 166 |
|
| 167 |
return base64.b64encode(request.public_bytes(serialization.Encoding.PEM)).decode() |