Package com.zebra.sdk.certificate
Class ZebraP12Info
Object
com.zebra.sdk.certificate.ZebraP12Info
A utility class used to extract info from certificate files and convert the contents into Zebra friendly formats.
package test.zebra.sdk.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.zebra.sdk.certificate.ZebraCertificateException;
import com.zebra.sdk.certificate.ZebraCertificateInfo;
import com.zebra.sdk.certificate.ZebraP12Info;
import com.zebra.sdk.comm.DriverPrinterConnection;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLinkOs;
public class ZebraP12InfoExample {
public static void main(String[] args) throws Exception {
InputStream p12InputStream = null;
ZebraP12Info zebraP12Info = null;
String p12FilePassword = "P12_PASSWORD";
String privateKeyEncryptionPassword = "1234";
try {
p12InputStream = new FileInputStream("/path/to/file.p12");
zebraP12Info = new ZebraP12Info(p12InputStream, p12FilePassword);
} catch (FileNotFoundException e) {
System.out.println("Could not find the p12 file.");
} catch (ZebraCertificateException e) {
System.out.println("Failed to extract contents from the p12 file. Make sure the provided p12 file and password are valid.");
} finally {
if (p12InputStream != null) {
try {
p12InputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DriverPrinterConnection conn = null;
try {
String caContent = zebraP12Info.getCaContent();
String clientCertificateContent = zebraP12Info.getCertificateContent();
String encryptedPrivateKeyContent = zebraP12Info.getEncryptedPrivateKeyContent(privateKeyEncryptionPassword, p12FilePassword);
conn = new DriverPrinterConnection("myPrinterDriverName");
conn.open();
ZebraPrinterLinkOs linkosPrinter = ZebraPrinterFactory.getLinkOsPrinter(conn);
linkosPrinter.storeFileOnPrinter(clientCertificateContent.getBytes(), ZebraCertificateInfo.CLIENT_CERT_NRD_PRINTER_FILE_NAME);
linkosPrinter.storeFileOnPrinter(caContent.getBytes(), ZebraCertificateInfo.CA_CERT_NRD_PRINTER_FILE_NAME);
linkosPrinter.storeFileOnPrinter(encryptedPrivateKeyContent.getBytes(), ZebraCertificateInfo.CLIENT_PRIVATE_KEY_NRD_PRINTER_FILE_NAME);
linkosPrinter.setSetting("wlan.private_key_password", privateKeyEncryptionPassword);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
-
Constructor Summary
ConstructorsConstructorDescriptionZebraP12Info(InputStream pkcs12Stream, String p12Password) Creates a wrapper that opens up the provided certificate keystore stream. -
Method Summary
Modifier and TypeMethodDescriptionGet a list of aliases present in the certificate keystore.Get the common name of the CA associated with the certificate file.getCaCommonName(String alias) Get the common name of the CA associated with the certificate file.Get the content of the ca, which is determined to be all entries in the certificate chain after the first entry.getCaContent(String alias) Get the content of the ca, which is determined to be all entries in the certificate chain after the first entry.Get the expiration data of the CA associated with the certificate file.getCaExpirationDate(String alias) Get the expiration data of the CA associated with the certificate file.Get the common name of the client certificate associated with the certificate file.getCertificateCommonName(String alias) Get the common name of the client certificate associated with the certificate file.Get the content of the first entry in the certificate's certificate chain.getCertificateContent(String alias) Get the content of the first entry in the certificate's certificate chain.Get the expiration data of the client certificate associated with the certificate file.Get the expiration data of the client certificate associated with the certificate file.Get the issuer of the client certificate.getCertificateIssuer(String alias) Get the issuer of the client certificate.getEncryptedPrivateKeyContent(String passwordToEncryptKey, String p12Password) Get the encrypted private key content.getEncryptedPrivateKeyContent(String alias, String passwordToEncryptKey, String p12Password) Get the encrypted private key content.Get the keystore of the processed client certificate.getPrivateKeyAlgorithm(String p12Password) Get the algorithm used by the private key.getPrivateKeyAlgorithm(String alias, String p12Password) Get the algorithm used by the private key.getPrivateKeyFormat(String p12Password) Get the format of the private key.getPrivateKeyFormat(String alias, String p12Password) Get the format of the private key.
-
Constructor Details
-
ZebraP12Info
Creates a wrapper that opens up the provided certificate keystore stream.- Parameters:
pkcs12Stream- the stream containing certificate keystore file contentsp12Password- the password used to access the certificate file- Throws:
ZebraCertificateException- thrown if the certificate stream contents cannot be accessed or if the certificate password was incorrect
-
-
Method Details
-
getAliases
Get a list of aliases present in the certificate keystore.- Returns:
- a list of the aliases present in the certificate keystore
- Throws:
ZebraCertificateException- if the keystore has not been initialized (loaded)
-
getCertificateContent
Get the content of the first entry in the certificate's certificate chain. The returned content is converted to PEM format. The resulting content can be stored to a printer for use with wireless security. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the Zebra-friendly certificate content
- Throws:
ZebraCertificateException- if the provided alias does not exist or the certificate content is corrupt
-
getCertificateContent
Get the content of the first entry in the certificate's certificate chain. The returned content is converted to PEM format. The resulting content can be stored to a printer for use with wireless security.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the Zebra-friendly certificate content
- Throws:
ZebraCertificateException- if the provided alias does not exist or the certificate content is corrupt
-
getCaContent
Get the content of the ca, which is determined to be all entries in the certificate chain after the first entry. The returned ca is converted to PEM format. The resulting content can be stored to a printer for use with wireless security. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the Zebra-friendly ca content
- Throws:
ZebraCertificateException- if the provided alias does not exist or the ca content is corrupt
-
getCaContent
Get the content of the ca, which is determined to be all entries in the certificate chain after the first entry. The returned ca is converted to PEM format. The resulting content can be stored to a printer for use with wireless security.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the Zebra-friendly ca content
- Throws:
ZebraCertificateException- if the provided alias does not exist or the ca content is corrupt
-
getEncryptedPrivateKeyContent
public String getEncryptedPrivateKeyContent(String passwordToEncryptKey, String p12Password) throws ZebraCertificateException Get the encrypted private key content. The returned key is encrypted and converted to PEM format. The resulting content can be stored to a printer for use with wireless security. Since no alias is provided, the first entry in the certificate file will be used. Note: Any printer using the generated private key must also be configured to use the password provided here to encrypt the private key. Send the following command to the printer to configure it to use a private key encryption password: ! U1 setvar "wlan.private_key_password" "value".- Parameters:
passwordToEncryptKey- the password used to encrypt the resulting private keyp12Password- the password used to access the certificate file- Returns:
- the encrypted private key in PEM format
- Throws:
ZebraCertificateException- if the provided alias does not exist, certificate password is incorrect, or private key content is corrupt
-
getEncryptedPrivateKeyContent
public String getEncryptedPrivateKeyContent(String alias, String passwordToEncryptKey, String p12Password) throws ZebraCertificateException Get the encrypted private key content. The returned key is encrypted and converted to PEM format. The resulting content can be stored to a printer for use with wireless security. Note: Any printer using the generated private key must also be configured to use the provided password to encrypt the private key. Send the following command to the printer to configure it to use a private key encryption password: ! U1 setvar "wlan.private_key_password" "value".- Parameters:
alias- the alias name of the specific entry to extract from the certificate filepasswordToEncryptKey- the password used to encrypt the resulting private keyp12Password- the password used to access the certificate file- Returns:
- the encrypted private key in PEM format
- Throws:
ZebraCertificateException- if the provided alias does not exist, certificate password is incorrect, or private key content is corrupt
-
getCertificateCommonName
Get the common name of the client certificate associated with the certificate file. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the certificate common name
- Throws:
ZebraCertificateException- if the provided alias does not exist or the underlying certificate could not be parsed.
-
getCertificateCommonName
Get the common name of the client certificate associated with the certificate file.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the certificate common name
- Throws:
ZebraCertificateException- if the provided alias does not exist or the underlying certificate could not be parsed.
-
getCaCommonName
Get the common name of the CA associated with the certificate file. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the CA common name
- Throws:
ZebraCertificateException- if the provided alias does not exist or the underlying certificate could not be parsed.
-
getCaCommonName
Get the common name of the CA associated with the certificate file.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the CA common name
- Throws:
ZebraCertificateException- if the provided alias does not exist or the underlying certificate could not be parsed.
-
getCertificateExpirationDate
Get the expiration data of the client certificate associated with the certificate file. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the client certificate expiration date
- Throws:
ZebraCertificateException- if the certificate keystore is invalid or the provided alias does not exist in the certificate keystore.
-
getCertificateExpirationDate
Get the expiration data of the client certificate associated with the certificate file.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the client certificate expiration date
- Throws:
ZebraCertificateException- if the certificate keystore is invalid or the provided alias does not exist in the certificate keystore.
-
getCaExpirationDate
Get the expiration data of the CA associated with the certificate file. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the CA expiration date
- Throws:
ZebraCertificateException- if the certificate keystore is invalid or the provided alias does not exist in the certificate keystore.
-
getCaExpirationDate
Get the expiration data of the CA associated with the certificate file.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the CA expiration date
- Throws:
ZebraCertificateException- if the certificate keystore is invalid or the provided alias does not exist in the certificate keystore.
-
getPrivateKeyAlgorithm
Get the algorithm used by the private key. Since no alias is provided, the first entry in the certificate file will be used.- Parameters:
p12Password- the password used to access the certificate file- Returns:
- the private key algorithm
- Throws:
ZebraCertificateException- if the provided certificate file is invalid, alias does not exist, or contained private key is corrupt or unsupported.
-
getPrivateKeyAlgorithm
public String getPrivateKeyAlgorithm(String alias, String p12Password) throws ZebraCertificateException Get the algorithm used by the private key.- Parameters:
alias- the alias name of the specific entry to extract from the certificate filep12Password- the password used to access the certificate file- Returns:
- the private key algorithm
- Throws:
ZebraCertificateException- if the provided certificate file is invalid, alias does not exist, or contained private key is corrupt or unsupported.
-
getPrivateKeyFormat
Get the format of the private key. Since no alias is provided, the first entry in the certificate file will be used.- Parameters:
p12Password- the password used to access the certificate file- Returns:
- the private key format
- Throws:
ZebraCertificateException- if the provided certificate file is invalid, alias does not exist, or contained private key is corrupt or unsupported.
-
getPrivateKeyFormat
public String getPrivateKeyFormat(String alias, String p12Password) throws ZebraCertificateException Get the format of the private key.- Parameters:
alias- the alias name of the specific entry to extract from the certificate filep12Password- the password used to access the certificate file- Returns:
- the private key format
- Throws:
ZebraCertificateException- if the provided certificate file is invalid, alias does not exist, or contained private key is corrupt or unsupported.
-
getCertificateIssuer
Get the issuer of the client certificate. Since no alias is provided, the first entry in the certificate file will be used.- Returns:
- the client certificate issuer
- Throws:
ZebraCertificateException- if the provided certificate file is invalid, alias does not exist, or contained private key is corrupt or unsupported.
-
getCertificateIssuer
Get the issuer of the client certificate.- Parameters:
alias- the alias name of the specific entry to extract from the certificate file- Returns:
- the client certificate issuer
- Throws:
ZebraCertificateException- if the provided certificate file is invalid, alias does not exist, or contained private key is corrupt or unsupported.
-
getKeyStore
Get the keystore of the processed client certificate.- Returns:
- the java keystore containing information about the processed certificate file
-