JavaKeyStore PureBasic Reference Documentation

JavaKeyStore

Current Version: 11.5.0

Chilkat.JavaKeyStore

Load, inspect, modify, convert, and save Java keystore data.

Chilkat.JavaKeyStore is the Chilkat class for working with Java keystore data. It supports private-key entries with aliases and certificate chains, trusted certificate entries, and secret-key entries. It can load and save keystores, import private-key entries from PFX, add trusted certificates and symmetric keys, retrieve private keys and certificate chains, manage aliases, change private-key passwords, and convert keystore contents to PEM, PFX, or JWK Set format.

Load and save keystores

Read Java keystore data into memory, inspect or modify its contents, and save the updated keystore when changes are complete.

Private-key entries

Work with private keys stored under aliases, including their associated certificate chains and entry-specific passwords.

Trusted certificates

Add, retrieve, and manage trusted certificate entries used for trust stores, TLS validation, and Java-based security configuration.

Secret-key entries

Store and retrieve symmetric secret keys when the keystore is used for application key material in addition to certificates.

Import and convert

Import private-key entries from PFX/P12 and export or convert keystore contents to PEM, PFX, or JWK Set format.

Alias management

Enumerate aliases, locate entries, rename or manage entry names, and organize keys and certificates by application-specific identifiers.

Common pattern: Load the keystore with its store password, locate the needed entry by alias, retrieve the private key, certificate chain, trusted certificate, or secret key, then save the keystore or export the selected material in the format required by the target system. Use JavaKeyStore when the data is organized as a Java keystore; use Cert, PrivateKey, Pfx, or TrustedRoots when working directly with individual certificate or key objects.

Object Creation

obj.i = CkJavaKeyStore::ckCreate()

; Make sure to dispose of the object when finished like this:
CkJavaKeyStore::ckDispose(obj);

Properties

DebugLogFilePath
Declare.s ckDebugLogFilePath(obj.i)
Declare setCkDebugLogFilePath(obj.i, value.s)

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
LastErrorHtml
Declare.s ckLastErrorHtml(obj.i) ; (read-only)

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
Declare.s ckLastErrorText(obj.i) ; (read-only)

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
Declare.s ckLastErrorXml(obj.i) ; (read-only)

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
Declare.i ckLastMethodSuccess(obj.i)
Declare setCkLastMethodSuccess(obj.i, value.i)

Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
NumPrivateKeys
Declare.i ckNumPrivateKeys(obj.i) ; (read-only)
Introduced in version 9.5.0.44

Returns the number of private-key entries currently contained in the keystore. Each private-key entry has an alias, a protected private key, and an associated certificate chain. Valid private-key indexes range from 0 through NumPrivateKeys - 1.

Entry password versus store password: The private key in each entry may have its own password. That entry password is supplied to methods such as PrivateKeyAt and PrivateKeyOf. It is distinct from the store password used when loading or saving the complete keystore.

top
NumSecretKeys
Declare.i ckNumSecretKeys(obj.i) ; (read-only)
Introduced in version 9.5.0.66

Returns the number of symmetric secret-key entries currently contained in the keystore. Examples include AES, HMAC, Blowfish, and ChaCha key material. Valid indexes range from 0 through NumSecretKeys - 1.

JCEKS entry type: Secret-key entries require the JCEKS format; the traditional JKS format stores private-key entries and trusted-certificate entries but does not provide the JCEKS secret-key entry type.

More Information and Examples
top
NumTrustedCerts
Declare.i ckNumTrustedCerts(obj.i) ; (read-only)
Introduced in version 9.5.0.44

Returns the number of trusted-certificate entries currently contained in the keystore. Each entry contains one certificate and an alias. Valid indexes range from 0 through NumTrustedCerts - 1.

A trusted entry is not necessarily a root CA: Java keystores can mark any certificate as trusted. The application or Java security configuration decides what that trust means; this class does not validate the certificate merely because it is stored as a trusted-certificate entry.

