
    ,h                     `   d Z 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	m
Z
mZmZmZmZmZmZ 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ZddlZddlZddlZddlZddlmZmZm Z  ddlm!Z!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.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 ed   Z7ed   Z8e	ejr                  jt                  ejv                  jx                  gdf   Z=de>defdZ?deee@f   de>fdZAdeee>eBe@f   de@fdZCdee*e&f   de@fdZD G d d      ZE G d deE      ZF eFej                  ej                  ej                  ej                  hej                  ej                  ej                  hej                  ej                  ej                  hej                  h      ZN eF eO        eO        eO        eO             ZPeNZQ	 	 	 dadeej                  j                  e>f   dej                  j                  deee>f   d eej                  j                     d!eeE   d"eVde,fd#ZW	 dbdeej                  j                  e>f   dej                  j                  deee>f   d eej                  j                     de(f
d$ZXd%e
ej                  j                  eej                  j                  ej                  j                  f   f   d&e3deee*      fd'Z]d(eejv                  jx                  eej                  j                  ej                  j                  f   f   deej                  j                  ej                  j                  f   fd)Z^d*e_d+e_de*ddfd,Z`	 	 	 dcd(eejv                  jx                  eej                  j                  ej                  j                  f   f   d&e3d%e
ej                  j                  eej                  j                  ej                  j                  f   f   d eej                  j                     d-eeB   d!eeE   ddfd.Za	 	 	 dcd(eejv                  jx                  eej                  j                  ej                  j                  f   f   d/eejv                  jx                  eej                  j                  ej                  j                  f   f   d%e
ej                  j                  eej                  j                  ej                  j                  f   f   d eej                  j                     d-eeB   d!eeE   ddfd0Zb	 	 	 	 	 	 	 ddd(eejv                  jx                  eej                  j                  ej                  j                  f   f   d2e8d3ej                  j                  d4e*d5eeee>e@eBf      d6eeee>e@eBf      d7ee@   d8eVd!eeE   d eej                  j                     d9eVde3fd:Zc	 dbd(eejv                  jx                  eej                  j                  ej                  j                  f   f   d&e3d eej                  j                     de_fd;Zde6j                  d<fd=e7dee@e>f   d>e@d?e@de*f
d@Zfe6j                  d<fd=e7dee@e>f   d>e@d?e@de&f
dAZgdBeej                  j                  e>f   dCeee>e_f      dDe@dee@e>f   de>f
dEZh	 dbd(eejv                  jx                  eej                  j                  ej                  j                  f   f   dFeeee>f      d eej                  j                     dej                  j                  fdGZidHej                  j                  dej                  j                  fdIZj	 dbdeej                  j                  e>f   dHej                  j                  deee>f   d eej                  j                     dej                  j                  f
dJZkdHej                  j                  dej                  j                  fdKZl	 	 	 	 	 	 dedLejr                  jt                  d(ejv                  jx                  d3ej                  j                  dMeee8e*f      dNeee8e*f      d5eeee>e@eBf      d6eeee>e@eBf      d7ee@   d!eeE   d eej                  j                     d9eVddfdOZm	 	 	 	 	 	 	 	 	 	 	 dfdPej                  j                  dLeejr                  jt                     d%eeee8e*f         dQeVdRee@   d5eeee>e@eBf      d6eeee>e@eBf      d7ee@   dSee1   dTee=   d!eeE   d9eVddfdUZp	 dbdPej                  j                  dLejr                  jt                  dTee=   ddfdVZqdW Zrej                  j                  dX      rAddYlumvZv ddZlwmxZx dd[lwmyZy dd\lwmzZz dd]lwm{Z{ dd^lwm|Z| dd_l}m~Z~mZ dd`lmZmZ ebZeaZecZefZegZd1ZnerZerZerZerZerZdZej                  ZGej                  Zej                  ZHej                  Zej                  Zej                  ZIej                  Zej                  Zej                  Zej                  ZJej                  Zej                   Zej"                  Zej$                  Zej&                  Zej(                  Zej*                  Zy)gz.Common DNSSEC-related functions and constants.    N)datetime)CallableDictListOptionalSetTupleUnioncast)	AlgorithmDSDigest	NSEC3Hash)AlgorithmKeyMismatchDeniedByPolicyUnsupportedAlgorithmValidationFailure)CDNSKEY)CDS)DNSKEY)DS)NSECBitmap)
NSEC3PARAM)RRSIGsigtime_to_posixtime)Flag)GenericPublicKeyzrsa.RSAPublicKeyzec.EllipticCurvePublicKeyzed25519.Ed25519PublicKeyzed448.Ed448PublicKey)GenericPrivateKeyzrsa.RSAPrivateKeyzec.EllipticCurvePrivateKeyzed25519.Ed25519PrivateKeyzed448.Ed448PrivateKeytextreturnc                 ,    t        j                  |       S )zConvert text into a DNSSEC algorithm value.

    *text*, a ``str``, the text to convert to into an algorithm value.

    Returns an ``int``.
    )r   	from_text)r   s    L/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/dns/dnssec.pyalgorithm_from_textr$   L   s     t$$    valuec                 ,    t        j                  |       S )zConvert a DNSSEC algorithm value to text

    *value*, a ``dns.dnssec.Algorithm``.

    Returns a ``str``, the name of a DNSSEC algorithm.
    )r   to_textr&   s    r#   algorithm_to_textr*   W   s     U##r%   c                     t        | t              rt        | j                               S t        | t              rt        |       S t        | t              rt        |       S t        | t              r| S t        d      )z%Convert various format to a timestampzUnsupported timestamp type)
isinstancer   int	timestampstrr   float	TypeErrorr)   s    r#   to_timestampr2   b   sb    %"5??$%%	E3	#E**	E5	!5z	E3	455r%   keyc                 `   | j                         }|J | j                  t        j                  k(  r|d   dz  |d   z   S d}t	        t        |      dz        D ]  }||d|z     dz  |d|z  dz      z   z  } t        |      dz  dk7  r||t        |      dz
     dz  z  }||dz	  dz  z  }|dz  S )	zReturn the key id (a 16-bit number) for the specified key.

    *key*, a ``dns.rdtypes.ANY.DNSKEY.DNSKEY``

    Returns an ``int`` between 0 and 65535
       r            i  )to_wire	algorithmr   RSAMD5rangelen)r3   rdatatotalis       r#   key_idrC   p   s     KKME
}}	(((b	Q%)++s5zQ' 	<AeAEla'5Q+;;;E	<u:>QU3u:>*a//E%2+''v~r%   c                   R    e Zd Zd ZdedefdZdedefdZdedefdZ	dedefdZ
y)	Policyc                      y N )selfs    r#   __init__zPolicy.__init__   s    r%   _r    c                      yNFrH   rI   rK   s     r#   
ok_to_signzPolicy.ok_to_sign       r%   c                      yrM   rH   rN   s     r#   ok_to_validatezPolicy.ok_to_validate   rP   r%   c                      yrM   rH   rN   s     r#   ok_to_create_dszPolicy.ok_to_create_ds   rP   r%   c                      yrM   rH   rN   s     r#   ok_to_validate_dszPolicy.ok_to_validate_ds   rP   r%   N)__name__
__module____qualname__rJ   r   boolrO   rR   r   rT   rV   rH   r%   r#   rE   rE      sS    F t  4  d 8  r%   rE   c                   ^     e Zd Z fdZdedefdZdedefdZdedefdZ	dedefdZ
 xZS )	
