cryptoservices/certificateandkeymgmt/tder/dergen.pl
author Santosh V Patil <santosh.v.patil@nokia.com>
Tue, 29 Sep 2009 16:08:12 +0530
changeset 6 50f2ff6984be
parent 0 2c201484c85f
child 11 9d767430696e
permissions -rw-r--r--
Fix for Bug 383 (Wrong license text in security package)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     1
#
6
50f2ff6984be Fix for Bug 383 (Wrong license text in security package)
Santosh V Patil <santosh.v.patil@nokia.com>
parents: 0
diff changeset
     2
# Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
0
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     3
# All rights reserved.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     4
# This component and the accompanying materials are made available
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     6
# which accompanies this distribution, and is available
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     8
#
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
     9
# Initial Contributors:
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    11
#
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    12
# Contributors:
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    13
#
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    14
# Description: 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    15
# Basic ASN.1 encoding library
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    16
# Some parts of this program requrie OpenSSL which may be freely downloaded
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    17
# from www.openssl.org
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    18
#
6
50f2ff6984be Fix for Bug 383 (Wrong license text in security package)
Santosh V Patil <santosh.v.patil@nokia.com>
parents: 0
diff changeset
    19
#!/bin/perl -w
0
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    20
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    21
use strict;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    22
use Digest::HMAC_MD5;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    23
use Digest::HMAC_SHA1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    24
use Getopt::Long;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    25
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    26
# 0 = off
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    27
# 1 = log parsing
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    28
# 2 = log parsing + encoding
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    29
# 3 = really verbose stuff
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    30
my $DEBUG=0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    31
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    32
# Turn on validation checks that attempt to only generate
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    33
# valid DER encodings.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    34
my $VALIDATE=0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    35
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    36
my $OID_PKCS = "1.2.840.113549.1";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    37
my $OID_PKCS7 ="${OID_PKCS}.7";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    38
my $OID_PKCS9 = "${OID_PKCS}.9";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    39
my $OID_PKCS9_CERTTYPES = "${OID_PKCS9}.22"; 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    40
my $OID_PKCS12 = "${OID_PKCS}.12";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    41
my $OID_PKCS12_BAGTYPES = "${OID_PKCS12}.10.1";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    42
my $OID_PKCS12_PBEIDS = "${OID_PKCS12}.1";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    43
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    44
my %OIDS = 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    45
	(
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    46
	 "MD5"  => "1.2.840.113549.2.5",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    47
	 "SHA1" => "1.3.14.3.2.26",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    48
	 "X509CRL" => "1.3.6.1.4.1.3627.4",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    49
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    50
	 "PKCS7_DATA" => "${OID_PKCS7}.1",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    51
	 "PKCS7_SIGNEDDATA" => "${OID_PKCS7}.2",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    52
	 "PKCS7_ENVELOPEDDATA" => "${OID_PKCS7}.3",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    53
	 "PKCS7_SIGNEDANDENVELOPEDDATA" => "${OID_PKCS7}.4",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    54
	 "PKCS7_DIGESTEDDATA" => "${OID_PKCS7}.5",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    55
	 "PKCS7_ENCRYPTEDDATA" => "${OID_PKCS7}.6",	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    56
	 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    57
	 "PKCS9_CERTTYPES_PKCS12_X509" => "${OID_PKCS9_CERTTYPES}.1",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    58
	 "PKCS9_FRIENDLY_NAME" => "${OID_PKCS9}.20",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    59
	 "PKCS9_LOCAL_KEYID" => "${OID_PKCS9}.21",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    60
	 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    61
	 "PKCS12_BAGTYPES_KEYBAG" => "${OID_PKCS12_BAGTYPES}.1",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    62
	 "PKCS12_BAGTYPES_PKCS8SHROUDEDKEYBAG" => "${OID_PKCS12_BAGTYPES}.2",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    63
	 "PKCS12_BAGTYPES_CERTBAG" => "${OID_PKCS12_BAGTYPES}.3",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    64
	 "PKCS12_BAGTYPES_CRLBAG" => "${OID_PKCS12_BAGTYPES}.4",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    65
	 "PKCS12_BAGTYPES_SECRETBAG" => "${OID_PKCS12_BAGTYPES}.5",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    66
	 "PKCS12_BAGTYPES_SAFECONTENTSBAG" => "${OID_PKCS12_BAGTYPES}.6",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    67
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    68
	 "PKCS12_PBEIDS_SHAAND128BITRC4" => "${OID_PKCS12_PBEIDS}.1",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    69
	 "PKCS12_PBEIDS_SHAAND40BITRC4" => "${OID_PKCS12_PBEIDS}.2",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    70
	 "PKCS12_PBEIDS_SHAAND3KEYTRIPLEDESCBC" => "${OID_PKCS12_PBEIDS}.3",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    71
	 "PKCS12_PBEIDS_SHAAND2KEYTRIPLEDESCBC" => "${OID_PKCS12_PBEIDS}.4",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    72
	 "PKCS12_PBEIDS_SHAAND128BITRC2CBC" => "${OID_PKCS12_PBEIDS}.5", 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    73
	 "PKCS12_PBEIDS_SHAAND40BITRC2CBC" => "${OID_PKCS12_PBEIDS}.6",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    74
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    75
	 # Symbian dev cert extensions
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    76
	 "SYMBIAN_DEVICE_ID_LIST" => "1.2.826.0.1.1796587.1.1.1.1",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    77
	 "SYMBIAN_SID_LIST" => "1.2.826.0.1.1796587.1.1.1.4",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    78
	 "SYMBIAN_VID_LIST" => "1.2.826.0.1.1796587.1.1.1.5",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    79
	 "SYMBIAN_CAPABILITIES" => "1.2.826.0.1.1796587.1.1.1.6"
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    80
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    81
);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    82
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    83
my $DER_BOOLEAN_TAG="01";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    84
my $DER_INTEGER_TAG="02";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    85
my $DER_BITSTRING_TAG="03";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    86
my $DER_OCTETSTRING_TAG="04";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    87
my $DER_NULL_TAG="05";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    88
my $DER_OID_TAG="06";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    89
my $DER_ENUMERATED_TAG="0A";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    90
my $DER_SEQUENCE_TAG="10";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    91
my $DER_SET_TAG="11";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    92
my $DER_UTF8STRING_TAG="0C";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    93
my $DER_PRINTABLESTRING_TAG="13";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    94
my $DER_IA5STRING_TAG="16";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    95
my $DER_UTCTIME_TAG="17";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    96
my $DER_BMPSTRING_TAG="1E";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    97
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    98
my $UNIVERSAL_CLASS="UNIVERSAL";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
    99