top
RequireCompleteChain
Declare.i ckRequireCompleteChain(obj.i)
Declare setCkRequireCompleteChain(obj.i, value.i)
Introduced in version 9.5.0.44

Controls whether a complete certificate chain is required when a private-key entry is added. The default is 1. When enabled, AddPrivateKey and AddPfx fail if Chilkat cannot build the chain through the issuing certificates to a root or self-signed certificate. When set to 0, an incomplete chain may be stored.

Providing missing issuers: Call UseCertVault to make additional certificates available for chain construction. Trusted CA roots supplied through Chilkat trusted-root facilities may also help complete the chain. This property affects chain construction while adding entries; it does not perform application trust-policy validation.

top
VerboseLogging
Declare.i ckVerboseLogging(obj.i)
Declare setCkVerboseLogging(obj.i, value.i)

If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
VerifyKeyedDigest
Declare.i ckVerifyKeyedDigest(obj.i)
Declare setCkVerifyKeyedDigest(obj.i, value.i)
Introduced in version 9.5.0.44

Controls validation of the keystore-level integrity digest when a JKS or JCEKS store is loaded. The default is 1. In this mode, the load method uses the supplied store password to verify that the serialized keystore has not been altered and that the password is appropriate for the store.

When set to 0, a load method may be called with an empty password to read the store without validating its integrity digest.

Security consequence: Disabling this check does not decrypt private or secret key entries and does not make their entry passwords unnecessary. It only skips store-level integrity verification, so corrupted or modified keystore data may be accepted. Leave this property enabled for normal use.

top
Version
Declare.s ckVersion(obj.i) ; (read-only)

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AddPfx
Declare.i ckAddPfx(obj.i, pfx.i, alias.s, password.s)
Introduced in version 9.5.0.44

Imports the private-key entries found in the Pfx object supplied in pfx. One Java keystore private-key entry is added for each private key in the PFX/PKCS #12 data. Certificates from the PFX are used to construct the certificate chain associated with each imported private key.

alias specifies the alias for the first imported private key. If it is empty, Chilkat chooses an alias in this order:

  1. The subject common name of the private key certificate.
  2. The subject email address.
  3. The PKCS #12 friendly-name attribute.
  4. The certificate serial number.

If the PFX contains additional private keys, their aliases are chosen automatically using the same preference order. password becomes the entry password used to protect the imported private keys in the Java keystore.

What is not imported: Standalone certificates from the PFX are not added as trusted-certificate entries. They are used only as possible members of private-key certificate chains. Call AddTrustedCert separately when a certificate should become an explicit trust entry.
Chain completion: If RequireCompleteChain is enabled, the method fails when any imported private-key chain cannot be completed. UseCertVault can supply additional issuer certificates.

Returns 1 for success, 0 for failure.

top
AddPrivateKey
Declare.i ckAddPrivateKey(obj.i, cert.i, alias.s, password.s)
Introduced in version 9.5.0.44

Adds a private-key entry using the certificate in cert. The Cert object must have access to its associated private key. Chilkat obtains that private key and builds the certificate chain beginning with cert.

alias is the entry alias. If it is empty, Chilkat chooses an alias from the certificate in this order: subject common name, subject email address, then certificate serial number. password is the password used to protect the private key inside the keystore.

Certificate-chain construction: A private-key entry consists of the private key and the certificate chain for its public key. If RequireCompleteChain is enabled, the method fails unless the chain can be completed. Use UseCertVault when additional issuer certificates are needed.

Returns 1 for success, 0 for failure.

top
AddSecretKey
Declare.i ckAddSecretKey(obj.i, encodedKeyBytes.s, encoding.s, algorithm.s, alias.s, password.s)
Introduced in version 9.5.0.66

Adds a symmetric secret-key entry. encodedKeyBytes contains the key bytes encoded as text, and encoding names that encoding, such as hex, base64, or base64url. algorithm records the key algorithm, such as AES, HMAC_SHA256, or CHACHA. alias is the alias, and password is the password used to protect the key entry.

