Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.DSI.Pkcs11Token

Feature notes: PKIX / CMS implementation notes

Classes, interfaces and records

recordtPkcs11SignMech

A signing capability this token actually has. Hash = haUndefined means the mechanism is hash-agnostic (it takes whatever we prepare).

Mech : CK_MECHANISM_TYPE; Hash : tHashAlgorithm; Encoding : tSignEncoding; Kind : tPkcs11SignKind; MinKeyBits: CK_ULONG; MaxKeyBits: CK_ULONG;

classePkcs11TokenError

inherits from Exception

(no public members)

classtPkcs11Token

constructor Create(aLib: tPkcs11Library); destructor Destroy; override; function Open(const aToken: string): Boolean;
Finds the token whose serial number or label matches aToken (case insensitive; an empty string takes the first token present) and opens a session on it. Returns False when no such token is present.
procedure Close; function Login(const aPassphrase: string; aCheckLength: Boolean = True): CK_RV;
Logs the user in with the CKU_USER credential -- the token's PIN, token password or passphrase; PKCS#11 has one name for all three. Full description »
procedure Logout; function GenerateRSAKeyPair(aBits : CK_ULONG; const aKeyLabel : string; const aID : tBytes; out aPublicKey : CK_OBJECT_HANDLE; out aPrivateKey: CK_OBJECT_HANDLE): CK_RV;
Generates an RSA key pair ON the token. The private half is created CKA_SENSITIVE and NOT CKA_EXTRACTABLE, so it has no exportable copy -- which is the whole point of putting a CA key on a device. Needs a logged-in read/write session. aID is the CKA_ID that will later pair the key with its certificate; pass the same value to CreateCertificate.
function GenerateECKeyPair(const aCurveParams: tBytes; const aKeyLabel : string; const aID : tBytes; out aPublicKey : CK_OBJECT_HANDLE; out aPrivateKey : CK_OBJECT_HANDLE): CK_RV;
Generates an EC key pair ON the token. aCurveParams is the DER-encoded ECParameters for CKA_EC_PARAMS -- in practice a named-curve OBJECT IDENTIFIER, which is what every device and every tool expects. Same custody guarantees as the RSA generator: CKA_SENSITIVE, not CKA_EXTRACTABLE, and CKA_DERIVE explicitly denied so a CA signing key cannot be turned to ECDH.
function CreateCertificate(const aDER : tBytes; const aCertLabel: string; const aID : tBytes; const aSubject : tBytes; const aIssuer : tBytes; const aSerial : tBytes; out aObject : CK_OBJECT_HANDLE): CK_RV;
Writes a DER-encoded X.509 certificate onto the token as a CKO_CERTIFICATE. aID must match the key pair's CKA_ID; aSubject, aIssuer and aSerial are the DER-encoded fields, which middleware indexes on and which the certificate-to-key pairing elsewhere relies on.
function CreateDataObject(const aValue : tBytes; const aDataLabel: string; aPrivate : Boolean; out aObject : CK_OBJECT_HANDLE): CK_RV;
Writes a CKO_DATA object -- arbitrary bytes under a label, on the token. For small state that has to live somewhere the holder of a FILE cannot reach: a high-water mark, a counter, an anchor. Full description »
function FindDataObjects(const aDataLabel: string): tCK_ObjectHandleArray;
Every CKO_DATA object carrying this label, newest last. Empty when the token has none -- or when it does not keep data objects at all.
function DestroyObject(aObject: CK_OBJECT_HANDLE): CK_RV;
Destroys a token object. For rolling back a half-finished provisioning run; never call it on a key you did not just create.
function ReopenReadWrite: CK_RV;
Re-opens the session read/write, which object creation needs -- the one Open establishes is read-only. Log in again afterwards.
function GenerateRandom(aByteLen: Integer): tBytes;
Random bytes from the TOKEN's own generator. For a CA this is the right source: the same validated module that holds the key produces the certificate serials, and nothing needs a software DRBG (with its entropy source and health tests) linked into the program. Raises when the token has no RNG -- see HasRNG.
function FindObjects(const aTemplate: array of CK_ATTRIBUTE): tCK_ObjectHandleArray;
Object search on the open session. aTemplate is the match template; an empty one matches every object the session can see.
function GetAttribute(aObject: CK_OBJECT_HANDLE; aType : CK_ATTRIBUTE_TYPE; out aValue : tBytes): Boolean;
Reads one attribute. False when the object does not have it, or will not disclose it (CKA_SENSITIVE material), which is normal rather than an error -- a private key's modulus is readable, its exponent is not.
function GetAttributeULong(aObject: CK_OBJECT_HANDLE; aType : CK_ATTRIBUTE_TYPE; out aValue : CK_ULONG): Boolean; function HasMechanism(aMech: CK_MECHANISM_TYPE; aFlags: CK_FLAGS): Boolean;
Raw mechanism query, straight from the device.
function MechanismInfo(aMech: CK_MECHANISM_TYPE; out aMinKeyBits, aMaxKeyBits: CK_ULONG): Boolean; function ResolveSign(aHash : tHashAlgorithm; aEncoding: tSignEncoding; aAllowed : tPkcs11SignKinds; out aMech : tPkcs11SignMech): Boolean;
Picks a mechanism for (aHash, aEncoding), restricted to the kinds the caller can feed. Preference order follows aAllowed's natural order: message, then DigestInfo, then digest, then raw. Returns False when this device cannot do it at all -- which is a real answer, not an error.
function CanSign(aHash: tHashAlgorithm; aEncoding: tSignEncoding): Boolean; function ConformantKinds(aEncoding: tSignEncoding): tPkcs11SignKinds;
The sign kinds that are CORRECT for aEncoding; see pskDigestPlain.
function SignHashAlgorithms(aEncoding: tSignEncoding): tHashAlgorithms;
Every hash algorithm this device can sign with under aEncoding. Computed from the device's mechanism list, so it differs from token to token. Use it to CHOOSE a digest rather than to check one.
function SignEncodings: tSignEncodings;
Every encoding with at least one usable hash.
function CapabilityReport: string;
Human-readable capability dump for diagnostics and error messages.
property Lib: tPkcs11Library read fLib; property Slot: CK_SLOT_ID read fSlot; property Session: CK_SESSION_HANDLE read fSession; property IsOpen: Boolean read fOpen; property LoggedIn: Boolean read fLoggedIn; property TokenLabel: string read fTokenLabel; property Serial: string read fSerial; property Model: string read fModel; property LoginNeeded: Boolean read fLoginNeeded; property HasRNG: Boolean read fHasRNG;
CKF_RNG: the token offers C_GenerateRandom.
property MinPinLen: CK_ULONG read fMinPinLen;
The credential length range the token declares, IN BYTES. Whatever the device calls it -- PIN, token password, passphrase -- it is one CKU_USER credential, and the limit is on its UTF-8 encoding, so a passphrase with non-ASCII characters spends more of the budget than its character count suggests.
property MaxPinLen: CK_ULONG read fMaxPinLen; property PinFinalTry: Boolean read fPinFinalTry; property PinLocked: Boolean read fPinLocked;

Types (enumerations, sets, aliases)

enumtPkcs11SignKind

How a mechanism wants its input, which decides what the adapter has to hand C_Sign:
pskMessage - the message itself; the token hashes and pads
pskDigestInfo - PKCS#1 v1.5 over a DigestInfo blob we build
pskDigest - the bare digest plus mechanism parameters (PSS)
pskRaw - raw modexp; the library did the padding
pskDigestPlain is PKCS#1 v1.5 over the supplied bytes VERBATIM, with no DigestInfo: what a TLS 1.0/1.1 signature needs (a bare MD5+SHA-1 concatenation), and what seEMSA3 with aNullMsg means. It must never be substituted for pskDigestInfo -- signing a bare digest as though it carried a DigestInfo yields a non-conforming signature.

  tPkcs11SignKind = (pskMessage, pskDigestInfo, pskDigestPlain, pskDigest, pskRaw);

settPkcs11SignKinds

  tPkcs11SignKinds = set of tPkcs11SignKind;

Routines