updated copyright
[gnutls.git] / tests / suite / asn1random.pl
blob79e70a6b62f2959d716c573e5d2db22b3870f817
1 #!/usr/bin/perl -w
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.
23 # Format:
25 # asn1random.pl >output
27 use strict;
29 my $depth = 0;
30 my $maxdepth = 12;
32 #print STDERR "SEED: ", srand(), "\n";
34 ###############################################################################
36 # Generate a header
38 ###############################################################################
39 sub emit_asn1_hdr($$)
41 my ($tag, $len) = @_;
42 my $output = "";
43 my $l;
45 if ($len < 0x80) {
46 $l = $len;
47 } elsif ($len <= 0xff) {
48 $l = 0x81;
49 } elsif ($len <= 0xffff) {
50 $l = 0x82;
51 } elsif ($len <= 0xffffff) {
52 $l = 0x83;
53 } else {
54 $l = 0x84;
57 $output .= pack("CC", $tag == -1 ? int(rand(255)) & ~0x20 : $tag, $l);
58 if ($len < 0x80) {
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);
65 } else {
66 $output .= pack("N", $len);
69 return $output;
72 ###############################################################################
74 # Generate a random primitive
76 ###############################################################################
77 sub emit_asn1_prim($)
79 my ($tag) = @_;
80 my $output;
81 my $len = int(rand(255));
83 $tag = int(rand(255)) & ~0x20
84 if ($tag == -1);
86 $output = emit_asn1_hdr($tag, $len);
88 my $i = $len;
89 while ($i > 16) {
90 $output .= "abcdefghijklmnop";
91 $i -= 16;
94 $output .= substr("abcdefghijklmnop", 0, $i);
95 return $output;
98 ###############################################################################
100 # Generate a random construct
102 ###############################################################################
103 sub emit_asn1_cons($);
104 sub emit_asn1_cons($)
106 my $output = "";
107 my $count = int(rand(20));
108 my ($tag) = @_;
110 if ($depth >= $maxdepth) {
111 return emit_asn1_prim($tag);
114 if ($tag == -1) {
115 $tag = int(rand(255)) & ~0x20;
116 if ($tag < 0x40 && $tag != 0x11) {
117 $tag = 0x10;
119 $tag |= 0x20;
122 $depth++;
123 while ($count > 0) {
124 if (int(rand(4 + $depth)) == 1) {
125 $output .= emit_asn1_cons(-1);
126 } else {
127 $output .= emit_asn1_prim(-1);
129 $count--;
131 $depth--;
133 return emit_asn1_hdr($tag, length($output)) . $output;
136 print emit_asn1_cons(-1);