Skip to content

Commit

Permalink
SNOW-1820480 making OCSP validation code more resillient (#2107)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mkeller authored Nov 25, 2024
1 parent 9ddb205 commit 557774d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
- v3.12.4(TBD)
- Fixed a bug where multipart uploads to Azure would be missing their MD5 hashes.
- Fixed a bug where OpenTelemetry header injection would sometimes cause Exceptions to be thrown.
- Fixed a bug where OCSP checks would throw TypeError and make mainly GCP blob storage unreachable.

- v3.12.3(October 25,2024)
- Improved the error message for SSL-related issues to provide clearer guidance when an SSL error occurs.
Expand Down
18 changes: 17 additions & 1 deletion src/snowflake/connector/ocsp_asn1crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import typing
from base64 import b64decode, b64encode
from collections import OrderedDict
from datetime import datetime, timezone
Expand All @@ -28,6 +29,9 @@
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding, utils
from cryptography.hazmat.primitives.asymmetric.dsa import DSAPublicKey
from cryptography.hazmat.primitives.asymmetric.ec import ECDSA, EllipticCurvePublicKey
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
from OpenSSL.SSL import Connection

from snowflake.connector.errorcode import (
Expand Down Expand Up @@ -368,9 +372,21 @@ def verify_signature(self, signature_algorithm, signature, cert, data):
hasher = hashes.Hash(chosen_hash, backend)
hasher.update(data.dump())
digest = hasher.finalize()
additional_kwargs: dict[str, typing.Any] = dict()
if isinstance(public_key, RSAPublicKey):
additional_kwargs["padding"] = padding.PKCS1v15()
additional_kwargs["algorithm"] = utils.Prehashed(chosen_hash)
elif isinstance(public_key, DSAPublicKey):
additional_kwargs["algorithm"] = utils.Prehashed(chosen_hash)
elif isinstance(public_key, EllipticCurvePublicKey):
additional_kwargs["signature_algorithm"] = ECDSA(
utils.Prehashed(chosen_hash)
)
try:
public_key.verify(
signature, digest, padding.PKCS1v15(), utils.Prehashed(chosen_hash)
signature,
digest,
**additional_kwargs,
)
except InvalidSignature:
raise RevocationCheckError(msg="Failed to verify the signature")
Expand Down

0 comments on commit 557774d

Please sign in to comment.