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.15.2634
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
Desktop
using System;
using System.IO;
using System.Text;
using Zebra.Sdk.Certificate;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;

public class CertificateParserExample {

    private const string PRIV_KEY_BASE_FILE_NAME = "PRIVKEY.NRD";
    private const string CA_CERT_BASE_FILE_NAME = "CACERTSV.NRD";
    private const string CLIENT_CERT_BASE_FILE_NAME = "CERTCLN.NRD";

    public static void Main(string[] args) {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string p12File = Path.Combine(path, "certificate.p12");
        string password = "passwordForCertFile";

        FileInfo certFile = new FileInfo(p12File);
        if (!certFile.Exists) {
            Console.WriteLine($"The provided certificate file ({p12File}) cannot be found.");
            return;
        }

        Console.WriteLine($"Accessing certificate file {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) {
                        string tempPath = Path.Combine(path, PRIV_KEY_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] privateKey = Encoding.UTF8.GetBytes(myCertificateInfo.PrivateKey);
                                fs.Write(privateKey, 0, privateKey.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }

                    if (myCertificateInfo.ClientCertificate != null) {
                        string tempPath = Path.Combine(path, CLIENT_CERT_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] clientCertificate = Encoding.UTF8.GetBytes(myCertificateInfo.ClientCertificate);
                                fs.Write(clientCertificate, 0, clientCertificate.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }

                    if (myCertificateInfo.GetCaChain() != null) {
                        string tempPath = Path.Combine(path, CA_CERT_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] caChain = Encoding.UTF8.GetBytes(myCertificateInfo.GetCaChain());
                                fs.Write(caChain, 0, caChain.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }
                }
            }

            printer.ResetNetwork();
        } catch (Exception e) {
            Console.WriteLine($"Error while setting up certificates: {e.Message}");
        } finally {
            if (connection != null) {
                connection.Close();
            }
        }
    }
}
Examples
Android™
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Zebra.Sdk.Certificate;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;

public class CertificateParserExample : Activity {

    private const string PRIV_KEY_BASE_FILE_NAME = "PRIVKEY.NRD";
    private const string CA_CERT_BASE_FILE_NAME = "CACERTSV.NRD";
    private const string CLIENT_CERT_BASE_FILE_NAME = "CERTCLN.NRD";

    private readonly string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);

        LinearLayout layout = (LinearLayout)View.Inflate(this, Android.Resource.Layout.ActivityListItem, null);
        layout.Orientation = Orientation.Vertical;

        Button testButton = new Button(this) {
            Text = "Run Certificate Parser Example",
            LayoutParameters = new ViewGroup.LayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent))
        };
        layout.AddView(testButton);

        SetContentView(layout);

        testButton.Click += async (sender, e) => {
            await Task.Run(() => {
                string ipAddress = "1.2.3.4";
                string p12File = Path.Combine(path, "certificate.p12");
                string password = "passwordForCertFile";

                ParseCertificate(ipAddress, p12File, password);
            });
        };
    }

    public void ParseCertificate(string ipAddress, string p12File, string password) {
        FileInfo certFile = new FileInfo(p12File);
        if (!certFile.Exists) {
            Console.WriteLine($"The provided certificate file ({p12File}) cannot be found.");
            return;
        }

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

        Connection connection = new TcpConnection(ipAddress, 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) {
                        string tempPath = Path.Combine(path, PRIV_KEY_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] privateKey = Encoding.UTF8.GetBytes(myCertificateInfo.PrivateKey);
                                fs.Write(privateKey, 0, privateKey.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }

                    if (myCertificateInfo.ClientCertificate != null) {
                        string tempPath = Path.Combine(path, CLIENT_CERT_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] clientCertificate = Encoding.UTF8.GetBytes(myCertificateInfo.ClientCertificate);
                                fs.Write(clientCertificate, 0, clientCertificate.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }

                    if (myCertificateInfo.GetCaChain() != null) {
                        string tempPath = Path.Combine(path, CA_CERT_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] caChain = Encoding.UTF8.GetBytes(myCertificateInfo.GetCaChain());
                                fs.Write(caChain, 0, caChain.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }
                }
            }

            printer.ResetNetwork();
        } catch (Exception e) {
            Console.WriteLine($"Error while setting up certificates: {e.Message}");
        } finally {
            if (connection != null) {
                connection.Close();
            }
        }
    }
}
Examples
iOS
using CoreGraphics;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using UIKit;
using Zebra.Sdk.Certificate;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;

public class CertificateParserExample : UIViewController {

    private const string PRIV_KEY_BASE_FILE_NAME = "PRIVKEY.NRD";
    private const string CA_CERT_BASE_FILE_NAME = "CACERTSV.NRD";
    private const string CLIENT_CERT_BASE_FILE_NAME = "CERTCLN.NRD";

    private readonly string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    public CertificateParserExample(IntPtr handle) : base(handle) {
        UIButton testButton = new UIButton(UIButtonType.System) {
            Frame = new CGRect(25, 25, 300, 150)
        };

        testButton.SetTitle("Run Certificate Parser Example", UIControlState.Normal);

        testButton.TouchUpInside += async (sender, e) => {
            await Task.Run(() => {
                string ipAddress = "1.2.3.4";
                string p12File = Path.Combine(path, "certificate.p12");
                string password = "passwordForCertFile";

                ParseCertificate(ipAddress, p12File, password);
            });
        };

        View.AddSubview(testButton);
    }

    public void ParseCertificate(string ipAddress, string p12File, string password) {
        FileInfo certFile = new FileInfo(p12File);
        if (!certFile.Exists) {
            Console.WriteLine($"The provided certificate file ({p12File}) cannot be found.");
            return;
        }

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

        Connection connection = new TcpConnection(ipAddress, 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) {
                        string tempPath = Path.Combine(path, PRIV_KEY_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] privateKey = Encoding.UTF8.GetBytes(myCertificateInfo.PrivateKey);
                                fs.Write(privateKey, 0, privateKey.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }

                    if (myCertificateInfo.ClientCertificate != null) {
                        string tempPath = Path.Combine(path, CLIENT_CERT_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] clientCertificate = Encoding.UTF8.GetBytes(myCertificateInfo.ClientCertificate);
                                fs.Write(clientCertificate, 0, clientCertificate.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }

                    if (myCertificateInfo.GetCaChain() != null) {
                        string tempPath = Path.Combine(path, CA_CERT_BASE_FILE_NAME);
                        try {
                            using (FileStream fs = new FileStream(tempPath, FileMode.Create)) {
                                byte[] caChain = Encoding.UTF8.GetBytes(myCertificateInfo.GetCaChain());
                                fs.Write(caChain, 0, caChain.Length);
                            };
                            printer.StoreFileOnPrinter(tempPath);
                        } finally {
                            File.Delete(tempPath);
                        }
                    }
                }
            }

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