U
    �AcjôO  ã                   @  s¦  d Z ddlmZ ddlZddlZddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, e$ƒ Z-dddœdd„Z.dddœdd„Z/G dd„ dƒZ0eeƒG dd„ de0ƒƒZ1eeƒG dd „ d ƒƒZ2dd!dd"œd#d$„Z3eeƒG d%d&„ d&e0e,ƒƒZ4G d'd(„ d(ƒZ5G d)d*„ d*ƒZ6dS )+zE
An implementation of the OpenSSH known_hosts database.

@since: 8.2
é    )ÚannotationsN)ÚErrorÚ
a2b_base64Ú
b2a_base64)Úclosing)Úsha1)ÚIOÚCallableÚIterableÚLiteral)Úimplementer)ÚHostKeyChangedÚInvalidEntryÚUserRejectedKey)ÚIKnownHostEntry)ÚBadKeyErrorÚFingerprintFormatsÚKey)Údefer)ÚDeferred)ÚLogger)ÚnativeString)ÚFilePath)ÚsecureRandom)ÚFancyEqMixinÚbytes)ÚsÚreturnc                 C  s   t | ƒ ¡ S )z’
    Encode a binary string as base64 with no trailing newline.

    @param s: The string to encode.

    @return: The base64-encoded string.
    )r   Ústrip)r   © r   úC/tmp/pip-unpacked-wheel-nrvc_22y/twisted/conch/client/knownhosts.pyÚ
_b64encode$   s    r!   z&tuple[bytes, bytes, Key, bytes | None]©Ústringr   c           	      C  sz   |   dd¡}t|ƒdkrtƒ ‚|\}}}|  dd¡}t|ƒdkrT|\}}| d¡}n|d }d}t t|ƒ¡}||||fS )a…  
    Extract common elements of base64 keys from an entry in a hosts file.

    @param string: A known hosts file entry (a single line).

    @return: a 4-tuple of hostname data (L{bytes}), ssh key type (L{bytes}), key
        (L{Key}), and comment (L{bytes} or L{None}).  The hostname data is
        simply the beginning of the line up to the first occurrence of
        whitespace.
    Né   é   é   ó   
r   )ÚsplitÚlenr   Úrstripr   Ú
fromStringr   )	r#   ÚelementsÚ	hostnamesÚkeyTypeZkeyAndCommentZsplitkeyZ	keyStringÚcommentÚkeyr   r   r    Ú_extractCommon/   s    
r1   c                   @  s4   e Zd ZdZdddddœdd„Zdd	d
œdd„ZdS )Ú
_BaseEntryaµ  
    Abstract base of both hashed and non-hashed entry objects, since they
    represent keys and key types the same way.

    @ivar keyType: The type of the key; either ssh-dss or ssh-rsa.
    @type keyType: L{bytes}

    @ivar publicKey: The server public key indicated by this line.
    @type publicKey: L{twisted.conch.ssh.keys.Key}

    @ivar comment: Trailing garbage after the key line.
    @type comment: L{bytes} or C{None}
    r   r   úbytes | NoneÚNone)r.   Ú	publicKeyr/   r   c                 C  s   || _ || _|| _d S ©N)r.   r5   r/   )Úselfr.   r5   r/   r   r   r    Ú__init__X   s    z_BaseEntry.__init__Úbool)Ú	keyObjectr   c                 C  s   | j |k}|S )zå
        Check to see if this entry matches a given key object.

        @param keyObject: A public key object to check.

        @return: C{True} if this entry's key matches C{keyObject}, C{False}
            otherwise.
        )r5   )r7   r:   Úresultr   r   r    Ú
matchesKey]   s    	
z_BaseEntry.matchesKeyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   r<   r   r   r   r    r2   I   s   r2   c                      s^   e Zd ZdZdddddœ‡ fdd„Zedd d	œd
d„ƒZdddœdd„Zddœdd„Z‡  Z	S )Ú
PlainEntryz¬
    A L{PlainEntry} is a representation of a plain-text entry in a known_hosts
    file.

    @ivar _hostnames: the list of all host-names associated with this entry.
    zlist[bytes]r   r   r3   )r-   r.   r5   r/   c                   s   || _ tƒ  |||¡ d S r6   )Ú