SimpleDenyc                 Z    t         |           || _        || _        || _        || _        y rG   )superrJ   
_deny_sign_deny_validate_deny_create_ds_deny_validate_ds)rI   	deny_signdeny_validatedeny_create_dsdeny_validate_ds	__class__s        r#   rJ   zSimpleDeny.__init__   s-    #+-!1r%   r3   r    c                 2    |j                   | j                  vS rG   )r<   r_   rI   r3   s     r#   rO   zSimpleDeny.ok_to_sign   s    }}DOO33r%   c                 2    |j                   | j                  vS rG   )r<   r`   ri   s     r#   rR   zSimpleDeny.ok_to_validate   s    }}D$7$777r%   r<   c                     || j                   vS rG   )ra   rI   r<   s     r#   rT   zSimpleDeny.ok_to_create_ds   s     4 444r%   c                     || j                   vS rG   )rb   rl   s     r#   rV   zSimpleDeny.ok_to_validate_ds   s     6 666r%   )rW   rX   rY   rJ   r   rZ   rO   rR   r   rT   rV   __classcell__)rg   s   @r#   r\   r\      sT    24f 4 48& 8T 85 5d 578 7 7r%   r\   Fnamer<   originpolicy
validatingc           	         |t         }	 t        |t              rt        |j	                            }|r|j                  }n|j                  } ||      st        t        |t        t        f      st        d      |t        j                  k(  rt        j                         }n_|t        j                   k(  rt        j"                         }n7|t        j$                  k(  rt        j&                         }nt        d| d      t        | t              r t(        j*                  j-                  | |      } | j/                         j1                         }|j1                  |      }	||	J |j3                  |       |j3                  |	       |j5                         }
t7        j8                  dt;        |      |j<                  |      |
z   }t(        j>                  jA                  t(        jB                  jD                  t(        jF                  jH                  |dtK        |            }tM        tH        |      S # t
        $ r t        d| d      w xY w)a  Create a DS record for a DNSSEC key.

    *name*, a ``dns.name.Name`` or ``str``, the owner name of the DS record.

    *key*, a ``dns.rdtypes.ANY.DNSKEY.DNSKEY`` or ``dns.rdtypes.ANY.DNSKEY.CDNSKEY``,
    the key the DS is about.

    *algorithm*, a ``str`` or ``int`` specifying the hash algorithm.
    The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
    does not matter for these strings.

    *origin*, a ``dns.name.Name`` or ``None``.  If *key* is a relative name,
    then it will be made absolute using the specified origin.

    *policy*, a ``dns.dnssec.Policy`` or ``None``.  If ``None``, the default policy,
    ``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.

    *validating*, a ``bool``.  If ``True``, then policy is checked in
    validating mode, i.e. "Is it ok to validate using this digest algorithm?".
    Otherwise the policy is checked in creating mode, i.e. "Is it ok to create a DS with
    this digest algorithm?".

    Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.

    Raises ``DeniedByPolicy`` if the algorithm is denied by policy.

    Returns a ``dns.rdtypes.ANY.DS.DS``
    unsupported algorithm ""zkey is not a DNSKEY/CDNSKEYrp   z!HBBr   )'default_policyr,   r/   r   upper	Exceptionr   rV   rT   r   r   r   
