]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
params: add conditional params to the generation script
authorPauli <ppzgs1@gmail.com>
Mon, 4 Aug 2025 00:51:26 +0000 (10:51 +1000)
committerPauli <ppzgs1@gmail.com>
Fri, 8 Aug 2025 00:33:14 +0000 (10:33 +1000)
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/28163)

util/perl/OpenSSL/paramnames.pm

index 1d0dbdf70e53380a454809f6852b1173ae732921..2418d07577aff81bc8f014e58d64b663375e1d07 100644 (file)
@@ -17,6 +17,7 @@ our @EXPORT_OK = qw(generate_public_macros
                     produce_param_decoder);
 
 my $case_sensitive = 1;
+my $need_break = 0;
 
 my %params = (
 # Well known parameter names that core passes to providers
@@ -628,6 +629,23 @@ my %params = (
     'SKEY_PARAM_KEY_LENGTH' => "key-length",
 );
 
+sub output_ifdef {
+    my $cond = shift;
+
+    if (defined($cond)) {
+        print "# if" . $cond . "\n";
+    }
+}
+
+sub output_endifdef {
+    my $cond = shift;
+
+    if (defined($cond)) {
+        print "# endif\n";
+        $need_break = 1;
+    }
+}
+
 # Generate string based macros for public consumption
 sub generate_public_macros {
     my @macros = ();
@@ -670,6 +688,7 @@ sub generate_decoder_from_trie {
     my $trieref = shift;
     my $identmap = shift;
     my $concat_num = shift;
+    my $ifdefs = shift;
     my $idt = "    ";
     my $indent0 = $idt x ($n + 3);
     my $indent1 = $indent0 . $idt;
@@ -682,6 +701,7 @@ sub generate_decoder_from_trie {
 
         $field = $identmap->{$trieref->{'name'}};
         my $num = $concat_num->{$field};
+        output_ifdef($ifdefs->{$field});
         printf "%sif (ossl_likely($strcmp(\"$suf\", s + $n) == 0", $indent0;
         if (not $case_sensitive) {
             $suf =~ tr/_/-/;
@@ -691,19 +711,23 @@ sub generate_decoder_from_trie {
         print ")) {\n";
         trie_matched($field, $num, $indent1, $indent2);
         printf "%s}\n", $indent0;
+        output_endifdef($ifdefs->{$field});
         return;
     }
 
     printf "%sswitch(s\[%d\]) {\n", $indent0, $n;
     printf "%sdefault:\n", $indent0;
     for my $l (sort keys %$trieref) {
+        $need_break = 0;
         if ($l eq 'val') {
             $field = $identmap->{$trieref->{'val'}};
             my $num = $concat_num->{$field};
             printf "%sbreak;\n", $indent1;
+            output_ifdef($ifdefs->{$field});
             printf "%scase '\\0':\n", $indent0;
             trie_matched($field, $num, $indent1, $indent2);
-        } else {
+            output_endifdef($ifdefs->{$field});
+       } else {
             printf "%sbreak;\n", $indent1;
             printf "%scase '%s':", $indent0, $l;
             if (not $case_sensitive) {
@@ -711,9 +735,12 @@ sub generate_decoder_from_trie {
                 printf "   case '%s':", uc $l if ($l =~ /[a-z]/);
             }
             print "\n";
-            generate_decoder_from_trie($n + 1, $trieref->{$l}, $identmap, $concat_num);
+            generate_decoder_from_trie($n + 1, $trieref->{$l}, $identmap, $concat_num, $ifdefs);
         }
     }
+    if ($need_break) {
+        printf "%sbreak;\n", $indent1;
+    }
     printf "%s}\n", $indent0;
     return;
 }
@@ -786,6 +813,7 @@ sub output_param_decoder {
     my @keys = ();
     my %prms = ();
     my %concat_num = ();
+    my %ifdefs = ();
 
     print "/* Machine generated by util/perl/OpenSSL/paramnames.pm */\n";
     # Output ettable param array
@@ -799,20 +827,25 @@ sub output_param_decoder {
 
         $prms{$pname} = $pident;
 
-        if (defined $pnum && $pnum eq 'hidden') {
-            # Don't output the list entry for hidden items.
-            # Otherwise treat them as scalars, by deleting the count entry.
-            # Hidden arrays will not be supported until necessary.
-            undef $pnum;
-            $params[$i][3] = undef;
-        } else {
-            print "    OSSL_PARAM_$ptype(OSSL_$pname, NULL";
-            print ", 0" if $ptype eq "octet_string" || $ptype eq "octet_ptr"
-                           || $ptype eq "utf8_string" || $ptype eq "utf8_ptr";
-            printf "),\n";
+        if (defined $pnum) {
+            if ($pnum eq 'hidden') {
+                next;
+            } elsif ($pnum eq 'fips') {
+                # The `#if' is added on output
+                $ifdefs{$pident} = ' defined(FIPS_MODULE)';
+            } elsif (substr($pnum, 0, 3) eq '#if') {
+                # Trim the `#if' from the front
+                $ifdefs{$pident} = substr($pnum, 3);
+            } else {
+                $concat_num{$pident} = $pnum;
+            }
         }
-
-        $concat_num{$pident} = $pnum if defined($pnum);
+        output_ifdef($ifdefs{$pident});
+        print "    OSSL_PARAM_$ptype(OSSL_$pname, NULL";
+        print ", 0" if $ptype eq "octet_string" || $ptype eq "octet_ptr"
+                       || $ptype eq "utf8_string" || $ptype eq "utf8_ptr";
+        printf "),\n";
+        output_endifdef($ifdefs{$pident});
     }
     print "    OSSL_PARAM_END\n};\n#endif\n\n";
 
@@ -823,12 +856,14 @@ sub output_param_decoder {
     foreach my $pident (sort values %prms) {
         if (not defined $done_prms{$pident}) {
             $done_prms{$pident} = 1;
+            output_ifdef($ifdefs{$pident});
             if (defined($concat_num{$pident})) {
                 printf "    OSSL_PARAM *%s[%s];\n", $pident, $concat_num{$pident};
                 printf "    int num_%s;\n", $pident;
             } else {
                 printf "    OSSL_PARAM *%s;\n", $pident;
             }
+            output_endifdef($ifdefs{$pident});
         }
     }
     print "};\n#endif\n\n";
@@ -845,7 +880,7 @@ sub output_param_decoder {
     print "    memset(r, 0, sizeof(*r));\n";
     print "    if (p != NULL)\n";
     print "        for (; (s = p->key) != NULL; p++)\n";
-    generate_decoder_from_trie(0, \%t, \%prms, \%concat_num);
+    generate_decoder_from_trie(0, \%t, \%prms, \%concat_num, \%ifdefs);
     print "    return 1;\n";
     print "}\n#endif\n";
     print "/* End of machine generated */";