| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | #############################################################################
 | 
|---|
| 3 | ##
 | 
|---|
| 4 | ## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 5 | ## All rights reserved.
 | 
|---|
| 6 | ## Contact: Nokia Corporation (qt-info@nokia.com)
 | 
|---|
| 7 | ##
 | 
|---|
| 8 | ## This file is part of the S60 port of the Qt Toolkit.
 | 
|---|
| 9 | ##
 | 
|---|
| 10 | ## $QT_BEGIN_LICENSE:LGPL$
 | 
|---|
| 11 | ## Commercial Usage
 | 
|---|
| 12 | ## Licensees holding valid Qt Commercial licenses may use this file in
 | 
|---|
| 13 | ## accordance with the Qt Commercial License Agreement provided with the
 | 
|---|
| 14 | ## Software or, alternatively, in accordance with the terms contained in
 | 
|---|
| 15 | ## a written agreement between you and Nokia.
 | 
|---|
| 16 | ##
 | 
|---|
| 17 | ## GNU Lesser General Public License Usage
 | 
|---|
| 18 | ## Alternatively, this file may be used under the terms of the GNU Lesser
 | 
|---|
| 19 | ## General Public License version 2.1 as published by the Free Software
 | 
|---|
| 20 | ## Foundation and appearing in the file LICENSE.LGPL included in the
 | 
|---|
| 21 | ## packaging of this file.  Please review the following information to
 | 
|---|
| 22 | ## ensure the GNU Lesser General Public License version 2.1 requirements
 | 
|---|
| 23 | ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
|---|
| 24 | ##
 | 
|---|
| 25 | ## In addition, as a special exception, Nokia gives you certain additional
 | 
|---|
| 26 | ## rights.  These rights are described in the Nokia Qt LGPL Exception
 | 
|---|
| 27 | ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 | 
|---|
| 28 | ##
 | 
|---|
| 29 | ## GNU General Public License Usage
 | 
|---|
| 30 | ## Alternatively, this file may be used under the terms of the GNU
 | 
|---|
| 31 | ## General Public License version 3.0 as published by the Free Software
 | 
|---|
| 32 | ## Foundation and appearing in the file LICENSE.GPL included in the
 | 
|---|
| 33 | ## packaging of this file.  Please review the following information to
 | 
|---|
| 34 | ## ensure the GNU General Public License version 3.0 requirements will be
 | 
|---|
| 35 | ## met: http://www.gnu.org/copyleft/gpl.html.
 | 
|---|
| 36 | ##
 | 
|---|
| 37 | ## If you have questions regarding the use of this file, please contact
 | 
|---|
| 38 | ## Nokia at qt-info@nokia.com.
 | 
|---|
| 39 | ## $QT_END_LICENSE$
 | 
|---|
| 40 | ##
 | 
|---|
| 41 | #############################################################################
 | 
|---|
| 42 | 
 | 
|---|
| 43 | ############################################################################################
 | 
|---|
| 44 | #
 | 
|---|
| 45 | # Convenience script for creating signed packages you can install on your phone.
 | 
|---|
| 46 | #
 | 
|---|
| 47 | ############################################################################################
 | 
|---|
| 48 | 
 | 
|---|
| 49 | use strict;
 | 
|---|
| 50 | 
 | 
|---|
| 51 | # use a command-line parsing module
 | 
|---|
| 52 | use Getopt::Long;
 | 
|---|
| 53 | # Use file name parsing module
 | 
|---|
| 54 | use File::Basename;
 | 
|---|
| 55 | # Use File::Spec services mainly rel2abs
 | 
|---|
| 56 | use File::Spec;
 | 
|---|
| 57 | # use CWD abs_bath, which is exported only on request
 | 
|---|
| 58 | use Cwd 'abs_path';
 | 
|---|
| 59 | 
 | 
|---|
| 60 | 
 | 
