PKCS11Provider - Reference Documentation

Class implementing support for cryptographic token with PKCS#11 interface

Index of Methods

Properties

TypeNameDescription
StringproviderNameName under which this provider is registered

Constructor

Prototype

PKCS11Provider(String providerName)

Description

Load and access provider module.

Arguments

TypeNameDescription
StringproviderNameName and path of .DLL or shared object

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType of argument is invalid for call

Example


// var p = new PKCS11Provider("c:/programme/smart card bundle/opensc-pkcs11.dll");
var p = new PKCS11Provider("C:/usr/local/lsm/bin/lsmpkcs11.dll");
// var p = new PKCS11Provider("C:/programme/opensc/bin/pkcs11-spy.dll");

getSlots()

Prototype

Array getSlots()

Description

Return an array of native PKCS11Slot objects.

The objects in the array are of type org.opensc.pkcs11.wrap.PKCS11Slot.

Return

ArrayArray of native PKCS11Slot objects

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType of argument is invalid for call

Example


var slots = p.getSlots();
var slot = 0;

for (var i = 0; i < slots.length; i++) {
	var s = slots[i];
	print("Slot #" + s.getId());
	print(" Description       : " + s.getDescription());
	print(" Manufacturer      : " + s.getManufacturer());
	print(" Hardware Version  : " + s.getHardwareVersion());
	print(" Firmware Version  : " + s.getFirmwareVersion());
	print(" isTokenPresent    : " + s.isTokenPresent());
	print(" isHardwareDevice  : " + s.isHardwareDevice());
	print(" isRemovableDevice : " + s.isRemovableDevice());
	
	if (s.isTokenPresent()) {
		var label = s.getTokenLabel();

		if (label == "SBOXTEST1") {
			print("Found " + label);
			slot = s.getId();
		}
	
		print("  Token :");
		print("  Label                     : " + label);
		print("  Manufacturer              : " + s.getTokenManufacturer());
		print("  Model                     : " + s.getTokenModel());
		print("  Serial Number             : " + s.getTokenSerialNumber());
		print("  Max PIN Length            : " + s.getTokenMaxPinLen());
		print("  Min PIN Length            : " + s.getTokenMinPinLen());
		print("  hasTokenProtectedAuthPath : " + s.hasTokenProtectedAuthPath());
		
		var mechs = s.getMechanisms();
		for (var j = 0; j < mechs.length; j++) {
			print("   Mechanisms #" + j);
			var m = mechs[j];
			print("   Type         : " + m.getType() + " (" + m.getTypeName() + ")");
			print("   Min Key Size : " + m.getMinKeySize());
			print("   Max Key Size : " + m.getMaxKeySize());
			print("   Flags        : " + m.getFlags());
		}
	}
}

initToken()

Prototype

initToken(Number slotId, String soPIN, String label)

Description

Initialize token in referenced slot and set the Security Officer (SO) PIN and label.

If the token is already initialized, then the SO PIN must match the tokens SO PIN.

Arguments

TypeNameDescription
NumberslotIdThe slot id of the slot to initialize
StringsoPINThe Security Officers PIN
StringlabelThe tokens label

Return

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType of argument is invalid for call

Example


p.initToken(slot, "abcdefgh", "SBOXTEST1");

getKeyStore()

Prototype

KeyStore getKeyStore(Number slot)

KeyStore getKeyStore(Number slot, String userPIN)

Description

Obtain the key store for a token in the selected slot.

The key store can be used to create key objects that represent keys in the token.

Arguments

TypeNameDescription
NumberslotSlot number
StringuserPINUser PIN for token

Return

KeyStoreKeyStore object

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType of argument is invalid for call

Example


var ks = p.getKeyStore(slot, "12345678");

var aliases = ks.getAliases();
for (var i = 0; i < aliases.length; i++) {
	print(aliases[i]);
}

var k = new Key();
k.setType(Key.PRIVATE);
k.setID(aliases[0]);

ks.getKeyFromKeyStore(k);

var c = new Crypto(p.providerName);

var message = new ByteString("Hello World", ASCII);
var signature = c.sign(k, Crypto.RSA, message);

print(signature);


cleanup()

Prototype

cleanup()

Description

Cleanup provider and release used system resources.

This will be done automatically when the Java Runtime terminates.

Return

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType of argument is invalid for call

Example


p.cleanup();