my $APPLICATION_CLASS="APPLICATION";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   100
my $CONTEXT_SPECIFIC_CLASS="CONTEXT-SPECIFIC";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   101
my $PRIVATE_CLASS="PRIVATE";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   102
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   103
my %PARSE = 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   104
	(
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   105
	 "BOOL" => \&parseBoolean,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   106
	 "BOOLEAN" => \&parseBoolean,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   107
	 "BIGINTEGER" => \&parseBigInteger,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   108
	 "BITSTRING" => \&parseBitString,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   109
	 "BITSTRING_WRAPPER" => \&parseBitStringWrapper,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   110
	 "BMPSTRING" => \&parseBmpString,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   111
	 "BMPSTRING_FILE" => \&parseBmpStringFile,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   112
	 "ENUMERATED" => \&parseEnumerated,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   113
	 "IA5STRING" => \&parseIA5String,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   114
	 "IA5STRING_FILE" => \&parseIA5StringFile,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   115
	 "INCLUDE" => \&parseInclude,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   116
	 "INCLUDE_BINARY_FILE" => \&parseIncludeBinaryFile,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   117
	 "INTEGER" => \&parseInteger,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   118
	 "INT" => \&parseInteger,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   119
	 "IMPLICIT" => \&parseImplicit,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   120
	 "ENCRYPT" => \&parseEncrypt,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   121
	 "EXPLICIT" => \&parseExplicit,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   122
	 "HASH" => \&parseHash,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   123
	 "HMAC" => \&parseHmac,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   124
	 "NULL" => \&parseNull,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   125
	 "OCTETSTRING" => \&parseOctetString,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   126
	 "OUTPUT_BINARY_FILE" => \&parseOutputFile,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   127
	 "OID" => \&parseOid,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   128
	 "PRINTABLESTRING" => \&parsePrintableString,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   129
	 "PRINTABLESTRING_FILE" => \&parsePrintableStringFile,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   130
	 "RAW" => \&parseRaw,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   131
	 "SEQUENCE" => \&parseSequence,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   132
	 "SEQ" => \&parseSequence,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   133
	 "SET" => \&parseSet,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   134
	 "SHELL" => \&parseShell,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   135
	 "SIGN" => \&parseSign,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   136
	 "UTCTIME" => \&parseUtcTime,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   137
	 "UTF8STRING" => \&parseUtf8String,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   138
	 "UTF8STRING_FILE" => \&parseUtf8StringFile,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   139
	 );
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   140
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   141
my $TABS = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   142
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   143
&main;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   144
exit(0);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   145
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   146
sub main() {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   147
	my $hex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   148
	my $out;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   149
	my $in;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   150
	my @lines;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   151
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   152
	GetOptions('debug=i' => \$DEBUG,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   153
			   'hex' => \$hex, 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   154
			   'in=s' => \$in,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   155
			   'out=s' => \$out);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   156
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   157
	if (! defined $in) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   158
		$in = $ARGV[0];
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   159
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   160
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   161
	if (! defined $out) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   162
		$out = $ARGV[1];
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   163
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   164
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   165
	if (defined $in) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   166
		@lines = readFile($in);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   167
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   168
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   169
		die "No input file specified.\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   170
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   171
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   172
	if (defined $out) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   173
		open OUT, ">$out" || die "Cannot open output file $out";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   174
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   175
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   176
		*OUT = *STDOUT;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   177
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   178
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   179
	my $oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   180
	my $asnHex = parseScript(\@lines, \$oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   181
	$asnHex = tidyHex($asnHex);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   182
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   183
	if ((!defined $hex) && (defined $out)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   184
		binmode(OUT);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   185
		print OUT toBin($asnHex);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   186
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   187
	elsif (defined $out) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   188
		print OUT $asnHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   189
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   190
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   191
		print $asnHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   192
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   193
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   194
	close OUT;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   195
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   196
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   197
sub tidyHex($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   198
	my ($input) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   199
	$input =~ s/:+/:/g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   200
	$input =~ s/(^:|:$)//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   201
	return uc($input);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   202
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   203
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   204
sub toBin($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   205
	my ($asnHex) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   206
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   207
	$asnHex =~ s/[\s:]//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   208
	$asnHex = uc($asnHex);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   209
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   210
	my $len = length($asnHex);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   211
	if ($len % 2 != 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   212
		die "toBin: hex string contains an odd number ($len) of octets.\n$asnHex\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   213
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   214
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   215
	my $binary;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   216
	$binary .= pack("H${len}", $asnHex);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   217
#	for (my $i = 0; $i < length($asnHex); $i+=2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   218
#		$binary .= pack('C', substr($asnHex, $i, 2));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   219
#	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   220
	return $binary;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   221
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   222
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   223
sub parseScript($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   224
	my ($lines, $oc, $params) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   225
	my $derHex = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   226
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   227
	nest();
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   228
	substVars($lines, $params);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   229
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   230
	while (my $line = shift @$lines) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   231
		chomp($line);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   232
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   233
		# Remove leading spaces
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   234
		$line =~ s/^\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   235
  
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   236
		# skip comments 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   237
		next if ($line =~ /^\/\//);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   238
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   239
		if ($DEBUG == 3) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   240
			print "${TABS}:PARSE parseScript: $line\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   241
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   242
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   243
		my $argString;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   244
		my $cmd;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   245
		if ($line =~ /(\w+)\s*\{/ ) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   246
			# parse block commands e.g. large integer
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   247
			$cmd = uc($1);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   248
			
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   249
			$line =~ s/.*\{//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   250
			while (defined $line && !($line =~ /(^|[^\\]+)\}/) ) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   251
				$argString .= $line;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   252
				$line = shift(@$lines);				
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   253
			}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   254
			if (defined $line) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   255
				# append everything up to the closing curly bracket
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   256
				$line =~ s/(^|[^\\])\}.*/$1/g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   257
				$argString .= $line;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   258
			}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   259
		}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   260
		elsif ($line =~ /(\w+)\s*=*(.*)/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   261
			# parse commands of the form key = value
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   262
			$cmd = uc($1);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   263
			$argString = defined $2 ? $2 : "";			
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   264
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   265
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   266
		if (defined $cmd) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   267
			if ($cmd =~ /^END/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   268
				leaveNest();
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   269
				if ($DEBUG) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   270
					print "${TABS}:PARSE END\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   271
				}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   272
				return $derHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   273
			}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   274
			elsif (! defined $PARSE{$cmd}) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   275
				die "parseScript: Unknown command: $cmd\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   276
			}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   277
			else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   278
				if ($DEBUG) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   279
					print "${TABS}:PARSE CMD=$cmd";					
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   280
					if ($argString ne "") {print " ARG: $argString";}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   281
					print "\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   282
				}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   283
				
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   284
				# Substitue variables in argString
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   285
				$derHex .= ":" . &{$PARSE{$cmd}}($argString, $oc, $lines);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   286
			}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   287
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   288
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   289
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   290
	leaveNest();
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   291
	return $derHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   292
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   293
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   294
sub substVars($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   295
	my ($lines, $params) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   296
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   297
	if (! defined $params) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   298
		@$params = ();
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   299
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   300
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   301
	for (my $i = 0; $i < scalar(@$lines); $i++) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   302
		my $line = @$lines[$i];
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   303
		my $paramIndex = 1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   304
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   305
		# For each parameter search for the a use of $N where
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   306
		# N is the index of the parameter and replace $N with the
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   307
		# value of the parameter
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   308
		foreach (@$params) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   309
			$line =~ s/\$${paramIndex}(\D|$)/$_$1/g;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   310
			++$paramIndex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   311
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   312
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   313
		# Remove any unused parameters
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   314
		$line =~ s/\$\d+//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   315
		@$lines[$i] = $line;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   316
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   317
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   318
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   319
sub readFile($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   320
	my ($fileName) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   321
	my $inFile;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   322
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   323
	if ($DEBUG) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   324
		print "readFile, $fileName\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   325
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   326
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   327
	open($inFile, $fileName) || die "readFile: cannot open $fileName\n";	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   328
	my @lines = <$inFile>;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   329
	close $inFile;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   330
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   331
	return @lines;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   332
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   333
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   334
sub parseBitString($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   335
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   336
	return encodeBitString($argString, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   337
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   338
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   339
sub parseBitStringWrapper($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   340
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   341
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   342
	my $contents_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   343
	my $contents = parseScript($lines, \$contents_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   344
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   345
	my $binary = toBin($contents);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   346
	my $bitCount = $contents_oc * 8;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   347
	my $bitStr = unpack("B${bitCount}", $binary);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   348
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   349
	# remove trailing zeros - breaks signatures so disable for the moment
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   350
	# $bitStr =~ s/0*$//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   351
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   352
	return encodeBitString($bitStr, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   353
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   354
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   355
sub parseBmpString($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   356
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   357
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   358
	my $bmpString_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   359
	my $bmpString = asciiToBmpString($argString, \$bmpString_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   360
	return encodeBmpString($bmpString, $bmpString_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   361
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   362
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   363
sub parseBmpStringFile($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   364
	my ($binFName, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   365
	$binFName =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   366
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   367
	my $bmpString_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   368
	my $bmpString = encodeBinaryFile($binFName, \$bmpString_oc);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   369
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   370
	return encodeBmpString($bmpString, $bmpString_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   371
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   372
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   373
sub parseBoolean($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   374
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   375
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   376
	$argString =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   377
	$argString = lc($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   378
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   379
	my $bool;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   380
	if ($argString eq "t" || $argString eq "true" || $argString eq "1") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   381
		$bool = 1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   382
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   383
	elsif ($argString eq "f" || $argString eq "false" || $argString eq "0") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   384
		$bool = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   385
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   386
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   387
		die "parseBoolean: Invalid boolean value \'$argString\'";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   388
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   389
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   390
	return encodeBoolean($bool, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   391
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   392
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   393
sub parseHash($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   394
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   395
	my ($algorithm) = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   396
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   397
	if (! defined $algorithm) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   398
		die "parseHash: missing algortithm";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   399
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   400
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   401
	my $hashIn_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   402
	my $hashIn = parseScript($lines, \$hashIn_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   403
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   404
	my $hashInFName = '_hashin.tmp';
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   405
	my $hashOutFName = '_hashout.tmp';
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   406
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   407
	# Create binary hash file
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   408
	my $hashInFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   409
	open($hashInFh, ">$hashInFName") or die "Cannot create $hashInFName";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   410
	binmode($hashInFh);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   411
	print $hashInFh toBin($hashIn);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   412
	close $hashInFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   413
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   414
	my @command = ("cmd",
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   415
				   "/C \"openssl dgst -${algorithm} -binary $hashInFName > $hashOutFName\"");	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   416
	if ($DEBUG == 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   417
		print "${TABS}:parseHash:" . join(" ", @command) . "\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   418
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   419
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   420
	if ((my $err = system(@command)) != 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   421
		die "parseHash: " . join(" ", @command) . "\nreturned error $err";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   422
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   423
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   424
	my $derHex = parseIncludeBinaryFile($hashOutFName, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   425
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   426
	if (! $DEBUG) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   427
		unlink($hashInFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   428
		unlink($hashOutFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   429
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   430
	return $derHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   431
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   432
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   433
sub parseHmac($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   434
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   435
	my ($algorithm, $key) = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   436
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   437
	if (! defined $algorithm) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   438
		die "parseHmac: missing algortithm";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   439
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   440
	$algorithm = uc($algorithm);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   441
	if (! $algorithm =~ /MD5|SHA1/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   442
		die "parseHmac: invalid algorithm $algorithm";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   443
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   444
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   445
	if (! defined $key) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   446
		die "parseHmac: missing key";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   447
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   448
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   449
	my $hmacIn_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   450
	my $hmacIn = toBin(parseScript($lines, \$hmacIn_oc));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   451
	my $hmac;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   452
	my $binKey = toBin($key);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   453
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   454
	if ($algorithm eq "SHA1") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   455
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   456
		$hmac = Digest::HMAC_SHA1->new($binKey);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   457
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   458
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   459
		$hmac = Digest::HMAC_MD5->new($binKey);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   460
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   461
	$hmac->add($hmacIn);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   462
	my $digest = $hmac->digest;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   463
	$$oc += length($digest);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   464
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   465
	return toHex($digest);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   466
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   467
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   468
sub parseIA5String($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   469
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   470
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   471
	my $ia5String_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   472
	my $ia5String = asciiToIA5String($argString, \$ia5String_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   473
	return encodeIA5String($ia5String, $ia5String_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   474
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   475
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   476
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   477
sub parseIA5StringFile($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   478
	my ($binFName, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   479
	$binFName =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   480
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   481
	my $ia5String_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   482
	my $ia5String = encodeBinaryFile($binFName, \$ia5String_oc);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   483
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   484
	return encodeIA5String($ia5String, $ia5String_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   485
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   486
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   487
sub parseIncludeBinaryFile($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   488
	my ($binFName, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   489
	$binFName =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   490
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   491
	return encodeBinaryFile($binFName, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   492
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   493
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   494
sub parseInclude($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   495
	my ($argString, $oc, $lines) = @_;   
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   496
	my @args = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   497
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   498
   	my $fileName = shift(@args);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   499
	if (! (defined $fileName && $fileName ne "")) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   500
		die "parseInclude: Filename not specified\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   501
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   502
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   503
	my $derHex = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   504
	my @lines = readFile($fileName);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   505
	$derHex = parseScript(\@lines, $oc, \@args);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   506
	return $derHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   507
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   508
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   509
sub parseInteger($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   510
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   511
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   512
	$argString =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   513
	return encodeInteger($argString, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   514
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   515
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   516
sub parseBigInteger($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   517
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   518
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   519
	$argString =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   520
	return encodeBigInteger($argString, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   521
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   522
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   523
sub parseEncrypt($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   524
	my ($argString, $oc, $lines) = @_;		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   525
	my ($cipher, $key, $iv) = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   526
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   527
	if (! defined $cipher) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   528
		die "parseEncrypt: missing cipher\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   529
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   530
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   531
	if (! defined $key) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   532
		die "parseEncrypt: missing key\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   533
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   534
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   535
	my $plainText_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   536
	my $plainText = parseScript($lines, \$plainText_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   537
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   538
	my $plainTextFName = '_plaintext.tmp';
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   539
	my $cipherTextFName = '_ciphertext.tmp';
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   540
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   541
	# Create binary plaintext file
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   542
	my $plainTextFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   543
	open($plainTextFh, ">$plainTextFName") or die "Cannot create $plainTextFName";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   544
	binmode($plainTextFh);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   545
	print $plainTextFh toBin($plainText);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   546
	close $plainTextFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   547
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   548
	my @command = ('openssl', 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   549
				   'enc', 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   550
				   "-${cipher}", 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   551
				   '-e',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   552
				   '-K', $key,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   553
				   '-in', $plainTextFName, 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   554
				   '-out', $cipherTextFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   555
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   556
	if (defined $iv) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   557
		push @command, '-iv', $iv;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   558
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   559
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   560
	if ($DEBUG == 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   561
		print "${TABS}:parseEncrypt:" . join(" ", @command) . "\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   562
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   563
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   564
	if ((my $err = system(@command)) != 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   565
		die "parseEncrypt: " . join(" ", @command) . "\nreturned error $err";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   566
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   567
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   568
	my $derHex = parseIncludeBinaryFile($cipherTextFName, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   569
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   570
	if (! $DEBUG) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   571
		unlink($plainTextFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   572
		unlink($cipherTextFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   573
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   574
	return $derHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   575
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   576
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   577
sub parseEnumerated($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   578
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   579
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   580
	$argString =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   581
	return encodeEnumerated($argString, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   582
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   583
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   584
sub parseExplicit($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   585
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   586
	my ($tagNumber, $class) = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   587
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   588
	if (! defined $tagNumber || $tagNumber =~ /^\s*$/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   589
		$tagNumber = "0";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   590
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   591
	elsif (!($tagNumber =~ /^[0-9A-Fa-f]+$/)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   592
		die "parseExplicit: invalid tag number: \'$tagNumber\'";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   593
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   594
	$tagNumber = hex($tagNumber);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   595
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   596
	if (!defined $class || $class =~ /^\s*$/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   597
		$class = $CONTEXT_SPECIFIC_CLASS;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   598
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   599
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   600
		$class =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   601
		$class = uc($class);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   602
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   603
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   604
	if (! isValidClass($class)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   605
		die "parseExplicit: invalid class \'$class\'";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   606
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   607
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   608
	my $nested_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   609
	my $nested = parseScript($lines, \$nested_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   610
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   611
	return encodeExplicit($class, $tagNumber, $nested, $nested_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   612
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   613
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   614
sub parseImplicit($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   615
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   616
	my ($tagNumber, $class) = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   617
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   618
	if (! defined $tagNumber || $tagNumber =~ /^\s*$/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   619
		$tagNumber = "0";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   620
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   621
	elsif (!($tagNumber =~ /^[0-9A-Fa-f]+$/)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   622
		die "parseImplicit: invalid tag number: \'$tagNumber\'";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   623
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   624
	$tagNumber = hex($tagNumber);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   625
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   626
	if (!defined $class || $class =~ /^\s*$/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   627
		$class = $CONTEXT_SPECIFIC_CLASS;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   628
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   629
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   630
		$class =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   631
		$class = uc($class);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   632
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   633
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   634
	if (! isValidClass($class)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   635
		die "parseImplicit: invalid class \'$class\'";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   636
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   637
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   638
	my $nested_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   639
	my $nested = tidyHex(parseScript($lines, \$nested_oc));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   640
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   641
	# De-construct the nested data to allow the underlying type tag to be
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   642
	# changed. The output of parseScript had better be valid DER or this 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   643
	# will go horribly wrong !
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   644
	my $uClass = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   645
	my $uConstructed = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   646
	my $uTag = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   647
	my $uLength = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   648
	my $uValue = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   649
	getTlv($nested, \$uClass, \$uConstructed, \$uTag, \$uLength, \$uValue);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   650
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   651
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   652
		print "${TABS}parseImplicit: underlyingType \'$uTag\'\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   653
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   654
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   655
	# This only works for low tag numbers because we are assuming that the type
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   656
	# tag is a single octet
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   657
	return encodeImplicit($class, $uConstructed, $tagNumber, $uValue, $uLength, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   658
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   659
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   660
sub parseNull($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   661
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   662
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   663
	return encodeNull($oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   664
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   665
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   666
sub parseOctetString($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   667
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   668
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   669
	my $octetString_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   670
	my $octetString = parseScript($lines, \$octetString_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   671
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   672
	return encodeOctetString($octetString, $octetString_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   673
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   674
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   675
sub parseOid($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   676
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   677
	$argString =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   678
	$argString = uc($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   679
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   680
	if (! defined $argString) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   681
		die "parseOid: Missing OID value.";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   682
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   683
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   684
	foreach (keys %OIDS) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   685
		if ($argString =~ /$_/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   686
			$argString =~ s/\Q$_\E/$OIDS{$_}/g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   687
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   688
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   689
	return encodeOid($argString, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   690
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   691
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   692
sub parseOutputFile($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   693
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   694
	my ($outputFile,$echo) = split(/,/, $argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   695
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   696
	if (! defined $outputFile) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   697
		die "parseOutputFile: Missing file-name.\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   698
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   699
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   700
	my $content_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   701
	my $content = parseScript($lines, \$content_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   702
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   703
	my $outFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   704
	if (! open($outFh, ">${outputFile}")) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   705
		die "parseOutputFile: Cannot create $outputFile\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   706
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   707
	binmode($outFh);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   708
	print $outFh toBin($content);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   709
	close $outFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   710
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   711
	# If echo is specified then include then contents of the output 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   712
	# file at this point in the stream.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   713
	if (defined $echo && $echo =~ /(1|t|true)/i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   714
		$$oc += $content_oc;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   715
		return $content;		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   716
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   717
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   718
		return "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   719
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   720
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   721
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   722
sub parsePrintableString($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   723
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   724
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   725
	my $printableString_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   726
	my $printableString = asciiToPrintableString($argString, \$printableString_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   727
	return encodePrintableString($printableString, $printableString_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   728
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   729
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   730
sub parsePrintableStringFile($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   731
	my ($binFName, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   732
	$binFName =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   733
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   734
	my $printableString_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   735
	my $printableString = encodeBinaryFile($binFName, \$printableString_oc);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   736
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   737
	return encodePrintableString($printableString, $printableString_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   738
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   739
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   740
sub parseRaw($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   741
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   742
	$argString =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   743
	$argString = uc($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   744
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   745
	my $asnHex = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   746
	if (! ($argString =~ /(([A-Fa-f\d][A-Fa-f\d])[ :]*)+/)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   747
		die "parseRaw: Invalid hex string: $argString\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   748
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   749
	my $binary = toBin($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   750
	$$oc += length($binary);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   751
	return tidyHex(toHex($binary));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   752
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   753
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   754
sub parseSequence($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   755
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   756
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   757
	my $sequence_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   758
	my $sequence = parseScript($lines, \$sequence_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   759
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   760
	return encodeSequence($sequence, $sequence_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   761
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   762
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   763
sub parseSet($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   764
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   765
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   766
	my $set_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   767
	my $set = parseScript($lines, \$set_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   768
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   769
	return encodeSet($set, $set_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   770
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   771
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   772
# Create a PKCS#7 signed data object for a chunk of data using 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   773
# OpenSSL's SMIME command
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   774
sub parseSign($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   775
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   776
	my ($signerCert, $signerKey) = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   777
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   778
	if (! defined $signerCert) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   779
		die "parseSign: missing signing certificate";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   780
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   781
	elsif (! -f $signerCert) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   782
		die "parseSign: signing certificate \'$signerCert\' does not exist.";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   783
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   784
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   785
	if (! defined $signerKey) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   786
		die "parseSign: missing signing certificate";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   787
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   788
	elsif (! -f $signerKey) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   789
		die "parseSign: signing key \'$signerKey\' does not exist.";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   790
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   791
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   792
	my $unsigned_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   793
	my $unsigned = parseScript($lines, \$unsigned_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   794
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   795
	my $unsignedFName = '_unsigned.tmp';
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   796
	my $signedFName = '_signed.tmp';
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   797
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   798
	# Create binary unsigned data file
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   799
	my $unsignedFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   800
	open($unsignedFh, ">$unsignedFName") or die "Cannot create $unsignedFName";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   801
	binmode($unsignedFh);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   802
	print $unsignedFh toBin($unsigned);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   803
	close $unsignedFh;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   804
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   805
	my @command = ('openssl', 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   806
				   'smime', 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   807
				   '-pk7out', 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   808
				   '-nodetach',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   809
				   '-outform',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   810
				   'der',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   811
				   '-sign',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   812
				   '-signer',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   813
				   $signerCert,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   814
				   '-inkey',
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   815
				   $signerKey,
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   816
				   '-in', $unsignedFName, 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   817
				   '-out', $signedFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   818
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   819
	if ($DEBUG == 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   820
		print "${TABS}:parseSign:" . join(" ", @command) . "\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   821
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   822
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   823
	if ((my $err = system(@command)) != 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   824
		die "parseSign: " . join(" ", @command) . "\nreturned error $err";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   825
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   826
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   827
	my $derHex = parseIncludeBinaryFile($signedFName, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   828
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   829
	if (! $DEBUG) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   830
		unlink($unsignedFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   831
		unlink($signedFName);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   832
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   833
	return $derHex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   834
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   835
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   836
sub parseShell($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   837
	my ($argString, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   838
	my @command = getArgs($argString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   839
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   840
	if (scalar(@command) < 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   841
		die "parseShell: no arguments";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   842
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   843
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   844
	if ($DEBUG == 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   845
		print "${TABS}:parseShell:" . join(" ", @command) . "\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   846
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   847
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   848
	if ((my $err = system(@command)) != 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   849
		die "parseShell: " . join(" ", @command) . "\nreturned error $err";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   850
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   851
	return "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   852
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   853
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   854
sub parseUtcTime($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   855
	my ($time, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   856
	$time =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   857
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   858
	my $time_oc = length($time);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   859
	return encodeUtcTime(toHex($time), $time_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   860
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   861
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   862
sub parseUtf8String($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   863
	my ($argString, $oc, $lines) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   864
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   865
	my $utf8String_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   866
	my $utf8String = asciiToUtf8String($argString, \$utf8String_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   867
	return encodeUtf8String($utf8String, $utf8String_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   868
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   869
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   870
sub parseUtf8StringFile($$;$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   871
	my ($binFName, $oc, $lines) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   872
	$binFName =~ s/\s*//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   873
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   874
	my $utf8String_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   875
	my $utf8String = encodeBinaryFile($binFName, \$utf8String_oc);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   876
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   877
	return encodeUtf8String($utf8String, $utf8String_oc, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   878
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   879
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   880
sub toHex($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   881
	my ($bin) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   882
	my $hex = unpack("H" . (length($bin) * 2), $bin);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   883
	$hex =~ s/(..)/$1:/g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   884
	return $hex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   885
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   886
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   887
sub encodeBinaryFile($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   888
	my ($binFName, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   889
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   890
	my $binFH;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   891
	open($binFH, "$binFName") || die "encodeBinaryFile: Cannot open $binFName\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   892
	binmode($binFH);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   893
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   894
	my $binBuf;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   895
	my $readBuf;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   896
	my $derHex = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   897
	while (my $len = sysread($binFH, $readBuf, 1024)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   898
		$binBuf .= $readBuf;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   899
		$$oc += $len;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   900
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   901
	close $binFH;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   902
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   903
	return toHex($binBuf);;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   904
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   905
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   906
# Creates a hex representation of the DER encoding of an arbitrary length bit string
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   907
sub encodeBitString($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   908
	my ($text, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   909
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   910
	# Bit string in hex including padding length octet
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   911
	my $bit_str = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   912
	my $bit_str_oc = 1; # one octet for padding
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   913
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   914
	# Current byte
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   915
	my $byte = 0;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   916
	my $len = length($text);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   917
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   918
	if ($len == 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   919
		$$oc+=2;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   920
		return "03:00";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   921
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   922
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   923
	my $i = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   924
	while ($i < $len) {		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   925
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   926
		# Read the ith character and insert it in the correct place in the byte
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   927
		# (fill from the left)
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   928
		my $c = substr($text, $i, 1);		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   929
		if ($c eq "1") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   930
			$byte |= (1 << (7 - ($i % 8)));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   931
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   932
		elsif ($c ne "0") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   933
			die "Invalid character $c in bit string $text";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   934
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   935
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   936
		if (++$i % 8 == 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   937
			# Received 8 bits so output byte in hex
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   938
			if ($bit_str ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   939
				$bit_str .= ":";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   940
			}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   941
			$bit_str .= sprintf("%2.2x", $byte);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   942
			$bit_str_oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   943
			$byte = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   944
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   945
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   946
	# Pad any remaining bits / make sure 0 is output for empty string
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   947
	if ($byte != 0 || $bit_str_oc == 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   948
		if ($bit_str ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   949
			$bit_str .= ":";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   950
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   951
		$bit_str .= sprintf("%2.2x", $byte);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   952
		$bit_str_oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   953
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   954
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   955
	my $pad_length = "00";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   956
	if ($len % 8 > 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   957
		# If this isn't a multiple of 8 bits then calculated
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   958
		# the number of padding bits added.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   959
		$pad_length = sprintf("%2.2x", 8 - ($len % 8));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   960
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   961
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   962
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   963
		print "${TABS}:ENC:encodeBitString, $bit_str_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   964
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   965
	return encodeTlv($oc, $DER_BITSTRING_TAG, $bit_str_oc, "$pad_length:$bit_str");
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   966
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   967
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   968
# Creates a hex represenation of the DER encoding of a BMPSTRING
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   969
sub encodeBmpString($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   970
	my ($bmpString, $bmpString_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   971
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   972
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   973
		print "${TABS}:ENC:encodeBmpString, $bmpString_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   974
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   975
	return encodeTlv($oc, $DER_BMPSTRING_TAG, $bmpString_oc, $bmpString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   976
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   977
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   978
sub encodeBoolean($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   979
	my ($value, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   980
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   981
	my $boolean = "00";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   982
	if ($value) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   983
		$boolean = "FF";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   984
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   985
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   986
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   987
		print "${TABS}:ENC:encodeBoolean, 1\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   988
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   989
	return encodeTlv($oc, $DER_BOOLEAN_TAG, 1, $boolean);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   990
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   991
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   992
sub encodeEnumerated($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   993
	my ($int, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   994
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   995
	$int =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   996
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   997
	if (! ($int =~ /^-??\d+$/ || $int =~ /0x[0-9A-Fa-f]+/)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   998
		die "encodeEnumerated: Invalid argument: $int\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
   999
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1000
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1001
	if ($int =~ s/^0x//) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1002
		$int = hex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1003
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1004
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1005
	# Convert the enumerated to base 256 hex and find out how
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1006
	# many octets were required
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1007
	my $hex_enumerated_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1008
	my $hex_enumerated = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1009
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1010
	if ($int ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1011
		$hex_enumerated = encodeBase256($int, \$hex_enumerated_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1012
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1013
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1014
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1015
		print "${TABS}:ENC: , $hex_enumerated_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1016
	}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1017
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1018
	return encodeTlv($oc, $DER_ENUMERATED_TAG, $hex_enumerated_oc, $hex_enumerated);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1019
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1020
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1021
# explicit tags are always constructed
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1022
sub encodeExplicit($$$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1023
	my ($class, $tagNumber, $explicit, $explicit_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1024
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1025
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1026
		print "${TABS}:ENC: explicit, $explicit_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1027
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1028
	return encodeTlv($oc, $tagNumber, $explicit_oc, $explicit, 1, $class);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1029
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1030
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1031
# Creates a hex represenation of the DER encoding of an IA5 string
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1032
sub encodeIA5String($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1033
	my ($ia5String, $ia5String_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1034
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1035
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1036
		print "${TABS}:ENC:encodeIA5String, $ia5String_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1037
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1038
	return encodeTlv($oc, $DER_IA5STRING_TAG, $ia5String_oc, $ia5String);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1039
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1040
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1041
sub encodeImplicit($$$$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1042
	my ($class, $constructed, $tagNumber, $implicit, $implicit_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1043
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1044
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1045
		print "${TABS}:ENC: implicit, $implicit_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1046
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1047
	return encodeTlv($oc, $tagNumber, $implicit_oc, $implicit, $constructed, $class);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1048
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1049
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1050
sub encodeBigInteger($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1051
	my ($hexString, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1052
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1053
	my $bin = toBin($hexString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1054
	my $int = toHex($bin);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1055
	my $int_oc = length($bin);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1056
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1057
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1058
		print "${TABS}:ENC: bigInteger, $int_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1059
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1060
	return encodeTlv($oc, $DER_INTEGER_TAG, $int_oc, $int)
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1061
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1062
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1063
sub encodeInteger($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1064
	my ($int, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1065
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1066
	$int =~ s/\s//g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1067
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1068
	if (! ($int =~ /^-??\d+$/ || $int =~ /0x[0-9A-Fa-f]+/)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1069
		die "encodeInteger: Invalid argument: $int\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1070
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1071
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1072
	if ($int =~ s/^0x//) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1073
		$int = hex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1074
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1075
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1076
	# Convert the integer to base 256 hex and find out how
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1077
	# many octets were required
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1078
	my $hex_integer_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1079
	my $hex_integer = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1080
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1081
	if ($int ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1082
		$hex_integer = encodeBase256($int, \$hex_integer_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1083
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1084
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1085
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1086
		print "${TABS}:ENC: integer, $hex_integer_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1087
	}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1088
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1089
	return encodeTlv($oc, $DER_INTEGER_TAG, $hex_integer_oc, $hex_integer);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1090
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1091
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1092
sub encodeNull($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1093
	my ($oc) = @_;	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1094
	return encodeTlv($oc, $DER_NULL_TAG, 0, "");
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1095
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1096
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1097
sub encodeOctetString($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1098
	my ($octetString, $octetString_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1099
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1100
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1101
		print "${TABS}:ENC: octetString, $octetString_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1102
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1103
	return encodeTlv($oc, $DER_OCTETSTRING_TAG, $octetString_oc, $octetString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1104
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1105
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1106
sub encodeOid($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1107
	my ($text, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1108
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1109
	my @fields = split /\./, $text;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1110
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1111
	if (! ($fields[0] >= 0 && $fields[0] <=2) ) { 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1112
		die "Invalid OID: $text\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1113
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1114
	if (! ($fields[1] >= 0 && $fields[1] <= 39) ) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1115
		die "Invalid OID: $text";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1116
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1117
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1118
	my $oid = sprintf("%2.2x", (40 * $fields[0]) + $fields[1]);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1119
	my $oid_oc = 1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1120
	shift @fields;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1121
	shift @fields;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1122
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1123
	foreach (@fields) {		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1124
		$oid .= ":" . encodeBase128($_, \$oid_oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1125
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1126
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1127
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1128
		print "${TABS}:ENC:encodeOid, $oid_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1129
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1130
	return encodeTlv($oc, $DER_OID_TAG, $oid_oc, $oid);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1131
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1132
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1133
# Creates a hex represenation of the DER encoding of a PRINTABLE string
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1134
sub encodePrintableString($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1135
	my ($printableString, $printableString_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1136
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1137
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1138
		print "${TABS}:ENC:encodePrintableString, $printableString_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1139
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1140
	return encodeTlv($oc, $DER_PRINTABLESTRING_TAG, $printableString_oc, $printableString);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1141
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1142
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1143
sub encodeSet($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1144
	my ($set, $set_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1145
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1146
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1147
		print "${TABS}:ENC: set, $set_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1148
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1149
	return encodeTlv($oc, $DER_SET_TAG, $set_oc, $set, 1);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1150
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1151
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1152
sub encodeSequence($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1153
	my ($sequence, $sequence_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1154
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1155
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1156
		print "${TABS}:ENC: sequence, $sequence_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1157
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1158
	return encodeTlv($oc, $DER_SEQUENCE_TAG, $sequence_oc, $sequence, 1);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1159
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1160
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1161
sub encodeUtcTime($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1162
	my ($utcTime, $utcTime_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1163
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1164
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1165
		print "${TABS}:ENC: UTCTime, $utcTime_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1166
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1167
	return encodeTlv($oc, $DER_UTCTIME_TAG, $utcTime_oc, $utcTime);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1168
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1169
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1170
# Creates a hex represenation of the DER encoding of a UTF-8 string.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1171
sub encodeUtf8String($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1172
	my ($utf8String, $utf8String_oc, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1173
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1174
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1175
		print "${TABS}:ENC:encodeUTF8String, $utf8String_oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1176
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1177
	return encodeTlv($oc, $DER_UTF8STRING_TAG, $utf8String_oc, $utf8String);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1178
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1179
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1180
sub asciiToBmpString($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1181
	my ($input, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1182
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1183
	my $bmpString = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1184
	my $input_len = length($input);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1185
	$$oc += $input_len * 2;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1186
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1187
	for (my $i = 0; $i < $input_len; ++$i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1188
		my $hex_val = ord(substr($input, $i, 1));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1189
		if ($bmpString ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1190
			$bmpString .= ":";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1191
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1192
		$bmpString .= sprintf(":00:%2.2x", $hex_val);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1193
	}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1194
	return $bmpString;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1195
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1196
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1197
sub asciiToIA5String($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1198
	my ($input, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1199
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1200
	my $printableString = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1201
	my $input_len = length($input);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1202
	$$oc += $input_len;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1203
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1204
	for (my $i = 0; $i < $input_len; ++$i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1205
		my $hex_val = ord(substr($input, $i, 1));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1206
		if ($printableString ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1207
			$printableString .= ":";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1208
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1209
		$printableString .= sprintf(":%2.2x", $hex_val);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1210
	}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1211
	return $printableString;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1212
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1213
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1214
sub asciiToPrintableString($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1215
	my ($input, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1216
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1217
	my $ia5String = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1218
	my $input_len = length($input);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1219
	$$oc += $input_len;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1220
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1221
	for (my $i = 0; $i < $input_len; ++$i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1222
		my $hex_val = ord(substr($input, $i, 1));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1223
		if ($ia5String ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1224
			$ia5String .= ":";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1225
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1226
		$ia5String .= sprintf(":%2.2x", $hex_val);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1227
	}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1228
	return $ia5String;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1229
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1230
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1231
sub asciiToUtf8String($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1232
	my ($input, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1233
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1234
	my $utf8String = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1235
	my $input_len = length($input);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1236
	$$oc += $input_len;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1237
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1238
	for (my $i = 0; $i < $input_len; ++$i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1239
		my $hex_val = ord(substr($input, $i, 1));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1240
		if ($utf8String ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1241
			$utf8String .= ":";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1242
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1243
		$utf8String .= sprintf(":%2.2x", $hex_val);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1244
	}	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1245
	return $utf8String;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1246
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1247
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1248
sub encodeBase128($$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1249
	my ($num, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1250
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1251
	my $base128 = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1252
	$num = int($num);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1253
	my $base128_length = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1254
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1255
	while ($num > 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1256
		my $hexoctet;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1257
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1258
		if ($base128 eq "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1259
			$hexoctet = sprintf("%2.2x", $num & 0x7f);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1260
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1261
		else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1262
			$hexoctet = sprintf("%2.2x", ($num & 0x7f) | 0x80);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1263
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1264
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1265
		if ($base128 eq "") {			
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1266
			$base128 = $hexoctet;	   
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1267
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1268
		else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1269
			$base128 = "$hexoctet:$base128";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1270
		}		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1271
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1272
		$num >>= 7;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1273
		$base128_length++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1274
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1275
	if ($base128 eq "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1276
		$base128 = "00";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1277
		$base128_length++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1278
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1279
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1280
	$$oc += $base128_length;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1281
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1282
	if ($DEBUG == 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1283
		print "${TABS}:ENC: base128, $base128_length, $$oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1284
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1285
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1286
	return $base128;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1287
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1288
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1289
# Return a hex represenation of the length using DER primitive (definate length encoding)
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1290
sub encodeLength($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1291
	my ($num, $oc) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1292
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1293
	if ($num < 128) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1294
		# Number is < 128 so encode in short form
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1295
		$$oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1296
		return sprintf("%2.2x", $num);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1297
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1298
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1299
		# Number >= 128 so encode in long form
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1300
		my $length_oc = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1301
		my $base256 = &encodeBase256($num, \$length_oc, 1);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1302
		if ($length_oc > 127) {die "Encoding overflow.";}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1303
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1304
		$$oc += 1 + $length_oc;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1305
		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1306
		# Set the top bit of the length octet to indicate long form		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1307
		return "" . sprintf("%2.2x", ($length_oc | 0x80)) . ":$base256";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1308
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1309
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1310
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1311
# Convert an integer into an ascii hex representation in base 256
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1312
# $num    - the number to encode
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1313
# $octets - refernce to the octet count to increment
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1314
# $unsigned - assume unsigned
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1315
sub encodeBase256($$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1316
	my ($numIn, $oc, $unsigned) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1317
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1318
	my $base256 = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1319
	my $num = int($numIn);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1320
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1321
	while ($num != 0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1322
		my $hexoctet = sprintf("%2.2x", $num & 0xFF);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1323
		if ($base256 ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1324
			$base256 = "$hexoctet:$base256";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1325
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1326
		else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1327
			$base256 = $hexoctet;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1328
		}		
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1329
		$num >>= 8;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1330
		$$oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1331
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1332
	if ($base256 eq "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1333
		$base256 = "00";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1334
		$$oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1335
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1336
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1337
	# If the integer is +ve and the MSB is 1 then padd with a leading zero 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1338
	# octet otherwise it will look -ve
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1339
	if ((! $unsigned) && $numIn > 0 && $base256 =~ /^:*[8ABCDEF]/i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1340
		$base256 = "00:$base256";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1341
		$$oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1342
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1343
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1344
	# If the first octet is all ones and the msb of the next bit
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1345
	# is also one then drop the first octet because negative
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1346
	# numbers should not be padded
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1347
	while ($base256 =~ s/^(FF:)([8ABCDEF][0-9A-F].*)/$2/i) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1348
		$$oc--;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1349
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1350
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1351
	return $base256;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1352
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1353
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1354
# Encode the Type
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1355
# Only low tag form is supported at the moment
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1356
sub encodeType($$;$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1357
	my ($oc, $tagNumber, $constructed, $class) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1358
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1359
	$tagNumber = hex($tagNumber);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1360
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1361
	if ($tagNumber < 0 || $tagNumber > 30) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1362
		die "encodeType: Currently, only low tag numbers (0 - 30) are supported.";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1363
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1364
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1365
	if (! defined $class) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1366
		$class = "UNIVERSAL";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1367
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1368
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1369
	$class = uc($class);	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1370
	if (! isValidClass($class)) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1371
		die "encodeType: invalid class \'$class\'";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1372
	}   
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1373
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1374
	# If the type is constructed then set bit 6
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1375
	if (defined $constructed && $constructed == 1) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1376
		$tagNumber |= 0x20;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1377
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1378
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1379
	if ($class eq $UNIVERSAL_CLASS) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1380
	   # do nothing, bits 7 and 8 are zero
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1381
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1382
	elsif ($class eq $APPLICATION_CLASS) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1383
		# set bit 7
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1384
		$tagNumber |= 0x40;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1385
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1386
	elsif ($class eq $CONTEXT_SPECIFIC_CLASS) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1387
		# set bit 8
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1388
		$tagNumber |= 0x80;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1389
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1390
	elsif ($class eq $PRIVATE_CLASS) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1391
		# set bits 7 and 8
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1392
		$tagNumber |= 0xC0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1393
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1394
	$$oc++;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1395
	return sprintf("%2.2x", $tagNumber);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1396
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1397
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1398
sub encodeTlv($$$$;$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1399
	my ($oc, $tag, $length, $value, $constructed, $class) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1400
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1401
	if ($DEBUG == 3) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1402
		print "${TABS}encodeTlv\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1403
		print "${TABS}oc=$$oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1404
		print "${TABS}tag=$tag\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1405
		print "${TABS}length=$length\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1406
		print "${TABS}value=$value\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1407
		if (defined $constructed) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1408
			print "${TABS}constructed=$constructed\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1409
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1410
		if (defined $class) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1411
			print "${TABS}class=$class\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1412
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1413
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1414
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1415
	my $hex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1416
	$hex = encodeType($oc, $tag, $constructed, $class);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1417
	$hex .= ":" . encodeLength($length, $oc);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1418
	$$oc += $length;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1419
	$hex .= ":" . $value;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1420
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1421
	if ($DEBUG == 3) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1422
		print "${TABS}oc=$$oc\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1423
		print "${TABS}encoding=$hex\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1424
		print "${TABS}end\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1425
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1426
		toBin($hex);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1427
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1428
	return $hex;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1429
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1430
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1431
# increment debug tabbing level
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1432
sub nest() {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1433
	$TABS .= "   ";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1434
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1435
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1436
# decrement debug tabbing level
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1437
sub leaveNest() {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1438
	$TABS =~ s/^...//;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1439
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1440
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1441
sub isValidClass($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1442
	my ($class) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1443
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1444
	if (defined $class &&
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1445
		$class =~ /^(UNIVERSAL|APPLICATION|CONTEXT-SPECIFIC|PRIVATE)$/) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1446
		return 1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1447
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1448
	return 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1449
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1450
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1451
# Parse a DER field
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1452
sub getTlv($$$$$$) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1453
	my ($input, $class, $constructed, $tag, $length, $value) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1454
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1455
	my @hexOctets = split(/:+/,tidyHex($input));
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1456
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1457
	if (scalar(@hexOctets) < 2) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1458
		die "getTlv: too short";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1459
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1460
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1461
	my $type = hex(shift @hexOctets);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1462
	if (($type & 0xC0) == 0x00) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1463
		# universal: bit 8 = 0, bit 7 = 0
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1464
		$$class = $UNIVERSAL_CLASS;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1465
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1466
	elsif (($type & 0xC0) == 0x40) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1467
		# application: bit 8 = 0, bit 7 = 1
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1468
		$$class = $APPLICATION_CLASS;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1469
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1470
	elsif (($type & 0xC0) == 0x80) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1471
		# application: bit 8 = 1, bit 7 = 0
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1472
		$$class = $CONTEXT_SPECIFIC_CLASS;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1473
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1474
	elsif (($type & 0xC0) == 0xC0) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1475
		# application: bit 8 = 1, bit 7 = 1
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1476
		$$class = $PRIVATE_CLASS;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1477
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1478
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1479
		die "getTlv: assert";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1480
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1481
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1482
	if ($type & 0x20) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1483
		# constructed if bit 6 = 1
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1484
		$$constructed = 1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1485
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1486
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1487
		$$constructed = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1488
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1489
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1490
	# We assumme the tag number is in low form
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1491
	# and just look at the bottom 5 hits
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1492
	$$tag = $type & 0x1F;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1493
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1494
	$$length = hex(shift @hexOctets);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1495
	if ($$length & 0x80) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1496
		# long form
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1497
		my $length_oc = $$length & 0x7F;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1498
		$$length = 0;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1499
		for (my $i = 0; $i < $length_oc; $i++) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1500
			# length is encoded base 256
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1501
			$$length *= 256;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1502
			$$length += hex(shift @hexOctets);
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1503
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1504
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1505
	else {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1506
		# short form
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1507
		# don't do anything here, length is just bits 7 - 1 and 
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1508
		# we already know bit 8 is zero.
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1509
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1510
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1511
	$$value = "";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1512
	foreach (@hexOctets) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1513
		$$value .= ":$_";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1514
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1515
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1516
	if ($DEBUG == 3) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1517
		print "${TABS} class=$$class\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1518
		print "${TABS} constructed=$$constructed\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1519
		print "${TABS} tag=$$tag\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1520
		print "${TABS} length=$$length\n";
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1521
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1522
}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1523
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1524
# parse an escaped (\) comma seperated argument string
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1525
# into an array
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1526
sub getArgs($) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1527
	my ($argString) = @_;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1528
	my @args = ();
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1529
	
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1530
	while ($argString =~ /(^|.*?[^\\]),(.*)/ ) {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1531
		my $match = $1;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1532
		$argString = $2;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1533
		if ($match ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1534
			
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1535
			# unescape
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1536
			$match =~ s/(\\)([^\\])/$2/g;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1537
			push @args, $match;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1538
		}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1539
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1540
	if ($argString ne "") {
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1541
		push @args, $argString;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1542
	}
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1543
    return @args;
2c201484c85f Move the Security package to EPL, and add the implementations of the cryptographic algorithms
Santosh Patil <santosh.v.patil@nokia.com>
parents:
diff changeset
  1544
}