|---|
| 61 | sub Usage() {
 | 
|---|
| 62 |     print <<ENDUSAGESTRING;
 | 
|---|
| 63 | 
 | 
|---|
| 64 | ==============================================================================================
 | 
|---|
| 65 | Convenience script for creating signed packages you can install on your phone.
 | 
|---|
| 66 | 
 | 
|---|
| 67 | Usage: createpackage.pl [options] templatepkg [target]-[platform] [certificate key [passphrase]]
 | 
|---|
| 68 | 
 | 
|---|
| 69 | Where supported optiobns are as follows:
 | 
|---|
| 70 |      [-i|install]            = Install the package right away using PC suite
 | 
|---|
| 71 |      [-p|preprocess]         = Only preprocess the template .pkg file.
 | 
|---|
| 72 |      [-c|certfile=<file>]    = The file containing certificate information for signing.
 | 
|---|
| 73 |                                The file can have several certificates, each specified in
 | 
|---|
| 74 |                                separate line. The certificate, key and passphrase in line
 | 
|---|
| 75 |                                must be ';' separated. Lines starting with '#' are treated
 | 
|---|
| 76 |                                as a comments. Also empty lines are ignored. The paths in
 | 
|---|
| 77 |                                <file> can be absolute or relative to <file>.
 | 
|---|
| 78 |      [-u|unsigned]           = Preserves the unsigned package
 | 
|---|
| 79 | Where parameters are as follows:
 | 
|---|
| 80 |      templatepkg             = Name of .pkg file template
 | 
|---|
| 81 |      target                  = Either debug or release
 | 
|---|
| 82 |      platform                = One of the supported platform
 | 
|---|
| 83 |                                winscw | gcce | armv5 | armv6 | armv7
 | 
|---|
| 84 |      certificate             = The certificate file used for signing
 | 
|---|
| 85 |      key                     = The certificate's private key file
 | 
|---|
| 86 |      passphrase              = The certificate's private key file's passphrase
 | 
|---|
| 87 | 
 | 
|---|
| 88 | Example:
 | 
|---|
| 89 |      createpackage.pl fluidlauncher_template.pkg release-armv5
 | 
|---|
| 90 | 
 | 
|---|
| 91 | Example with certfile:
 | 
|---|
| 92 |      createpackage.pl -c=mycerts.txt fluidlauncher_template.pkg release-armv5
 | 
|---|
| 93 | 
 | 
|---|
| 94 |      Content of 'mycerts.txt' must be something like this:
 | 
|---|
| 95 |         # This is comment line, also the empty lines are ignored
 | 
|---|
| 96 |         rd.cer;rd-key.pem
 | 
|---|
| 97 |         .\\cert\\mycert.cer;.\\cert\\mykey.key;yourpassword
 | 
|---|
| 98 |         X:\\QtS60\\s60installs\\selfsigned.cer;X:\\QtS60\\s60installs\\selfsigned.key
 | 
|---|
| 99 | 
 | 
|---|
| 100 | If no certificate and key files are provided, either a RnD certificate or
 | 
|---|
| 101 | a self-signed certificate from QtDir\\src\\s60installs directory is used.
 | 
|---|
| 102 | ==============================================================================================
 | 
|---|
| 103 | 
 | 
|---|
| 104 | ENDUSAGESTRING
 | 
|---|
| 105 | 
 | 
|---|
| 106 |     exit();
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | # Read given options
 | 
|---|
| 110 | my $install = "";
 | 
|---|
| 111 | my $preprocessonly = "";
 | 
|---|
| 112 | my $certfile = "";
 | 
|---|
| 113 | my $preserveUnsigned = "";
 | 
|---|
| 114 | 
 | 
|---|
| 115 | unless (GetOptions('i|install' => \$install,
 | 
|---|
| 116 |                    'p|preprocess' => \$preprocessonly,
 | 
|---|
| 117 |                    'c|certfile=s' => \$certfile,
 | 
|---|
| 118 |                    'u|unsigned' => \$preserveUnsigned,)){
 | 
|---|
| 119 |     Usage();
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | my $certfilepath = abs_path(dirname($certfile));
 | 
|---|
| 123 | 
 | 
|---|
| 124 | # Read params to variables
 | 
|---|
| 125 | my $templatepkg = $ARGV[0];
 | 
|---|
| 126 | my $targetplatform = lc $ARGV[1];
 | 
|---|
| 127 | 
 | 
|---|
| 128 | my @tmpvalues = split('-', $targetplatform);
 | 
|---|
| 129 | my $target = $tmpvalues[0];
 | 
|---|
| 130 | my $platform = $tmpvalues[1];;
 | 
|---|
| 131 | 
 | 
|---|
| 132 | # Convert visual target to real target (debug->udeb and release->urel)
 | 
|---|
| 133 | $target =~ s/debug/udeb/i;
 | 
|---|
| 134 | $target =~ s/release/urel/i;
 | 
|---|
| 135 | 
 | 
|---|
| 136 | my $certificate = $ARGV[2];
 | 
|---|
| 137 | my $key = $ARGV[3];
 | 
|---|
| 138 | my $passphrase = $ARGV[4];
 | 
|---|
| 139 | 
 | 
|---|
| 140 | # Generate output pkg basename (i.e. file name without extension)
 | 
|---|
| 141 | my $pkgoutputbasename = $templatepkg;
 | 
|---|
| 142 | my $preservePkgOutput = "";
 | 
|---|
| 143 | $pkgoutputbasename =~ s/_template/_$targetplatform/g;
 | 
|---|
| 144 | if ($pkgoutputbasename eq $templatepkg) {
 | 
|---|
| 145 |     $preservePkgOutput = "1";
 | 
|---|
| 146 | }
 | 
|---|
| 147 | $pkgoutputbasename =~ s/\.pkg//g;
 | 
|---|
| 148 | $pkgoutputbasename = lc($pkgoutputbasename);
 | 
|---|
| 149 | 
 | 
|---|
| 150 | # Store output file names to variables
 | 
|---|
| 151 | my $pkgoutput = lc($pkgoutputbasename.".pkg");
 | 
|---|
| 152 | my $sisoutputbasename = lc($pkgoutputbasename);
 | 
|---|
| 153 | $sisoutputbasename =~ s/_$targetplatform//g;
 | 
|---|
| 154 | my $unsigned_sis_name = $sisoutputbasename."_unsigned.sis";
 | 
|---|
| 155 | my $signed_sis_name = $sisoutputbasename.".sis";
 | 
|---|
| 156 | 
 | 
|---|
| 157 | # Store some utility variables
 | 
|---|
| 158 | my $scriptpath = dirname(__FILE__);
 | 
|---|
| 159 | my $certtext = $certificate;
 | 
|---|
| 160 | my $certpath = $scriptpath;
 | 
|---|
| 161 | $certpath =~ s-^(.*[^\\])$-$1\\-o;          # ensure path ends with a backslash
 | 
|---|
| 162 | $certpath =~ s-/-\\-go;                     # for those working with UNIX shells
 | 
|---|
| 163 | $certpath =~ s-bin\\$-src\\s60installs\\-;  # certificates are one step up in hierarcy
 | 
|---|
| 164 | 
 | 
|---|
| 165 | # Check some pre-conditions and print error messages if needed.
 | 
|---|
| 166 | unless (length($templatepkg)) {
 | 
|---|
| 167 |     print "\nError: Template PKG filename is not defined!\n";
 | 
|---|
| 168 |     Usage();
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | # If the pkg file is not actually a template, there is no need for plaform or target.
 | 
|---|
| 172 | if ($templatepkg =~ m/_template\.pkg/i) {
 | 
|---|
| 173 |     unless (length($platform) && length($target)) {
 | 
|---|
| 174 |         print "\nError: Platform or target is not defined!\n";
 | 
|---|
| 175 |         Usage();
 | 
|---|
| 176 |     }
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | # Check template exist
 | 
|---|
| 180 | stat($templatepkg);
 | 
|---|
| 181 | unless( -e _ ) {
 | 
|---|
| 182 |     print "\nError: Package description file '$templatepkg' does not exist!\n";
 | 
|---|
| 183 |     Usage();
 | 
|---|
| 184 | }
 | 
|---|
| 185 | 
 | 
|---|
| 186 | # Check certifcate preconditions and set default certificate variables if needed
 | 
|---|
| 187 | if (length($certificate)) {
 | 
|---|
| 188 |     unless(length($key)) {
 | 
|---|
| 189 |         print "\nError: Custom certificate key file parameter missing.!\n";
 | 
|---|
| 190 |         Usage();
 | 
|---|
| 191 |     }
 | 
|---|
| 192 | } else {
 | 
|---|
| 193 |     #If no certificate is given, check default options
 | 
|---|
| 194 |     $certtext = "RnD";
 | 
|---|
| 195 |     $certificate = $certpath."rd.cer";
 | 
|---|
| 196 |     $key = $certpath."rd-key.pem";
 | 
|---|
| 197 | 
 | 
|---|
| 198 |     stat($certificate);
 | 
|---|
| 199 |     unless( -e _ ) {
 | 
|---|
| 200 |         $certtext = "Self Signed";
 | 
|---|
| 201 |         $certificate = $certpath."selfsigned.cer";
 | 
|---|
| 202 |         $key = $certpath."selfsigned.key";
 | 
|---|
| 203 |     }
 | 
|---|
| 204 | }
 | 
|---|
| 205 | 
 | 
|---|
| 206 | # Read the certificates from file to two dimensional array
 | 
|---|
| 207 | my @certificates;
 | 
|---|
| 208 | if (length($certfile)) {
 | 
|---|
| 209 |     open CERTFILE, "<$certfile" or die $!;
 | 
|---|
| 210 |     while(<CERTFILE>){
 | 
|---|
| 211 |         s/#.*//;                            # ignore comments by erasing them
 | 
|---|
| 212 |         next if /^(\s)*$/;                  # skip blank lines
 | 
|---|
| 213 |         chomp;                              # remove trailing newline characters
 | 
|---|
| 214 |         my @certinfo = split(';', $_);      # split row to certinfo
 | 
|---|
| 215 | 
 | 
|---|
| 216 |         # Trim spaces
 | 
|---|
| 217 |         for(@certinfo) {
 | 
|---|
| 218 |             s/^\s+//;
 | 
|---|
| 219 |             s/\s+$//;
 | 
|---|
| 220 |         }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         # Do some validation
 | 
|---|
| 223 |         unless(scalar(@certinfo) >= 2 && scalar(@certinfo) <= 3 && length($certinfo[0]) && length($certinfo[1]) ) {
 | 
|---|
| 224 |             print "\nError: $certfile line '$_' does not contain valid information!\n";
 | 
|---|
| 225 |             Usage();
 | 
|---|
| 226 |         }
 | 
|---|
| 227 | 
 | 
|---|
| 228 |         push @certificates, [@certinfo];    # push data to two dimensional array
 | 
|---|
| 229 |     }
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | # Remove any existing .sis packages
 | 
|---|
| 233 | unlink $unsigned_sis_name;
 | 
|---|
| 234 | unlink $signed_sis_name;
 | 
|---|
| 235 | if (!$preservePkgOutput) {
 | 
|---|
| 236 |     unlink $pkgoutput;
 | 
|---|
| 237 | }
 | 
|---|
| 238 | 
 | 
|---|
| 239 | # Preprocess PKG
 | 
|---|
| 240 | local $/;
 | 
|---|
| 241 | # read template file
 | 
|---|
| 242 | open( TEMPLATE, $templatepkg) or die "Error '$templatepkg': $!\n";
 | 
|---|
| 243 | $_=<TEMPLATE>;
 | 
|---|
| 244 | close (TEMPLATE);
 | 
|---|
| 245 | 
 | 
|---|
| 246 | # replace the PKG variables
 | 
|---|
| 247 | s/\$\(PLATFORM\)/$platform/gm;
 | 
|---|
| 248 | s/\$\(TARGET\)/$target/gm;
 | 
|---|
| 249 | 
 | 
|---|
| 250 | #write the output
 | 
|---|
| 251 | open( OUTPUT, ">$pkgoutput" ) or die "Error '$pkgoutput' $!\n";
 | 
|---|
| 252 | print OUTPUT $_;
 | 
|---|
| 253 | close OUTPUT;
 | 
|---|
| 254 | 
 | 
|---|
| 255 | if ($preprocessonly) {
 | 
|---|
| 256 |     exit;
 | 
|---|
| 257 | }
 | 
|---|
| 258 | 
 | 
|---|
| 259 | # Create SIS.
 | 
|---|
| 260 | system ("makesis $pkgoutput $unsigned_sis_name");
 | 
|---|
| 261 | 
 | 
|---|
| 262 | # Sign SIS with certificate info given as an argument.
 | 
|---|
| 263 | system ("signsis $unsigned_sis_name $signed_sis_name $certificate $key $passphrase");
 | 
|---|
| 264 | 
 | 
|---|
| 265 | # Check if creating signed SIS Succeeded
 | 
|---|
| 266 | stat($signed_sis_name);
 | 
|---|
| 267 | if( -e _ ) {
 | 
|---|
| 268 |     my $targetInsert = "";
 | 
|---|
| 269 |     if ($targetplatform ne "-") {
 | 
|---|
| 270 |         $targetInsert = "for $targetplatform ";
 | 
|---|
| 271 |     }
 | 
|---|
| 272 |     print ("\nSuccessfully created $signed_sis_name ${targetInsert}using certificate: $certtext!\n");
 | 
|---|
| 273 | 
 | 
|---|
| 274 |     # Sign with additional certificates & keys
 | 
|---|
| 275 |     for my $row ( @certificates ) {
 | 
|---|
| 276 |         # Get certificate absolute file names, relative paths are relative to certfilepath
 | 
|---|
| 277 |         my $abscert = File::Spec->rel2abs( $row->[0], $certfilepath);
 | 
|---|
| 278 |         my $abskey = File::Spec->rel2abs( $row->[1], $certfilepath);
 | 
|---|
| 279 | 
 | 
|---|
| 280 |         system ("signsis $signed_sis_name $signed_sis_name $abscert $abskey $row->[2]");
 | 
|---|
| 281 |         print ("\tAdditionally signed the SIS with certificate: $row->[0]!\n");
 | 
|---|
| 282 |     }
 | 
|---|
| 283 | 
 | 
|---|
| 284 |     # remove temporary pkg and unsigned sis
 | 
|---|
| 285 |     if (!$preservePkgOutput) {
 | 
|---|
| 286 |         unlink $pkgoutput;
 | 
|---|
| 287 |     }
 | 
|---|
| 288 |     if (!$preserveUnsigned) {
 | 
|---|
| 289 |         unlink $unsigned_sis_name;
 | 
|---|
| 290 |     }
 | 
|---|
| 291 | 
 | 
|---|
| 292 |     # Install the sis if requested
 | 
|---|
| 293 |     if ($install) {
 | 
|---|
| 294 |         print ("\nInstalling $signed_sis_name...\n");
 | 
|---|
| 295 |         system ("$signed_sis_name");
 | 
|---|
| 296 |     }
 | 
|---|
| 297 | } else {
 | 
|---|
| 298 |     # Lets leave the generated PKG for problem solving purposes
 | 
|---|
| 299 |     print ("\nSIS creation failed!\n");
 | 
|---|
| 300 | }
 | 
|---|
| 301 | 
 | 
|---|
| 302 | 
 | 
|---|
| 303 | #end of file
 | 
|---|