
    ph,                        S SK r S SKrS SKrS SKJrJr  S SKrS SKrS SKJ	r	J
r
  S SKJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S S	KJr  S S
KJrJr  S SKJrJr  S SKJr  S SKJ r J!r!  S r"SS jr#S r$S r%SS jr& " S S5      r'g)    N)urljoinurlparse)hazmatx509)InvalidSignature)backends)DSAPublicKey)ECDSAEllipticCurvePublicKey)PKCS1v15)RSAPublicKey)SHA1Hash)EncodingPublicFormat)ocsp)AuthorizationErrorConnectionErrorc                 r   U R                  5       n [        U[        5      (       a;  UR                  UR                  UR
                  [        5       UR                  5        g [        U[        5      (       a2  UR                  UR                  UR
                  UR                  5        g [        U[        5      (       a;  UR                  UR                  UR
                  [        UR                  5      5        g UR                  UR                  UR
                  5        g ! [         a    [        S5      ef = f)Nzfailed to valid ocsp response)
public_key
isinstancer   verify	signaturetbs_response_bytesr   signature_hash_algorithmr	   r   r
   r   r   )issuer_certocsp_responsepubkeys      D/home/james-whalen/.local/lib/python3.13/site-packages/redis/ocsp.py_verify_responser       s    ##%F?fl++MM''00
66	 --MM''0066
  677MM''00m<<= MM-11=3S3ST ?=>>?s    AD  "AD  )AD  9&D   D6c                    [         R                  " U5      nUR                  [         R                  R                  :X  a  [        S5      eUR                  [         R                  R                  :X  a]  UR                  [         R                  R                  :w  a4  [        S[        UR                  5      R                  S5      S    S35      eO[        S5      eUR                  [        R                  R                  5       :  a  [        S5      eUR                   (       a7  UR                   [        R                  R                  5       :  a  [        S5      eUR"                  nUR$                  nUR&                  nU nUb  X@R(                  :X  d  Xe:X  a  U nOUR*                  n[-        XXF5      n	 U	S	   n
U
R0                  R3                  [4        R6                  5      nUb2  [4        R8                  R:                  R<                  UR>                  ;  a  [        S5      eU
nU(       a  [A        Xs5        g! [.         a    [        S
5      ef = f)z=A wrapper the return the validity of a known ocsp certificatez4you are not authorized to view this ocsp certificatezReceived an .   z ocsp certificate statusz@failed to retrieve a successful response from the ocsp responderz)ocsp certificate was issued in the futurez1ocsp certificate has invalid update - in the pastr   z'no certificates found for the responderz'delegate not autorized for ocsp signingT)!r   load_der_ocsp_responseresponse_statusOCSPResponseStatusUNAUTHORIZEDr   
SUCCESSFULcertificate_statusOCSPCertStatusGOODr   strsplitthis_updatedatetimenownext_updateresponder_nameissuer_key_hashresponder_key_hashsubjectcertificates_get_certificates
IndexError
extensionsget_extension_for_classr   ExtendedKeyUsageoidExtendedKeyUsageOIDOCSP_SIGNINGvaluer    )r   
ocsp_bytesvalidater   r2   issuer_hashresponder_hashcert_to_validatecertsresponder_certsresponder_certexts               r   _check_certificaterI   2   s    //
;M$$(?(?(L(LL !WXX$$(?(?(J(JJ++t/B/B/G/GG!s=#C#CDJJ3OPQRS T* *  H N
 	
   H$5$5$9$9$;;IJJ 	!!%%(9(9(=(=(??QRR"11N//K"55N""111(&**+
	M,Q/N ''??@U@UV;$((66CC399T!"KLL))9  	M!"KLL	Ms   2H5 5Ic                 "   UcA  U  Vs/ s H2  n[        U5      U:X  d  M  UR                  UR                  :X  d  M0  UPM4     nnU$ U  Vs/ s H3  nUR                  U:X  d  M  UR                  UR                  :X  d  M1  UPM5     nnU$ s  snf s  snf N)_get_pubkey_hashissuerr5   )rE   r   r2   rC   cr6   s         r   r7   r7   m   s     
"n4 9:[EXEX9X  	 
 	 
yyN* /0xx;;N;N/N  	 
 

s!   BBB	B!B=Bc                    U R                  5       n[        U[        5      (       a/  UR                  [        R
                  [        R                  5      nOr[        U[        5      (       a/  UR                  [        R                  [        R                  5      nO.UR                  [        R
                  [        R                  5      n[        [        5       [        R                  " 5       S9nUR!                  U5        UR#                  5       $ )N)backend)r   r   r   public_bytesr   DERr   PKCS1r   X962UncompressedPointSubjectPublicKeyInfor   r   r   default_backendupdatefinalize)certificater   hsha1s       r   rL   rL   ~   s    ##%F &,''l.@.@A	F2	3	3|/M/MNl.O.OP 8 8 :;DKKN==?    c                 r   US;   a  [        S5      eSnU R                  5       R                  5       nU R                  5        H1  nUR                  5       nUR                  UR
                  :X  d  M/  Un  O   Uc  [        S5      eUb&  [        R                  " U5      nXG:w  a  [        S5      e[        X15      $ )zAn implementation of a function for set_ocsp_client_callback in PyOpenSSL.

This function validates that the provide ocsp_bytes response is valid,
and matches the expected, stapled responses.
)r]   Nzno ocsp response presentNz2no matching issuer cert found in certificate chainz/received and expected certificates do not match)	r   get_peer_certificateto_cryptographyget_peer_cert_chainr5   rM   r   load_pem_x509_certificaterI   )conr@   expectedr   	peer_certrN   certes           r   ocsp_staple_verifierrh      s     [ 899K((*::<I$$&  "<<9+++K	 ' RSS**84>!"STTk66r]   c                   L    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rSrg)OCSPVerifier   zA class to verify ssl sockets for RFC6960/RFC6961. This can be used
when using direct validation of OCSP responses and certificate revocations.

@see https://datatracker.ietf.org/doc/html/rfc6960
@see https://datatracker.ietf.org/doc/html/rfc6961
Nc                 4    Xl         X l        X0l        X@l        g rK   )SOCKHOSTPORTCA_CERTS)selfsockhostportca_certss        r   __init__OCSPVerifier.__init__   s    			 r]   c                     [         R                  " U5      n[        R                  " UR	                  5       [
        R                  " 5       5      nU$ )z?Convert SSL certificates in a binary (DER) format to ASCII PEM.)sslDER_cert_to_PEM_certr   rb   encoder   rW   )rq   derpemrf   s       r   
_bin2asciiOCSPVerifier._bin2ascii   s:     &&s+--cjjlH<T<T<VWr]   c                     U R                   R                  S5      nUSL a  [        S5      eU R                  U5      nU R	                  U5      $ )zThis function returns the certificate, primary issuer, and primary ocsp
server in the chain for a socket already wrapped with ssl.
TFz!no certificate found for ssl peer)rm   getpeercertr   r~   _certificate_components)rq   r|   rf   s      r   components_from_socket#OCSPVerifier.components_from_socket   sJ     ii##D)%<!"EFFs#++D11r]   c                     UR                   R                  [        R                  R                  R
                  5      R                  nU Vs/ s H9  nUR                  [        R                  R                  R                  :X  d  M7  UPM;     nn US   R                  R                  nU Vs/ s H9  nUR                  [        R                  R                  R                  :X  d  M7  UPM;     nn US   R                  R                  nXU4$ ! [        R                  R                   R                   a    [        S5      ef = fs  snf ! [         a    Sn Nf = fs  snf ! [         a    [        S5      ef = f)zGiven an SSL certificate, retract the useful components for
validating the certificate status with an OCSP server.

Args:
    cert ([bytes]): A PEM encoded ssl certificate
z-No AIA information present in ssl certificater   Nzno ocsp servers in certificate)r9   get_extension_for_oidr   r<   ExtensionOIDAUTHORITY_INFORMATION_ACCESSr?   cryptographyExtensionNotFoundr   access_methodAuthorityInformationAccessOID
CA_ISSUERSaccess_locationr8   OCSP)rq   rf   aiaiissuersrM   ocspsr   s           r   r   $OCSPVerifier._certificate_components   sc   	S//77%%BBe  
$(("H"H"S"SS  	 

	QZ//55F 
$(("H"H"M"MM  	 
	D8++11D T!!5   ++== 	S!"QRR	S
  	F	
  	D!"BCC	DsB   AD 6EEE	 -6E'E0E  4E	EE E6c                     [         R                  " U R                  U R                  4U R                  S9n[
        R                  " UR                  5       [        R                  " 5       5      nU R                  U5      $ )zReturn the certificate, primary issuer, and primary ocsp server
from the host defined by the socket. This is useful in cases where
different certificates are occasionally presented.
)ru   )ry   get_server_certificatern   ro   rp   r   rb   r{   r   rW   r   )rq   r}   rf   s      r   !components_from_direct_connection.OCSPVerifier.components_from_direct_connection   sY     (($))TYY)?$--X--cjjlH<T<T<VW++D11r]   c                    [         R                  " 5       nUR                  X#[        R                  R
                  R                  R                  5       5      nUR                  5       n[        R                  " UR                  [        R
                  R                  R                  R                  5      5      n[        XR!                  S5      5      nU$ )z#Return the complete url to the ocspascii)r   OCSPRequestBuilderadd_certificater   r   
primitiveshashesSHA256buildbase64	b64encoderQ   serializationr   rR   r   decode)rq   serverrf   r   orbrequestpathurls           r   build_certificate_url"OCSPVerifier.build_certificate_url   s    %%' !!|22==DDKKM
 ))+  !2!2!@!@!I!I!M!MN
 fkk'23
r]   c                    [         R                  " U5      nUR                  (       d  [        S5      eUR                  nU R                  U5      nU R                  XU5      n[        U5      R                  SS.n[         R                  " XxS9nUR                  (       d  [        S5      e[        XdR                  S5      $ )z3Checks the validity of an ocsp server for an issuerz"failed to fetch issuer certificatezapplication/ocsp-request)HostzContent-Type)headersz failed to fetch ocsp certificateT)
requestsgetokr   contentr~   r   r   netlocrI   )	rq   r   rf   
issuer_urlrr|   r   ocsp_urlheaders	            r   check_certificateOCSPVerifier.check_certificate  s     LL$tt!"FGGiiooc*--fKH X&--6
 LL2tt!"DEE!+yy$??r]   c                      U R                  5       u  pnUc  [        S5      eU R                  X1U5      $ ! [         a6    U R	                  5       u  pnUc  [        S5      eU R                  X1U5      s $ f = f)a  Returns the validity of the certificate wrapping our socket.
This first retrieves for validate the certificate, issuer_url,
and ocsp_server for certificate validate. Then retrieves the
issuer certificate from the issuer_url, and finally checks
the validity of OCSP revocation status.
z%no issuers found in certificate chain)r   r   r   r   r   )rq   rf   r   ocsp_servers       r   is_validOCSPVerifier.is_valid"  s    		I,0,G,G,I)Dk!%&MNN))+ZHH! 	I,0,R,R,T)Dk!%&MNN))+ZHH		Is   25 =A54A5)rp   rn   ro   rm   rK   )__name__
__module____qualname____firstlineno____doc__rv   r~   r   r   r   r   r   r   __static_attributes__ r]   r   rj   rj      s2    !
2&"P2 @*Ir]   rj   )TrK   )(r   r/   ry   urllib.parser   r   %cryptography.hazmat.primitives.hashesr   r   r   r   cryptography.exceptionsr   cryptography.hazmatr   -cryptography.hazmat.primitives.asymmetric.dsar	   ,cryptography.hazmat.primitives.asymmetric.ecr
   r   1cryptography.hazmat.primitives.asymmetric.paddingr   -cryptography.hazmat.primitives.asymmetric.rsar   r   r   ,cryptography.hazmat.primitives.serializationr   r   cryptography.x509r   redis.exceptionsr   r   r    rI   r7   rL   rh   rj   r   r]   r   <module>r      s`      
 * ,  % 4 ( F V F F < O " @?88v" 78JI JIr]   