Click or drag to resize

CertificateParser Class

Takes in a certificate file (P12, DER, PEM, etc) and processes it into a ZebraCertificateInfo object which contains the selected certificate, Certificate Authority certificate chain, and private key (if applicable).
Inheritance Hierarchy
SystemObject
  Zebra.Sdk.CertificateCertificateParser

Namespace:  Zebra.Sdk.Certificate
Assembly:  SdkApi_Core (in SdkApi_Core.dll) Version: 2.14.1989
Syntax
public class CertificateParser

The CertificateParser type exposes the following members.

Constructors
  NameDescription
Public methodCertificateParser
Initializes a new instance of the CertificateParser class
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodStatic memberParseCertificate
Takes in a certificate file (P12, DER, PEM, etc) and processes it into a ZebraCertificateInfo object which contains the selected certificate, Certificate Authority certificate chain, and private key (if applicable).
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Examples
using System;
using System.IO;
using Zebra.Sdk.Certificate;
using System.Text;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;

class CertificateParserExample {

    public static string p12File = "C:\\Users\\[user]\\Documents\\certificate.p12";
    private static string password = "passwordForCertFile";

    public static string PRIV_KEY_BASE_FILE_NAME = "PRIVKEY.NRD";
    public static string CA_CERT_BASE_FILE_NAME = "CACERTSV.NRD";
    public static string CLIENT_CERT_BASE_FILE_NAME = "CERTCLN.NRD";

    public static void Main(string[] args) {
        FileInfo certFile = new FileInfo(p12File);
        if (!(certFile.Exists)) {
            Console.WriteLine("The provided certificate file (" + p12File + ") cannot be found.");
            return;
        }

        Console.WriteLine("Accessing certificate file {0}", certFile.FullName);

        Connection connection = new TcpConnection("1.2.3.4", 9100);
        ZebraPrinterLinkOs printer = null;

        try {
            connection.Open();
            printer = ZebraPrinterFactory.GetLinkOsPrinter(connection);

            using (FileStream certStream = new FileStream(certFile.FullName, FileMode.Open)) {
                ZebraCertificateInfo myCertificateInfo = CertificateParser.ParseCertificate(certStream, null, password);

                if (null != myCertificateInfo && printer != null) {
                    if (myCertificateInfo.PrivateKey != null) {
                        try {
                            using (FileStream fs = new FileStream("C:\\Users\\[user]\\Documents\\" + PRIV_KEY_BASE_FILE_NAME, FileMode.Create)) {
                                byte[] privateKey = Encoding.UTF8.GetBytes(myCertificateInfo.PrivateKey);
                                fs.Write(privateKey, 0, privateKey.Length);
                            };
                            printer.StoreFileOnPrinter("C:\\Users\\[user]\\Documents\\" + PRIV_KEY_BASE_FILE_NAME);
                        } finally {
                            File.Delete("C:\\Users\\[user]\\Documents\\" + PRIV_KEY_BASE_FILE_NAME);
                        }
                    }

                    if (myCertificateInfo.ClientCertificate != null) {
                        try {
                            using (FileStream fs = new FileStream("C:\\Users\\[user]\\Documents\\" + CLIENT_CERT_BASE_FILE_NAME, FileMode.Create)) {
                                byte[] clientCertificate = Encoding.UTF8.GetBytes(myCertificateInfo.ClientCertificate);
                                fs.Write(clientCertificate, 0, clientCertificate.Length);
                            };
                            printer.StoreFileOnPrinter("C:\\Users\\[user]\\Documents\\" + CLIENT_CERT_BASE_FILE_NAME);
                        } finally {
                            File.Delete("C:\\Users\\[user]\\Documents\\" + CLIENT_CERT_BASE_FILE_NAME);
                        }
                    }

                    if (myCertificateInfo.GetCaChain() != null) {
                        try {
                            using (FileStream fs = new FileStream("C:\\Users\\[user]\\Documents\\" + CA_CERT_BASE_FILE_NAME, FileMode.Create)) {

                                byte[] caChain = Encoding.UTF8.GetBytes(myCertificateInfo.GetCaChain());
                                fs.Write(caChain, 0, caChain.Length);
                            };
                            printer.StoreFileOnPrinter("C:\\Users\\[user]\\Documents\\" + CA_CERT_BASE_FILE_NAME);
                        } finally {
                            File.Delete("C:\\Users\\[user]\\Documents\\" + CA_CERT_BASE_FILE_NAME);
                        }
                    }
                }
            }

            printer.ResetNetwork();
        } catch(Exception e) {
            Console.WriteLine("Error while setting up certificates: {0}", e.Message);
        } finally {
            if (connection != null) {
                connection.Close();
            }
        }
    }
}
See Also