| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
using System; |
| 31 |
using System.Security.Cryptography; |
| 32 |
|
| 33 |
#if !(SILVERLIGHT || READ_ONLY) |
| 34 |
|
| 35 |
namespace Mono.Security.Cryptography { |
| 36 |
|
| 37 |
static class CryptoConvert { |
| 38 |
|
| 39 |
static private int ToInt32LE (byte [] bytes, int offset) |
| 40 |
{ |
| 41 |
return (bytes [offset+3] << 24) | (bytes [offset+2] << 16) | (bytes [offset+1] << 8) | bytes [offset]; |
| 42 |
} |
| 43 |
|
| 44 |
static private uint ToUInt32LE (byte [] bytes, int offset) |
| 45 |
{ |
| 46 |
return (uint)((bytes [offset+3] << 24) | (bytes [offset+2] << 16) | (bytes [offset+1] << 8) | bytes [offset]); |
| 47 |
} |
| 48 |
|
| 49 |
static private byte[] Trim (byte[] array) |
| 50 |
{ |
| 51 |
for (int i=0; i < array.Length; i++) { |
| 52 |
if (array [i] != 0x00) { |
| 53 |
byte[] result = new byte [array.Length - i]; |
| 54 |
Buffer.BlockCopy (array, i, result, 0, result.Length); |
| 55 |
return result; |
| 56 |
} |
| 57 |
} |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
static RSA FromCapiPrivateKeyBlob (byte[] blob, int offset) |
| 62 |
{ |
| 63 |
RSAParameters rsap = new RSAParameters (); |
| 64 |
try { |
| 65 |
if ((blob [offset] != 0x07) || |
| 66 |
(blob [offset+1] != 0x02) || |
| 67 |
(blob [offset+2] != 0x00) || |
| 68 |
(blob [offset+3] != 0x00) || |
| 69 |
(ToUInt32LE (blob, offset+8) != 0x32415352)) |
| 70 |
throw new CryptographicException ("Invalid blob header"); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int bitLen = ToInt32LE (blob, offset+12); |
| 77 |
|
| 78 |
|
| 79 |
byte[] exp = new byte [4]; |
| 80 |
Buffer.BlockCopy (blob, offset+16, exp, 0, 4); |
| 81 |
Array.Reverse (exp); |
| 82 |
rsap.Exponent = Trim (exp); |
| 83 |
|
| 84 |
int pos = offset+20; |
| 85 |
|
| 86 |
int byteLen = (bitLen >> 3); |
| 87 |
rsap.Modulus = new byte [byteLen]; |
| 88 |
Buffer.BlockCopy (blob, pos, rsap.Modulus, 0, byteLen); |
| 89 |
Array.Reverse (rsap.Modulus); |
| 90 |
pos += byteLen; |
| 91 |
|
| 92 |
|
| 93 |
int byteHalfLen = (byteLen >> 1); |
| 94 |
rsap.P = new byte [byteHalfLen]; |
| 95 |
Buffer.BlockCopy (blob, pos, rsap.P, 0, byteHalfLen); |
| 96 |
Array.Reverse (rsap.P); |
| 97 |
pos += byteHalfLen; |
| 98 |
|
| 99 |
|
| 100 |
rsap.Q = new byte [byteHalfLen]; |
| 101 |
Buffer.BlockCopy (blob, pos, rsap.Q, 0, byteHalfLen); |
| 102 |
Array.Reverse (rsap.Q); |
| 103 |
pos += byteHalfLen; |
| 104 |
|
| 105 |
|
| 106 |
rsap.DP = new byte [byteHalfLen]; |
| 107 |
Buffer.BlockCopy (blob, pos, rsap.DP, 0, byteHalfLen); |
| 108 |
Array.Reverse (rsap.DP); |
| 109 |
pos += byteHalfLen; |
| 110 |
|
| 111 |
|
| 112 |
rsap.DQ = new byte [byteHalfLen]; |
| 113 |
Buffer.BlockCopy (blob, pos, rsap.DQ, 0, byteHalfLen); |
| 114 |
Array.Reverse (rsap.DQ); |
| 115 |
pos += byteHalfLen; |
| 116 |
|
| 117 |
|
| 118 |
rsap.InverseQ = new byte [byteHalfLen]; |
| 119 |
Buffer.BlockCopy (blob, pos, rsap.InverseQ, 0, byteHalfLen); |
| 120 |
Array.Reverse (rsap.InverseQ); |
| 121 |
pos += byteHalfLen; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
rsap.D = new byte [byteLen]; |
| 127 |
if (pos + byteLen + offset <= blob.Length) { |
| 128 |
|
| 129 |
Buffer.BlockCopy (blob, pos, rsap.D, 0, byteLen); |
| 130 |
Array.Reverse (rsap.D); |
| 131 |
} |
| 132 |
} |
| 133 |
catch (Exception e) { |
| 134 |
throw new CryptographicException ("Invalid blob.", e); |
| 135 |
} |
| 136 |
|
| 137 |
RSA rsa = null; |
| 138 |
try { |
| 139 |
rsa = RSA.Create (); |
| 140 |
rsa.ImportParameters (rsap); |
| 141 |
} |
| 142 |
catch (CryptographicException ce) { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
try { |
| 147 |
CspParameters csp = new CspParameters (); |
| 148 |
csp.Flags = CspProviderFlags.UseMachineKeyStore; |
| 149 |
rsa = new RSACryptoServiceProvider (csp); |
| 150 |
rsa.ImportParameters (rsap); |
| 151 |
} |
| 152 |
catch { |
| 153 |
|
| 154 |
throw ce; |
| 155 |
} |
| 156 |
} |
| 157 |
return rsa; |
| 158 |
} |
| 159 |
|
| 160 |
static RSA FromCapiPublicKeyBlob (byte[] blob, int offset) |
| 161 |
{ |
| 162 |
try { |
| 163 |
if ((blob [offset] != 0x06) || |
| 164 |
(blob [offset+1] != 0x02) || |
| 165 |
(blob [offset+2] != 0x00) || |
| 166 |
(blob [offset+3] != 0x00) || |
| 167 |
(ToUInt32LE (blob, offset+8) != 0x31415352)) |
| 168 |
throw new CryptographicException ("Invalid blob header"); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
int bitLen = ToInt32LE (blob, offset+12); |
| 175 |
|
| 176 |
|
| 177 |
RSAParameters rsap = new RSAParameters (); |
| 178 |
rsap.Exponent = new byte [3]; |
| 179 |
rsap.Exponent [0] = blob [offset+18]; |
| 180 |
rsap.Exponent [1] = blob [offset+17]; |
| 181 |
rsap.Exponent [2] = blob [offset+16]; |
| 182 |
|
| 183 |
int pos = offset+20; |
| 184 |
|
| 185 |
int byteLen = (bitLen >> 3); |
| 186 |
rsap.Modulus = new byte [byteLen]; |
| 187 |
Buffer.BlockCopy (blob, pos, rsap.Modulus, 0, byteLen); |
| 188 |
Array.Reverse (rsap.Modulus); |
| 189 |
|
| 190 |
RSA rsa = null; |
| 191 |
try { |
| 192 |
rsa = RSA.Create (); |
| 193 |
rsa.ImportParameters (rsap); |
| 194 |
} |
| 195 |
catch (CryptographicException) { |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
CspParameters csp = new CspParameters (); |
| 200 |
csp.Flags = CspProviderFlags.UseMachineKeyStore; |
| 201 |
rsa = new RSACryptoServiceProvider (csp); |
| 202 |
rsa.ImportParameters (rsap); |
| 203 |
} |
| 204 |
return rsa; |
| 205 |
} |
| 206 |
catch (Exception e) { |
| 207 |
throw new CryptographicException ("Invalid blob.", e); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static public RSA FromCapiKeyBlob (byte[] blob) |
| 214 |
{ |
| 215 |
return FromCapiKeyBlob (blob, 0); |
| 216 |
} |
| 217 |
|
| 218 |
static public RSA FromCapiKeyBlob (byte[] blob, int offset) |
| 219 |
{ |
| 220 |
if (blob == null) |
| 221 |
throw new ArgumentNullException ("blob"); |
| 222 |
if (offset >= blob.Length) |
| 223 |
throw new ArgumentException ("blob is too small."); |
| 224 |
|
| 225 |
switch (blob [offset]) { |
| 226 |
case 0x00: |
| 227 |
|
| 228 |
|
| 229 |
if (blob [offset + 12] == 0x06) { |
| 230 |
return FromCapiPublicKeyBlob (blob, offset + 12); |
| 231 |
} |
| 232 |
break; |
| 233 |
case 0x06: |
| 234 |
return FromCapiPublicKeyBlob (blob, offset); |
| 235 |
case 0x07: |
| 236 |
return FromCapiPrivateKeyBlob (blob, offset); |
| 237 |
} |
| 238 |
throw new CryptographicException ("Unknown blob format."); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
#endif |