-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx509v3ext2req.pl
executable file
·85 lines (76 loc) · 2.75 KB
/
x509v3ext2req.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl
# x509v3ext2req
# copies the X.509v3 extensions from a DER-encoded certificate
# into a DER-encoded certificate request (CSR)
use Data::Walk;
use Encoding::BER::DER;
my ($file_crt, $file_req) = @ARGV;
if (not defined $file_crt) {
die "Usage: $0 <file.crt> <file.csr>";
}
my $data_crt, $data_req, $extensions, $req_ext;
{
open my $file, "<", $file_crt;
binmode $file;
local $/;
$data_crt = <$file>;
close $file;
open my $file, "<", $file_req;
binmode $file;
local $/;
$data_req = <$file>;
close $file;
}
sub process {
glob $extensions;
if (ref($_) eq 'HASH' && $_->{type}[2] eq 'extensions') {
$extensions = $_;
}
if (ref($_) eq 'HASH' && $_->{type}[2] eq 'req_ext') {
$req_ext = $_;
}
}
my $enc = Encoding::BER::DER->new(debug => 0);
$enc->add_implicit_tag('context', 'primitive', 'req_ext', 0, 'content_end');
$enc->add_implicit_tag('context', 'primitive', 'extensions', 3, 'content_end');
my $crt = $enc->decode( $data_crt );
walk \&process, $crt;
my $req = $enc->decode( $data_req );
walk \&process, $req;
$req_ext->{value} = [
{
'identval' => 48,
'tagnum' => 16,
'value' => [
{
'identval' => 6,
'tagnum' => 6,
'value' => '1.2.840.113549.1.9.14',
'type' => [
'universal',
'primitive',
'oid'
]
},
{
'identval' => 49,
'tagnum' => 17,
'value' => $extensions->{value},
'type' => [
'universal',
'constructed',
'set'
]
}
],
'type' => [
'universal',
'constructed',
'sequence'
]
}
];
open(OUTPUT, ">", $file_req);
binmode(OUTPUT);
print OUTPUT $enc->encode( $req );
close OUTPUT;