The algorithm value describes the intended key type; it does not transform the supplied key bytes. For example, a 128-bit AES key is supplied as exactly 16 decoded bytes and algorithm is set to AES.

JCEKS and legacy protection: Secret-key entries are stored in JCEKS form. JCEKS protects these entries using the legacy PBEWithMD5AndTripleDES mechanism defined by that format. Use this format when interoperability requires JCEKS, and protect both the entry password and the resulting file.

Returns 1 for success, 0 for failure.

More Information and Examples
top
AddTrustedCert
Declare.i ckAddTrustedCert(obj.i, cert.i, alias.s)
Introduced in version 9.5.0.44

Adds cert as a trusted-certificate entry under the alias supplied in alias. A trusted-certificate entry contains the certificate only; it does not contain a private key or a certificate chain.

Trust is an application decision: Adding a certificate as a trusted entry does not prove that it is a CA certificate, validate its signature chain, or make it universally trustworthy. It records that the keystore owner intends to trust that certificate for the consuming Java application or security configuration.

Returns 1 for success, 0 for failure.

More Information and Examples
top
CertChainAt
Declare.i ckCertChainAt(obj.i, index.l, certChain.i)
Introduced in version 11.0.0

Loads certChain with the certificate chain belonging to the private-key entry at zero-based index index. Valid indexes range from 0 through NumPrivateKeys - 1. The first certificate contains the public key corresponding to the private key; subsequent certificates are issuing certificates toward the root.

Returns 1 for success, 0 for failure.

top
CertChainOf
Declare.i ckCertChainOf(obj.i, alias.s, caseSensitive.l, certChain.i)
Introduced in version 11.0.0

Searches the private-key entries for an alias equal to alias and loads the matching certificate chain into certChain. caseSensitive controls whether the alias comparison is case-sensitive. The first certificate in the chain contains the public key corresponding to the private key.

Alias case behavior: Java keystore implementations can differ in alias case handling. Avoid aliases that differ only by case, even when a case-sensitive search is requested.

Returns 1 for success, 0 for failure.

top
ChangePassword
Declare.i ckChangePassword(obj.i, index.l, oldPassword.s, newPassword.s)
Introduced in version 9.5.0.44

Changes the password protecting the private-key entry at index. Indexing begins at 0. oldPassword is the current entry password and newPassword is the new entry password.

This does not change only the store password: A JKS/JCEKS store can have one password for store-level integrity and separate passwords for private-key entries. To migrate a keystore from one common password to another, load it using the old store password, call this method for each private-key entry that uses the old password, and then save the store using the new store password.

Returns 1 for success, 0 for failure.

More Information and Examples
top
GetPrivateKeyAlias
Declare.s ckGetPrivateKeyAlias(obj.i, index.l)
Introduced in version 9.5.0.44

Returns the alias of the private-key entry at index. Indexing begins at 0; valid indexes range from 0 through NumPrivateKeys - 1.

Alias portability: Java keystore alias case behavior is implementation-dependent. Avoid creating aliases that differ only by letter case, even when a Chilkat lookup method offers a case-sensitive option.

Returns an empty string on failure. Use the LastMethodSuccess property to check for success.

top
GetSecretKey
Declare.s ckGetSecretKey(obj.i, password.s, index.l, encoding.s)
Introduced in version 9.5.0.66

Recovers the secret-key entry at index and returns its raw key bytes encoded using encoding. password is the password protecting that entry. Indexing begins at 0. Common output encodings include hex, base64, and base64url.

Secret material: The returned text represents the actual symmetric key bytes, not a fingerprint or identifier. Avoid writing it to logs, command histories, or unprotected configuration files.

Returns an empty string on failure. Use the LastMethodSuccess property to check for success.

More Information and Examples
top
GetSecretKeyAlias
Declare.s ckGetSecretKeyAlias(obj.i, index.l)
Introduced in version 9.5.0.66

Returns the alias of the secret-key entry at index. Indexing begins at 0; valid indexes range from 0 through NumSecretKeys - 1.

