
    Bfi%                         d Z dZdZdZdZdZdZddlZddlZ ej                  e
      Z	 dd	lmZ dd
lmZ dZ G d de      Zy# e$ r$ZdZdZddlZ ej,                  e       Y dZ[0dZ[ww xY w)zAndg_httpsclient - module containing SSL peer verification class.
zP J Kershaw (STFC)z09/12/11z2(C) 2012 Science and Technology Facilities Councilz-BSD - see LICENSE file in top-level directoryzPhilip.Kershaw@stfc.ac.ukz$Id$    N)SubjectAltName)decoderTFzPSubjectAltName support is disabled - check pyasn1 package installation to enablec                   <   e Zd ZdZdddddddd	d
dd
ZdZddj                   eej                                eej                               z         z  Z
 ej                  e
      ZdZddZd Zd Zed        Zd Zd Z eeed      Zd Zd Z eeed      Zy)ServerSSLCertVerificationzyCheck server identity.  If hostname doesn't match, allow match of
    host's Distinguished Name against server DN settingCNOUOCEMAILADDRESSLSTSTREETDCUID)

commonNameorganisationalUnitNameorganisationcountryNameemailAddresslocalityNamestateOrProvinceNamestreetAddressdomainComponentuserids   subjectAltNamez/(%s)=|)
__hostname__certDN__subj_alt_name_matchNc                     d| _         d| _        ||| _        ||| _        |r+t        st
        j                  d       d| _        yd| _        yt
        j                  d       d| _        y)a  Override parent class __init__ to enable setting of certDN
        setting

        @type certDN: string
        @param certDN: Set the expected Distinguished Name of the
        server to avoid errors matching hostnames.  This is useful
        where the hostname is not fully qualified
        @type hostname: string
        @param hostname: hostname to match against peer certificate 
        subjectAltNames or subject common name
        @type subj_alt_name_match: bool
        @param subj_alt_name_match: flag to enable/disable matching of hostname
        against peer certificate subjectAltNames.  Nb. A setting of True will 
        be ignored if the pyasn1 package is not installed
        NzdOverriding "subj_alt_name_match" keyword setting: peer verification with subjectAltNames is disabledFTz9Disabling peer verification with subject subjectAltNames!)	"_ServerSSLCertVerification__certDN$_ServerSSLCertVerification__hostnamecertDNhostnameSUBJ_ALT_NAME_SUPPORTlogwarning/_ServerSSLCertVerification__subj_alt_name_matchdebug)selfr"   r#   subj_alt_name_matchs       w/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/ndg/httpsclient/ssl_peer_verification.py__init__z"ServerSSLCertVerification.__init__2   sn       DK$DM( Q R-2*-1*II ) *).D&    c                    |j                         r%t        j                  d|j                                y|dk(  r|j                         }|j	                         }|j                          | j                  | j                  t        j                  d       y| j                  r!| j                  |      }| j                  |v r|S |j                  | j                  k(  r|S t        j                  d|j                  | j                         y|| j                  k(  r|S t        j                  d|| j                         y|S )a
  Verify server certificate

        @type connection: OpenSSL.SSL.Connection
        @param connection: SSL connection object
        @type peerCert: basestring
        @param peerCert: server host certificate as OpenSSL.crypto.X509
        instance
        @type errorStatus: int
        @param errorStatus: error status passed from caller.  This is the value
        returned by the OpenSSL C function X509_STORE_CTX_get_error().  Look-up
        x509_vfy.h in the OpenSSL source to get the meanings of the different
        codes.  PyOpenSSL doesn't help you!
        @type errorDepth: int
        @param errorDepth: a non-negative integer representing where in the
        certificate chain the error occurred. If it is zero it occured in the
        end entity certificate, one if it is the certificate which signed the
        end entity certificate and so on.

        @type preverifyOK: int
        @param preverifyOK: the error status - 0 = Error, 1 = OK of the current
        SSL context irrespective of any verification checks done here.  If this
        function yields an OK status, it should enforce the preverifyOK value
        so that any error set upstream overrides and is honoured.
        @rtype: int
        @return: status code - 0/False = Error, 1/True = OK
        z4Certificate %r in peer certificate chain has expiredFr   z?No "hostname" or "certDN" set to check peer certificate againstz7Peer certificate CN %r doesn't match the expected CN %rz7Peer certificate DN %r doesn't match the expected DN %r)has_expiredr%   errorget_subjectget_componentssortr"   r#   r'   _get_subj_alt_namer   )	r)   
