1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tder/importdumpasn1.pl Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,336 @@
1.4 +#
1.5 +# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +# All rights reserved.
1.7 +# This component and the accompanying materials are made available
1.8 +# under the terms of the License "Eclipse Public License v1.0"
1.9 +# which accompanies this distribution, and is available
1.10 +# at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +#
1.12 +# Initial Contributors:
1.13 +# Nokia Corporation - initial contribution.
1.14 +#
1.15 +# Contributors:
1.16 +#
1.17 +# Description:
1.18 +#
1.19 +#!/bin/perl
1.20 +
1.21 +use strict;
1.22 +use Getopt::Long;
1.23 +
1.24 +my $DEBUG = 0;
1.25 +my $TABS = "";
1.26 +my $OUTPUT_BUFFER = "";
1.27 +
1.28 +main();
1.29 +exit;
1.30 +
1.31 +sub main() {
1.32 + my $out;
1.33 + my $outFh;
1.34 + my $in;
1.35 + my @lines;
1.36 +
1.37 + GetOptions('debug=i' => \$DEBUG,
1.38 + 'in=s' => \$in,
1.39 + 'out=s' => \$out);
1.40 +
1.41 + if (! defined $in && defined $ARGV[0]) {
1.42 + $in = $ARGV[0];
1.43 + }
1.44 +
1.45 + if (defined $in) {
1.46 + @lines = decompile($in);
1.47 + }
1.48 + else {
1.49 + die "No input file specified.\n";
1.50 + }
1.51 +
1.52 + if (! defined $out && defined $ARGV[1]) {
1.53 + $out = $ARGV[1];
1.54 + }
1.55 + if (defined $out) {
1.56 + open $outFh, ">$out" || die "Cannot open output file $out";
1.57 + }
1.58 + else {
1.59 + $outFh = *STDOUT;
1.60 + }
1.61 + translate(\@lines);
1.62 + print $outFh $OUTPUT_BUFFER;
1.63 +}
1.64 +
1.65 +sub translate($) {
1.66 + my ($lines) = @_;
1.67 + my $lineCount = scalar(@$lines);
1.68 +
1.69 + for (my $i = 0; $i < $lineCount; ++$i) {
1.70 + $_ = @$lines[$i];
1.71 + s/^\s*//g;
1.72 + s/\n//g;
1.73 +
1.74 + if ($DEBUG >= 3) {
1.75 + print "$_\n";
1.76 + }
1.77 +
1.78 + if ( /^OBJECT\s+IDENTIFIER\s*$/ ) {
1.79 + if ($DEBUG >= 3) {
1.80 + print "reading OID value from next line\n";
1.81 + }
1.82 + if (defined @$lines[$i+1]) {
1.83 + $_ .= @$lines[++$i];
1.84 + }
1.85 + }
1.86 +
1.87 + if ( /BIT\s+STRING,\s+encapsulates/i ) {
1.88 + addToOutput("BITSTRING_WRAPPER");
1.89 + nest();
1.90 + }
1.91 + elsif ( /^\s*BIT\s+STRING/i ) {
1.92 + # bitstring defined in binary
1.93 + if ( $$lines[$i+1] =~ /\'([01]+)\'/ ) {
1.94 + $i++;
1.95 + addToOutput("BITSTRING=$1");
1.96 + }
1.97 + # bit string defined in hex
1.98 + elsif ( /^\s*BIT\s+STRING\s+(([A-F0-9][A-F0-9]\s*)+)/i ) {
1.99 + my $bitStr = toBitString($1);
1.100 + addToOutput("BITSTRING=$bitStr");
1.101 + }
1.102 + else {
1.103 + # bit string wrapper
1.104 + addToOutput("BITSTRING_WRAPPER");
1.105 + nest();
1.106 + addToOutput("RAW \{");
1.107 + nest();
1.108 + $i++;
1.109 + addToOutput(getRawHex($lines,\$i));
1.110 + leaveNest();
1.111 + addToOutput(" \}");
1.112 + leaveNest();
1.113 + addToOutput("END");
1.114 + }
1.115 + }
1.116 + elsif ( /^(BMPSTRING\s+)\'(.*)\'/i ) {
1.117 + addToOutput("BMPSTRING=$2");
1.118 + }
1.119 + elsif ( /^(BOOLEAN\s+)(.*)/i ) {
1.120 + addToOutput("BOOLEAN=$2");
1.121 + }
1.122 + elsif ( /(^ENUMERATED\s+)(\d+)*$/i ) {
1.123 + # small integer - non hex incoded
1.124 + addToOutput("ENUMERATED=$2");
1.125 + }
1.126 + elsif ( /^\[(\d+)\]\s*\'(.*)\'/ ) {
1.127 + addToOutput("IMPLICIT=$1");
1.128 + nest();
1.129 + addToOutput("PRINTABLESTRING=$2");
1.130 + leaveNest();
1.131 + addToOutput("END");
1.132 + }
1.133 + elsif ( /^\[(\d+)\]/ ) {
1.134 + # general case for implicit & explicit tags
1.135 + my $tag=$1;
1.136 + if (defined @$lines[$i+1] && isRawData(@$lines[$i+1])) {
1.137 + # if there is only raw data assume implicit
1.138 + addToOutput("IMPLICIT=$tag");
1.139 + nest();
1.140 + addToOutput("OCTETSTRING");
1.141 + nest();
1.142 + addToOutput("RAW \{");
1.143 + while (isRawData(@$lines[++$i])) {
1.144 + addToOutput("" . @$lines[$i] . "");
1.145 + }
1.146 + --$i;
1.147 + addToOutput("\}");
1.148 + leaveNest();
1.149 + addToOutput("END");
1.150 + leaveNest();
1.151 + addToOutput("END");
1.152 + leaveNest();
1.153 + }
1.154 + else {
1.155 + # otherwise assume explicit
1.156 + addToOutput("EXPLICIT=$tag");
1.157 + }
1.158 + nest();
1.159 + }
1.160 + elsif ( /^(IA5STRING\s+)\'(.*)\'/i ) {
1.161 + addToOutput("IA5STRING=$2");
1.162 + }
1.163 + elsif ( /(^INTEGER\s+)(\d+)*$/i ) {
1.164 + # small integer - non hex incoded
1.165 + addToOutput("INTEGER=$2");
1.166 + }
1.167 + elsif (/^INTEGER/) {
1.168 + # big integer
1.169 + addToOutput("BIGINTEGER {");
1.170 + my $tmp = $_;
1.171 + $tmp =~ s/.*INTEGER\s+//g;
1.172 + nest();
1.173 + if (isRawData($tmp)) {
1.174 + addToOutput($tmp);
1.175 + }
1.176 +
1.177 + $i++;
1.178 + addToOutput(getRawHex($lines,\$i));
1.179 + leaveNest();
1.180 + addToOutput("\}");
1.181 + }
1.182 + elsif ( /^NULL/i ) {
1.183 + addToOutput("NULL");
1.184 + }
1.185 + elsif ( /^OCTET STRING\s*$/i ) {
1.186 + $i++;
1.187 + addToOutput("OCTETSTRING");
1.188 + nest();
1.189 + addToOutput("RAW \{");
1.190 + nest();
1.191 + addToOutput(getRawHex($lines,\$i));
1.192 + leaveNest();
1.193 + addToOutput("\}");
1.194 + leaveNest();
1.195 + addToOutput("END");
1.196 + }
1.197 + elsif ( /^OCTET\s+STRING.*encapsulates/i ) {
1.198 + addToOutput("OCTETSTRING");
1.199 + nest();
1.200 + }
1.201 + elsif ( /^OCTET\s+STRING/i ) {
1.202 + addToOutput("OCTETSTRING");
1.203 + nest();
1.204 + my $hex = $_;
1.205 + $hex =~ s/OCTET\s+STRING\s+//g;
1.206 + addToOutput("RAW=$hex");
1.207 + leaveNest();
1.208 + addToOutput("END");
1.209 + }
1.210 + elsif ( /^OBJECT\s+IDENTIFIER\s+\'([\d ]+)\'/i ) {
1.211 + # plain oid
1.212 + my $oid = $1;
1.213 + $oid =~ s/ /./g;
1.214 + addToOutput("OID=$oid");
1.215 + }
1.216 + elsif ( /(^OBJECT\s+IDENTIFIER.*\()([\d ]+)/i ) {
1.217 + # extra information printed with oid
1.218 + my $oid = $2;
1.219 + $oid =~ s/ /./g;
1.220 + addToOutput("OID=$oid");
1.221 + }
1.222 + elsif ( /(^PRINTABLESTRING\s*\')([^\']*)/i ) {
1.223 + addToOutput("PRINTABLESTRING=$2");
1.224 + }
1.225 + elsif ( /^SEQUENCE/i ) {
1.226 + addToOutput("SEQUENCE");
1.227 + nest();
1.228 + }
1.229 + elsif ( /^SET/i ) {
1.230 + addToOutput("SET");
1.231 + nest();
1.232 + }
1.233 + elsif (/^(UTCTIME\s+\')([^\']+)/i) {
1.234 + addToOutput("UTCTIME=$2");
1.235 + }
1.236 + elsif ( /^(UTF8STRING\s+)\'(.*)\'/i ) {
1.237 + addToOutput("UTF8STRING=$2");
1.238 + }
1.239 + elsif ( /^\}/) {
1.240 + leaveNest();
1.241 + addToOutput("END");
1.242 + }
1.243 + elsif (isRawData($_)) {
1.244 + my $raw = s/\s+/ /g;
1.245 + addToOutput("RAW=$_");
1.246 + }
1.247 +
1.248 + }
1.249 +}
1.250 +
1.251 +sub addToOutput($) {
1.252 + my ($text) = @_;
1.253 +
1.254 + if ($DEBUG >= 3) {
1.255 + print "+${TABS}$text\n";
1.256 + }
1.257 + $OUTPUT_BUFFER .= "${TABS}$text\n";
1.258 +}
1.259 +
1.260 +sub getRawHex($) {
1.261 + my ($lines,$index) = @_;
1.262 + my $translated = '';
1.263 +
1.264 + my $end = 0;
1.265 + do {
1.266 + my $line = $$lines[$$index];
1.267 + last if (!defined $line);
1.268 + chomp($line);
1.269 +
1.270 + if (isRawData($line)) {
1.271 + $line =~ s/^\s+//g;
1.272 + addToOutput("$line");
1.273 + $$index++;
1.274 + }
1.275 + else {
1.276 + $$index--;
1.277 + $end = 1;
1.278 + }
1.279 + } while (! $end);
1.280 + return $translated;
1.281 +}
1.282 +
1.283 +sub isRawData() {
1.284 + my ($line) = @_;
1.285 + my $retVal = ($line =~ /^\s*([A-F0-9][A-F0-9]\s?)+$/i);
1.286 + if ($DEBUG >= 3 && $retVal) {
1.287 + print "RAW: ";
1.288 + }
1.289 + return $retVal;
1.290 +}
1.291 +
1.292 +sub toBitString() {
1.293 + my ($hex) = @_;
1.294 + my $bitStr = "";
1.295 + $hex =~ s/\s//g;
1.296 +
1.297 + for (my $i=0; $i < length($hex); $i+=2) {
1.298 + my $num = hex(substr($hex, $i, 2));
1.299 + print ".$num";
1.300 + for (my $j=0; $j < 8; $j++) {
1.301 + $bitStr .= ($num & 0x80) ? '1' : '0';
1.302 + $num <<= 1;
1.303 + }
1.304 + }
1.305 + if ($DEBUG >= 2) {
1.306 + print "\nbitStr: $hex = $bitStr\n";
1.307 + }
1.308 + return $bitStr;
1.309 +}
1.310 +
1.311 +# increment debug tabbing level
1.312 +sub nest() {
1.313 + $TABS .= " ";
1.314 +}
1.315 +
1.316 +# decrement debug tabbing level
1.317 +sub leaveNest() {
1.318 + $TABS =~ s/^...//;
1.319 +}
1.320 +
1.321 +
1.322 +sub decompile($) {
1.323 + my ($inFile) = @_;
1.324 +
1.325 +
1.326 + my @command = ("cmd",
1.327 + "/C \"dumpasn1 -apruz $inFile > _dump.tmp\"");
1.328 +
1.329 + if ((my $err = system(@command)) != 0) {
1.330 + die "decode: " . join(" ", @command) . "\nreturned error $err";
1.331 + }
1.332 +
1.333 + my $dumpFh;
1.334 + open $dumpFh, "_dump.tmp";
1.335 + my @lines = <$dumpFh>;
1.336 + close $dumpFh;
1.337 +
1.338 + return @lines;
1.339 +}