3131# define SERIAL_RAND_BITS 159
3232
3333BIO * bio_err ;
34+ #if OPENSSL_API_COMPAT >= 30000
3435OSSL_LIB_CTX * libctx = NULL ;
36+ #endif
3537static const char * propq = NULL ;
3638static unsigned long nmflag = 0 ;
3739static char nmflag_set = 0 ;
@@ -165,7 +167,11 @@ int do_X509_REQ_verify(X509_REQ *x, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *vf
165167 int rv = 0 ;
166168
167169 if (do_x509_req_init (x , vfyopts ) > 0 ){
170+ #if OPENSSL_API_COMPAT <= 10100
171+ rv = X509_REQ_verify (x , pkey );
172+ #else
168173 rv = X509_REQ_verify_ex (x , pkey , libctx , propq );
174+ #endif
169175 }
170176 else
171177 rv = -1 ;
@@ -242,25 +248,42 @@ unsigned long get_nameopt(void)
242248 nmflag_set ? nmflag : XN_FLAG_SEP_CPLUS_SPC | ASN1_STRFLGS_UTF8_CONVERT ;
243249}
244250
251+ #if OPENSSL_API_COMPAT >= 30000
245252static int do_sign_init (EVP_MD_CTX * ctx , EVP_PKEY * pkey , const char * md , STACK_OF (OPENSSL_STRING ) * sigopts )
253+ #else
254+ static int do_sign_init (EVP_MD_CTX * ctx , EVP_PKEY * pkey , const EVP_MD * md , STACK_OF (OPENSSL_STRING ) * sigopts )
255+ #endif
246256{
247257 EVP_PKEY_CTX * pkctx = NULL ;
258+ #if OPENSSL_API_COMPAT >= 30000
248259 char def_md [80 ];
260+ #else
261+ int def_nid ;
262+ #endif
249263
250264 if (ctx == NULL )
251265 return 0 ;
252266 /*
253267 * EVP_PKEY_get_default_digest_name() returns 2 if the digest is mandatory
254268 * for this algorithm.
255269 */
270+ #if OPENSSL_API_COMPAT >= 30000
256271 if (EVP_PKEY_get_default_digest_name (pkey , def_md , sizeof (def_md )) == 2
257272 && strcmp (def_md , "UNDEF" ) == 0 ) {
273+ #else
274+ if (EVP_PKEY_get_default_digest_nid (pkey , & def_nid ) == 2
275+ && def_nid == NID_undef ) {
276+ #endif
258277 /* The signing algorithm requires there to be no digest */
259278 md = NULL ;
260279 }
261280
281+ #if OPENSSL_API_COMPAT >= 30000
262282 int val = EVP_DigestSignInit_ex (ctx , & pkctx , md , libctx ,
263283 propq , pkey , NULL );
284+ #else
285+ int val = EVP_DigestSignInit (ctx , & pkctx , md , NULL , pkey );
286+ #endif
264287 return val
265288 && do_pkey_ctx_init (pkctx , sigopts );
266289}
@@ -412,7 +435,11 @@ SV * sign(self, request_SV, days, name_SV, text, sigopts)
412435
413436 // Create a new certificate store
414437 X509 * x ;
438+ #if OPENSSL_API_COMPAT <= 10100
439+ if ((x = X509_new ()) == NULL )
440+ #else
415441 if ((x = X509_new_ex (libctx , propq )) == NULL )
442+ #endif
416443 croak ("X509_new_ex failed ...\n" );
417444
418445 // FIXME need to look at this
@@ -460,18 +487,30 @@ SV * sign(self, request_SV, days, name_SV, text, sigopts)
460487
461488 // Create the X509 v3 extensions for the certificate
462489 X509V3_CTX ext_ctx ;
463- X509V3_set_ctx (& ext_ctx , issuer_cert , x , csr /*NULL*/ , NULL , X509V3_CTX_REPLACE );
464490
465491 // Set the certificate issuer from the private key
492+ #if OPENSSL_API_COMPAT >= 30000
493+ X509V3_set_ctx (& ext_ctx , issuer_cert , x , NULL , NULL , X509V3_CTX_REPLACE );
466494 if (!X509V3_set_issuer_pkey (& ext_ctx , private_key ))
467495 croak ("X509V3_set_issuer_pkey cannot set issuer private key\n" );
496+ #else
497+ X509V3_set_ctx (& ext_ctx , issuer_cert , x , csr , NULL , X509V3_CTX_REPLACE );
498+ #endif
468499
469500 // Set the X509 version of the certificate
501+ #if OPENSSL_API_COMPAT >= 30000
470502 if (!X509_set_version (x , X509_VERSION_3 ))
503+ #else
504+ if (!X509_set_version (x , 2 ))
505+ #endif
471506 croak ("X509_set_version cannot set version 3\n" );
472507
473508 // Get digestname parameter - verify that it is valid
509+ #if OPENSSL_API_COMPAT >= 30300
510+ const EVP_MD * dgst ;
511+ #else
474512 EVP_MD * md ;
513+ #endif
475514 digestname = (unsigned char * ) SvPV (name_SV , digestname_length );
476515 md = (EVP_MD * )EVP_get_digestbyname (digestname );
477516 if (md != NULL )
@@ -483,7 +522,11 @@ SV * sign(self, request_SV, days, name_SV, text, sigopts)
483522 mctx = EVP_MD_CTX_new ();
484523
485524 // Sign the new certificate
525+ #if OPENSSL_API_COMPAT >= 30000
486526 if (mctx != NULL && do_sign_init (mctx , private_key , digestname , NULL /*sigopts*/ ) > 0 )
527+ #else
528+ if (mctx != NULL && do_sign_init (mctx , private_key , md , NULL /*sigopts*/ ) > 0 )
529+ #endif
487530 rv = (X509_sign_ctx (x , mctx ) > 0 );
488531
489532 if (rv == 0 )
0 commit comments