_hostnamesÚsuperr8   )r7   r-   r.   r5   r/   ©Ú	__class__r   r    r8   s   s    zPlainEntry.__init__r"   c                 C  s(   t |ƒ\}}}}| | d¡|||ƒ}|S )a¦  
        Parse a plain-text entry in a known_hosts file, and return a
        corresponding L{PlainEntry}.

        @param string: a space-separated string formatted like "hostname
            key-type base64-key-data comment".

        @raise DecodeError: if the key is not valid encoded as valid base64.

        @raise InvalidEntry: if the entry does not have the right number of
            elements and is therefore invalid.

        @raise BadKeyError: if the key, once decoded from base64, is not
            actually an SSH key.

        @return: an IKnownHostEntry representing the hostname and key in the
            input line.

        @rtype: L{PlainEntry}
        ó   ,)r1   r(   )Úclsr#   r-   r.   r0   r/   r7   r   r   r    r+   }   s    zPlainEntry.fromStringúbytes | strr9   ©Úhostnamer   c                 C  s   t |tƒr| d¡}|| jkS )a  
        Check to see if this entry matches a given hostname.

        @param hostname: A hostname or IP address literal to check against this
            entry.

        @return: C{True} if this entry is for the given hostname or IP address,
            C{False} otherwise.
        úutf-8)Ú
isinstanceÚstrÚencoderB   ©r7   rJ   r   r   r    ÚmatchesHost—   s    


zPlainEntry.matchesHost©r   c                 C  s>   d  | j¡| jt| j ¡ ƒg}| jdk	r4| | j¡ d  |¡S )zù
        Implement L{IKnownHostEntry.toString} by recording the comma-separated
        hostnames, key type, and base-64 encoded key.

        @return: The string representation of this entry, with unhashed hostname
            information.
        rF   Nó    )ÚjoinrB   r.   r!   r5   Úblobr/   Úappend©r7   Úfieldsr   r   r    ÚtoString¥   s    	
ý
zPlainEntry.toString)
r=   r>   r?   r@   r8   Úclassmethodr+   rP   rX   Ú__classcell__r   r   rD   r    rA   j   s   
rA   c                   @  s\   e Zd ZU dZdZded< dddœdd„Zdd	d
œdd„Zdd	dœdd„Zddœdd„Z	dS )ÚUnparsedEntryzŒ
    L{UnparsedEntry} is an entry in a L{KnownHostsFile} which can't actually be
    parsed; therefore it matches no keys and no hosts.
    Nr4   r.   r   r"   c                 C  s
   || _ dS )zv
        Create an unparsed entry from a line in a known_hosts file which cannot
        otherwise be parsed.
        N)Ú_string)r7   r#   r   r   r    r8   À   s    zUnparsedEntry.__init__r9   rI   c                 C  s   dS ©z'
        Always returns False.
        Fr   rO   r   r   r    rP   Ç   s    zUnparsedEntry.matchesHostr   )r0   r   c                 C  s   dS r]   r   )r7   r0   r   r   r    r<   Í   s    zUnparsedEntry.matchesKeyrQ   c                 C  s   | j  d¡S )a  
        Returns the input line, without its newline if one was given.

        @return: The string representation of this entry, almost exactly as was
            used to initialize this entry but without a trailing newline.
        @rtype: L{bytes}
        r'   )r\   r*   ©r7   r   r   r    rX   Ó   s    zUnparsedEntry.toString)
r=   r>   r?   r@   r.   Ú__annotations__r8   rP   r<   rX   r   r   r   r    r[   ·   s   
r[   rH   )r0   r#   r   c                 C  s4   t j| td�}t|tƒr"| d¡}| |¡ | ¡ S )z±
    Return the SHA-1 HMAC hash of the given key and string.

    @param key: The HMAC key.

    @param string: The string to be hashed.

    @return: The keyed hash value.
    )Ú	digestmodrK   )ÚhmacÚHMACr   rL   rM   rN   ÚupdateÚdigest)r0   r#   Úhashr   r   r    Ú_hmacedStringÞ   s
    