Returns an empty string on failure. Use the LastMethodSuccess property to check for success.

More Information and Examples
top
GetTrustedCertAlias
Declare.s ckGetTrustedCertAlias(obj.i, index.l)
Introduced in version 9.5.0.44

Returns the alias of the trusted-certificate entry at index. Indexing begins at 0; valid indexes range from 0 through NumTrustedCerts - 1.

Returns an empty string on failure. Use the LastMethodSuccess property to check for success.

top
LoadBd
Declare.i ckLoadBd(obj.i, password.s, bd.i)
Introduced in version 9.5.0.77

Loads a JKS or JCEKS keystore from the bytes currently contained in bd. password is the store password used for integrity verification when VerifyKeyedDigest is enabled.

Returns 1 for success, 0 for failure.

top
LoadEncoded
Declare.i ckLoadEncoded(obj.i, password.s, jksEncData.s, encoding.s)
Introduced in version 9.5.0.44

Decodes the serialized keystore text in jksEncData using the binary encoding named by encoding, then loads the resulting JKS or JCEKS bytes. Common encodings include base64, hex, and base64url. password is the store password used for integrity verification.

Encoding is not encryption: encoding describes how binary keystore bytes are represented as text. It does not name the keystore format and does not protect the data cryptographically.

Returns 1 for success, 0 for failure.

top
LoadFile
Declare.i ckLoadFile(obj.i, password.s, path.s)
Introduced in version 9.5.0.44

Loads a JKS or JCEKS keystore from the file at path. password is the store password used to verify the keystore-level integrity digest when VerifyKeyedDigest is enabled.

JKS/JCEKS versus PKCS #12: This class is intended for the proprietary Java JKS and JCEKS formats. Modern Java releases use PKCS #12 as the default keystore type; use Chilkat.Pfx when the input is a .p12 or .pfx container.

Returns 1 for success, 0 for failure.

More Information and Examples
top
LoadJwkSet
Declare.i ckLoadJwkSet(obj.i, password.s, jwkSet.i)
Introduced in version 9.5.0.66

Loads supported keys from the JWK Set in jwkSet into this Java keystore object. password is used to protect imported private-key and secret-key entries. A JWK kid value is used as the entry alias when present.

JWK Set structure: jwkSet should contain a JSON object with a keys array. Private asymmetric JWKs contain private parameters such as d; symmetric JWKs use kty: "oct" and contain the key in k. Treat the input as sensitive whenever it contains private or symmetric key material.

Returns 1 for success, 0 for failure.

top
PrivateKeyAt
Declare.i ckPrivateKeyAt(obj.i, password.s, index.l, privKey.i)
Introduced in version 11.0.0

Loads privKey with the private key at zero-based index index. password is the password protecting that private-key entry. Valid indexes range from 0 through NumPrivateKeys - 1.

Use the entry password: password is not necessarily the password used to load the complete keystore. Java keystores can protect each private-key entry with a different password.

Returns 1 for success, 0 for failure.

top
PrivateKeyOf
Declare.i ckPrivateKeyOf(obj.i, password.s, alias.s, caseSensitive.l, privKey.i)
Introduced in version 11.0.0

Searches the private-key entries for an alias equal to alias and loads the matching private key into privKey. password is the password protecting that key entry, and caseSensitive controls whether the alias comparison is case-sensitive.

Returns 1 for success, 0 for failure.

top
RemoveEntry
Declare.i ckRemoveEntry(obj.i, entryType.l, index.l)
Introduced in version 9.5.0.44

Removes an entry selected by type and zero-based index. entryType uses the following documented values:

entryTypeEntry selected
1Trusted-certificate entry; index indexes the trusted-certificate list.
2Private-key entry; index indexes the private-key list.

index is an index within the selected entry category, not an index across all entries in the keystore.

In-memory change: This method modifies the current object. Call ToFile, ToBd, or another To* method to persist the updated keystore.

Returns 1 for success, 0 for failure.

top
SetAlias
Declare.i ckSetAlias(obj.i, entryType.l, index.l, alias.s)
Introduced in version 9.5.0.44

