ó
L]c           @   sÂ   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 m Z m Z m	 Z	 m
 Z
 e
 d ƒ Z e
 d ƒ Z d d d d „ Z d d „ Z d	 „  Z d
 d „ Z d d d g Z d S(   s1   0MQ authentication related functions and classes.iÿÿÿÿN(   t   bytest   unicodet   bt   us›   #   ****  Generated on {0} by pyzmq  ****
#   ZeroMQ CURVE **Secret** Certificate
#   DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions.

s
  #   ****  Generated on {0} by pyzmq  ****
#   ZeroMQ CURVE Public Certificate
#   Exchange securely, or use a secure mechanism to verify the contents
#   of this file after exchange. Store public certificates in your home
#   directory, in the .curve subdirectory.

s   utf-8c   	   	   C   st  t  | t ƒ r! | j | ƒ } n  t  | t ƒ rB | j | ƒ } n  t j |  d d d ƒ} | j | j t j j ƒ  ƒ ƒ | j t	 d ƒ ƒ | rx~ | j
 ƒ  D]m \ } } t  | t ƒ rÏ | j | ƒ } n  t  | t ƒ rð | j | ƒ } n  | j t	 d ƒ j | | ƒ ƒ q¢ Wn  | j t	 d ƒ ƒ | j t	 d ƒ j | ƒ ƒ | rj| j t	 d ƒ j | ƒ ƒ n  Wd	 QXd	 S(
   s   Create a certificate filet   wt   encodingt   utf8s	   metadata
s       {0} = {1}
s   curve
s       public-key = "{0}"
s       secret-key = "{0}"
N(   t
   isinstanceR    t   decodet   iot   opent   writet   formatt   datetimet   nowR   t   items(	   t   key_filenamet   bannert
   public_keyt
   secret_keyt   metadataR   t   ft   kt   v(    (    s-   lib/python2.7/site-packages/zmq/auth/certs.pyt   _write_key_file   s$    &c   	      C   sœ   t  j ƒ  \ } } t j j |  | ƒ } d j | ƒ } d j | ƒ } t j j ƒ  } t | t	 j | ƒ | ƒ t | t
 j | ƒ | d | d | ƒ| | f S(   si   Create zmq certificates.
    
    Returns the file paths to the public and secret certificate files.
    s   {0}.key_secrets   {0}.keyR   R   (   t   zmqt   curve_keypairt   ost   patht   joinR   R   R   R   t   _cert_public_bannert   _cert_secret_banner(	   t   key_dirt   nameR   R   R   t   base_filenamet   secret_key_filet   public_key_fileR   (    (    s-   lib/python2.7/site-packages/zmq/auth/certs.pyt   create_certificates6   s    c         C   s  d	 } d	 } t j j |  ƒ s6 t d j |  ƒ ƒ ‚ n  t |  d ƒ « } x¡ | D]™ } | j ƒ  } | j d ƒ rv qO n  | j d ƒ r§ | j	 d d ƒ d j d ƒ } n  | j d ƒ rØ | j	 d d ƒ d j d ƒ } n  | rO | rO PqO qO WWd	 QX| d	 k rt
 d
 |  ƒ ‚ n  | | f S(   s  Load public and secret key from a zmq certificate.
    
    Returns (public_key, secret_key)
    
    If the certificate file only contains the public key,
    secret_key will be None.
    
    If there is no public key found in the file, ValueError will be raised.
    s   Invalid certificate file: {0}t   rbt   #s
   public-keyt   =i   s    	'"s
   secret-keyNs   No public key found in %s(   t   NoneR   R   t   existst   IOErrorR   R
   t   stript
   startswitht   splitt
   ValueError(   t   filenameR   R   R   t   line(    (    s-   lib/python2.7/site-packages/zmq/auth/certs.pyt   load_certificateN   s$    
""t   .c         C   sŽ   i  } t  j j |  ƒ s0 t d j |  ƒ ƒ ‚ n  t  j j |  d ƒ } t j | ƒ } x3 | D]+ } t | ƒ \ } } | r[ t | | <q[ q[ W| S(   s5   Load public keys from all certificates in a directorys"   Invalid certificate directory: {0}s   *.key(	   R   R   t   isdirR+   R   R   t   globR2   t   True(   t	   directoryt   certst   glob_stringt
   cert_filest	   cert_fileR   t   _(    (    s-   lib/python2.7/site-packages/zmq/auth/certs.pyt   load_certificateso   s    R%   R2   R=   (   t   __doc__R   R5   R	   R   R   t   zmq.utils.strtypesR    R   R   R   R   R   R)   R   R%   R2   R=   t   __all__(    (    (    s-   lib/python2.7/site-packages/zmq/auth/certs.pyt   <module>   s   "			!