<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'security.cert.Certificate',
    'security.Principal',
    'security.crypto.PublicKey',
    'security.OpenSslUtil',
    'util.Date'
  );

  /**
   * X.509 certificate
   *
   * <code>
   *   uses('security.cert.X509Certificate');
   *   
   *   $x509= X509Certificate::fromString(<<<EOC
   * -----BEGIN CERTIFICATE-----
   * [...]
   * -----END CERTIFICATE-----
   * EOC
   *  );
   *   
   *   printf(<<<EOP
   * Certificate information
   * ------------------------------------------------------------------------
   * Subject     %s
   * Issuer      %s
   * 
   * Serial#     %d
   * Version#    %d
   * Hash        %s
   * 
   * Valid from  %s
   * Valid until %s
   * Valid       %s
   * 
   * Purposes    %s
   * ------------------------------------------------------------------------
   * 
   * EOP
   *     ,
   *     $x509->getSubjectDN()->getName(),
   *     $x509->getIssuerDN()->getName(),
   *     $x509->getSerialNumber(),
   *     $x509->getVersion(),
   *     $x509->getHash(),
   *     $x509->getNotBefore()->toString(),
   *     $x509->getNotAfter()->toString(),
   *     $x509->checkValidity() ? 'yes' : 'no',
   *     var_export($x509->getKeyUsage(), 1)
   *   );
   * </code>
   *
   * @ext      openssl
   * @see      rfc://2459 Internet X.509 Public Key Infrastructure Certificate and CRL Profile
   * @test     xp://net.xp_framework.unittest.security.CertificateTest
   * @purpose  Represent X509 certificate
   */
  class X509Certificate extends Certificate {
    public
      $_res=  NULL,
      $_info= array();
    
    /**
     * Constructor
     *
     * @param   array _info information from openssl_x509_parse() or NULL to trigger auto-parsing
     * @param   resource _res resource handle of x.509 certificate
     */
    public function __construct($_info, $_res) {
      $this->_info= $_info;
      $this->_res= $_res;

      if (NULL === $_info) {
        if (!is_array($this->_info= openssl_x509_parse($_res, TRUE))) {
          throw new CertificateException(
            'Cannot parse certificate information', OpenSslUtil::getErrors()
          );
        }
      }
    }
    
    /**
     * Checks validity
     *
     * @param   util.Date date default NULL (date to check against, defaulting to now)
     * @return  bool TRUE if this certificate is valid for the given date
     */
    public function checkValidity($date= NULL) {
      if (NULL === $date) $date= new Date();
      return (
        ($date->getTime() >= $this->_info['validFrom_time_t']) ||
        ($date->getTime() <= $this->_info['validTo_time_t'])
      );
    }
    
    /**
     * Gets the notBefore date from the validity period of the certificate.
     *
     * @return  util.Date
     */
    public function getNotBefore() {
      return new Date($this->_info['validFrom_time_t']);
    }
    
    /**
     * Gets the notAfter date from the validity period of the certificate.
     *
     * @return  util.Date
     */
    public function getNotAfter() {
      return new Date($this->_info['validTo_time_t']);
    }

    /**
     * Callback for decoding
     *
     * @param   string value
     * @return  string
     */
    protected static function decode($value) {
      return iconv('utf-8', xp::ENCODING, $value);
    }
    
    /**
     * Gets the issuer DN (distinguished name)
     *
     * @return  security.Principal
     */
    public function getIssuerDN() {
      if (version_compare(phpversion(), '5.2.2', '<')) {
        return new Principal($this->_info['issuer']);
      } else {
        return new Principal(array_map(array('self', 'decode'), $this->_info['issuer']));
      }
    }
    
    /**
     * Gets the subject DN (distinguished name)
     *
     * @return  security.Principal
     */
    public function getSubjectDN() {
      if (version_compare(phpversion(), '5.2.2', '<')) {
        return new Principal($this->_info['subject']);
      } else {
        return new Principal(array_map(array('self', 'decode'), $this->_info['subject']));
      }
    }
    
    /**
     * Retrieve certificate's version
     *
     * @return  int version
     */
    public function getVersion() {
      return $this->_info['version'];
    }

    /**
     * Retrieve certificate's serial number
     *
     * @return  int serial number
     */
    public function getSerialNumber() {<commit_msg>- 5.3.0+: Remove check for PHP < 5.2.2<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'security.cert.Certificate',
    'security.Principal',
    'security.crypto.PublicKey',
    'security.OpenSslUtil',
    'util.Date'
  );

  /**
   * X.509 certificate
   *
   * <code>
   *   uses('security.cert.X509Certificate');
   *   
   *   $x509= X509Certificate::fromString(<<<EOC
   * -----BEGIN CERTIFICATE-----
   * [...]
   * -----END CERTIFICATE-----
   * EOC
   *  );
   *   
   *   printf(<<<EOP
   * Certificate information
   * ------------------------------------------------------------------------
   * Subject     %s
   * Issuer      %s
   * 
   * Serial#     %d
   * Version#    %d
   * Hash        %s
   * 
   * Valid from  %s
   * Valid until %s
   * Valid       %s
   * 
   * Purposes    %s
   * ------------------------------------------------------------------------
   * 
   * EOP
   *     ,
   *     $x509->getSubjectDN()->getName(),
   *     $x509->getIssuerDN()->getName(),
   *     $x509->getSerialNumber(),
   *     $x509->getVersion(),
   *     $x509->getHash(),
   *     $x509->getNotBefore()->toString(),
   *     $x509->getNotAfter()->toString(),
   *     $x509->checkValidity() ? 'yes' : 'no',
   *     var_export($x509->getKeyUsage(), 1)
   *   );
   * </code>
   *
   * @ext      openssl
   * @see      rfc://2459 Internet X.509 Public Key Infrastructure Certificate and CRL Profile
   * @test     xp://net.xp_framework.unittest.security.CertificateTest
   * @purpose  Represent X509 certificate
   */
  class X509Certificate extends Certificate {
    public
      $_res=  NULL,
      $_info= array();
    
    /**
     * Constructor
     *
     * @param   array _info information from openssl_x509_parse() or NULL to trigger auto-parsing
     * @param   resource _res resource handle of x.509 certificate
     */
    public function __construct($_info, $_res) {
      $this->_info= $_info;
      $this->_res= $_res;

      if (NULL === $_info) {
        if (!is_array($this->_info= openssl_x509_parse($_res, TRUE))) {
          throw new CertificateException(
            'Cannot parse certificate information', OpenSslUtil::getErrors()
          );
        }
      }
    }
    
    /**
     * Checks validity
     *
     * @param   util.Date date default NULL (date to check against, defaulting to now)
     * @return  bool TRUE if this certificate is valid for the given date
     */
    public function checkValidity($date= NULL) {
      if (NULL === $date) $date= new Date();
      return (
        ($date->getTime() >= $this->_info['validFrom_time_t']) ||
        ($date->getTime() <= $this->_info['validTo_time_t'])
      );
    }
    
    /**
     * Gets the notBefore date from the validity period of the certificate.
     *
     * @return  util.Date
     */
    public function getNotBefore() {
      return new Date($this->_info['validFrom_time_t']);
    }
    
    /**
     * Gets the notAfter date from the validity period of the certificate.
     *
     * @return  util.Date
     */
    public function getNotAfter() {
      return new Date($this->_info['validTo_time_t']);
    }

    /**
     * Callback for decoding
     *
     * @param   string value
     * @return  string
     */
    protected static function decode($value) {
      return iconv('utf-8', xp::ENCODING, $value);
    }
    
    /**
     * Gets the issuer DN (distinguished name)
     *
     * @return  security.Principal
     */
    public function getIssuerDN() {
      return new Principal(array_map(array('self', 'decode'), $this->_info['issuer']));
    }
    
    /**
     * Gets the subject DN (distinguished name)
     *
     * @return  security.Principal
     */
    public function getSubjectDN() {
      return new Principal(array_map(array('self', 'decode'), $this->_info['subject']));
    }
    
    /**
     * Retrieve certificate's version
     *
     * @return  int version
     */
    public function getVersion() {
      return $this->_info['version'];
    }

    /**
     * Retrieve certificate's serial number
     *
     * @return  int serial number
     */
    public function getSerialNumber() {
{'fraction_non_alphanumeric': 0.17571661617338616, 'fraction_numerical': 0.016080167793055232, 'mean_word_length': 1.8354806739345888, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3519182', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2581, 'n_words': 590}