Changes the alias of an entry selected by type and zero-based index. entryType uses the following documented values:

entryTypeEntry selected
1Trusted-certificate entry; index indexes the trusted-certificate list.
2Private-key entry; index indexes the private-key list.

alias is the new alias. The index is relative to the selected category, not the complete set of keystore entries.

Choose aliases carefully: Aliases identify entries within a Java keystore, but case sensitivity is implementation-dependent. Use unique aliases that do not differ only by case.

Returns 1 for success, 0 for failure.

top
ToBd
Declare.i ckToBd(obj.i, password.s, bd.i)
Introduced in version 11.0.0

Serializes the current Java keystore and writes the complete JKS or JCEKS file image to bd. password is the store password used to create the keystore-level integrity digest.

Separate entry protection: Private-key and secret-key entries may have passwords distinct from the store password. Serializing with password does not automatically change those entry passwords.

Returns 1 for success, 0 for failure.

top
ToEncodedString
Declare.s ckToEncodedString(obj.i, password.s, encoding.s)
Introduced in version 9.5.0.44

Serializes the current Java keystore and returns the resulting JKS or JCEKS bytes encoded as text using encoding. Common choices include base64, hex, and base64url. password is the store password used to create the keystore-level integrity digest.

Returns an empty string on failure. Use the LastMethodSuccess property to check for success.

top
ToFile
Declare.i ckToFile(obj.i, password.s, path.s)
Introduced in version 9.5.0.44

Serializes the current Java keystore and writes the complete JKS or JCEKS file image to path. password is the store password used to create the keystore-level integrity digest.

Password roles: The store password may differ from the passwords protecting individual private-key or secret-key entries. Saving with a new store password does not automatically change each entry password; use ChangePassword for private-key entries when needed.

Returns 1 for success, 0 for failure.

top
ToJwkSet
Declare.i ckToJwkSet(obj.i, password.s, sbJwkSet.i)
Introduced in version 9.5.0.66

Exports supported private-key and secret-key entries as a JSON Web Key Set and writes the JSON to sbJwkSet. password is used to recover protected key entries. Each exported JWK receives a kid value derived from the keystore alias.

Sensitive output: A JWK containing private parameter d or symmetric parameter k contains secret key material. Protect the resulting JSON as carefully as the original keystore and do not log or transmit it over an untrusted channel.

Returns 1 for success, 0 for failure.

top
ToPemObj
Declare.i ckToPemObj(obj.i, password.s, pemObj.i)
Introduced in version 11.0.0

Converts the keystore material supported by the Pem class and loads the result into pemObj. password is used to decrypt private-key entries during the conversion.

Output handling: The destination Pem object receives the converted material in memory. Its own export methods and properties control whether later PEM text is encrypted and how it is formatted. Treat unencrypted private-key PEM as sensitive data.

Returns 1 for success, 0 for failure.

top
ToPfxObj
Declare.i ckToPfxObj(obj.i, password.s, pfxObj.i)
Introduced in version 11.0.0

Converts the certificates and recoverable private-key entries in the Java keystore and loads the result into pfxObj. password is used to decrypt private-key entries during the conversion.

Conversion password: password is an input password for recovering the Java keystore entries. It is not automatically the password that will protect a later serialized PFX; use the destination Pfx export method to choose the PFX password.

Returns 1 for success, 0 for failure.

top
TrustedCertAt
Declare.i ckTrustedCertAt(obj.i, index.l, cert.i)
Introduced in version 11.0.0

Loads cert with the trusted certificate at the zero-based index index. Valid indexes range from 0 through NumTrustedCerts - 1. The returned certificate is the single certificate stored in the trust entry and has no private key merely by virtue of being trusted.

Returns 1 for success, 0 for failure.

top
TrustedCertOf
Declare.i ckTrustedCertOf(obj.i, alias.s, caseSensitive.l, cert.i)
Introduced in version 11.0.0

