3 # Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 # Written by David Howells (dhowells@redhat.com)
6 # This file is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This file is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this file; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 # Generate random but valid ASN.1 data.
25 # asn1random.pl >output
32 #print STDERR "SEED: ", srand(), "\n";
34 ###############################################################################
38 ###############################################################################
47 } elsif ($len <= 0xff) {
49 } elsif ($len <= 0xffff) {
51 } elsif ($len <= 0xffffff) {
57 $output .= pack("CC", $tag == -1 ?
int(rand(255)) & ~0x20 : $tag, $l);
59 } elsif ($len <= 0xff) {
60 $output .= pack("C", $len);
61 } elsif ($len <= 0xffff) {
62 $output .= pack("n", $len);
63 } elsif ($len <= 0xffffff) {
64 $output .= pack("Cn", $len >> 16, $len & 0xffff);
66 $output .= pack("N", $len);
72 ###############################################################################
74 # Generate a random primitive
76 ###############################################################################
81 my $len = int(rand(255));
83 $tag = int(rand(255)) & ~0x20
86 $output = emit_asn1_hdr
($tag, $len);
90 $output .= "abcdefghijklmnop";
94 $output .= substr("abcdefghijklmnop", 0, $i);
98 ###############################################################################
100 # Generate a random construct
102 ###############################################################################
103 sub emit_asn1_cons
($);
104 sub emit_asn1_cons
($)
107 my $count = int(rand(20));
110 if ($depth >= $maxdepth) {
111 return emit_asn1_prim
($tag);
115 $tag = int(rand(255)) & ~0x20;
116 if ($tag < 0x40 && $tag != 0x11) {
124 if (int(rand(4 + $depth)) == 1) {
125 $output .= emit_asn1_cons
(-1);
127 $output .= emit_asn1_prim
(-1);
133 return emit_asn1_hdr
($tag, length($output)) . $output;
136 print emit_asn1_cons
(-1);