|
| 1 | +// |
| 2 | +// Copyright (c) 2019 The nanoFramework project contributors |
| 3 | +// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// See LICENSE file in the project root for full license information. |
| 5 | +// |
| 6 | + |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace System.Security.Cryptography.X509Certificates |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Represents an X.509 certificate. |
| 14 | + /// </summary> |
| 15 | + public class X509Certificate2 : X509Certificate |
| 16 | + { |
| 17 | +#pragma warning disable S3459 // Unassigned members should be removed |
| 18 | + // these fields are required and set in native code |
| 19 | + private readonly byte[] _privateKey; |
| 20 | +#pragma warning restore S3459 // Unassigned members should be removed |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class. |
| 24 | + /// </summary> |
| 25 | + public X509Certificate2() |
| 26 | + : base() |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using information from a byte array. |
| 32 | + /// </summary> |
| 33 | + /// <param name="rawData">A byte array containing data from an X.509 certificate.</param> |
| 34 | + public X509Certificate2(byte[] rawData) |
| 35 | + : base(rawData) |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using a byte array and a password. |
| 41 | + /// </summary> |
| 42 | + /// <param name="rawData">A byte array containing data from an X.509 certificate.</param> |
| 43 | + /// <param name="password">The password required to access the X.509 certificate data.</param> |
| 44 | + public X509Certificate2(byte[] rawData, string password) |
| 45 | + : base(rawData, password) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 certificate. |
| 52 | + /// </summary> |
| 53 | + /// <param name="certificate">A string containing a X.509 certificate.</param> |
| 54 | + /// <remarks> |
| 55 | + /// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter. |
| 56 | + /// </remarks> |
| 57 | + public X509Certificate2(string certificate) |
| 58 | + : base(certificate) |
| 59 | + { |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 certificate and a password used to access the certificate. |
| 64 | + /// </summary> |
| 65 | + /// <param name="certificate">A string containing a X.509 certificate.</param> |
| 66 | + /// <param name="password">The password required to access the X.509 certificate data.</param> |
| 67 | + /// <remarks> |
| 68 | + /// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter. |
| 69 | + /// </remarks> |
| 70 | + public X509Certificate2(string certificate, string password) |
| 71 | + : base(certificate, password) |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate. |
| 77 | + /// </summary> |
| 78 | + /// <param name="certificate">A string containing a X.509 certificate.</param> |
| 79 | + /// <param name="key">A string containing a PEM private key.</param> |
| 80 | + /// <param name="password">The password required to access the X.509 certificate data.</param> |
| 81 | + /// <remarks> |
| 82 | + /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. |
| 83 | + /// </remarks> |
| 84 | + public X509Certificate2(string certificate, string key, string password) |
| 85 | + : base(certificate, password) |
| 86 | + { |
| 87 | + var tempKey = Encoding.UTF8.GetBytes(key); |
| 88 | + |
| 89 | + ////////////////////////////////////////////// |
| 90 | + // because this is parsing from a string // |
| 91 | + // we need to keep the terminator // |
| 92 | + ////////////////////////////////////////////// |
| 93 | + var keyBuffer = new byte[tempKey.Length + 1]; |
| 94 | + Array.Copy(tempKey, keyBuffer, tempKey.Length); |
| 95 | + keyBuffer[keyBuffer.Length - 1] = 0; |
| 96 | + |
| 97 | + _privateKey = keyBuffer; |
| 98 | + |
| 99 | + DecodePrivateKeyNative(keyBuffer, password); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate. |
| 104 | + /// </summary> |
| 105 | + /// <param name="rawData">A byte array containing data from an X.509 certificate.</param> |
| 106 | + /// <param name="key">A string containing a PEM private key.</param> |
| 107 | + /// <param name="password">The password required to access the X.509 certificate data.</param> |
| 108 | + /// <remarks> |
| 109 | + /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. |
| 110 | + /// </remarks> |
| 111 | + public X509Certificate2(byte[] rawData, string key, string password) |
| 112 | + : base(rawData, password) |
| 113 | + { |
| 114 | + var tempKey = Encoding.UTF8.GetBytes(key); |
| 115 | + |
| 116 | + ////////////////////////////////////////////// |
| 117 | + // because this is parsing from a string // |
| 118 | + // we need to keep the terminator // |
| 119 | + ////////////////////////////////////////////// |
| 120 | + var keyBuffer = new byte[tempKey.Length + 1]; |
| 121 | + Array.Copy(tempKey, keyBuffer, tempKey.Length); |
| 122 | + keyBuffer[keyBuffer.Length - 1] = 0; |
| 123 | + |
| 124 | + _privateKey = keyBuffer; |
| 125 | + |
| 126 | + DecodePrivateKeyNative(keyBuffer, password); |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate. |
| 131 | + /// </summary> |
| 132 | + /// <param name="rawData">A byte array containing data from an X.509 certificate.</param> |
| 133 | + /// <param name="key">A byte array containing a PEM private key.</param> |
| 134 | + /// <param name="password">The password required to access the X.509 certificate data.</param> |
| 135 | + /// <remarks> |
| 136 | + /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. |
| 137 | + /// </remarks> |
| 138 | + public X509Certificate2(byte[] rawData, byte[] key, string password) |
| 139 | + : base(rawData, password) |
| 140 | + { |
| 141 | + _privateKey = key; |
| 142 | + |
| 143 | + DecodePrivateKeyNative(key, password); |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Gets a value that indicates whether an X509Certificate2 object contains a private key. |
| 148 | + /// </summary> |
| 149 | + /// <value><see langword="true"/> if the <see cref="X509Certificate2"/> object contains a private key; otherwise, <see langword="false"/>.</value> |
| 150 | + public bool HasPrivateKey |
| 151 | + { |
| 152 | + get |
| 153 | + { |
| 154 | + return (_privateKey != null); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Gets the date in local time after which a certificate is no longer valid. |
| 160 | + /// </summary> |
| 161 | + /// <value>A <see cref="DateTime"/> object that represents the expiration date for the certificate.</value> |
| 162 | + public DateTime NotAfter |
| 163 | + { |
| 164 | + get |
| 165 | + { |
| 166 | + return _expirationDate; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Gets the date in local time on which a certificate becomes valid. |
| 172 | + /// </summary> |
| 173 | + /// <value>A <see cref="DateTime"/> object that represents the effective date of the certificate.</value> |
| 174 | + public DateTime NotBefore |
| 175 | + { |
| 176 | + get |
| 177 | + { |
| 178 | + return _effectiveDate; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Gets the raw data of a certificate. |
| 184 | + /// </summary> |
| 185 | + /// <value>The raw data of the certificate as a byte array.</value> |
| 186 | + public byte[] RawData |
| 187 | + { |
| 188 | + get |
| 189 | + { |
| 190 | + return base.GetRawCertData(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 195 | + internal static extern void DecodePrivateKeyNative(byte[] keyBuffer, string password); |
| 196 | + } |
| 197 | +} |
0 commit comments