public class ZebraP12Info extends Object
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 and Description |
---|
ZebraP12Info(InputStream pkcs12Stream,
String p12Password)
Creates a wrapper that opens up the provided certificate keystore stream.
|
Modifier and Type | Method and Description |
---|---|
List<String> |
getAliases()
Get a list of aliases present in the certificate keystore.
|
String |
getCaCommonName()
Get the common name of the CA associated with the certificate file.
|
String |
getCaCommonName(String alias)
Get the common name of the CA associated with the certificate file.
|
String |
getCaContent()
Get the content of the ca, which is determined to be all entries in the certificate chain after the first entry.
|
String |
getCaContent(String alias)
Get the content of the ca, which is determined to be all entries in the certificate chain after the first entry.
|
Date |
getCaExpirationDate()
Get the expiration data of the CA associated with the certificate file.
|
Date |
getCaExpirationDate(String alias)
Get the expiration data of the CA associated with the certificate file.
|
String |
getCertificateCommonName()
Get the common name of the client certificate associated with the certificate file.
|
String |
getCertificateCommonName(String alias)
Get the common name of the client certificate associated with the certificate file.
|
String |
getCertificateContent()
Get the content of the first entry in the certificate's certificate chain.
|
String |
getCertificateContent(String alias)
Get the content of the first entry in the certificate's certificate chain.
|
Date |
getCertificateExpirationDate()
Get the expiration data of the client certificate associated with the certificate file.
|
Date |
getCertificateExpirationDate(String alias)
Get the expiration data of the client certificate associated with the certificate file.
|
String |
getCertificateIssuer()
Get the issuer of the client certificate.
|
String |
getCertificateIssuer(String alias)
Get the issuer of the client certificate.
|
String |
getEncryptedPrivateKeyContent(String passwordToEncryptKey,
String p12Password)
Get the encrypted private key content.
|
String |
getEncryptedPrivateKeyContent(String alias,
String passwordToEncryptKey,
String p12Password)
Get the encrypted private key content.
|
KeyStore |
getKeyStore()
Get the keystore of the processed client certificate.
|
String |
getPrivateKeyAlgorithm(String p12Password)
Get the algorithm used by the private key.
|
String |
getPrivateKeyAlgorithm(String alias,
String p12Password)
Get the algorithm used by the private key.
|
String |
getPrivateKeyFormat(String p12Password)
Get the format of the private key.
|
String |
getPrivateKeyFormat(String alias,
String p12Password)
Get the format of the private key.
|
public ZebraP12Info(InputStream pkcs12Stream, String p12Password) throws ZebraCertificateException
pkcs12Stream
- the stream containing certificate keystore file contentsp12Password
- the password used to access the certificate fileZebraCertificateException
- thrown if the certificate stream contents cannot be accessed or if the
certificate password was incorrectpublic List<String> getAliases() throws ZebraCertificateException
ZebraCertificateException
- if the keystore has not been initialized (loaded)public String getCertificateContent() throws ZebraCertificateException
ZebraCertificateException
- If the provided alias does not exist or the certificate content is corruptpublic String getCertificateContent(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- If the provided alias does not exist or the certificate content is corruptpublic String getCaContent() throws ZebraCertificateException
ZebraCertificateException
- If the provided alias does not exist or the ca content is corruptpublic String getCaContent(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- If the provided alias does not exist or the ca content is corruptpublic String getEncryptedPrivateKeyContent(String passwordToEncryptKey, String p12Password) throws ZebraCertificateException
passwordToEncryptKey
- the password used to encrypt the resulting private keyp12Password
- the password used to access the certificate fileZebraCertificateException
- If the provided alias does not exist, certificate password is incorrect, or
private key content is corruptpublic String getEncryptedPrivateKeyContent(String alias, String passwordToEncryptKey, String p12Password) throws ZebraCertificateException
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 fileZebraCertificateException
- If the provided alias does not exist, certificate password is incorrect, or
private key content is corruptpublic String getCertificateCommonName() throws ZebraCertificateException
ZebraCertificateException
- If the provided alias does not exist or the underlying certificate could not be
parsed.public String getCertificateCommonName(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- If the provided alias does not exist or the underlying certificate could not be
parsed.public String getCaCommonName() throws ZebraCertificateException
ZebraCertificateException
- If the provided alias does not exist or the underlying certificate could not be
parsed.public String getCaCommonName(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- If the provided alias does not exist or the underlying certificate could not be
parsed.public Date getCertificateExpirationDate() throws ZebraCertificateException
ZebraCertificateException
- if the certificate keystore is invalid or the provided alias does not exist in
the certificate keystore.public Date getCertificateExpirationDate(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- if the certificate keystore is invalid or the provided alias does not exist in
the certificate keystore.public Date getCaExpirationDate() throws ZebraCertificateException
ZebraCertificateException
- if the certificate keystore is invalid or the provided alias does not exist in
the certificate keystore.public Date getCaExpirationDate(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- if the certificate keystore is invalid or the provided alias does not exist in
the certificate keystore.public String getPrivateKeyAlgorithm(String p12Password) throws ZebraCertificateException
p12Password
- the password used to access the certificate fileZebraCertificateException
- if the provided certificate file is invalid, alias does not exist, or contained
private key is corrupt or unsupported.public String getPrivateKeyAlgorithm(String alias, String p12Password) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate filep12Password
- the password used to access the certificate fileZebraCertificateException
- if the provided certificate file is invalid, alias does not exist, or contained
private key is corrupt or unsupported.public String getPrivateKeyFormat(String p12Password) throws ZebraCertificateException
p12Password
- the password used to access the certificate fileZebraCertificateException
- if the provided certificate file is invalid, alias does not exist, or contained
private key is corrupt or unsupported.public String getPrivateKeyFormat(String alias, String p12Password) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate filep12Password
- the password used to access the certificate fileZebraCertificateException
- if the provided certificate file is invalid, alias does not exist, or contained
private key is corrupt or unsupported.public String getCertificateIssuer() throws ZebraCertificateException
ZebraCertificateException
- if the provided certificate file is invalid, alias does not exist, or contained
private key is corrupt or unsupported.public String getCertificateIssuer(String alias) throws ZebraCertificateException
alias
- the alias name of the specific entry to extract from the certificate fileZebraCertificateException
- if the provided certificate file is invalid, alias does not exist, or contained
private key is corrupt or unsupported.public KeyStore getKeyStore()
© 2015 ZIH Corp. All Rights Reserved.