Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RSAKeyFormatHelper.cs
Go to the documentation of this file.
3
5
6internal static class RSAKeyFormatHelper
7{
8 private static readonly string[] s_validOids = new string[1] { "1.2.840.113549.1.1.1" };
9
10 internal static void FromPkcs1PrivateKey(ReadOnlyMemory<byte> keyData, in AlgorithmIdentifierAsn algId, out RSAParameters ret)
11 {
12 RSAPrivateKeyAsn rSAPrivateKeyAsn = RSAPrivateKeyAsn.Decode(keyData, AsnEncodingRules.BER);
13 if (!algId.HasNullEquivalentParameters())
14 {
16 }
17 if (rSAPrivateKeyAsn.Version > 0)
18 {
20 }
21 byte[] array = rSAPrivateKeyAsn.Modulus.ToByteArray(isUnsigned: true, isBigEndian: true);
22 int length = (array.Length + 1) / 2;
23 ret = new RSAParameters
24 {
25 Modulus = array,
26 Exponent = rSAPrivateKeyAsn.PublicExponent.ToByteArray(isUnsigned: true, isBigEndian: true),
27 D = rSAPrivateKeyAsn.PrivateExponent.ExportKeyParameter(array.Length),
28 P = rSAPrivateKeyAsn.Prime1.ExportKeyParameter(length),
29 Q = rSAPrivateKeyAsn.Prime2.ExportKeyParameter(length),
30 DP = rSAPrivateKeyAsn.Exponent1.ExportKeyParameter(length),
31 DQ = rSAPrivateKeyAsn.Exponent2.ExportKeyParameter(length),
32 InverseQ = rSAPrivateKeyAsn.Coefficient.ExportKeyParameter(length)
33 };
34 }
35
36 internal static void ReadRsaPublicKey(ReadOnlyMemory<byte> keyData, in AlgorithmIdentifierAsn algId, out RSAParameters ret)
37 {
38 RSAPublicKeyAsn rSAPublicKeyAsn = RSAPublicKeyAsn.Decode(keyData, AsnEncodingRules.BER);
39 ret = new RSAParameters
40 {
41 Modulus = rSAPublicKeyAsn.Modulus.ToByteArray(isUnsigned: true, isBigEndian: true),
42 Exponent = rSAPublicKeyAsn.PublicExponent.ToByteArray(isUnsigned: true, isBigEndian: true)
43 };
44 }
45
50
51 internal static ReadOnlyMemory<byte> ReadPkcs8(ReadOnlyMemory<byte> source, out int bytesRead)
52 {
53 return KeyFormatHelper.ReadPkcs8(s_validOids, source, out bytesRead);
54 }
55
57 {
58 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
59 asnWriter.PushSequence();
60 WriteAlgorithmIdentifier(asnWriter);
61 asnWriter.WriteBitString(pkcs1PublicKey);
62 asnWriter.PopSequence();
63 return asnWriter;
64 }
65
66 internal static AsnWriter WritePkcs8PrivateKey(ReadOnlySpan<byte> pkcs1PrivateKey, AsnWriter copyFrom = null)
67 {
68 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.BER);
69 using (asnWriter.PushSequence())
70 {
71 asnWriter.WriteInteger(0L);
72 WriteAlgorithmIdentifier(asnWriter);
73 if (copyFrom != null)
74 {
75 using (asnWriter.PushOctetString())
76 {
77 copyFrom.CopyTo(asnWriter);
78 }
79 }
80 else
81 {
82 asnWriter.WriteOctetString(pkcs1PrivateKey);
83 }
84 }
85 return asnWriter;
86 }
87
89 {
90 writer.PushSequence();
91 writer.WriteObjectIdentifier("1.2.840.113549.1.1.1");
92 writer.WriteNull();
93 writer.PopSequence();
94 }
95
96 internal static AsnWriter WritePkcs1PublicKey(in RSAParameters rsaParameters)
97 {
98 if (rsaParameters.Modulus == null || rsaParameters.Exponent == null)
99 {
101 }
102 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
103 asnWriter.PushSequence();
104 asnWriter.WriteKeyParameterInteger(rsaParameters.Modulus);
105 asnWriter.WriteKeyParameterInteger(rsaParameters.Exponent);
106 asnWriter.PopSequence();
107 return asnWriter;
108 }
109
110 internal static AsnWriter WritePkcs1PrivateKey(in RSAParameters rsaParameters)
111 {
112 if (rsaParameters.Modulus == null || rsaParameters.Exponent == null)
113 {
115 }
116 if (rsaParameters.D == null || rsaParameters.P == null || rsaParameters.Q == null || rsaParameters.DP == null || rsaParameters.DQ == null || rsaParameters.InverseQ == null)
117 {
119 }
120 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
121 asnWriter.PushSequence();
122 asnWriter.WriteInteger(0L);
123 asnWriter.WriteKeyParameterInteger(rsaParameters.Modulus);
124 asnWriter.WriteKeyParameterInteger(rsaParameters.Exponent);
125 asnWriter.WriteKeyParameterInteger(rsaParameters.D);
126 asnWriter.WriteKeyParameterInteger(rsaParameters.P);
127 asnWriter.WriteKeyParameterInteger(rsaParameters.Q);
128 asnWriter.WriteKeyParameterInteger(rsaParameters.DP);
129 asnWriter.WriteKeyParameterInteger(rsaParameters.DQ);
130 asnWriter.WriteKeyParameterInteger(rsaParameters.InverseQ);
131 asnWriter.PopSequence();
132 return asnWriter;
133 }
134
135 internal static void ReadEncryptedPkcs8(ReadOnlySpan<byte> source, ReadOnlySpan<char> password, out int bytesRead, out RSAParameters key)
136 {
137 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, password, (KeyFormatHelper.KeyReader<RSAParameters>)FromPkcs1PrivateKey, out bytesRead, out key);
138 }
139
140 internal static void ReadEncryptedPkcs8(ReadOnlySpan<byte> source, ReadOnlySpan<byte> passwordBytes, out int bytesRead, out RSAParameters key)
141 {
142 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, passwordBytes, (KeyFormatHelper.KeyReader<RSAParameters>)FromPkcs1PrivateKey, out bytesRead, out key);
143 }
144}
void WriteOctetString(ReadOnlySpan< byte > value, Asn1Tag? tag=null)
Definition AsnWriter.cs:904
Scope PushSequence(Asn1Tag? tag=null)
Scope PushOctetString(Asn1Tag? tag=null)
Definition AsnWriter.cs:892
void WriteInteger(long value, Asn1Tag? tag=null)
Definition AsnWriter.cs:665
void WriteBitString(ReadOnlySpan< byte > value, int unusedBitCount=0, Asn1Tag? tag=null)
Definition AsnWriter.cs:485
void PopSequence(Asn1Tag? tag=null)
static string Cryptography_NotValidPrivateKey
Definition SR.cs:120
static string Cryptography_Der_Invalid_Encoding
Definition SR.cs:50
static string Cryptography_RSAPrivateKey_VersionTooNew
Definition SR.cs:140
static string Cryptography_InvalidRsaParameters
Definition SR.cs:108
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
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 ReadOnlyMemory< byte > ReadSubjectPublicKeyInfo(ReadOnlyMemory< byte > source, out int bytesRead)
static AsnWriter WriteSubjectPublicKeyInfo(ReadOnlySpan< byte > pkcs1PublicKey)
static void FromPkcs1PrivateKey(ReadOnlyMemory< byte > keyData, in AlgorithmIdentifierAsn algId, out RSAParameters ret)
static ReadOnlyMemory< byte > ReadPkcs8(ReadOnlyMemory< byte > source, out int bytesRead)
static AsnWriter WritePkcs1PrivateKey(in RSAParameters rsaParameters)
static void ReadEncryptedPkcs8(ReadOnlySpan< byte > source, ReadOnlySpan< byte > passwordBytes, out int bytesRead, out RSAParameters key)
static void ReadEncryptedPkcs8(ReadOnlySpan< byte > source, ReadOnlySpan< char > password, out int bytesRead, out RSAParameters key)
static AsnWriter WritePkcs1PublicKey(in RSAParameters rsaParameters)
static void ReadRsaPublicKey(ReadOnlyMemory< byte > keyData, in AlgorithmIdentifierAsn algId, out RSAParameters ret)
static AsnWriter WritePkcs8PrivateKey(ReadOnlySpan< byte > pkcs1PrivateKey, AsnWriter copyFrom=null)
static void WriteAlgorithmIdentifier(AsnWriter writer)
static RSAPrivateKeyAsn Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
static RSAPublicKeyAsn Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)