Ticket #302: x509-track-sha1.patch

File x509-track-sha1.patch, 3.2 KB (added by Samuli Seppänen, 11 years ago)

Patch: Extended x509-track to allow SHA1 certificate hash to be extracted

  • ssl_verify_openssl.c

    Extended x509-track to allow SHA1 certificate hash to be extracted,
    e.g.:
    
      x509-track "+SHA1"
    
    will extract the SHA1 certificate hash for all certs in the
    client chain.
    
     
    589589{
    590590  X509_NAME *x509_name = X509_get_subject_name (x509);
    591591  const char nullc = '\0';
    592   int i;
    593592
    594593  while (xt)
    595594    {
    596595      if (depth == 0 || (xt->flags & XT_FULL_CHAIN))
    597596        {
    598           i = X509_NAME_get_index_by_NID(x509_name, xt->nid, -1);
    599           if (i >= 0)
     597          switch (xt->nid)
    600598            {
    601               X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509_name, i);
    602               if (ent)
    603                 {
    604                   ASN1_STRING *val = X509_NAME_ENTRY_get_data (ent);
    605                   unsigned char *buf;
    606                   buf = (unsigned char *)1; /* bug in OpenSSL 0.9.6b ASN1_STRING_to_UTF8 requires this workaround */
    607                   if (ASN1_STRING_to_UTF8 (&buf, val) > 0)
    608                     {
    609                       do_setenv_x509(es, xt->name, (char *)buf, depth);
    610                       OPENSSL_free (buf);
    611                     }
    612                 }
     599            case NID_sha1:
     600              {
     601                int i;
     602                const int hl = SHA_DIGEST_LENGTH*3+1;
     603                char hash_str[hl];
     604                char *hs = hash_str;
     605                const unsigned char *src = x509->sha1_hash;
     606                for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
     607                  {
     608                    openvpn_snprintf(hs, 4, "%02X:", src[i]);
     609                    hs += 3;
     610                  }
     611                --hs; /* wipe the trailing ':' */
     612                *hs = '\0';
     613                do_setenv_x509(es, xt->name, hash_str, depth);
     614              }
     615              break;
     616            default:
     617              {
     618                int i = X509_NAME_get_index_by_NID(x509_name, xt->nid, -1);
     619                if (i >= 0)
     620                  {
     621                    X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509_name, i);
     622                    if (ent)
     623                      {
     624                        ASN1_STRING *val = X509_NAME_ENTRY_get_data (ent);
     625                        unsigned char *buf;
     626                        buf = (unsigned char *)1; /* bug in OpenSSL 0.9.6b ASN1_STRING_to_UTF8 requires this workaround */
     627                        if (ASN1_STRING_to_UTF8 (&buf, val) > 0)
     628                          {
     629                            do_setenv_x509(es, xt->name, (char *)buf, depth);
     630                            OPENSSL_free (buf);
     631                          }
     632                      }
     633                  }
     634                else
     635                  {
     636                    i = X509_get_ext_by_NID(x509, xt->nid, -1);
     637                    if (i >= 0)
     638                      {
     639                        X509_EXTENSION *ext = X509_get_ext(x509, i);
     640                        if (ext)
     641                          {
     642                            BIO *bio = BIO_new(BIO_s_mem());
     643                            if (bio)
     644                              {
     645                                if (X509V3_EXT_print(bio, ext, 0, 0))
     646                                  {
     647                                    if (BIO_write(bio, &nullc, 1) == 1)
     648                                      {
     649                                        char *str;
     650                                        BIO_get_mem_data(bio, &str);
     651                                        do_setenv_x509(es, xt->name, str, depth);
     652                                      }
     653                                  }
     654                                BIO_free(bio);
     655                              }
     656                          }
     657                      }
     658                  }
     659              }
    613660            }
    614           else
    615             {
    616               i = X509_get_ext_by_NID(x509, xt->nid, -1);
    617               if (i >= 0)
    618                 {
    619                   X509_EXTENSION *ext = X509_get_ext(x509, i);
    620                   if (ext)
    621                     {
    622                       BIO *bio = BIO_new(BIO_s_mem());
    623                       if (bio)
    624                         {
    625                           if (X509V3_EXT_print(bio, ext, 0, 0))
    626                             {
    627                               if (BIO_write(bio, &nullc, 1) == 1)
    628                                 {
    629                                   char *str;
    630                                   BIO_get_mem_data(bio, &str);
    631                                   do_setenv_x509(es, xt->name, str, depth);
    632                                 }
    633                             }
    634                           BIO_free(bio);
    635                         }
    636                     }
    637                 }
    638             }
    639661        }
    640662      xt = xt->next;
    641663    }