コード例 #1
0
ファイル: PemKeyHelpers.cs プロジェクト: mikem8361/runtime
        public static unsafe bool TryExportToPem <T>(
            T arg,
            string label,
            TryExportKeyAction <T> exporter,
            Span <char> destination,
            out int charsWritten)
        {
            int bufferSize = 4096;

            while (true)
            {
                byte[] buffer       = CryptoPool.Rent(bufferSize);
                int    bytesWritten = 0;
                bufferSize = buffer.Length;

                // Fixed to prevent GC moves.
                fixed(byte *bufferPtr = buffer)
                {
                    try
                    {
                        if (exporter(arg, buffer, out bytesWritten))
                        {
                            Span <byte> writtenSpan = new Span <byte>(buffer, 0, bytesWritten);
                            return(PemEncoding.TryWrite(label, writtenSpan, destination, out charsWritten));
                        }
                    }
                    finally
                    {
                        CryptoPool.Return(buffer, bytesWritten);
                    }

                    bufferSize = checked (bufferSize * 2);
                }
            }
        }
コード例 #2
0
ファイル: PemKeyHelpers.cs プロジェクト: mikem8361/runtime
        public static void ImportEncryptedPem <TPass>(
            ReadOnlySpan <char> input,
            ReadOnlySpan <TPass> password,
            ImportEncryptedKeyAction <TPass> importAction)
        {
            bool                foundEncryptedPem = false;
            PemFields           foundFields       = default;
            ReadOnlySpan <char> foundSlice        = default;

            ReadOnlySpan <char> pem = input;

            while (PemEncoding.TryFind(pem, out PemFields fields))
            {
                ReadOnlySpan <char> label = pem[fields.Label];

                if (label.SequenceEqual(PemLabels.EncryptedPkcs8PrivateKey))
                {
                    if (foundEncryptedPem)
                    {
                        throw new ArgumentException(SR.Argument_PemImport_AmbiguousPem, nameof(input));
                    }

                    foundEncryptedPem = true;
                    foundFields       = fields;
                    foundSlice        = pem;
                }

                Index offset = fields.Location.End;
                pem = pem[offset..];
コード例 #3
0
        internal static string CreatePemFromData(string label, ReadOnlyMemory <byte> data)
        {
            int pemSize = PemEncoding.GetEncodedSize(label.Length, data.Length);

            return(string.Create(pemSize, (label, data), static (destination, args) =>
            {
                (string label, ReadOnlyMemory <byte> data) = args;

                if (!PemEncoding.TryWrite(label, data.Span, destination, out int charsWritten) ||
                    charsWritten != destination.Length)
                {
                    Debug.Fail("Pre-allocated buffer was not the correct size.");
                    throw new CryptographicException();
                }
            }));
コード例 #4
0
ファイル: PemKeyHelpers.cs プロジェクト: mikem8361/runtime
        public static unsafe bool TryExportToEncryptedPem <T>(
            T arg,
            ReadOnlySpan <char> password,
            PbeParameters pbeParameters,
            TryExportEncryptedKeyAction <T> exporter,
            Span <char> destination,
            out int charsWritten)
        {
            int bufferSize = 4096;

            while (true)
            {
                byte[] buffer       = CryptoPool.Rent(bufferSize);
                int    bytesWritten = 0;
                bufferSize = buffer.Length;

                // Fixed to prevent GC moves.
                fixed(byte *bufferPtr = buffer)
                {
                    try
                    {
                        if (exporter(arg, password, pbeParameters, buffer, out bytesWritten))
                        {
                            Span <byte> writtenSpan = new Span <byte>(buffer, 0, bytesWritten);
                            return(PemEncoding.TryWrite(PemLabels.EncryptedPkcs8PrivateKey, writtenSpan, destination, out charsWritten));
                        }
                    }
                    finally
                    {
                        CryptoPool.Return(buffer, bytesWritten);
                    }

                    bufferSize = checked (bufferSize * 2);
                }
            }
        }