ValueErrorSHA1hashlibsha1SHA256sha256SHA384sha384dnsro   r"   canonicalizer;   updatedigeststructpackrC   r<   r@   	from_wire
rdataclassIN	rdatatyper   r?   r   )ro   r3   r<   rp   rq   rr   checkdshashwirekwirer   dsrdatadss                r#   make_dsr      s   J ~Ki% !23I ((&&cFG,-677HMM!	hoo	%!	hoo	%!"%<YKq#IJJ$xx!!$/&&(DKKvK&E 11
MM$
MM%]]_Fkk&&+s}}iH6QG			3==++WaW
B B<A  K"%<YKq#IJJKs   'H4 4Ic                     t        | |||      }t        |j                  t        j                  j                  |j
                  |j                  |j                  |j                        S )a  Create a CDS record for a DNSSEC key.

    *name*, a ``dns.name.Name`` or ``str``, the owner name of the DS record.

    *key*, a ``dns.rdtypes.ANY.DNSKEY.DNSKEY`` or ``dns.rdtypes.ANY.DNSKEY.CDNSKEY``,
    the key the DS is about.

    *algorithm*, a ``str`` or ``int`` specifying the hash algorithm.
    The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
    does not matter for these strings.

    *origin*, a ``dns.name.Name`` or ``None``.  If *key* is a relative name,
    then it will be made absolute using the specified origin.

    Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.

    Returns a ``dns.rdtypes.ANY.DS.CDS``
    rdclassrdtypekey_tagr<   digest_typer   )	r   r   r   r   r   r   r<   r   r   )ro   r3   r<   rp   r   s        r#   make_cdsr     sQ    2 
sIv	.B

}}  

,,NNyy r%   keysrrsigc                 2   | j                  |j                        }t        |t        j                  j
                        rC|j                  t        j                  j                  t        j                  j                        }n|}|y |D cg c]  }|j                  |j                  k(  ret        |      |j                  k(  rM|j                  t        j                   z  t        j                   k(  r|j"                  dk(  rt%        t        |       c}S c c}w )N   )getsignerr,   r   nodeNodeget_rdatasetr   r   r   r   r<   rC   r   flagsr   ZONEprotocolr   )r   r   r&   rdatasetrds        r#   _find_candidate_keysr   *  s     HHU\\"E%'%%cnn&7&79M9MN <<5??*2J%--'XX		!dii/KK1 	VR  s   BDrrsetc                 R    t        | t              r
| d   | d   fS | j                  | fS )Nr   r9   )r,   tuplero   )r   s    r#   _get_rrname_rdatasetr   >  s0     %Qxq!!zz5  r%   sigdatac                     t        |      j                  }	 |j                  |      }|j                  | |       y # t        $ r t	        d      w xY w)Nzinvalid public key)get_algorithm_cls_from_dnskey
public_clsfrom_dnskeyrz   r   verify)r   r   r3   r   
public_keys        r#   _validate_signaturer   G  sV    .s3>>J6++C0
 c4   6 4556s	   ; Anowc                    |t         }t        ||      }|t        d      |t        j                         }|j                  |k  rt        d      |j
                  |kD  rt        d      t        | ||      }|D ]-  }|j                  |      s	 t        |j                  ||        y t        d      # t        t        f$ r Y Lw xY w)a  Validate an RRset against a single signature rdata, throwing an
    exception if validation is not successful.

    *rrset*, the RRset to validate.  This can be a
    ``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
    tuple.

    *rrsig*, a ``dns.rdata.Rdata``, the signature to validate.

    *keys*, the key dictionary, used to find the DNSKEY associated
    with a given name.  The dictionary is keyed by a
    ``dns.name.Name``, and has ``dns.node.Node`` or
    ``dns.rdataset.Rdataset`` values.

    *origin*, a ``dns.name.Name`` or ``None``, the origin to use for relative
    names.

    *now*, a ``float`` or ``None``, the time, in seconds since the epoch, to
    use as the current time when validating.  If ``None``, the actual current
    time is used.

    *policy*, a ``dns.dnssec.Policy`` or ``None``.  If ``None``, the default policy,
    ``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.

    Raises ``ValidationFailure`` if the signature is expired, not yet valid,
    the public key is invalid, the algorithm is unknown, the verification
    fails, etc.

    Raises ``UnsupportedAlgorithm`` if the algorithm is recognized by
    dnspython but not implemented.
    Nzunknown keyexpiredznot yet validzverify failure)rw   r   r   time
