Certificate
|
The CertificateParser type exposes the following members.
Name | Description | |
---|---|---|
CertificateParser | Initializes a new instance of the CertificateParser class |
Name | Description | |
---|---|---|
Equals | (Inherited from Object) | |
GetHashCode | (Inherited from Object) | |
GetType | (Inherited from Object) | |
ParseCertificate | 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). | |
ToString | (Inherited from Object) |
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(); } } } }