
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
."dCB                 @   s6  d dl Z d dlZd dlZd dlZd dlZd dlZddlmZ ddlmZ dd d<D Z	dZ
d=Zd dhdddhdddhddhdddhdddhdddhdddhdddhdddhd!
ZG d"d# d#ZG d$d% d%ejZd&d' Ze jd(d>Zd-d. Zd?d0d1ZG d2d3 d3eZd4d5 ZG d6d7 d7Zd8d9 ZG d:d; d;ZdS )@    N   )	alg_lists)
validationc             C   s   i | ]
}d |qS )r    ).0kr   r   C./usr/share/crypto-policies/python/cryptopolicies/cryptopolicies.py
<dictcomp>   s    r	   arbitrary_dh_groupsmin_dh_sizemin_dsa_sizemin_rsa_sizesha1_in_certs	ssh_certsssh_etm*tlssslopensslnssgnutlsjava-tlssshopensshopenssh-serveropenssh-clientlibsshipsecike	libreswankerberoskrb5dnssecbind)
r#   r   zjava-tlsr!   r   r   r   zopenssh-clientzopenssh-serverr   c               @   s(   e Zd ZefddZdd Zdd ZdS )ScopeSelectorc             C   s   |j   | _}|jd | _| jr&|n
|dd }tjj|| jd tjj|| jd |jdrr|dd jdn|g| _	tjj
| j	t| jd dS )a=  
        Initialize a scope selector.
        An example would be `ssh` in `ciphers@ssh = -NULL`.
        When openssh backend will request the configuration,
        it'll offer (`{'ssh', 'openssh'}`) as scopes
        and the rule above will be taken into account.
        Both patterns and scopes are cast to lowercase.
        For more examples, refer to tests/unit/parsing/test_scope_selector.py
        >>> ss = ScopeSelector('!{SSH,IPsec}')
        >>> ss.matches({'ipsec', 'libreswan'})
        False
        >>> ss.matches({'tls', 'openssl'})
        True
        !r   N)Zoriginal_pattern{,)lowerpattern
startswith	_positiver   scopeZillegal_charactersZcurly_bracketssplit_globsZresulting_globs
ALL_SCOPES)selfr*   pr   r   r   __init__5   s    $zScopeSelector.__init__c             C   s   dt | j dS )Nz<ScopeSelector pattern=>)reprr*   )r1   r   r   r   __str__P   s    zScopeSelector.__str__c                sh   | j tkrdS dd  D  tdd  D s2t| jrPt fdd| jD S t fdd| jD S )aE  
        Checks whether ScopeSelector matches one of the scopes.
        For more examples, refer to tests/unit/parsing/test_scope_selector.py
        >>> ScopeSelector('{SSH,IPsec}').matches({'ipsec', 'libreswan'})
        True
        >>> ScopeSelector('!{SSH,IPsec}').matches({'ipsec', 'libreswan'})
        False
        Tc             S   s   g | ]}|j  qS r   )r)   )r   sr   r   r   
<listcomp>^   s    z)ScopeSelector.matches.<locals>.<listcomp>c             s   s   | ]}|t kV  qd S )N)r0   )r   r7   r   r   r   	<genexpr>_   s    z(ScopeSelector.matches.<locals>.<genexpr>c             3   s   | ]}t j |V  qd S )N)fnmatchfilter)r   g)scopesr   r   r9   a   s    c             3   s   | ]}t j | V  qd S )N)r:   r;   )r   r<   )r=   r   r   r9   b   s    )r*   	SCOPE_ANYallAssertionErrorr,   anyr/   )r1   r=   r   )r=   r   matchesS   s    	
zScopeSelector.matchesN)__name__
__module____qualname__r>   r3   r6   rB   r   r   r   r   r$   4   s   r$   c               @   s$   e Zd ZdZdZdZdZdZdZdS )	OperationzM
    An operation that comes with the right-hand value of the directive.
    r               N)	rC   rD   rE   __doc__RESETPREPENDAPPENDOMITSET_INTr   r   r   r   rF   g   s   rF   c                s  dd  | j  rftjkr2tkr2tjt| fgS tjkrJtjj	qztjksXt