rf   c                      sj   e Zd ZdZdZdZdddddddœ‡ fd	d
„Zedd dœdd„ƒZdddœdd„Z	ddœdd„Z
‡  ZS )ÚHashedEntrya�  
    A L{HashedEntry} is a representation of an entry in a known_hosts file
    where the hostname has been hashed and salted.

    @ivar _hostSalt: the salt to combine with a hostname for hashing.

    @ivar _hostHash: the hashed representation of the hostname.

    @cvar MAGIC: the 'hash magic' string used to identify a hashed line in a
    known_hosts file as opposed to a plaintext one.
    s   |1|)Ú	_hostSaltÚ	_hostHashr.   r5   r/   r   r   r3   r4   )ÚhostSaltÚhostHashr.   r5   r/   r   c                   s    || _ || _tƒ  |||¡ d S r6   )rh   ri   rC   r8   )r7   rj   rk   r.   r5   r/   rD   r   r    r8     s    zHashedEntry.__init__r"   c           
      C  s^   t |ƒ\}}}}|t| jƒd…  d¡}t|ƒdkr:tƒ ‚|\}}| t|ƒt|ƒ|||ƒ}	|	S )a  
        Load a hashed entry from a string representing a line in a known_hosts
        file.

        @param string: A complete single line from a I{known_hosts} file,
            formatted as defined by OpenSSH.

        @raise DecodeError: if the key, the hostname, or the is not valid
            encoded as valid base64

        @raise InvalidEntry: if the entry does not have the right number of
            elements and is therefore invalid, or the host/hash portion
            contains more items than just the host and hash.

        @raise BadKeyError: if the key, once decoded from base64, is not
            actually an SSH key.

        @return: The newly created L{HashedEntry} instance, initialized with
            the information from C{string}.
        Nó   |r$   )r1   r)   ÚMAGICr(   r   r   )
rG   r#   Ústuffr.   r0   r/   ZsaltAndHashrj   rk   r7   r   r   r    r+     s    zHashedEntry.fromStringr9   rI   c                 C  s   t  t| j|ƒ| j¡S )a…  
        Implement L{IKnownHostEntry.matchesHost} to compare the hash of the
        input to the stored hash.

        @param hostname: A hostname or IP address literal to check against this
            entry.
        @type hostname: L{bytes}

        @return: C{True} if this entry is for the given hostname or IP address,
            C{False} otherwise.
        @rtype: L{bool}
        )ra   Úcompare_digestrf   rh   ri   rO   r   r   r    rP   +  s    
 ÿzHashedEntry.matchesHostrQ   c                 C  sR   | j d t| jƒt| jƒg¡ | jt| j ¡ ƒg}| jdk	rH| 	| j¡ d |¡S )zï
        Implement L{IKnownHostEntry.toString} by base64-encoding the salt, host
        hash, and key.

        @return: The string representation of this entry, with the hostname part
            hashed.
        @rtype: L{bytes}
        rl   NrR   )
rm   rS   r!   rh   ri   r.   r5   rT   r/   rU   rV   r   r   r    rX   <  s    
ÿü
zHashedEntry.toString)r=   r>   r?   r@   rm   ZcompareAttributesr8   rY   r+   rP   rX   rZ   r   r   rD   r    rg   ï   s   rg   c                   @  sœ   e Zd ZdZdddœdd„Zeddœdd	„ƒZd
dœdd„Zddddœdd„Zddddddœdd„Z	ddddœdd„Z
ddœdd„Zedd dœdd„ƒZd S )!ÚKnownHostsFileaz  
    A structured representation of an OpenSSH-format ~/.ssh/known_hosts file.

    @ivar _added: A list of L{IKnownHostEntry} providers which have been added
        to this instance in memory but not yet saved.

    @ivar _clobber: A flag indicating whether the current contents of the save
        path will be disregarded and potentially overwritten or not.  If
        C{True}, this will be done.  If C{False}, entries in the save path will
        be read and new entries will be saved by appending rather than
        overwriting.
    @type _clobber: L{bool}

    @ivar _savePath: See C{savePath} parameter of L{__init__}.
    zFilePath[str]r4   )ÚsavePathr   c                 C  s   g | _ || _d| _dS )a$  
        Create a new, empty KnownHostsFile.

        Unless you want to erase the current contents of C{savePath}, you want
        to use L{KnownHostsFile.fromPath} instead.

        @param savePath: The L{FilePath} to which to save new entries.
        @type savePath: L{FilePath}
        TN)Ú_addedÚ	_savePathÚ_clobber)r7   rq   r   r   r    r8   a  s    
