lunedì 26 dicembre 2016

Use the Java Keystore to sign a Windows Executable file

If you have already a valid Java Keystore released by a certification authority (CA), you can use it to sign a Windows Executable file (for example a setup).

You need a Java Runtime and a Windows SDK:

set JAVA_HOME=c:\Programs Files\Java\jdk8
set WIN_SDK=C:\Program Files (x86)\Windows Kits\10

Convert the Java Keystore to the Personal Information Exchange (PFX) format:
%JAVA_HOME%\bin\keytool -importkeystore -srckeystore keystore.jks
 -destkeystore keystore.pfx -deststoretype PKCS12
Sign the executable file
%WIN_SDK%\bin\x64\signtool.exe sign /v /f keystore.pfx 
 /t http://timestamp.verisign.com/scripts/timstamp.dll 
 /p mypassword mysetup.exe
To verify your file
%WIN_SDK%\bin\x64\signtool.exe verify /pa mysetup.exe 

venerdì 3 luglio 2015

Create a custom dialog box with Modern UI 1.5.0

The Modern UIfor WPF is a good solution to create pretty WPF application.
Unfortunately the FirstFloor.ModernUI.Windows.Controls.ModernDialog class is usefull only to show simple text (alert dialog).

The following function helps you to create a custom dialog box using the ModernDialog class.

/// <summary>Simple input dialog box</summary>
/// <returns>null if the dialog box was closed with "cancel",
/// otherwise the user input<returns>
public static string ShowInputDialog(string initialText=null)
{
  // create the dialog content
  TextBox content = new TextBox();
  content.Text = initialText;

  // create the ModernUI dialog component with the buttons
  var dlg = new ModernDialog
  {
    Title = "Insert a text",
    Content = content,
    MinHeight = 0,
    MinWidth = 0,
    MaxHeight = 480,
    MaxWidth = 640,
  };
  dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton };

  // register the event to retrieve the result of the dialog box 
  string result = null;
  dlg.OkButton.Click += (object sender, RoutedEventArgs e) =>
  {
    result = content.Text;
  };

  dlg.ShowDialog();
  return result;
}

giovedì 2 luglio 2015

Retrieve the version of the .NET runtime

Sometimes the simplest questions remains unanswered: what's the .NET runtime version executes my app? .NET 4.0 or .Net 4.5?
Hier you find a simple C# function to retrieve the version of the .NET runtime on which your app is executed.

public static string RetrieveHumanRuntimeVersion()
{
 string text = Environment.Version.ToString();
 if (!text.StartsWith("4.0.30319.")) return text;
 int build, start = text.LastIndexOf('.');
 if (!Int32.TryParse(text.Substring(start + 1), out build)) return text;
 if (build <= 17000) return ".NET 4.0";
 if (build <= 19000) return ".NET 4.5";
 if (build <= 34000) return ".NET 4.5.1";
 return ".NET 4.5.2";
}

More details here

giovedì 2 aprile 2015

Simple uniq alternative

uniq is a Unix utility which, when fed a text file, outputs the file with adjacent identical lines collapsed to one.

Here a simple alternative

alias uniq='while read line; do if [ "$line" != "$PREVIOUS" ]; then echo $line; PREVIOUS=$line; fi; done'

domenica 1 febbraio 2015

Experience with the Google Play Developer API

If the Android developer console is a powerful tool, the manage of a multi language Android app is oft a long time-consuming procedure.
So I've tested the Google Play Developer Publishing API.

As described in the Getting Started page, I've linked my Google Play Developer Console to an API project and created a OAuth2 Client in the API Access page on the Google Play Developer Console .

Here there's the code of a simple bash script:
#!/bin/sh

PACKAGE_NAME=<package name of the Android app>
BASE_URL=https://www.googleapis.com/androidpublisher/v2/applications/$PACKAGE_NAME

# see https://play.google.com/apps/publish/#ApiAccessPlace
CLIENT_ID=<CLIENT ID value>
CLIENT_SECRET=<CLIENT SECRET value>
REDIRECT_URL=urn:ietf:wg:oauth:2.0:oob

#-----------------------------------------------
# https://developers.google.com/accounts/docs/OAuth2InstalledApp
CODE=$1
if [ "$CODE" = "" ]
then
	echo "opening the browser to obtain a client code ..."
	xdg-open "https://accounts.google.com/o/oauth2/auth\
?scope=https://www.googleapis.com/auth/androidpublisher\
&response_type=code\
&access_type=offline\
&redirect_uri=$REDIRECT_URL\
&client_id=$CLIENT_ID"
	exit 255
fi

#-----------------------------------------------------------------------
# https://developers.google.com/android-publisher/authorization
REQUEST_URL="https://accounts.google.com/o/oauth2/token"
REQUEST_BODY="grant_type=authorization_code\
&code=$CODE\
&client_id=$CLIENT_ID\
&client_secret=$CLIENT_SECRET\
&redirect_uri=$REDIRECT_URL"
RESPONSE="/tmp/response-auth.json"
echo "Retrieving the authorization ..."

curl -s -o $RESPONSE -X POST -d "$REQUEST_BODY" $REQUEST_URL
AUTHORIZATION=`grep "access_token" $RESPONSE | cut -d '"' -f 4`
if [ "$AUTHORIZATION" = "" ]; then exit 255; fi

#-----------------------------------------------
# https://developers.google.com/android-publisher/edits/workflow
REQUEST_URL="$BASE_URL/edits"
REQUEST_BODY='{"id": "?","expiryTimeSeconds": 9}'
RESPONSE="/tmp/response-editid.json"

echo "Acquiring a edit id ..."
curl -s -o $RESPONSE -X POST -d \
-H "Content-Type: application/json" "$REQUEST_BODY" \
"$REQUEST_URL?access_token=$AUTHORIZATION"
EDIT_ID=`grep "id" $RESPONSE | cut -d '"' -f 4`
if [ "$EDIT_ID" = "" ]; then exit 255; fi

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/listings/list
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/listings"

echo "Retrieving the app description ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/images/
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/listings/en-US/phoneScreenshots"

echo "Retrieving the phone screenshots ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/apks/list
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/apks"

echo "Retrieving the APKs list ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

mercoledì 3 settembre 2014

Show the Android app version and permissions

Here there are some helpful commands to check an Android app using the setup file (APK):

# application version
aapt d badging file.apk | head -n 1

# application permissions
aapt d permissions file.apk

Identity of an Android app creator

To show or verify the identity of the Android app creator, you need the Android application setup (APK file).
Each Android app (file .apk) are digitally signed with a certificate.
This certificate identifies the author of an app.

You can choose one of the following commands:

# Direct with standard Java tools
jarsigner -verify -certs -verbose file.apk

# Indirect with standard Java tools
unzip -p file.apk  META-INF/CERT.RSA \
| keytool -printcert

# Indirect with OpenSSL
unzip -p file.apk  META-INF/CERT.RSA \
| openssl pkcs7 -inform DER -print_certs -text

# Indirect with OpenSSL
unzip -p file.apk  META-INF/CERT.RSA \
| openssl pkcs7 -inform DER -print_certs -text


With OpenSSL you can retrieve more information:
# extract the certificate in PKCS#7 format
unzip -p file.apk META-INF/CERT.RSA > /tmp/cert.pkcs7
# extract the certificate in PEM format
openssl pkcs7 -inform DER -in /tmp/cert.pkcs7 -print_certs \
| tail -n +3 > /tmp/cert.pem
# dump the certificate
openssl x509 -inform PEM -in /tmp/cert.pem -text