Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DSAKeyFormatHelper.cs
Go to the documentation of this file.
4
6
7internal static class DSAKeyFormatHelper
8{
9 private static readonly string[] s_validOids = new string[1] { "1.2.840.10040.4.1" };
10
11 internal static void ReadDsaPrivateKey(ReadOnlyMemory<byte> xBytes, in AlgorithmIdentifierAsn algId, out DSAParameters ret)
12 {
13 if (!algId.Parameters.HasValue)
14 {
16 }
17 DssParms dssParms = DssParms.Decode(algId.Parameters.Value, AsnEncodingRules.BER);
18 ret = new DSAParameters
19 {
20 P = dssParms.P.ToByteArray(isUnsigned: true, isBigEndian: true),
21 Q = dssParms.Q.ToByteArray(isUnsigned: true, isBigEndian: true)
22 };
23 ret.G = dssParms.G.ExportKeyParameter(ret.P.Length);
24 BigInteger bigInteger;
25 try
26 {
27 int bytesConsumed;
29 if (bytesConsumed != xBytes.Length)
30 {
32 }
33 bigInteger = new BigInteger(value, isUnsigned: true, isBigEndian: true);
34 }
35 catch (AsnContentException inner)
36 {
38 }
39 ret.X = bigInteger.ExportKeyParameter(ret.Q.Length);
40 BigInteger value2 = BigInteger.ModPow(dssParms.G, bigInteger, dssParms.P);
41 ret.Y = value2.ExportKeyParameter(ret.P.Length);
42 }
43
44 internal static void ReadDsaPublicKey(ReadOnlyMemory<byte> yBytes, in AlgorithmIdentifierAsn algId, out DSAParameters ret)
45 {
47 try
48 {
49 value = AsnDecoder.ReadInteger(yBytes.Span, AsnEncodingRules.DER, out var bytesConsumed);
50 if (bytesConsumed != yBytes.Length)
51 {
53 }
54 }
55 catch (AsnContentException inner)
56 {
58 }
59 if (!algId.Parameters.HasValue)
60 {
62 }
63 DssParms dssParms = DssParms.Decode(algId.Parameters.Value, AsnEncodingRules.BER);
64 ret = new DSAParameters
65 {
66 P = dssParms.P.ToByteArray(isUnsigned: true, isBigEndian: true),
67 Q = dssParms.Q.ToByteArray(isUnsigned: true, isBigEndian: true)
68 };
69 ret.G = dssParms.G.ExportKeyParameter(ret.P.Length);
70 ret.Y = value.ExportKeyParameter(ret.P.Length);
71 }
72
77
78 internal static void ReadPkcs8(ReadOnlySpan<byte> source, out int bytesRead, out DSAParameters key)
79 {
81 }
82
83 internal static void ReadEncryptedPkcs8(ReadOnlySpan<byte> source, ReadOnlySpan<char> password, out int bytesRead, out DSAParameters key)
84 {
85 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, password, (KeyFormatHelper.KeyReader<DSAParameters>)ReadDsaPrivateKey, out bytesRead, out key);
86 }
87
88 internal static void ReadEncryptedPkcs8(ReadOnlySpan<byte> source, ReadOnlySpan<byte> passwordBytes, out int bytesRead, out DSAParameters key)
89 {
90 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, passwordBytes, (KeyFormatHelper.KeyReader<DSAParameters>)ReadDsaPrivateKey, out bytesRead, out key);
91 }
92
93 internal static AsnWriter WriteSubjectPublicKeyInfo(in DSAParameters dsaParameters)
94 {
95 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
96 asnWriter.PushSequence();
97 WriteAlgorithmId(asnWriter, in dsaParameters);
98 WriteKeyComponent(asnWriter, dsaParameters.Y, bitString: true);
99 asnWriter.PopSequence();
100 return asnWriter;
101 }
102
103 internal static AsnWriter WritePkcs8(in DSAParameters dsaParameters)
104 {
105 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
106 asnWriter.PushSequence();
107 asnWriter.WriteInteger(0L);
108 WriteAlgorithmId(asnWriter, in dsaParameters);
109 WriteKeyComponent(asnWriter, dsaParameters.X, bitString: false);
110 asnWriter.PopSequence();
111 return asnWriter;
112 }
113
114 private static void WriteAlgorithmId(AsnWriter writer, in DSAParameters dsaParameters)
115 {
116 writer.PushSequence();
117 writer.WriteObjectIdentifier("1.2.840.10040.4.1");
118 writer.PushSequence();
119 writer.WriteKeyParameterInteger(dsaParameters.P);
120 writer.WriteKeyParameterInteger(dsaParameters.Q);
121 writer.WriteKeyParameterInteger(dsaParameters.G);
122 writer.PopSequence();
123 writer.PopSequence();
124 }
125
126 private static void WriteKeyComponent(AsnWriter writer, byte[] component, bool bitString)
127 {
128 if (bitString)
129 {
130 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
131 asnWriter.WriteKeyParameterInteger(component);
133 if (!asnWriter.TryEncode(array, out var bytesWritten))
134 {
135 throw new CryptographicException();
136 }
137 writer.WriteBitString(array.AsSpan(0, bytesWritten));
139 return;
140 }
141 using (writer.PushOctetString())
142 {
143 writer.WriteKeyParameterInteger(component);
144 }
145 }
146}
static ReadOnlySpan< byte > ReadIntegerBytes(ReadOnlySpan< byte > source, AsnEncodingRules ruleSet, out int bytesConsumed, Asn1Tag? expectedTag=null)
static BigInteger ReadInteger(ReadOnlySpan< byte > source, AsnEncodingRules ruleSet, out int bytesConsumed, Asn1Tag? expectedTag=null)
bool TryEncode(Span< byte > destination, out int bytesWritten)
Definition AsnWriter.cs:173
Scope PushSequence(Asn1Tag? tag=null)
void WriteInteger(long value, Asn1Tag? tag=null)
Definition AsnWriter.cs:665
void PopSequence(Asn1Tag? tag=null)
static string Cryptography_Der_Invalid_Encoding
Definition SR.cs:50
Definition SR.cs:7
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
static void ReadEncryptedPkcs8(ReadOnlySpan< byte > source, ReadOnlySpan< byte > passwordBytes, out int bytesRead, out DSAParameters key)
static void WriteAlgorithmId(AsnWriter writer, in DSAParameters dsaParameters)
static AsnWriter WriteSubjectPublicKeyInfo(in DSAParameters dsaParameters)
static void WriteKeyComponent(AsnWriter writer, byte[] component, bool bitString)
static void ReadSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead, out DSAParameters key)
static void ReadEncryptedPkcs8(ReadOnlySpan< byte > source, ReadOnlySpan< char > password, out int bytesRead, out DSAParameters key)
static void ReadDsaPrivateKey(ReadOnlyMemory< byte > xBytes, in AlgorithmIdentifierAsn algId, out DSAParameters ret)
static void ReadDsaPublicKey(ReadOnlyMemory< byte > yBytes, in AlgorithmIdentifierAsn algId, out DSAParameters ret)
static void ReadPkcs8(ReadOnlySpan< byte > source, out int bytesRead, out DSAParameters key)
static AsnWriter WritePkcs8(in DSAParameters dsaParameters)
static ReadOnlyMemory< byte > ReadSubjectPublicKeyInfo(string[] validOids, ReadOnlyMemory< byte > source, out int bytesRead)
static ReadOnlyMemory< byte > ReadPkcs8(string[] validOids, ReadOnlyMemory< byte > source, out int bytesRead)
static BigInteger ModPow(BigInteger value, BigInteger exponent, BigInteger modulus)
unsafe ReadOnlySpan< T > Span
static DssParms Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
Definition DssParms.cs:14