tkszt
ntkrztjj| j }t fdd|D stfdd|D g }tjdfgdd |D  S t fd	d|D rg }x|D ]}|jd
r"tjtj|dd ddd }n\|jd
rTtjtj|dd ddd }n*|jdsdt
tjtj|dd }|jfdd|D  qW |S tjj| dS )a7  
    Parses right-hand parts of the directives
    into lists of operation/value pairs.
    For more examples, refer to tests/unit/test_parsing.py
    >>> parse_rhs('', 'cipher')
    [(<Operation.RESET: 1>, None)]
    >>> parse_rhs('IDEA-CBC SEED-CBC', 'cipher')
    [(<Operation.RESET: 1>, None),
     (<Operation.APPEND: 3>, 'IDEA-CBC'),
     (<Operation.APPEND: 3>, 'SEED-CBC')]
    >>> # 3DES-CBC gets prepended last for higher prio
    >>> parse_rhs('+*DES-CBC', 'cipher')
    [(<Operation.PREPEND: 2>, 'DES-CBC'),
     (<Operation.PREPEND: 2>, '3DES-CBC')]
    c             S   s   | j dp| jdp| j dS )N+-)r+   endswith)vr   r   r   differential   s    zparse_rhs.<locals>.differentialc             3   s   | ]} |V  qd S )Nr   )r   rT   )rU   r   r   r9      s    zparse_rhs.<locals>.<genexpr>c                s   g | ]}t j| qS r   )r   glob)r   rT   )	prop_namer   r   r8      s    zparse_rhs.<locals>.<listcomp>Nc             S   s   g | ]}t j|fqS r   )rF   rN   )r   rT   r   r   r   r8      s    c             3   s   | ]} |V  qd S )Nr   )r   rT   )rU   r   r   r9      s    rQ   r   rR   c                s   g | ]} |fqS r   r   )r   rT   )opr   r   r8      s    r(   r(   r(   )isdigitr   ALLINT_DEFAULTSrF   rP   intr   rulesZNonIntPropertyIntValueErrorr@   ZIntPropertyNonIntValueErrorr.   rA   sumrL   r?   r+   rM   rV   rS   rN   rO   extendZ%MixedDifferentialNonDifferentialError)rhsrW   valuesZ
operationsvalueZunglobr   )rU   rX   rW   r   	parse_rhsr   s<    


  rc   	DirectiverW   r-   	operationrb   c                s   | j  sg S tjj|  | jd\}}|j  |j   }}tjj||  d|krZ|jddn|tf\  fddt| D S )ae  
    Parses configuration lines into tuples of directives.
    For more examples, refer to tests/unit/test_parsing.py
    >>> parse_line('cipher@TLS = RC4* NULL')
    [Directive(prop_name='cipher', scope='tls',
               operation=<Operation.RESET: 1>, value=None),
     Directive(prop_name='cipher', scope='tls',
               operation=<Operation.APPEND: 3>, value='RC4-40'),
     Directive(prop_name='cipher', scope='tls',
               operation=<Operation.APPEND: 3>, value='RC4-128'),
     Directive(prop_name='cipher', scope='tls',
               operation=<Operation.APPEND: 3>, value='NULL')]
    =@r   c                s$   g | ]\}}t  j ||d qS ))rW   r-   re   rb   )rd   r)   )r   re   rb   )rW   r-   r   r   r8      s   zparse_line.<locals>.<listcomp>)stripr   r]   Zcount_equals_signsr.   Z	empty_lhsr>   rc   )lineZlhsr`   r   )rW   r-   r   
parse_line   s     rj   Fc             C   s^   y$t | }x|D ]}t|j qW W n4 tjk
rX } z|s> tj| W Y d d }~X nX d S )N)rj   r$   r-   r   ZPolicySyntaxErrorwarningswarn)ri   rl   ldexr   r   r   syntax_check_line   s    
rp   c                   s   e Zd Z fddZ  ZS )PolicySyntaxDeprecationWarningc                s@   |j dd}d| d}|d| d7 }|d7 }t j| d S )N
z and zoption z is deprecatedz", please rewrite your rules using z; z2be advised that it is not always a 1-1 replacement)replacesuperr3   )r1   Z
deprecatedZreplacementmsg)	__class__r   r   r3      s
    z'PolicySyntaxDeprecationWarning.__init__)rC   rD   rE   r3   __classcell__r   r   )rv   r   rq      s   rq   c             C   s  t jdd| } | jdd} djdd | jdD } | jdd} djd	d | jdD } djd
d | jdD } t jdd| j } t jd| rtjt	dd ddddd}xr|j
 D ]f\}}d| d }t j|| }|rtjt	|| t j|d| } x"|D ]}| d| d| 7 } qW qW t jdd| j } ddd}xN|j
 D ]B\}}d| d }t j|| r|tjt	|| t j||| } qJW ttjddd% }xZ|rdjdd |dd& D }	t jd|d'  d |	rd |	 nd| } |j  qW t jd!d| } ttjddd( }
xZ|
r|djd"d |
dd) D }	t jd#|
d*  d |	rhd |	 nd| } |
j  q$W t jd$d| } | S )+a  
    Preprocesses text before parsing.
    Fixes line breaks, handles backwards compatibility.
    >>> preprocess_text('cipher = c1 \\ \nc2#x')
    'cipher = c1 c2'
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("ignore")
    ...     preprocess_text('ike_protocol = IKEv2')
    'protocol@IKE = IKEv2'
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("ignore")
    ...     preprocess_text('min_tls_version=TLS1.3')
    'protocol@TLS = -SSL2.0 -SSL3.0 -TLS1.0 -TLS1.1 -TLS1.2'
    z#.* rf   z = rr   c             s   s   | ]}|j  V  qd S )N)rh   )r   rm   r   r   r   r9      s    z"preprocess_text.<locals>.<genexpr>z\
c             s   s   | ]}|j  V  qd S )N)rh   )r   rm   r   r   r   r9      s    c             s   s   | ]}t jd d|V  qdS )z\s+ N)resub)r   rm   r   r   r   r9      s    z
+z\bprotocol\s*=protocolzprotocol@TLSz
cipher@TLSz
cipher@SSHz	group@SSHzprotocol@IKE)Z
tls_cipherZ
ssh_cipherZ	ssh_groupZike_protocolz\bz\s*=(.*)z

z =z7hash@DNSSec = -SHA1
sign@DNSSec = -RSA-SHA1 -ECDSA-SHA1z7hash@DNSSec = SHA1+
sign@DNSSec = RSA-SHA1+ ECDSA-SHA1+)zsha1_in_dnssec = 0zsha1_in_dnssec = 1Nr   ry   c             s   s   | ]}d | V  qdS )rR   Nr   )r   rT   r   r   r   r9     s    z\bmin_dtls_version = zprotocol@TLS = z\bmin_dtls_version = 0\bc             s   s   | ]}d | V  qdS )rR   Nr   )r   rT   r   r   r   r9   $  s    z\bmin_tls_version = z\bmin_tls_version = 0\br(   r(   r(   r(   r(   r(   )rz   r{   rs   joinr.   rh   findallrk   rl   rq   itemssearchlistr   ZDTLS_PROTOCOLSpopZTLS_PROTOCOLS)textZPOSTFIX_REPLACEMENTSfrZtoZregexZmsmZPLAIN_REPLACEMENTSZdtls_versionsnegZtls_versionsr   r   r   preprocess_text   sZ    
r   c               @   sJ   e Zd ZdZdddZedd Zedd Zed	d
 Zedd Z	dS )ScopedPolicya  
    An entity constructing lists of what's `.enabled` and what's `.disabled`
    when the given scopes are active.
    >>> sp = ScopedPolicy(parse_line('cipher@TLS = RC4* NULL'), {'tls'})
    >>> 'AES-256-GCM' in sp.disabled['cipher']
    True
    >>> sp.enabled['cipher']
    ['RC4-40', 'RC4-128', 'NULL']
    >>> ScopedPolicy(parse_line('min_dh_size=2048')).integers['min_dh_size']
    2048
    Nc                sX  |pt  }tj _dd tjD _x|D ] t j}|j	|r, j
tjkr^g j j< q, j
tjkrj j } j|kr|j j q, j
tjkr̈j j } j|kr|j j |jd j q, j
tjkr fddj j D j j< q, j
tjkst jj j< q,W tjtt jks>tfddtjD _d S )Nc             S   s   i | ]
}g |qS r   r   )r   rW   r   r   r   r	   >  s    z)ScopedPolicy.__init__.<locals>.<dictcomp>r   c                s   g | ]}| j kr|qS r   )rb   )r   e)	directiver   r   r8   R  s    z)ScopedPolicy.__init__.<locals>.<listcomp>c                s(   i | ]   fd dt j  D  qS )c                s   g | ]}|j   kr|qS r   )enabled)r   r   )rW   r1   r   r   r8   Z  s    z4ScopedPolicy.__init__.<locals>.<dictcomp>.<listcomp>)r   rZ   )r   )r1   )rW   r   r	   Z  s   )setr[   copyintegersr   rZ   r   r$   r-   rB   re   rF   rL   rW   rN   rb   appendrM   removeinsertrO   rP   r@   lenZdisabled)r1   
directivesZrelevant_scopesZssr   r   )r   r1   r   r3   ;  s0    






$
zScopedPolicy.__init__c             C   s   t j| jd S )Nr|   )r   min_tls_versionr   )r1   r   r   r   r   ^  s    zScopedPolicy.min_tls_versionc             C   s   t j| jd S )Nr|   )r   max_tls_versionr   )r1   r   r   r   r   b  s    zScopedPolicy.max_tls_versionc             C   s   t j| jd S )Nr|   )r   min_dtls_versionr   )r1   r   r   r   r   f  s    zScopedPolicy.min_dtls_versionc             C   s   t j| jd S )Nr|   )r   max_dtls_versionr   )r1   r   r   r   r   j  s    zScopedPolicy.max_dtls_version)N)
rC   rD   rE   rK   r3   propertyr   r   r   r   r   r   r   r   r   /  s   
#r   c             C   s@   x,|D ]$}t jj||}t j|t jr|S qW tj| ||d S )N)ospathr}   accessR_OKr   ZPolicyFileNotFoundError)
policynamefnamepathsrn   r2   r   r   r   lookup_fileq  s
    
r   c               @   sF   e Zd ZdZdZddddZdd Zdd	d
ZdddZdd Z	dS )UnscopedCryptoPolicyz/etc/crypto-policiesz/usr/share/crypto-policiesN)	policydirc            G   sR   || _ dj|f| | _g | _| j|}x|D ]}|| j|dd7 }q.W || _d S )N:T)	subpolicy)r   r}   r   linesread_policy_file_directives)r1   Zpolicy_namer   Zsubpolicy_namesr   Zsubpolicy_namer   r   r   r3     s    

zUnscopedCryptoPolicy.__init__c             C   s   | j  S )N)r   )r1   r   r   r   is_empty  s    zUnscopedCryptoPolicy.is_emptyc             C   s   t | j|pi S )N)r   r   )r1   r=   r   r   r   scoped  s    zUnscopedCryptoPolicy.scopedFc       	      C   s   | j pd}|rtjj|d}t|||s*dnd tjj|tjj| j|tjj| j|f}t|}|j	 }W d Q R X t
|}|jd}x|D ]}t|dd qW x|D ]}t| qW tdd	 |D g S )
NZpoliciesmodulesz.polz.pmodrr   T)rl   c             S   s   g | ]}t |qS r   )rj   )r   rm   r   r   r   r8     s    z9UnscopedCryptoPolicy.read_policy_file.<locals>.<listcomp>)r   r   r   r}   r   curdir
CONFIG_DIR	SHARE_DIRopenreadr   r.   rp   r^   )	r1   namer   Zpdirr2   fr   r   rm   r   r   r   r     s$    




z%UnscopedCryptoPolicy.read_policy_filec             C   s  dd }| j  }d| j d}|d7 }|d7 }|d7 }|d7 }|d7 }|j|j}x"|j D ]\}}||||7 }q\W d	}xvtj D ]j\}}	| j |	d
}
|
j|
j}xH|j D ]<\}}||| kr|s|d7 }d}||| d| |7 }qW qW |s |d7 }|S )Nc             S   s2   t |trdj|nt|}|  d| j d S )Nry   z = rr   )
isinstancer   r}   strrstrip)keyrb   r7   r   r   r   fmt  s    z)UnscopedCryptoPolicy.__str__.<locals>.fmtz	# Policy z dump
z#
z?# Do not parse the contents of this file with automated tools,
z.# it is provided for review convenience only.
z"# Baseline values for all scopes:
F)r=   z9# Scope-specific properties derived for select backends:
Trg   z&# No scope-specific properties found.
)r   r   r   r   r   DUMPABLE_SCOPES)r1   r   Zgeneric_scopedr7   Zgeneric_allrW   rb   Zanything_scope_specificZ
scope_nameZ	scope_setZspecific_scopedZspecific_allr   r   r   r6     s2     zUnscopedCryptoPolicy.__str__)N)F)
rC   rD   rE   r   r   r3   r   r   r   r6   r   r   r   r   r   {  s   

r   )r
   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   )rW   r-   re   rb   )F)collectionsenumr:   r   rz   rk   rx   r   r   r[   r>   r0   r   r$   EnumrF   rc   
namedtuplerd   rj   rp   FutureWarningrq   r   r   r   r   r   r   r   r   <module>   sP          3;
	LB
