Exemple #1
0
        /// <summary>
        /// Imports the public/private keypair from a PKCS#8 EncryptedPrivateKeyInfo
        /// structure after decrypting with a byte-based password, replacing the
        /// keys for this object.
        /// </summary>
        /// <param name="password">The password to use when decrypting the key material.</param>
        /// <param name="source">
        /// The bytes of a PKCS#8 EncryptedPrivateKeyInfo structure in the ASN.1-BER encoding.
        /// </param>
        /// <param name="bytesRead">
        /// When this method returns, contains a value that indicates the number
        /// of bytes read from <paramref name="source" />. This parameter is treated as uninitialized.
        /// </param>
        /// <exception cref="CryptographicException">
        /// <p>
        ///   The contents of <paramref name="source" /> do not represent an
        ///   ASN.1-BER-encoded PKCS#8 EncryptedPrivateKeyInfo structure.
        /// </p>
        /// <p>-or-</p>
        /// <p>
        ///   The contents of <paramref name="source" /> indicate the key is for
        ///   an algorithm other than the algorithm represented by this instance.
        /// </p>
        /// <p>-or-</p>
        /// <p>
        ///   The contents of <paramref name="source" /> represent the key in a format
        ///   that is not supported.
        /// </p>
        /// <p>-or-</p>
        /// <p>The algorithm-specific key import failed.</p>
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// A derived class has not provided an implementation for <see cref="ImportParameters" />.
        /// </exception>
        /// <remarks>
        /// <p>
        ///   When the contents of <paramref name="source" /> indicate an algorithm that uses PBKDF1
        ///   (Password-Based Key Derivation Function 1) or PBKDF2 (Password-Based Key Derivation Function 2),
        ///   the password is converted to bytes via the UTF-8 encoding.
        /// </p>
        /// <p>
        ///   This method only supports the binary (BER/CER/DER) encoding of EncryptedPrivateKeyInfo.
        ///   If the value is Base64-encoded, the caller must Base64-decode the contents before calling this method.
        ///   If the contents are PEM-encoded, <see cref="ImportFromEncryptedPem(ReadOnlySpan{char}, ReadOnlySpan{char})" />
        ///   should be used.
        /// </p>
        /// </remarks>
        public override unsafe void ImportEncryptedPkcs8PrivateKey(
            ReadOnlySpan <char> password,
            ReadOnlySpan <byte> source,
            out int bytesRead)
        {
            KeyFormatHelper.ReadEncryptedPkcs8 <ECParameters>(
                s_validOids,
                source,
                password,
                EccKeyFormatHelper.FromECPrivateKey,
                out int localRead,
                out ECParameters ret);

            fixed(byte *privPin = ret.D)
            {
                try
                {
                    ImportParameters(ret);
                    bytesRead = localRead;
                }
                finally
                {
                    CryptographicOperations.ZeroMemory(ret.D);
                }
            }
        }
Exemple #2
0
        public override unsafe void ImportEncryptedPkcs8PrivateKey(
            ReadOnlySpan <char> password,
            ReadOnlySpan <byte> source,
            out int bytesRead)
        {
            KeyFormatHelper.ReadEncryptedPkcs8 <RSAParameters, RSAPrivateKeyAsn>(
                s_validOids,
                source,
                password,
                FromPkcs1PrivateKey,
                out int localRead,
                out RSAParameters ret);

            fixed(byte *dPin = ret.D)
            fixed(byte *pPin    = ret.P)
            fixed(byte *qPin    = ret.Q)
            fixed(byte *dpPin   = ret.DP)
            fixed(byte *dqPin   = ret.DQ)
            fixed(byte *qInvPin = ret.InverseQ)
            {
                try
                {
                    ImportParameters(ret);
                }
                finally
                {
                    ClearPrivateParameters(ret);
                }
            }

            bytesRead = localRead;
        }
Exemple #3
0
 internal static void ReadEncryptedPkcs8(
     ReadOnlySpan <byte> source,
     ReadOnlySpan <byte> passwordBytes,
     out int bytesRead,
     out ECParameters key)
 {
     KeyFormatHelper.ReadEncryptedPkcs8 <ECParameters>(
         s_validOids,
         source,
         passwordBytes,
         FromECPrivateKey,
         out bytesRead,
         out key);
 }