public static IReadOnlyList <string> GetNameInfo(this X509Certificate2 certificate, string nameTypeOid, X509NameSource nameSource) { ThrowHelpers.CheckNullOrEempty(nameof(nameTypeOid), nameTypeOid); byte[] nameBytes = nameSource switch { X509NameSource.Issuer => certificate.IssuerName.RawData, X509NameSource.Subject => certificate.SubjectName.RawData, _ => ThrowHelpers.NotSupport <byte[], X509NameSource>(nameSource) }; List <string> result = new List <string>(); AsnReader nameReader = new AsnReader(nameBytes, AsnEncodingRules.DER); AsnReader mainSequence = nameReader.ReadSequence(); while (mainSequence.HasData) { AsnReader x509Name = mainSequence.ReadSetOf().ReadSequence(); string oid = x509Name.ReadObjectIdentifierAsString(); if (string.Equals(nameTypeOid, oid, StringComparison.Ordinal)) { result.Add(x509Name.GetCharacterString(UniversalTagNumber.PrintableString)); } } return(result); } }
public static void Update(this HashAlgorithm hashAlgorithm, byte[] input, int start, int length) { ThrowHelpers.CheckNull(nameof(input), input); if (start < 0 || start >= input.Length) { throw new ArgumentOutOfRangeException(nameof(start)); } if (length < 0 || start + length > input.Length) { throw new ArgumentOutOfRangeException(nameof(length)); } int offset = start; int size = hashAlgorithm.HashSize / 8; while (start + length - offset >= size) { offset += hashAlgorithm.TransformBlock(input, offset, size, null, 0); } if (start + length - offset > 0) { hashAlgorithm.TransformBlock(input, offset, input.Length - offset, null, 0); } }
public static byte[] GetEncoded(this X509Certificate2 certificate, AsnFormat format) { return(format switch { AsnFormat.Der => certificate.RawData, AsnFormat.Pem => PemFormater.ToPemBytes(certificate.RawData, "CERTIFICATE"), _ => ThrowHelpers.NotImplemented <byte[]>(nameof(X509Certificate2EncodeExtensions)) });
public RngState128(ulong s0, ulong s1) { if (0 == (s0 | s1)) { ThrowHelpers.ThrowArgumentExceptionForAllZeroes(); } this.S0 = s0; this.S1 = s1; }
public static void DeriveKey(string hmacAlgorithmName, byte[] key, ReadOnlySpan <byte> label = default, ReadOnlySpan <byte> context = default, Span <byte> derivedOutput = default, uint counter = 1) { ThrowHelpers.CheckNullOrEempty(nameof(hmacAlgorithmName), hmacAlgorithmName); DeriveKey(() => HMAC.Create(hmacAlgorithmName) ?? throw new ArgumentException($"Hash algorithm name {hmacAlgorithmName} is not supported."), key, label, context, derivedOutput, counter); }
public static void DeriveKey(string hmacAlgorithmName, byte[] key, ReadOnlySpan <byte> label = default, ReadOnlySpan <byte> context = default, Span <byte> derivedOutput = default, uint counter = 1) { ThrowHelpers.CheckNullOrEempty(nameof(hmacAlgorithmName), hmacAlgorithmName); DeriveKey(() => HMAC.Create(hmacAlgorithmName), key, label, context, derivedOutput, counter); }
public static bool TryEncode(ReadOnlySpan <byte> rawPkcs11Signature, Span <byte> destination, out int bytesWritten) { if (rawPkcs11Signature.Length == 0 || rawPkcs11Signature.Length % 2 != 0) { ThrowHelpers.ThrowArgumentException($"Parameter {nameof(rawPkcs11Signature)} is empty or has not even length."); } using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER); ConstructECDsaSigValue(rawPkcs11Signature, asnWriter); return(asnWriter.TryEncode(destination, out bytesWritten)); }
public static int Next(this IRandomGenerator generator, int minValue, int maxValue) { ThrowHelpers.CheckRange(nameof(minValue), minValue, nameof(maxValue), maxValue); Span <byte> intBytes = stackalloc byte[sizeof(int)]; generator.NextBytes(intBytes); int randomValue = BitConverter.ToInt32(intBytes); return((randomValue % (maxValue - minValue)) + minValue); }
public static void DeriveKey(Func <HMAC> hmacFactory, byte[] key, ReadOnlySpan <byte> label = default, ReadOnlySpan <byte> context = default, Span <byte> derivedOutput = default, uint counter = 1) { ThrowHelpers.CheckNull(nameof(hmacFactory), hmacFactory); ThrowHelpers.CheckNull(nameof(key), key); using HMAC hmac = hmacFactory(); hmac.Key = key; int bufferLen = CalculateBufferLenght(label, context); Span <byte> buffer = (bufferLen > StackAllocTreshold) ? stackalloc byte[bufferLen] : new byte[bufferLen]; FillBuffer(buffer, label, context, checked ((uint)(derivedOutput.Length << 3))); DeriveKey(hmac, buffer, derivedOutput, counter); }
public ContextMetadata(Type contextType) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (contextType == null) { ThrowHelpers.ThrowArgumentNullException(nameof(contextType)); } ContextType = contextType; Properties = contextType.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanWrite && IsDocumentSet(p.PropertyType)) .Select(p => new DocumentSetMetadata(p)) .ToArray(); Initialize = CreateInitializeAction(); }
/// <summary> /// Formats the bucket name and optionally the scope and collection names into an expression /// for inclusion in a N1QL query. This includes all required escaping. /// </summary> /// <param name="queryable">Collection being queried.</param> /// <returns>The expression string.</returns> public static string GetCollectionExpression(ICollectionQueryable queryable) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (queryable == null) { ThrowHelpers.ThrowArgumentNullException(nameof(queryable)); } var bucketName = queryable.BucketName; var scopeName = queryable.ScopeName; var collectionName = queryable.CollectionName; return(scopeName == DefaultScopeName && collectionName == DefaultCollectionName ? EscapeIdentifier(bucketName) : $"{EscapeIdentifier(queryable.BucketName)}.{EscapeIdentifier(scopeName)}.{EscapeIdentifier(collectionName)}"); }
public static void Update(this HashAlgorithm hashAlgorithm, byte[] input) { ThrowHelpers.CheckNull(nameof(input), input); int offset = 0; int size = hashAlgorithm.HashSize / 8; while (input.Length - offset >= size) { offset += hashAlgorithm.TransformBlock(input, offset, size, null, 0); } if (input.Length - offset > 0) { hashAlgorithm.TransformBlock(input, offset, input.Length - offset, null, 0); } }
private static byte[] CreateRawAsn1(string policyOid, HashAlgorithmName algorithmNameForPolicy, ReadOnlySpan <byte> policyHashValue) { ThrowHelpers.CheckNullOrEempty(nameof(policyOid), policyOid); using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER); asnWriter.PushSequence(); asnWriter.WriteObjectIdentifier(policyOid); asnWriter.PushSequence(); asnWriter.PushSequence(); asnWriter.WriteObjectIdentifier(HashAlgorithmConvertor.ToOid(algorithmNameForPolicy)); asnWriter.WriteNull(); asnWriter.PopSequence(); asnWriter.WriteOctetString(policyHashValue); asnWriter.PopSequence(); asnWriter.PopSequence(); return(asnWriter.Encode()); }
public static bool IsForUsage(this X509Certificate2 certificate, X509KeyUsageFlags usageFlag) { ThrowHelpers.CheckNull(nameof(certificate), certificate); foreach (X509Extension certificateExtension in certificate.Extensions) { if (certificateExtension is X509KeyUsageExtension usage) { if (usage.KeyUsages.HasFlag(usageFlag)) { return(true); } } } return(false); }
public static async Task Update(this HashAlgorithm hashAlgorithm, Stream stream, CancellationToken cancellationToken = default) { ThrowHelpers.CheckNull(nameof(stream), stream); int size = hashAlgorithm.HashSize / 8; #if NETCOREAPP byte[] buffer = GC.AllocateUninitializedArray <byte>(size, false); #else byte[] buffer = new byte[size]; #endif int read; while ((read = await stream.ReadAsync(buffer, 0, size, cancellationToken).ConfigureAwait(false)) > 0) { hashAlgorithm.TransformBlock(buffer, 0, read, null, 0); } }
private static byte[] CreateRawAsn1(string fileName, string contentType) { ThrowHelpers.CheckNullOrEempty(nameof(fileName), fileName); ThrowHelpers.CheckNullOrEempty(nameof(contentType), contentType); string idDataString = string.Concat("MIME-Version: 1.0\r\nContent-Type: ", contentType, "\r\nContent-Disposition: attachment; filename=\"", fileName, "\""); using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER); asnWriter.PushSequence(); asnWriter.WriteCharacterString(UniversalTagNumber.UTF8String, idDataString); asnWriter.WriteObjectIdentifier(Pkcs7Oids.IdData); asnWriter.PopSequence(); return(asnWriter.Encode()); }
private static bool IsForExtendedKeyUsage(this X509Certificate2 certificate, string exceptedUsageOid) { ThrowHelpers.CheckNull(nameof(certificate), certificate); foreach (X509Extension certificateExtension in certificate.Extensions) { if (certificateExtension is X509EnhancedKeyUsageExtension usage) { foreach (System.Security.Cryptography.Oid usageOid in usage.EnhancedKeyUsages) { if (string.Equals(usageOid.Value, exceptedUsageOid, StringComparison.Ordinal)) { return(true); } } } } return(false); }
private static byte[] CreateRawAsn1(IEnumerable <X509Certificate2> signingCertificates, HashAlgorithmName hashAlgorithmName) { ThrowHelpers.CheckNull(nameof(signingCertificates), signingCertificates); using HashAlgorithm hasher = HashAlgorithmConvertor.ToHashAlgorithm(hashAlgorithmName); Span <byte> hash = stackalloc byte[hasher.HashSize / 8]; using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER); asnWriter.PushSequence(); asnWriter.PushSequence(); foreach (X509Certificate2 signingCertificate in signingCertificates) { hasher.TryComputeHash(signingCertificate.RawData, hash, out _); // Begin essCertIDv2 asnWriter.PushSequence(); // Begin algorithm identifier asnWriter.PushSequence(); asnWriter.WriteObjectIdentifier(HashAlgorithmConvertor.ToOid(hashAlgorithmName)); asnWriter.WriteNull(); asnWriter.PopSequence(); // End Algorithm identifier asnWriter.WriteOctetString(hash); asnWriter.PopSequence(); // End essCertIDv2 } asnWriter.PopSequence(); asnWriter.PopSequence(); return(asnWriter.Encode()); }
public DocumentSet(BucketContext bucketContext, string scopeName, string collectionName) { // ReSharper disable ConditionIsAlwaysTrueOrFalse if (bucketContext == null) { ThrowHelpers.ThrowArgumentNullException(nameof(bucketContext)); } if (scopeName == null) { ThrowHelpers.ThrowArgumentNullException(nameof(scopeName)); } if (collectionName == null) { ThrowHelpers.ThrowArgumentNullException(nameof(collectionName)); } // ReSharper restore ConditionIsAlwaysTrueOrFalse _bucketContext = bucketContext; ScopeName = scopeName; CollectionName = collectionName; // Note: For this to work, we must implement ICollectionQueryable Expression = Expression.Constant(this); }
public static IReadOnlyList <string> GetNameInfo(this X509Certificate2 certificate, string nameTypeOid, X509NameSource nameSource) { ThrowHelpers.CheckNullOrEempty(nameof(nameTypeOid), nameTypeOid); return(GetNameInfo(certificate, nameTypeOid, nameSource == X509NameSource.Issuer)); }
public override string Format(bool multiLine) { return(ThrowHelpers.NotImplemented <string>(nameof(Pkcs7IdAaContentHint))); }
public override void CopyFrom(AsnEncodedData asnEncodedData) { ThrowHelpers.NotImplemented(nameof(Pkcs7IdAaContentHint)); }
public static IReadOnlyList <string> GetNameInfo(this X509Certificate2 certificate, string nameTypeOid, bool forIssuer) { ThrowHelpers.CheckNullOrEempty(nameof(nameTypeOid), nameTypeOid); return(GetNameInfo(certificate, nameTypeOid, forIssuer ? X509NameSource.Issuer : X509NameSource.Subject)); }
private static byte[] CreateRawAsn1(X509Certificate2 signingCertificate, HashAlgorithmName hashAlgorithmName) { ThrowHelpers.CheckNull(nameof(signingCertificate), signingCertificate); return(CreateRawAsn1(new X509Certificate2[] { signingCertificate }, hashAlgorithmName)); }
public void AddSeedMaterial(byte[] inSeed) { ThrowHelpers.CheckNull(nameof(inSeed), inSeed); }
public void NextBytes(byte[] buffer, int start, int len) { ThrowHelpers.CheckNull(nameof(buffer), buffer); this.prng.GetBytes(buffer, start, len); }
public void NextBytes(byte[] buffer) { ThrowHelpers.CheckNull(nameof(buffer), buffer); this.prng.GetBytes(buffer); }