Class ZebraP12Info

Object
com.zebra.sdk.certificate.ZebraP12Info

public class ZebraP12Info extends Object
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 Details

    • ZebraP12Info

      public ZebraP12Info(InputStream pkcs12Stream, String p12Password) throws ZebraCertificateException
      Creates a wrapper that opens up the provided certificate keystore stream.
      Parameters:
      pkcs12Stream - the stream containing certificate keystore file contents
      p12Password - 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

      public List<String> getAliases() throws ZebraCertificateException
      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

      public String getCertificateContent() throws ZebraCertificateException
      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

      public String getCertificateContent(String alias) throws ZebraCertificateException
      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

      public String getCaContent() throws ZebraCertificateException
      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

      public String getCaContent(String alias) throws ZebraCertificateException
      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 key
      p12Password - 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 file
      passwordToEncryptKey - the password used to encrypt the resulting private key
      p12Password - 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

      public String getCertificateCommonName() throws ZebraCertificateException
      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

      public String getCertificateCommonName(String alias) throws ZebraCertificateException
      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

      public String getCaCommonName() throws ZebraCertificateException
      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

      public String getCaCommonName(String alias) throws ZebraCertificateException
      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

      public Date getCertificateExpirationDate() throws ZebraCertificateException
      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

      public Date getCertificateExpirationDate(String alias) throws ZebraCertificateException
      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

      public Date getCaExpirationDate() throws ZebraCertificateException
      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

      public Date getCaExpirationDate(String alias) throws ZebraCertificateException
      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

      public String getPrivateKeyAlgorithm(String p12Password) throws ZebraCertificateException
      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 file
      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.
    • getPrivateKeyFormat

      public String getPrivateKeyFormat(String p12Password) throws ZebraCertificateException
      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 file
      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.
    • getCertificateIssuer

      public String getCertificateIssuer() throws ZebraCertificateException
      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

      public String getCertificateIssuer(String alias) throws ZebraCertificateException
      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

      public KeyStore getKeyStore()
      Get the keystore of the processed client certificate.
      Returns:
      the java keystore containing information about the processed certificate file