expiration	inception_make_rrsig_signature_datarR   r   	signatureInvalidSignature)	r   r   r   rp   r   rq   candidate_keysr   candidate_keys	            r#   _validate_rrsigr   Q  s    P ~)$6N..
{iik#	**00%eUF;D ( $$]3	}E ,
--	 !"34 		s   B66CCrrsigsetc           	      D   |t         }t        |t              r8t        j                  j                  |t        j                  j                        }t        | t              r| d   }n| j                  }t        |t              r|d   }|d   }n|j                  }|}|j                  |      }|j                  |      }||k7  rt        d      |D ]/  }	t        |	t              st        d      	 t        | |	||||        y t        d      # t        t        f$ r Y Nw xY w)a  Validate an RRset against a signature RRset, throwing an exception
    if none of the signatures validate.

    *rrset*, the RRset to validate.  This can be a
    ``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
    tuple.

    *rrsigset*, the signature RRset.  This can be a
    ``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
    tuple.

    *keys*, the key dictionary, used to find the DNSKEY associated
    with a given name.  The dictionary is keyed by a
    ``dns.name.Name``, and has ``dns.node.Node`` or
    ``dns.rdataset.Rdataset`` values.

    *origin*, a ``dns.name.Name``, the origin to use for relative names;
    defaults to None.

    *now*, an ``int`` or ``None``, the time, in seconds since the epoch, to
    use as the current time when validating.  If ``None``, the actual current
    time is used.

    *policy*, a ``dns.dnssec.Policy`` or ``None``.  If ``None``, the default policy,
    ``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.

    Raises ``ValidationFailure`` if the signature is expired, not yet valid,
    the public key is invalid, the algorithm is unknown, the verification
    fails, etc.
    Nr   r9   zowner names do not matchzexpected an RRSIGzno RRSIGs validated)rw   r,   r/   r   ro   r"   rootr   choose_relativityr   r   r   r   )
r   r   r   rp   r   rq   rrname	rrsignamerrsigrdatasetr   s
             r#   	_validater     s   N ~&###FCHHMM:%q(E"QK	 MM	 %%f-F++F3I :;; %'#$788	E5$VD 1
22 "#78 		s   /DDDTprivate_keyr   dnskeyr   r   lifetimer   deterministicc                    |t         }|j                  |      st        t        | t              r3| d   j
                  }| d   j                  }| d   }| d   j                  }n0| j
                  }| j                  }| j                  }| j                  }|t        |      }nt        t        j                               }|t        |      }n|||z   }nt        d      |	|j                  |	      }t        |      dz
  }|j                         r|dz  }t!        |t"        j$                  j                   ||j&                  ||||t)        |      |d      }t"        j*                  j-                  | ||	      }t        |t.              r|}n	 t1        |      } ||      }|j7                  |||
      }t9        t         |j;                  |            S # t2        $ r t5        d      w xY w)	aE  Sign RRset using private key.

    *rrset*, the RRset to validate.  This can be a
    ``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
    tuple.

    *private_key*, the private key to use for signing, a
    ``cryptography.hazmat.primitives.asymmetric`` private key class applicable
    for DNSSEC.

    *signer*, a ``dns.name.Name``, the Signer's name.

    *dnskey*, a ``DNSKEY`` matching ``private_key``.

    *inception*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the
    signature inception time.  If ``None``, the current time is used.  If a ``str``, the
    format is "YYYYMMDDHHMMSS" or alternatively the number of seconds since the UNIX
    epoch in text form; this is the same the RRSIG rdata's text form.
    Values of type `int` or `float` are interpreted as seconds since the UNIX epoch.

    *expiration*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the signature
    expiration time.  If ``None``, the expiration time will be the inception time plus
    the value of the *lifetime* parameter.  See the description of *inception* above
    for how the various parameter types are interpreted.

    *lifetime*, an ``int`` or ``None``, the signature lifetime in seconds.  This
    parameter is only meaningful if *expiration* is ``None``.

    *verify*, a ``bool``.  If set to ``True``, the signer will verify signatures
    after they are created; the default is ``False``.

    *policy*, a ``dns.dnssec.Policy`` or ``None``.  If ``None``, the default policy,
    ``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.

    *origin*, a ``dns.name.Name`` or ``None``.  If ``None``, the default, then all
    names in the rrset (including its owner name) must be absolute; otherwise the
    specified origin will be used to make names absolute when signing.

    *deterministic*, a ``bool``. If ``True``, the default, use deterministic
    (reproducible) signatures when supported by the algorithm used for signing.
    Currently, this only affects ECDSA.

    Raises ``DeniedByPolicy`` if the signature is denied by policy.
    r9   r   z(expiration or lifetime must be specifiedr%   )r   r   type_coveredr<   labelsoriginal_ttlr   r   r   r   r   r3   zUnsupported key algorithm)r   )rw   rO   r   r,   r   r   r   ttlro   r2   r-   r   rz   derelativizer?   is_wildr   r   r   r<   rC   dnssecr   r   r   r   r1   signr   replace)r   r   r   r   r   r   r   r   rq   rp   r   r   r   r   r   rrsig_inceptionrrsig_expirationr   rrsig_templater   signing_keyprivate_clsr   s                          r#   _signr     s   t ~V$%(""qqQx||--yy&y1diik*'