connectionpeerCerterrorStatus
errorDepthpreverifyOKpeerCertSubj
peerCertDN	dns_namess	            r+   __call__z"ServerSSLCertVerification.__call__W   s*   8 !IIL**,. 1_ $//1L%446JOO{{"==(II 4 5  -- $ 7 7 AI}}	1**  **dmm;&&II /0<0G0G"mm- !,&&II /0:DKKI r-   c                       fd}|S )Nc                 .    j                  | ||||      S N)r=   )r5   r6   r7   r8   r9   r)   s        r+   verify_server_certzQServerSSLCertVerification.get_verify_server_cert_func.<locals>.verify_server_cert   s    ==X{!+[: :r-    )r)   rA   s   ` r+   get_verify_server_cert_funcz5ServerSSLCertVerification.get_verify_server_cert_func   s    	:
 "!r-   c           	         g }t               }t        |j                               D ]  }|j                  |      }|j	                         }|| j
                  k(  s4|j                         }t        j                  ||      }|D ]e  }	t        |	t               st        t        |	            D ];  }
|	j                  |
      }|j                  t        |j                                      = g  |S )a  Extract subjectAltName DNS name settings from certificate extensions
        
        @param peer_cert: peer certificate in SSL connection.  subjectAltName
        settings if any will be extracted from this
        @type peer_cert: OpenSSL.crypto.X509
        )asn1Spec)r   rangeget_extension_countget_extensionget_short_nameSUBJ_ALT_NAME_EXT_NAMEget_datader_decoderdecode
isinstancelengetComponentByPositionappendstrgetComponent)cls	peer_certdns_namegeneral_namesiextext_nameext_datdecoded_datnameentry	components               r+   r4   z,ServerSSLCertVerification._get_subj_alt_name   s     &(y4467 	KA))!,C))+H3555,,.)00:GI ( KD!$7%*3t9%5 KE(,(C(CE(JI$OOC	0F0F0H,IJKK	K r-   c                     | j                   S r@   )r    r)   s    r+   
_getCertDNz$ServerSSLCertVerification._getCertDN   s    }}r-   c           	         t        |t              r|j                  d      }| j                  j                  j                  |      }t        |      dk  rt        d|z        t        t        |dd d   |dd d               | _
        | j                  j                          y t        |t              s(|D ]  }t        |      dk(  rt        d       || _
        y t        d      )N"   zError parsing DN string: "%s"   zSExpecting list of two element DN field, DN field value pairs for "certDN" attributez4Expecting list or string type for "certDN" attribute)rN   rR   strip	__class__	PARSER_REsplitrO   	TypeErrorlistzipr    r3   )r)   valr"   dnFieldsrX   s        r+   
_setCertDNz$ServerSSLCertVerification._setCertDN   s    c3YYs^F~~//55f=H8}q  ?& HII Xadd^Xadd^!DEDMMM C& 11v{# %0 1 11
  DM ( ) )r-   z)Distinguished Name for Server Certificate)fgetfsetdocc                     | j                   S r@   )r!   ra   s    r+   _getHostnamez&ServerSSLCertVerification._getHostname   s    r-   c                 H    t        |t              st        d      || _        y )Nz,Expecting string type for hostname attribute)rN   rR   rk   r!   )r)   rn   s     r+   _setHostnamez&ServerSSLCertVerification._setHostname   s%    #s# - . .r-   zhostname of server)NNT)__name__
__module____qualname____doc__DN_LUTrJ   joinrl   keysvaluesPARSER_RE_STRrecompileri   	__slots__r,   r=   rC   classmethodr4   rb   rp   propertyr"   ru   rw   r#   rB   r-   r+   r   r      s    ; %)$($'$'$2$'$($,$($)F /sxxV[[](;(,V]]_(=)>  ? ?M

=)ICI#/JGR"  6). :%EGF
 \)02Hr-   r   )r{   
__author____date____copyright____license____contact____revision__r   logging	getLoggerrx   r%   ndg.httpsclient.subj_alt_namer   pyasn1.codec.derr   rL   r$   ImportErroreSUBJ_ALT_NAME_SUPPORT_MSGwarningswarnobjectr   rB   r-   r+   <module>r      s   !
D=) 	 g!-<7 R2 R2  -!	!  HMM+,,-s   A A-	A((A-