]>
Commit | Line | Data |
---|---|---|
77de6bb3 RL |
1 | #! /usr/bin/env perl |
2 | # Copyright 2020 The OpenSSL Project Authors. All Rights Reserved. | |
3 | # | |
4 | # Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | # this file except in compliance with the License. You can obtain a copy | |
6 | # in the file LICENSE in the source distribution or at | |
7 | # https://www.openssl.org/source/license.html | |
8 | ||
9 | use strict; | |
10 | use warnings; | |
11 | ||
12 | package oids_to_c; | |
13 | ||
14 | use Carp; | |
15 | use File::Spec; | |
16 | use OpenSSL::OID; | |
17 | ||
18 | my $OID_name_re = qr/([a-z](?:[-_A-Za-z0-9]*[A-Za-z0-9])?)/; | |
19 | my $OID_value_re = qr/(\{.*?\})/s; | |
20 | my $OID_def_re = qr/ | |
21 | ${OID_name_re} \s+ OBJECT \s+ IDENTIFIER \s* | |
22 | ::= | |
23 | \s* ${OID_value_re} | |
24 | /x; | |
25 | ||
26 | use Data::Dumper; | |
27 | ||
28 | sub filter_to_H { | |
29 | my ($name, $comment) = @{ shift() }; | |
30 | my @oid_nums = @_; | |
31 | ||
32 | (my $C_name = $name) =~ s|-|_|g; | |
33 | my $C_bytes_size = 2 + scalar @_; | |
34 | ||
35 | return <<"_____"; | |
36 | extern const unsigned char der_oid_${C_name}[$C_bytes_size]; | |
37 | _____ | |
38 | } | |
39 | ||
40 | sub filter_to_C { | |
41 | my ($name, $comment) = @{ shift() }; | |
42 | my @oid_nums = @_; | |
43 | my $oid_size = scalar @oid_nums; | |
44 | ||
45 | croak "Unsupported OID size (>127 bytes)" if $oid_size > 127; | |
46 | ||
47 | (my $C_comment = $comment) =~ s|^| * |msg; | |
48 | $C_comment = "\n/*\n${C_comment}\n */" if $C_comment ne ''; | |
49 | (my $C_name = $name) =~ s|-|_|g; | |
50 | my $C_bytes_size = 2 + $oid_size; | |
51 | my $C_bytes = join(', ', map { sprintf("0x%02X", $_) } @oid_nums ); | |
52 | ||
53 | return <<"_____"; | |
54 | $C_comment | |
55 | #define DER_OID_V_${C_name} DER_P_OBJECT, $oid_size, ${C_bytes} | |
56 | #define DER_OID_SZ_${C_name} ${C_bytes_size} | |
57 | const unsigned char der_oid_${C_name}[DER_OID_SZ_${C_name}] = { | |
58 | DER_OID_V_${C_name} | |
59 | }; | |
60 | _____ | |
61 | } | |
62 | ||
63 | sub _process { | |
64 | my %opts = %{ pop @_ } if ref $_[$#_] eq 'HASH'; | |
65 | ||
66 | # To maintain input order | |
67 | my @OID_names = (); | |
68 | ||
69 | foreach my $file (@_) { | |
70 | my $input = File::Spec->catfile($opts{dir}, $file); | |
71 | open my $fh, $input or die "Reading $input: $!\n"; | |
72 | ||
73 | my $text = join('', | |
74 | map { | |
75 | s|--.*(\R)$|$1|; | |
76 | $_; | |
77 | } <$fh>); | |
78 | # print STDERR "-----BEGIN DEBUG-----\n"; | |
79 | # print STDERR $text; | |
80 | # print STDERR "-----END DEBUG-----\n"; | |
81 | use re 'debugcolor'; | |
82 | while ($text =~ m/${OID_def_re}/sg) { | |
83 | my $comment = $&; | |
84 | my $name = $1; | |
85 | my $value = $2; | |
86 | ||
87 | # print STDERR "-----BEGIN DEBUG $name-----\n"; | |
88 | # print STDERR $value,"\n"; | |
89 | # print STDERR "-----END DEBUG $name-----\n"; | |
90 | register_oid($name, $value); | |
91 | push @OID_names, [ $name, $comment ]; | |
92 | } | |
93 | } | |
94 | ||
95 | return @OID_names; | |
96 | } | |
97 | ||
98 | sub process_leaves { | |
99 | my %opts = %{ $_[$#_] } if ref $_[$#_] eq 'HASH'; | |
100 | my @OID_names = _process @_; | |
101 | ||
102 | my $text = ''; | |
103 | my %leaves = map { $_ => 1 } registered_oid_leaves; | |
104 | foreach (grep { defined $leaves{$_->[0]} } @OID_names) { | |
105 | my $lines = $opts{filter}->($_, encode_oid($_->[0])); | |
106 | $text .= $lines; | |
107 | } | |
108 | return $text; | |
109 | } | |
110 | ||
111 | 1; |