zKnownHostsFile.__init__rQ   c                 C  s   | j S )z<
        @see: C{savePath} parameter of L{__init__}
        )rs   r^   r   r   r    rq   o  s    zKnownHostsFile.savePathzIterable[IKnownHostEntry]c                 c  s°   | j D ]
}|V  q| jrdS z| j ¡ }W n tk
r@   Y dS X |�` |D ]T}z&| tj¡rjt |¡}n
t	 |¡}W n" t
ttfk
r˜   t|ƒ}Y nX |V  qLW 5 Q R X dS )aK  
        Iterate over the host entries in this file.

        @return: An iterable the elements of which provide L{IKnownHostEntry}.
            There is an element for each entry in the file as well as an element
            for each added but not yet saved entry.
        @rtype: iterable of L{IKnownHostEntry} providers
        N)rr   rt   rs   ÚopenÚOSErrorÚ
startswithrg   rm   r+   rA   ÚDecodeErrorr   r   r[   )r7   ÚentryÚfpÚliner   r   r    Úiterentriesv  s"    	
zKnownHostsFile.iterentriesr   r   r9   )rJ   r0   r   c                 C  sx   t |  ¡ t| jƒ ƒD ]\\}}|j| ¡ kr| |¡r| |¡rF dS |dk rXd}d}n|d }| j}t	|||ƒ‚qdS )a®  
        Check for an entry with matching hostname and key.

        @param hostname: A hostname or IP address literal to check for.

        @param key: The public key to check for.

        @return: C{True} if the given hostname and key are present in this
            file, C{False} if they are not.

        @raise HostKeyChanged: if the host key found for the given hostname
            does not match the given key.
        Tr   Nr&   F)
Ú	enumerater|   r)   rr   r.   ÚsshTyperP   r<   rs   r   )r7   rJ   r0   Zlineidxry   r{   Úpathr   r   r    Ú
hasHostKey•  s    
zKnownHostsFile.hasHostKeyÚ	ConsoleUIúDeferred[bool])ÚuirJ   Úipr0   r   c                   s6   t  ˆjˆ ˆ¡}dddœ‡ ‡‡‡‡fdd„}| |¡S )a¹  
        Verify the given host key for the given IP and host, asking for
        confirmation from, and notifying, the given UI about changes to this
        file.

        @param ui: The user interface to request an IP address from.

        @param hostname: The hostname that the user requested to connect to.

        @param ip: The string representation of the IP address that is actually
        being connected to.

        @param key: The public key of the server.

        @return: a L{Deferred} that fires with True when the key has been
            verified, or fires with an errback when the key either cannot be
            verified or has changed.
        @rtype: L{Deferred}
        r9   zbool | Deferred[bool])r;   r   c                   sÄ   | rRˆ  ˆˆ¡sNdˆ ¡ › dˆ ¡ › d�}ˆ | d¡¡ ˆ ˆˆ¡ ˆ ¡  | S dddœ‡ ‡‡‡fdd„}ˆ ¡ }|d	kr€d
}dtˆ ƒtˆƒ|ˆjt	j
d�f }ˆ | t ¡ ¡¡}| |¡S d S )NzWarning: Permanently added the z host key for IP address 'z' to the list of known hosts.
rK   r9   )Úresponser   c                   s2   | r(ˆ  ˆ ˆ¡ ˆ  ˆˆ¡ ˆ ¡  | S tƒ ‚d S r6   )Ú
addHostKeyÚsaver   )r…   )rJ   r„   r0   r7   r   r    ÚpromptResponseÙ  s    zGKnownHostsFile.verifyHostKey.<locals>.gotHasKey.<locals>.promptResponseZECZECDSAz‘The authenticity of host '%s (%s)' can't be established.
%s key fingerprint is SHA256:%s.
Are you sure you want to continue connecting (yes/no)? )Úformat)r€   ÚtypeÚdecodeÚwarnrN   r†   r‡   r   Úfingerprintr   ZSHA256_BASE64ÚpromptÚsysÚgetdefaultencodingÚaddCallback)r;   Z
addMessagerˆ   ZkeytyperŽ   Úproceed©rJ   r„   r0   r7   rƒ   r   r    Ú	gotHasKeyË  s,    ÿ	üüÿz/KnownHostsFile.verifyHostKey.<locals>.gotHasKey)r   Úexecuter€   r‘   )r7   rƒ   rJ   r„   r0   Zhhkr”   r   r“   r    ÚverifyHostKey³  s    +zKnownHostsFile.verifyHostKeyrg   c                 C  s6   t dƒ}| ¡ }t|t||ƒ||dƒ}| j |¡ |S )aï  
        Add a new L{HashedEntry} to the key database.

        Note that you still need to call L{KnownHostsFile.save} if you wish
        these changes to be persisted.

        @param hostname: A hostname or IP address literal to associate with the
            new entry.
        @type hostname: L{bytes}

        @param key: The public key to associate with the new entry.
        @type key: L{Key}

        @return: The L{HashedEntry} that was added.
        @rtype: L{HashedEntry}
        é   N)r   r~   rg   rf   rr   rU   )r7   rJ   r0   Zsaltr.   ry   r   r   r    r†   ø  s
    zKnownHostsFile.addHostKeyc              	   C  sv   | j  ¡ }| ¡ s| ¡  | jr$dnd}| j  |¡�2}| jrb| d dd„ | jD ƒ¡d ¡ g | _W 5 Q R X d| _dS )zM
        Save this L{KnownHostsFile} to the path it was loaded from.
        ÚwÚar'   c                 S  s   g | ]}|  ¡ ‘qS r   )rX   )Ú.0ry   r   r   r    Ú
<listcomp>  s     z'KnownHostsFile.save.<locals>.<listcomp>FN)	rs   ÚparentÚisdirÚmakedirsrt   ru   rr   ÚwriterS   )r7   ÚpÚmodeZhostsFileObjr   r   r    r‡     s    
ÿzKnownHostsFile.save)r   r   c                 C  s   | |ƒ}d|_ |S )a­  
        Create a new L{KnownHostsFile}, potentially reading existing known
        hosts information from the given file.

        @param path: A path object to use for both reading contents from and
            later saving to.  If no file exists at this path, it is not an
            error; a L{KnownHostsFile} with no entries is returned.

        @return: A L{KnownHostsFile} initialized with entries from C{path}.
        F)rt   )rG   r   Z
knownHostsr   r   r    ÚfromPath   s    zKnownHostsFile.fromPathN)r=   r>   r?   r@   r8   Úpropertyrq   r|   r€   r–   r†   r‡   rY   r¢   r   r   r   r    rp   P  s   Erp   c                   @  s@   e Zd ZdZdddœdd„Zddd	œd
d„Zddd	œdd„ZdS )r�   z†
    A UI object that can ask true/false questions and post notifications on the
    console, to be used during key verification.
    zCallable[[], IO[bytes]]r4   )Úopenerr   c                 C  s
   || _ dS )aA  
        @param opener: A no-argument callable which should open a console
            binary-mode file-like object to be used for reading and writing.
            This initializes the C{opener} attribute.
        @type opener: callable taking no arguments and returning a read/write
            file-like object
        N)r¤   )r7   r¤   r   r   r    r8   7  s    zConsoleUI.__init__r   r‚   )Útextr   c                   s"   t  d¡}‡ ‡fdd„}| |¡S )a¿  
        Write the given text as a prompt to the console output, then read a
        result from the console input.

        @param text: Something to present to a user to solicit a yes or no
            response.
        @type text: L{bytes}

        @return: a L{Deferred} which fires with L{True} when the user answers
            'yes' and L{False} when the user answers 'no'.  It may errback if
            there were any I/O errors.
        Nc              	     sn   t ˆ  ¡ ƒ�X}| ˆ¡ | ¡  ¡  ¡ }|dkr>W 5 Q R £ dS |dkrTW 5 Q R £ dS | d¡ qW 5 Q R X d S )Ns   yesT>   ó    ó   noFs   Please type 'yes' or 'no': )r   r¤   rŸ   Úreadliner   Úlower)ZignoredÚfZanswer©r7   r¥   r   r    ÚbodyP  s    
zConsoleUI.prompt.<locals>.body)r   Zsucceedr‘   )r7   r¥   Údr¬   r   r«   r    rŽ   A  s    
zConsoleUI.promptc              	   C  sJ   z&t |  ¡ ƒ�}| |¡ W 5 Q R X W n tk
rD   t d¡ Y nX dS )z¹
        Notify the user (non-interactively) of the provided text, by writing it
        to the console.

        @param text: Some information the user is to be made aware of.
        zFailed to write to consoleN)r   r¤   rŸ   Ú	ExceptionÚlogZfailure)r7   r¥   rª   r   r   r    rŒ   ^  s
    zConsoleUI.warnN)r=   r>   r?   r@   r8   rŽ   rŒ   r   r   r   r    r�   1  s   
r�   )7r@   Ú
__future__r   ra   r�   Úbinasciir   rx   r   r   Ú
contextlibr   Úhashlibr   Útypingr   r	   r
   r   Zzope.interfacer   Ztwisted.conch.errorr   r   r   Ztwisted.conch.interfacesr   Ztwisted.conch.ssh.keysr   r   r   Ztwisted.internetr   Ztwisted.internet.deferr   Ztwisted.loggerr   Ztwisted.python.compatr   Ztwisted.python.filepathr   Ztwisted.python.randbytesr   Ztwisted.python.utilr   r¯   r!   r1   r2   rA   r[   rf   rg   rp   r�   r   r   r   r    Ú<module>   s@   !L&` b