Searches the trusted-certificate entries for an alias equal to alias and loads the matching certificate into cert. caseSensitive controls whether the alias comparison is case-sensitive.

Returns 1 for success, 0 for failure.

top
UseCertVault
Declare.i ckUseCertVault(obj.i, vault.i)
Introduced in version 9.5.0.44

Adds vault to the certificate sources Chilkat searches when it needs issuer certificates to build a private-key certificate chain. This is useful before calling AddPrivateKey or AddPfx when the supplied certificate material does not already contain the complete chain.

Lookup source only: Registering a certificate vault does not copy all vault certificates into the Java keystore and does not create trusted-certificate entries. Certificates are consulted only as candidates for chain construction.

Returns 1 for success, 0 for failure.

top

Deprecated

FindCertChain
Declare.i ckFindCertChain(obj.i, alias.s, caseSensitive.l)
Introduced in version 9.5.0.44
This method is deprecated and replaced by CertChainOf

Deprecated: Use CertChainOf instead.

Returns the certificate chain belonging to the private-key entry whose alias matches alias. caseSensitive controls whether alias matching is case-sensitive. The first certificate in the returned chain corresponds to the private key.

Returns 0 on failure

top
FindPrivateKey
Declare.i ckFindPrivateKey(obj.i, password.s, alias.s, caseSensitive.l)
Introduced in version 9.5.0.44
This method is deprecated and replaced by PrivateKeyOf

Deprecated: Use PrivateKeyOf instead.

Returns the private key belonging to the entry whose alias matches alias. password is the entry password used to recover the key, and caseSensitive controls whether alias matching is case-sensitive.

Returns 0 on failure

top
FindTrustedCert
Declare.i ckFindTrustedCert(obj.i, alias.s, caseSensitive.l)
Introduced in version 9.5.0.44
This method is deprecated and replaced by TrustedCertOf

Deprecated: Use TrustedCertOf instead.

Returns the trusted certificate whose alias matches alias. caseSensitive controls whether alias matching is case-sensitive.

Returns 0 on failure

top
GetCertChain
Declare.i ckGetCertChain(obj.i, index.l)
Introduced in version 9.5.0.44
This method is deprecated and replaced by CertChainAt

Deprecated: Use CertChainAt instead.

Returns the certificate chain for the private-key entry at index. Indexing begins at 0, and the valid range is determined by NumPrivateKeys. The first certificate in the chain corresponds to the private key.

Returns 0 on failure

top
GetPrivateKey
Declare.i ckGetPrivateKey(obj.i, password.s, index.l)
Introduced in version 9.5.0.44
This method is deprecated and replaced by PrivateKeyAt

Deprecated: Use PrivateKeyAt instead.

Returns the private key for the entry at index. password is the password protecting that private-key entry. Indexing begins at 0.

Returns 0 on failure

top
GetTrustedCert
Declare.i ckGetTrustedCert(obj.i, index.l)
Introduced in version 9.5.0.44
This method is deprecated and replaced by TrustedCertAt

Deprecated: Use TrustedCertAt instead.

Returns the trusted certificate at index. Indexing begins at 0; the valid range is determined by NumTrustedCerts.

Returns 0 on failure

top
ToPem
Declare.i ckToPem(obj.i, password.s)
Introduced in version 9.5.0.49
This method is deprecated and replaced by ToPemObj

Deprecated: Use ToPemObj instead.

Converts the keystore material supported by the Pem class and returns a new Pem object. password is used to decrypt private-key entries during the conversion.

Private-key password: The supplied password must recover the private-key entries included in the conversion. The returned Pem object is an in-memory representation; its later serialization settings determine how PEM output is formatted or encrypted.

Returns 0 on failure

top
ToPfx
Declare.i ckToPfx(obj.i, password.s)
Introduced in version 9.5.0.49
This method is deprecated and replaced by ToPfxObj

Deprecated: Use ToPfxObj instead.

Converts the certificates and recoverable private-key entries in the Java keystore to a new Pfx object. password is used to decrypt the private-key entries being exported.

Returns 0 on failure

top