3		*X5CDD $$V,[1_F ~~!}}""""!#!vN ::00OD +01!	97?K%+6K   v}=I~--	-BCC $ 	9788	9s   G G'c                    t        |t              r8t        j                  j	                  |t        j                  j
                        }|j                  }|j                         s|t        d      |j                  |      }t        |       \  }}d}|j                  |      }|J ||dd z  }||j                  j                  |      z  }|j                         s|t        d      |j                  |      }t        |      }|j                         r|j                  |dz
  k7  rt        d      |dz
  |j                  k  rt        d	      |j                  |dz
  k  rA|j!                  |j                  dz         d   }	t        j                  j	                  d
|	      }|j                         }
t#        j$                  d|j&                  |j(                  |j*                        }|D cg c]  }|j                  |       }}t-        |      D ]5  }||
z  }||z  }t#        j$                  dt        |            }||z  }||z  }7 |S c c}w )a  Create signature rdata.

    *rrset*, the RRset to sign/validate.  This can be a
    ``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
    tuple.

    *rrsig*, a ``dns.rdata.Rdata``, the signature to validate, or the
    signature template used when signing.

    *origin*, a ``dns.name.Name`` or ``None``, the origin to use for relative
    names.

    Raises ``UnsupportedAlgorithm`` if the algorithm is recognized by
    dnspython but not implemented.
    Nz,relative RR name without an origin specifiedr%   rv      r8   z&wild owner name has wrong label lengthr9   z#owner name longer than RRSIG labels*z!HHIz!H)r,   r/   r   ro   r"   r   r   is_absoluter   r   r   r;   to_digestabler?   r   r   splitr   r   r   r   r   sorted)r   r   rp   r   r   r   r   r   name_lensuffix	rrnamebufrrfixedr@   rdatasrrlens                  r#   r   r   _  s   * &###FCHHMM:\\F>#$RSS$$V, ,E2FHD==='DD"IDELL&&v..D >#$RSS$$V,6{H~~ELLHqL8 HII!|ell" EFF	1	$ellQ./2##C0$$&Ikk&(//83C3CUEWEWXG7?@ee!!&)@F@ 	D#e*- K As   /Ir   r   r   r   c                     t        j                  |      }t        | t              r| j	                  ||      S t        |      j                  } ||       j	                  ||      S )a  Convert a public key to DNSKEY Rdata

    *public_key*, a ``PublicKey`` (``GenericPublicKey`` or
    ``cryptography.hazmat.primitives.asymmetric``) to convert.

    *algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.

    *flags*: DNSKEY flags field as an integer.

    *protocol*: DNSKEY protocol field as an integer.

    Raises ``ValueError`` if the specified key algorithm parameters are not
    unsupported, ``TypeError`` if the key type is unsupported,
    `UnsupportedAlgorithm` if the algorithm is unknown and
    `AlgorithmKeyMismatch` if the algorithm does not match the key type.

    Return DNSKEY ``Rdata``.
    )r   r   r   )r   maker,   r   	to_dnskeyget_algorithm_clsr   )r   r<   r   r   r   s        r#   _make_dnskeyr     s`    2 y)I *./##%(#CC&y1<<
j)33%(3SSr%   c                     t        | |||      }t        |j                  t        j                  j                  |j
                  |j                  |j                  |j                        S )a  Convert a public key to CDNSKEY Rdata

    *public_key*, the public key to convert, a
    ``cryptography.hazmat.primitives.asymmetric`` public key class applicable
    for DNSSEC.

    *algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.

    *flags*: DNSKEY flags field as an integer.

    *protocol*: DNSKEY protocol field as an integer.

    Raises ``ValueError`` if the specified key algorithm parameters are not
    unsupported, ``TypeError`` if the key type is unsupported,
    `UnsupportedAlgorithm` if the algorithm is unknown and
    `AlgorithmKeyMismatch` if the algorithm does not match the key type.

    Return CDNSKEY ``Rdata``.
    r   r   r   r   r<   r3   )	r   r   r   r   r   r   r   r<   r3   )r   r<   r   r   r   s        r#   _make_cdnskeyr     sS    4 *iAF}}$$ll""JJ r%   domainsalt
iterationsc                 l   t         j                  dd      }	 t        |t               rt        |j	                            }|t        j                  k7  rt        d      |d}nDt        |t               r2t        |      dz  dk(  rt        j                  |      }nt        d      |}t        | t        j                  j                        st        j                  j                  |       } | j                         j!                         }|J t#        j$                  ||z         j'                         }t)        |      D ](  }t#        j$                  ||z         j'                         }* t+        j,                  |      j/                  d      }	|	j1                  |      }	|	S # t
        $ r t        d      w xY w)	a  
    Calculate the NSEC3 hash, according to
    https://tools.ietf.org/html/rfc5155#section-5

    *domain*, a ``dns.name.Name`` or ``str``, the name to hash.

    *salt*, a ``str``, ``bytes``, or ``None``, the hash salt.  If a
    string, it is decoded as a hex string.

    *iterations*, an ``int``, the number of iterations.

    *algorithm*, a ``str`` or ``int``, the hash algorithm.
    The only defined algorithm is SHA1.

    Returns a ``str``, the encoded NSEC3 hash.
     ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 0123456789ABCDEFGHIJKLMNOPQRSTUVz-Wrong hash algorithm (only SHA1 is supported)r%   r8   r   zInvalid salt lengthzutf-8)r/   	maketransr,   r   rx   ry   rz   r{   r?   bytesfromhexr   ro   Namer"   r   r;   r|   r}   r   r>   base64	b32encodedecode	translate)
r   r   r   r<   b32_conversionsalt_encodeddomain_encodedr   rK   outputs
             r#   
nsec3_hashr    sy   . ]]*,NNJi%!)//"34I INN"HII|	D#	t9q=A ==.L233fchhmm,##F+((*224N%%\\.<78??AF: >f|34;;=> f%,,W5Fn-FM9  JHIIJs   'F F3
algorithmsc           	         t        |       \  }}|j                  t        j                  j                  t        j                  j
                  t        j                  j                  fvrt        d      t               }|D ];  }	 t        |t              rt        |j                            }|j                  |       = |j                  t        j                  j                  k(  rug }t!        |      D ]"  }|j"                  |v s|j%                  |       $ t'        |      dk(  rt        d      t        j(                  j+                  |j,                  |      S g }|D ]  }|j/                  t1        ||||             ! t        j(                  j+                  |j,                  |      S # t        $ r t        d| d      w xY w)a  Create a DS record from DNSKEY/CDNSKEY/CDS.

    *rrset*, the RRset to create DS Rdataset for.  This can be a
    ``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
    tuple.

    *algorithms*, a set of ``str`` or ``int`` specifying the hash algorithms.
    The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
    does not matter for these strings. If the RRset is a CDS, only digest
    algorithms matching algorithms are accepted.

    *origin*, a ``dns.name.Name`` or ``None``.  If `key` is a relative name,
    then it will be made absolute using the specified origin.

    Raises ``UnsupportedAlgorithm`` if any of the algorithms are unknown and
    ``ValueError`` if the given RRset is not usable.

    Returns a ``dns.rdataset.Rdataset``
    zrrset not a DNSKEY/CDNSKEY/CDSrt   ru   r   zno acceptable CDS rdata found)r   r   r   r   r   r   r   rz   setr,   r/   r   rx   ry   r   addcds_rdataset_to_ds_rdatasetr   appendr?   r   from_rdata_listr   extenddnskey_rdataset_to_cds_rdataset)	r   r  rp   r   r   _algorithmsr<   resr@   s	            r#   make_ds_rdatasetr  (  s   2 ,E2FH 
 9::%K #		O)S)$Y__%67	 		"# #--+++0: 	"E  K/

5!	" s8q=<==||++HLL#>>
C  Y	

268YPVWXY<<''c::!  	O&)@1'MNN	Os   'F))Gr   c                    | j                   t        j                  j                  k7  rt	        d      g }| D ]l  }|j                  t        |j                  t        j                  j                  |j                  |j                  |j                  |j                               n t        j                  j                  | j                  |      S )zCreate a CDS record from DS.

    *rdataset*, a ``dns.rdataset.Rdataset``, to create DS Rdataset for.

    Raises ``ValueError`` if the rdataset is not CDS.

    Returns a ``dns.rdataset.Rdataset``
    zrdataset not a CDSr   )r   r   r   r   rz   r  r   r   r   r<   r   r   r   r  r   r   r  r@   s      r#   r  r  b  s     #--+++-..
C 



}}''//!--||		


 <<''c::r%   c           	      8   |j                   t        j                  j                  t        j                  j                  fvrt        d      g }|D ]  }|j                  t        | |||             ! t        j                  j                  |j                  |      S )a  Create a CDS record from DNSKEY/CDNSKEY.

    *name*, a ``dns.name.Name`` or ``str``, the owner name of the CDS record.

    *rdataset*, a ``dns.rdataset.Rdataset``, to create DS Rdataset for.

    *algorithm*, a ``str`` or ``int`` specifying the hash algorithm.
    The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
    does not matter for these strings.

    *origin*, a ``dns.name.Name`` or ``None``.  If `key` is a relative name,
    then it will be made absolute using the specified origin.

    Raises ``UnsupportedAlgorithm`` if the algorithm is unknown or
    ``ValueError`` if the rdataset is not DNSKEY/CDNSKEY.

    Returns a ``dns.rdataset.Rdataset``
    zrdataset not a DNSKEY/CDNSKEY)r   r   r   r   r   rz   r  r   r   r  r   )ro   r   r<   rp   r  r@   s         r#   r  r    s{    2 s}}33S]]5J5JKK899
C =

8D%F;<=<<''c::r%   c                    | j                   t        j                  j                  k7  rt	        d      g }| D ]^  }|j                  t        | j                  | j                   |j                  |j                  |j                  |j                               ` t        j                  j                  | j                  |      S )zCreate a CDNSKEY record from DNSKEY.

    *rdataset*, a ``dns.rdataset.Rdataset``, to create CDNSKEY Rdataset for.

    Returns a ``dns.rdataset.Rdataset``
    zrdataset not a DNSKEYr   )r   r   r   r   rz   r  r   r   r   r   r<   r3   r   r  r   r  s      r#   #dnskey_rdataset_to_cdnskey_rdatasetr    s     #--...011
C 



 ((kk//II		


 <<''c::r%   txnkskszsksc                    |j                   t        t        j                  j                  j
                  t        j                  j                  j                  t        j                  j                  j                  g      v r|}n|}|D ]U  \  }}t        j                  j                  |||||||||	|

      }| j                  |j                  |j                  |       W y)zDefault RRset signer)
r   r   r   r   r   r   r   rq   rp   r   N)r   r	  r   r   	RdataTyper   r   r   r   r   r
  ro   r   )r  r   r   r  r  r   r   r   rq   rp   r   r   r   r   r   s                  r#   default_rrset_signerr    s     ||sMM##**MM##''MM##++	
  # .V

#!'   
 	

EIIu-.r%   zone
add_dnskey
dnskey_ttlnsec3rrset_signerc                 l   g }g }|rS|D ]E  }|d   j                   t        j                  z  r|j                  |       5|j                  |       G |s|}|s|}ng }|rt	        j
                  |      }n| j                         }| j                  t        d      |5 }|r||j                  | j                  t        j                  j                        }|r|j                  }n@|j                  | j                  t        j                  j                        }|j                  }|D ]"  \  }}|j                  | j                  ||       $ |rt!        d      |	xs7 t#        j$                  t&        | j                  ||||||
| j                  |
      }t)        | ||      cddd       S # 1 sw Y   yxY w)a  Sign zone.

    *zone*, a ``dns.zone.Zone``, the zone to sign.

    *txn*, a ``dns.transaction.Transaction``, an optional transaction to use for
    signing.

    *keys*, a list of (``PrivateKey``, ``DNSKEY``) tuples, to use for signing. KSK/ZSK
    roles are assigned automatically if the SEP flag is used, otherwise all RRsets are
    signed by all keys.

    *add_dnskey*, a ``bool``.  If ``True``, the default, all specified DNSKEYs are
    automatically added to the zone on signing.

    *dnskey_ttl*, a``int``, specifies the TTL for DNSKEY RRs. If not specified the TTL
    of the existing DNSKEY RRset used or the TTL of the SOA RRset.

    *inception*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the signature
    inception time.  If ``None``, the current time is used.  If a ``str``, the format is
    "YYYYMMDDHHMMSS" or alternatively the number of seconds since the UNIX epoch in text
    form; this is the same the RRSIG rdata's text form. Values of type `int` or `float`
    are interpreted as seconds since the UNIX epoch.

    *expiration*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the signature
    expiration time.  If ``None``, the expiration time will be the inception time plus
    the value of the *lifetime* parameter.  See the description of *inception* above for
    how the various parameter types are interpreted.

    *lifetime*, an ``int`` or ``None``, the signature lifetime in seconds.  This
    parameter is only meaningful if *expiration* is ``None``.

    *nsec3*, a ``NSEC3PARAM`` Rdata, configures signing using NSEC3. Not yet
    implemented.

    *rrset_signer*, a ``Callable``, an optional function for signing RRsets. The
    function requires two arguments: transaction and RRset. If the not specified,
    ``dns.dnssec.default_rrset_signer`` will be used.

    *deterministic*, a ``bool``. If ``True``, the default, use deterministic
    (reproducible) signatures when supported by the algorithm used for signing.
    Currently, this only affects ECDSA.

    Returns ``None``.
    r9   Nzno zone originz&Signing with NSEC3 not yet implemented)	r   r  r  r   r   r   rq   rp   r   )r   r   SEPr  
contextlibnullcontextwriterrp   rz   r   r   r   r   r   SOAr
  NotImplementedError	functoolspartialr  _sign_zone_nsec)r  r  r   r  r   r   r   r   r!  r"  rq   r   r  r  r3   cm_txnr   soarK   _rrset_signers                        r#   	sign_zoner1    s   v DD  	!C1v||dhh&C C 		!
 DD
0:0F0Fs0K[[]{{)**	 >t!$++s}}/C/CD!'J((4;;0A0ABC!$J! :	6j&9: %&NOO( I,=,=${{#%!{{+-M #4}=7> > >s   DF**F3c                    	 d
dt         j                  j                  dt         j                  j                  dt
        t         j                  j                     dt         j                  j                  dt        dt
        t           ddfd	}| j                         j                  }d}d}t        |j                               D ]  }|r|j                  |      r|j                  |t         j                   j"                        r|| j$                  k7  r|}nd}|r|j'                  |      }|r|j(                  D ]  }	|	j*                  t         j                   j,                  k(  r+|r(|	j*                  t         j                   j.                  k7  rUt        j0                  j2                  ||	j4                  g|	 }
 |||
        | ||||| j6                  ||       |} |r" |||| j$                  | j6                  ||       yy)zNSEC zone signerNr  ro   next_securer   r   r"  r    c           
      T   t        t        j                  j                  j                  t        j                  j                  j
                  g      }| j                  |      }|r|rt        |j                  D cg c]  }|j                   c}      |z  }	t        j                  t        |	            }
t        j                  j                  ||t        |t        j                  j                  j
                  ||
            }| j                  |       |r
 || |       yyyyc c}w )zNSEC zone signer helper)r   r   nextwindowsN)r	  r   r   r  r   r   get_node	rdatasetsr   r   from_rdtypeslistr   
from_rdatar
  )r  ro   r3  r   r   r"  mandatory_typesr   r   typesr6  r   s               r#   _txn_add_nsecz&_sign_zone_nsec.<locals>._txn_add_nsec_  s     ]]$$**CMM,C,C,H,HI
 ||D!KT^^DX__DEW  ))$u+6GII((#==2277$#		E GGENS%( !  4Ds   :D%rG   )r   transactionTransactionro   r   r   r   
RdataClassr-   RRsetSignerget_soaminimumr   iterate_namesis_subdomainr   r   NSrp   r7  r8  r   r   r   r   r;  r   r   )r  r  r"  r>  	rrsig_ttl
delegationlast_securero   r   r   r   s              r#   r,  r,  X  s    /3)__(()hhmm) chhmm,) **	)
 ) {+) 
)@ &&IJKs((*+ $++J7WWT3==++,1DJ J<<%D $ 	1H#--*=*== #3==;K;K(K  #		 4 4T8<< S( S$S%0	1 "#{D$,,	<X9< dkk4<<L	
 r%   c                      t        d      )Nz.DNSSEC validation requires python cryptography)ImportError)argskwargss     r#   
_need_pycarO    s    
8 r%   r   )r   )dsa)ec)ed448)rsa)ed25519)r   r   )r   r   )NNFrG   )NNN)NNNFNNT)NNNNNT)NNTNNNNNNNT)__doc__r   r%  r*  r|   r   r   r   typingr   r   r   r   r   r	   r
   r   dns._featuresr   dns.exceptiondns.namedns.node	dns.rdatadns.rdataclassdns.rdatasetdns.rdatatype	dns.rrsetdns.transactiondns.zonedns.dnssectypesr   r   r   r   r   r   r   dns.rdtypes.ANY.CDNSKEYr   dns.rdtypes.ANY.CDSr   dns.rdtypes.ANY.DNSKEYr   dns.rdtypes.ANY.DSr   dns.rdtypes.ANY.NSECr   r   dns.rdtypes.ANY.NSEC3PARAMr   dns.rdtypes.ANY.RRSIGr   r   dns.rdtypes.dnskeybaser   	PublicKey
PrivateKeyr?  r@  r   RRsetrB  r/   r$   r-   r*   r0   r2   rC   rE   r\   r=   DSADSANSEC3SHA1ECCGOSTNULLr{   GOSTrfc_8624_policyr	  allow_all_policyrw   ro   r   r@   RdatarZ   r   r   r   Rdatasetr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  Zoner1  r,  rO  	_featureshavecryptography.exceptionsr   )cryptography.hazmat.primitives.asymmetricrP  rQ  rR  rS  rT  dns.dnssecalgsr   r   dns.dnssecalgs.baser   r   validatevalidate_rrsigr   make_dnskeymake_cdnskey
_have_pycaDHECCRSASHA1RSASHA1NSEC3SHA1	RSASHA256	RSASHA512ECDSAP256SHA256ECDSAP384SHA384ED25519ED448INDIRECT
PRIVATEDNS
PRIVATEOIDrH   r%   r#   <module>r     s  $ 5        J J J            : :  , # ) ! - 1 = '	 
 33SYY__EtKL%c %i %$U9c>2 $s $6hUC78 6S 6fgo& 3 , "7 7* y}}i&<&<i>O>OPy}}i&<&<=]]HMM8==1]]O	 ceSUCE359  ! '+#J
s"
#J	J Xs]#J SXX]]#	J
 VJ J Jb '+	!
s"
#!	! Xs]#! SXX]]#	!
 	!H
sxx}}eCLL$9$9388==$HII
JSXd6l(!%s||7L7L(L"MMN!
388==#,,///0!!U !% !f ! ! '+#C.%s||7L7L(L"MMNC.C. sxx}}eCHHMM3<<3H3H$HII
JC. SXX]]#	C.
 
%C. VC. 
C.T '+#F3%s||7L7L(L"MMNF3CIIOOU388==#,,:O:O+O%PPQF3 sxx}}eCHHMM3<<3H3H$HII
JF3 SXX]]#	F3
 
%F3 VF3 
F3\ =A=A"#&*|D%s||7L7L(L"MMN|D|D HHMM|D 	|D
 hS%789|D xc589:|D sm|D |D V|D SXX]]#|D |D |DD '+@%s||7L7L(L"MMN@@ SXX]]#@ 	@L 	 T TS#X T  T 	 T
  TL 	##S#X# # 	#
 #L:#((--$%:
5e$
%: : S#X	:
 	:@ '+7;%s||7L7L(L"MMN7;E(C-()7; SXX]]#7; 	\\	7;t;ll##;\\;B '+	;
s"
#;ll##; Xs]#; SXX]]#	;
 	\\;B;ll##;\\;B =A=A"#&*'.		$	$'.99??'. HHMM'. uZ'(
)	'.
 uZ'(
)'. hS%789'. xc589:'. sm'. V'. SXX]]#'. '. 
'.X 266: $<@=A""&*.#p>
((--p>	#//--	.p> 4j&012
3p> 	p>
 p> hS%789p> xc589:p> smp> Jp> ;'p> Vp> p> 
p>l +/L

((--L
		$	$L
 ;'L
 
	L
^ ==h8=<?= HH$NDK LJHNDKLJ 
		\\mmmm


%%-- 		


++++


!!
!!
r%   