Skip to content

Commit d84238e

Browse files
committed
moz_kinto_publisher: extract subject and spki hash from the certificate when they are not in the remote settings record
1 parent 006b178 commit d84238e

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

moz_kinto_publisher/main.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from cryptography import x509
1818
from cryptography.hazmat.backends import default_backend
19+
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
1920
from kinto_http import Client
2021
from kinto_http.exceptions import KintoException
2122
from kinto_http.patch_type import BasicPatch
@@ -300,33 +301,37 @@ def __init__(self, **kwargs):
300301
[
301302
"derHash",
302303
"id",
303-
"pubKeyHash",
304-
"subject",
305304
],
306305
kwargs,
307306
):
308307
raise parseError
309308

310309
try:
311-
self.pubKeyHash = base64.b64decode(
312-
kwargs["pubKeyHash"], altchars="-_", validate=True
313-
) # sha256 of the SPKI
310+
if "pubKeyHash" in kwargs:
311+
self.pubKeyHash = base64.b64decode(
312+
kwargs["pubKeyHash"], altchars="-_", validate=True
313+
) # sha256 of the SPKI
314+
else:
315+
self.pubKeyHash = None
316+
except base64.binascii.Error:
317+
raise parseError
318+
319+
if self.pubKeyHash and len(self.pubKeyHash) != 32:
320+
raise IntermediateRecordError(f"Invalid pubkey hash: {kwargs}")
314321

322+
try:
315323
if "derHash" in kwargs:
316324
self.derHash = base64.b64decode(
317325
kwargs["derHash"], altchars="-_", validate=True
318326
)
327+
else:
328+
self.derHash = None
319329
except base64.binascii.Error:
320330
raise parseError
321331

322-
if len(self.pubKeyHash) != 32:
323-
raise IntermediateRecordError(f"Invalid pubkey hash: {kwargs}")
324-
325332
if self.derHash and len(self.derHash) != 32:
326333
raise IntermediateRecordError(f"Invalid DER hash. {kwargs}")
327334

328-
self.subject = kwargs["subject"]
329-
330335
if "pem" in kwargs:
331336
self.set_pem(kwargs["pem"])
332337

@@ -376,14 +381,28 @@ def set_pem(self, pem_data):
376381
self.pemData = pem_data
377382
self.pemHash = hashlib.sha256(pem_data.encode("utf-8")).hexdigest()
378383
derCert = asciiPemToBinaryDer(pem_data)
379-
self.derHash = hashlib.sha256(derCert).digest()
380384
try:
381385
self.cert = x509.load_pem_x509_certificate(
382386
pem_data.encode("utf-8"), default_backend()
383387
)
384388
except Exception as e:
385389
raise IntermediateRecordError("Cannot parse PEM data: {}".format(e))
386390

391+
derHash = hashlib.sha256(self.cert.public_bytes(Encoding.DER)).digest()
392+
if self.derHash and self.derHash != derHash:
393+
raise IntermediateRecordError("DER hash does not match")
394+
self.derHash = derHash
395+
396+
self.subject = self.cert.subject.rfc4514_string()
397+
398+
derSpki = self.cert.public_key().public_bytes(
399+
encoding=Encoding.DER, format=PublicFormat.SubjectPublicKeyInfo
400+
)
401+
spkiHash = hashlib.sha256(derSpki).digest()
402+
if self.pubKeyHash and self.pubKeyHash != spkiHash:
403+
raise IntermediateRecordError("SPKI hash does not match")
404+
self.pubKeyHash = spkiHash
405+
387406
def download_pem(self, kinto_client):
388407
if not self.pemAttachment:
389408
raise Exception("pemAttachment not set")

0 commit comments

Comments
 (0)