How Get .pfx Cert File Expiration with hb_ssl?

156 views
Skip to first unread message

Francolino

unread,
Dec 27, 2023, 11:04:49 AM12/27/23
to harbou...@googlegroups.com
Dear harbor users

How can I obtain the expiration date of a PFX certificate using the hb_ssl library?

I found that in Python it is achieved in the following way:


from OpenSSL import crypto
from cryptography import x509
from cryptography.hazmat.backends import default_backend

pkcs12 = crypto.load_pkcs12(open('cert.pfx', "rb").read(), '1234'.encode('ascii'))
pem_data = crypto.dump_certificate(crypto.FILETYPE_PEM, pkcs12.get_certificate())
cert = x509.load_pem_x509_certificate(pem_data, default_backend())
print(cert.not_valid_after) 



Thanks in advance,

Regards 

Juan Francolino

diego...@gmail.com

unread,
Dec 27, 2023, 4:50:03 PM12/27/23
to Harbour Users
Hola Juan, puedes agregar esta funcion al cms.c que arme hace unos años para la firma electronica.
Se podria mejorar para que devuelva directamente un tTimeStamp o sino asi como esta interpretar la string que devuelve. 

// HB_PFX_Expire( cPFXFile, cPassword ) --> cExpireDateTime ex: "Apr  2 15:18:57 2019 GMT" or nError if error
HB_FUNC(HB_PFX_EXPIRE)
{
   X509 *x509;
   EVP_PKEY *priKey;
   FILE *fp;
   PKCS12 *p12;
   STACK_OF(X509) *ca = NULL;
   OpenSSL_add_all_algorithms();
   OpenSSL_add_all_ciphers();

   if ((fp = fopen(hb_parc(1), "rb")) == NULL)
   {
      hb_retni(1);   //Error opening pfx file
      return;
   }
   p12 = d2i_PKCS12_fp(fp, NULL);
   fclose(fp);
   if (!p12)
   {
      hb_retni(2);   //Error pfx file format
      return;
   }
   if (!PKCS12_parse(p12, hb_parc(2), &priKey, &x509, &ca))
   {
      hb_retni(3);   //Password error
      return;
   }

   PKCS12_free(p12);

   ASN1_TIME *notAfter = X509_get0_notAfter(x509);
   char *str = (char *)ASN1_STRING_data(notAfter);

   BIO *bio;
   char buf[32];
   int write = 0;
   bio = BIO_new(BIO_s_mem());
   if (bio)
   {
      if (ASN1_TIME_print(bio, notAfter))
      {
         write = BIO_read(bio, buf, 32 - 1);
      }
      BIO_free(bio);
   }
   buf[write] = '\0';
   X509_free(x509);
   hb_retc(buf);
}


Un abz y buen comienzo de año.
Diego.

Reply all
Reply to author
Forward
0 new messages