Skip to content

Commit 9c21224

Browse files
committed
OpenSSL changes for version differences
1 parent 0ee5b86 commit 9c21224

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

Makefile.PL

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ use ExtUtils::MakeMaker;
1010

1111
use Config;
1212
use File::Spec;
13-
13+
use Crypt::OpenSSL::Guess;
1414
my %args;
1515

16+
my ($major, $minor, $patch) = openssl_version();
17+
print "Installed OpenSSL: $major.$minor.$patch\n";
1618
if ($^O ne 'MSWin32' and my $prefix = `brew --prefix --installed openssl\@1.1 2>@{[File::Spec->devnull]}`) {
1719
chomp $prefix;
1820
$args{INC} = "-I$prefix/include";
@@ -32,7 +34,7 @@ if ($^O eq 'MSWin32') {
3234
}
3335
}
3436

35-
my $cc_option_flags = ' -DOPENSSL_API_COMPAT=0x10100000L';
37+
my $cc_option_flags = $major ge 3 ? ' -DOPENSSL_API_COMPAT=30000' : ' -DOPENSSL_API_COMPAT=10100';
3638

3739
if ($Config::Config{cc} =~ /gcc/i) {
3840
$cc_option_flags .= $ENV{AUTHOR_TESTING} ? ' -Wall -Werror' : ' -Wall';
@@ -79,7 +81,7 @@ my %WriteMakefileArgs = (
7981
"File::Slurper" => "0.012",
8082
"File::Which" => 0
8183
},
82-
"VERSION" => "0.03",
84+
"VERSION" => "0.04",
8385
"test" => {
8486
"TESTS" => "t/*.t"
8587
}

SignCSR.xs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
# define SERIAL_RAND_BITS 159
3232

3333
BIO *bio_err;
34+
#if OPENSSL_API_COMPAT >= 30000
3435
OSSL_LIB_CTX *libctx = NULL;
36+
#endif
3537
static const char *propq = NULL;
3638
static unsigned long nmflag = 0;
3739
static 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
245252
static 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)

cpanfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is generated by Dist::Zilla::Plugin::CPANFile v6.030
2+
# Do not edit this file directly. To change prereqs, edit the `dist.ini` file.
3+
4+
requires "perl" => "5.008";
5+
6+
on 'test' => sub {
7+
requires "Crypt::OpenSSL::PKCS10" => "0.19";
8+
requires "Crypt::OpenSSL::RSA" => "0";
9+
requires "File::Slurper" => "0.012";
10+
requires "File::Which" => "0";
11+
};
12+
13+
on 'configure' => sub {
14+
requires "ExtUtils::MakeMaker" => "0";
15+
};
16+
17+
on 'develop' => sub {
18+
requires "Test::CPAN::Meta::JSON" => "0.16";
19+
requires "Test::Kwalitee" => "1.21";
20+
requires "Test::Pod" => "1.41";
21+
requires "Test::Spelling" => "0.12";
22+
};

lib/Crypt/OpenSSL/SignCSR.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# ABSTRACT OpenSSL Self Sign a Certificate Signing Request in XS.
22
package Crypt::OpenSSL::SignCSR;
33

4-
use 5.036001;
4+
use 5.008;
55
use strict;
66
use warnings;
77

88
require Exporter;
99

10-
our $VERSION = "0.03";
10+
our $VERSION = "0.04";
1111

1212
our @ISA = qw(Exporter);
1313

maint/Makefile_header.PL

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use Config;
22
use File::Spec;
3-
3+
use Crypt::OpenSSL::Guess;
44
my %args;
55

6+
my ($major, $minor, $patch) = openssl_version();
7+
print "Installed OpenSSL: $major.$minor.$patch\n";
68
if ($^O ne 'MSWin32' and my $prefix = `brew --prefix --installed openssl\@1.1 2>@{[File::Spec->devnull]}`) {
79
chomp $prefix;
810
$args{INC} = "-I$prefix/include";
@@ -22,7 +24,7 @@ if ($^O eq 'MSWin32') {
2224
}
2325
}
2426

25-
my $cc_option_flags = ' -DOPENSSL_API_COMPAT=0x10100000L';
27+
my $cc_option_flags = $major ge 3 ? ' -DOPENSSL_API_COMPAT=30000' : ' -DOPENSSL_API_COMPAT=10100';
2628

2729
if ($Config::Config{cc} =~ /gcc/i) {
2830
$cc_option_flags .= $ENV{AUTHOR_TESTING} ? ' -Wall -Werror' : ' -Wall';

0 commit comments

Comments
 (0)