private static DSA BuildDsaPublicKey(byte[] encodedKeyValue, byte[] encodedParameters) { SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = Oids.Dsa, Parameters = encodedParameters }, SubjectPublicKey = encodedKeyValue, }; AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); spki.Encode(writer); DSA dsa = new DSAOpenSsl(); try { dsa.ImportSubjectPublicKeyInfo(writer.Encode(), out _); return(dsa); } catch (Exception) { dsa.Dispose(); throw; } }
public override unsafe void ImportRSAPublicKey(ReadOnlySpan <byte> source, out int bytesRead) { ThrowIfDisposed(); fixed(byte *ptr = &MemoryMarshal.GetReference(source)) { using (MemoryManager <byte> manager = new PointerMemoryManager <byte>(ptr, source.Length)) { AsnReader reader = new AsnReader(manager.Memory, AsnEncodingRules.BER); ReadOnlyMemory <byte> firstElement = reader.PeekEncodedValue(); SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = new Oid(Oids.Rsa), Parameters = AlgorithmIdentifierAsn.ExplicitDerNull, }, SubjectPublicKey = firstElement, }; using (AsnWriter writer = new AsnWriter(AsnEncodingRules.DER)) { spki.Encode(writer); ImportSubjectPublicKeyInfo(writer.EncodeAsSpan(), out _); } bytesRead = firstElement.Length; } } }
private static AsymmetricAlgorithm DecodeDsaPublicKey(byte[] encodedKeyValue, byte[] encodedParameters) { SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = new Oid(Oids.Dsa, null), Parameters = encodedParameters }, SubjectPublicKey = encodedKeyValue, }; using (AsnWriter writer = new AsnWriter(AsnEncodingRules.DER)) { spki.Encode(writer); DSA dsa = DSA.Create(); try { dsa.ImportSubjectPublicKeyInfo(writer.EncodeAsSpan(), out _); return(dsa); } catch (Exception) { dsa.Dispose(); throw; } } }
private static DSA DecodeDsaPublicKey(byte[] encodedKeyValue, byte[] encodedParameters) { SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = Oids.Dsa, Parameters = encodedParameters }, SubjectPublicKey = encodedKeyValue, }; AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); spki.Encode(writer); byte[] rented = CryptoPool.Rent(writer.GetEncodedLength()); int written = writer.Encode(rented); DSA dsa = DSA.Create(); IDisposable?toDispose = dsa; try { dsa.ImportSubjectPublicKeyInfo(rented.AsSpan(0, written), out _); toDispose = null; return(dsa); } finally { toDispose?.Dispose(); CryptoPool.Return(rented, written); } }
public virtual byte[] ComputeCapiSha1OfPublicKey(PublicKey key) { // The CapiSha1 value is the SHA-1 of the SubjectPublicKeyInfo field, inclusive // of the DER structural bytes. SubjectPublicKeyInfoAsn spki = default; spki.Algorithm = new AlgorithmIdentifierAsn { Algorithm = key.Oid !.Value !, Parameters = key.EncodedParameters.RawData }; spki.SubjectPublicKey = key.EncodedKeyValue.RawData; AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); spki.Encode(writer); byte[] rented = CryptoPool.Rent(writer.GetEncodedLength()); try { if (!writer.TryEncode(rented, out int bytesWritten)) { Debug.Fail("TryEncode failed with a pre-allocated buffer"); throw new CryptographicException(); } return(SHA1.HashData(rented.AsSpan(0, bytesWritten))); } finally { CryptoPool.Return(rented, clearSize: 0); // SubjectPublicKeyInfo is not sensitive. } }
public override unsafe void ImportRSAPublicKey(ReadOnlySpan <byte> source, out int bytesRead) { ThrowIfDisposed(); fixed(byte *ptr = &MemoryMarshal.GetReference(source)) { using (MemoryManager <byte> manager = new PointerMemoryManager <byte>(ptr, source.Length)) { ReadOnlyMemory <byte> subjectPublicKey; try { AsnReader reader = new AsnReader(manager.Memory, AsnEncodingRules.BER); subjectPublicKey = reader.PeekEncodedValue(); } catch (AsnContentException e) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); } // Decoding the key on Android requires the encoded SubjectPublicKeyInfo, // not just the SubjectPublicKey, so we construct one. SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = Oids.Rsa, Parameters = AlgorithmIdentifierAsn.ExplicitDerNull, }, SubjectPublicKey = subjectPublicKey, }; AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); spki.Encode(writer); SafeRsaHandle key = Interop.AndroidCrypto.DecodeRsaSubjectPublicKeyInfo(writer.Encode()); if (key is null || key.IsInvalid) { key?.Dispose(); throw new CryptographicException(); } FreeKey(); _key = new Lazy <SafeRsaHandle>(key); SetKeySizeFromHandle(key); bytesRead = subjectPublicKey.Length; } } }
private AsnWriter EncodeSubjectPublicKeyInfo() { SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = _oid.Value ?? string.Empty, Parameters = EncodedParameters.RawData, }, SubjectPublicKey = EncodedKeyValue.RawData, }; AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); spki.Encode(writer); return(writer); }
private static DSA BuildDsaPublicKey(byte[] encodedKey, byte[] encodedParameters) { SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = new Oid(Oids.Dsa), Parameters = encodedParameters }, SubjectPublicKey = encodedKey, }; using (AsnWriter writer = new AsnWriter(AsnEncodingRules.DER)) { DSA dsa = new DSAOpenSsl(); spki.Encode(writer); dsa.ImportSubjectPublicKeyInfo(writer.EncodeAsSpan(), out _); return(dsa); } }
public virtual byte[] ComputeCapiSha1OfPublicKey(PublicKey key) { // The CapiSha1 value is the SHA-1 of the SubjectPublicKeyInfo field, inclusive // of the DER structural bytes. SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn(); spki.Algorithm = new AlgorithmIdentifierAsn { Algorithm = key.Oid, Parameters = key.EncodedParameters.RawData }; spki.SubjectPublicKey = key.EncodedKeyValue.RawData; using (AsnWriter writer = new AsnWriter(AsnEncodingRules.DER)) using (SHA1 hash = SHA1.Create()) { spki.Encode(writer); return(hash.ComputeHash(writer.Encode())); } }
private static AsymmetricAlgorithm DecodeDsaPublicKey(byte[] encodedKeyValue, byte[] encodedParameters) { SubjectPublicKeyInfoAsn spki = new SubjectPublicKeyInfoAsn { Algorithm = new AlgorithmIdentifierAsn { Algorithm = Oids.Dsa, Parameters = encodedParameters }, SubjectPublicKey = encodedKeyValue, }; AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); spki.Encode(writer); byte[] rented = CryptoPool.Rent(writer.GetEncodedLength()); if (!writer.TryEncode(rented, out int written)) { Debug.Fail("TryEncode failed with a pre-allocated buffer"); throw new InvalidOperationException(); } DSA dsa = DSA.Create(); IDisposable?toDispose = dsa; try { dsa.ImportSubjectPublicKeyInfo(rented.AsSpan(0, written), out _); toDispose = null; return(dsa); } finally { toDispose?.Dispose(); CryptoPool.Return(rented, written); } }