|
| 1 | +#nullable enable |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Security.Cryptography; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +using Org.BouncyCastle.Crypto.Generators; |
| 10 | +using Org.BouncyCastle.Crypto.Parameters; |
| 11 | + |
| 12 | +using Renci.SshNet.Abstractions; |
| 13 | +using Renci.SshNet.Common; |
| 14 | +using Renci.SshNet.Security; |
| 15 | +using Renci.SshNet.Security.Cryptography.Ciphers; |
| 16 | + |
| 17 | +namespace Renci.SshNet |
| 18 | +{ |
| 19 | + public partial class PrivateKeyFile |
| 20 | + { |
| 21 | + private sealed class PuTTY : IPrivateKeyParser |
| 22 | + { |
| 23 | + private readonly string _version; |
| 24 | + private readonly string _algorithmName; |
| 25 | + private readonly string _encryptionType; |
| 26 | + private readonly string _comment; |
| 27 | + private readonly byte[] _publicKey; |
| 28 | + private readonly string? _argon2Type; |
| 29 | + private readonly string? _argon2Salt; |
| 30 | + private readonly string? _argon2Iterations; |
| 31 | + private readonly string? _argon2Memory; |
| 32 | + private readonly string? _argon2Parallelism; |
| 33 | + private readonly byte[] _data; |
| 34 | + private readonly string _mac; |
| 35 | + private readonly string? _passPhrase; |
| 36 | + |
| 37 | + public PuTTY(string version, string algorithmName, string encryptionType, string comment, byte[] publicKey, string? argon2Type, string? argon2Salt, string? argon2Iterations, string? argon2Memory, string? argon2Parallelism, byte[] data, string mac, string? passPhrase) |
| 38 | + { |
| 39 | + _version = version; |
| 40 | + _algorithmName = algorithmName; |
| 41 | + _encryptionType = encryptionType; |
| 42 | + _comment = comment; |
| 43 | + _publicKey = publicKey; |
| 44 | + _argon2Type = argon2Type; |
| 45 | + _argon2Salt = argon2Salt; |
| 46 | + _argon2Iterations = argon2Iterations; |
| 47 | + _argon2Memory = argon2Memory; |
| 48 | + _argon2Parallelism = argon2Parallelism; |
| 49 | + _data = data; |
| 50 | + _mac = mac; |
| 51 | + _passPhrase = passPhrase; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Parses an PuTTY PPK key file. |
| 56 | + /// <see href="https://tartarus.org/~simon/putty-snapshots/htmldoc/AppendixC.html"/>. |
| 57 | + /// </summary> |
| 58 | + public Key Parse() |
| 59 | + { |
| 60 | + byte[] privateKey; |
| 61 | + HMAC hmac; |
| 62 | + switch (_encryptionType) |
| 63 | + { |
| 64 | + case "aes256-cbc": |
| 65 | + if (string.IsNullOrEmpty(_passPhrase)) |
| 66 | + { |
| 67 | + throw new SshPassPhraseNullOrEmptyException("Private key is encrypted but passphrase is empty."); |
| 68 | + } |
| 69 | + |
| 70 | + byte[] cipherKey; |
| 71 | + byte[] cipherIV; |
| 72 | + switch (_version) |
| 73 | + { |
| 74 | + case "3": |
| 75 | + ThrowHelper.ThrowIfNullOrEmpty(_argon2Type); |
| 76 | + ThrowHelper.ThrowIfNullOrEmpty(_argon2Iterations); |
| 77 | + ThrowHelper.ThrowIfNullOrEmpty(_argon2Memory); |
| 78 | + ThrowHelper.ThrowIfNullOrEmpty(_argon2Parallelism); |
| 79 | + ThrowHelper.ThrowIfNullOrEmpty(_argon2Salt); |
| 80 | + |
| 81 | + var keyData = Argon2( |
| 82 | + _argon2Type, |
| 83 | + Convert.ToInt32(_argon2Iterations), |
| 84 | + Convert.ToInt32(_argon2Memory), |
| 85 | + Convert.ToInt32(_argon2Parallelism), |
| 86 | +#if NET |
| 87 | + Convert.FromHexString(_argon2Salt), |
| 88 | +#else |
| 89 | + Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_argon2Salt), |
| 90 | +#endif |
| 91 | + _passPhrase); |
| 92 | + |
| 93 | + cipherKey = keyData.Take(32); |
| 94 | + cipherIV = keyData.Take(32, 16); |
| 95 | + |
| 96 | + var macKey = keyData.Take(48, 32); |
| 97 | + hmac = new HMACSHA256(macKey); |
| 98 | + |
| 99 | + break; |
| 100 | + case "2": |
| 101 | + keyData = V2KDF(_passPhrase); |
| 102 | + |
| 103 | + cipherKey = keyData.Take(32); |
| 104 | + cipherIV = new byte[16]; |
| 105 | + |
| 106 | + macKey = CryptoAbstraction.HashSHA1(Encoding.UTF8.GetBytes("putty-private-key-file-mac-key" + _passPhrase)).Take(20); |
| 107 | + hmac = new HMACSHA1(macKey); |
| 108 | + |
| 109 | + break; |
| 110 | + default: |
| 111 | + throw new SshException("PuTTY key file version " + _version + " is not supported"); |
| 112 | + } |
| 113 | + |
| 114 | + using (var cipher = new AesCipher(cipherKey, cipherIV, AesCipherMode.CBC, pkcs7Padding: false)) |
| 115 | + { |
| 116 | + privateKey = cipher.Decrypt(_data); |
| 117 | + } |
| 118 | + |
| 119 | + break; |
| 120 | + case "none": |
| 121 | + switch (_version) |
| 122 | + { |
| 123 | + case "3": |
| 124 | + hmac = new HMACSHA256(Array.Empty<byte>()); |
| 125 | + break; |
| 126 | + case "2": |
| 127 | + var macKey = CryptoAbstraction.HashSHA1(Encoding.UTF8.GetBytes("putty-private-key-file-mac-key")); |
| 128 | + hmac = new HMACSHA1(macKey); |
| 129 | + break; |
| 130 | + default: |
| 131 | + throw new SshException("PuTTY key file version " + _version + " is not supported"); |
| 132 | + } |
| 133 | + |
| 134 | + privateKey = _data; |
| 135 | + break; |
| 136 | + default: |
| 137 | + throw new SshException("Encryption " + _encryptionType + " is not supported for PuTTY key file"); |
| 138 | + } |
| 139 | + |
| 140 | + byte[] macData; |
| 141 | + using (var macStream = new SshDataStream(256)) |
| 142 | + { |
| 143 | + macStream.Write(_algorithmName, Encoding.UTF8); |
| 144 | + macStream.Write(_encryptionType, Encoding.UTF8); |
| 145 | + macStream.Write(_comment, Encoding.UTF8); |
| 146 | + macStream.WriteBinary(_publicKey); |
| 147 | + macStream.WriteBinary(privateKey); |
| 148 | + macData = macStream.ToArray(); |
| 149 | + } |
| 150 | + |
| 151 | + byte[] macValue; |
| 152 | + using (hmac) |
| 153 | + { |
| 154 | + macValue = hmac.ComputeHash(macData); |
| 155 | + } |
| 156 | +#if NET |
| 157 | + var reference = Convert.FromHexString(_mac); |
| 158 | +#else |
| 159 | + var reference = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_mac); |
| 160 | +#endif |
| 161 | + if (!macValue.SequenceEqual(reference)) |
| 162 | + { |
| 163 | + throw new SshException("MAC verification failed for PuTTY key file"); |
| 164 | + } |
| 165 | + |
| 166 | + var publicKeyReader = new SshDataReader(_publicKey); |
| 167 | + var keyType = publicKeyReader.ReadString(Encoding.UTF8); |
| 168 | + Debug.Assert(keyType == _algorithmName, $"{nameof(keyType)} is not the same as {nameof(_algorithmName)}"); |
| 169 | + |
| 170 | + var privateKeyReader = new SshDataReader(privateKey); |
| 171 | + |
| 172 | + Key parsedKey; |
| 173 | + |
| 174 | + switch (keyType) |
| 175 | + { |
| 176 | + case "ssh-ed25519": |
| 177 | + parsedKey = new ED25519Key(privateKeyReader.ReadBignum2()); |
| 178 | + break; |
| 179 | + case "ecdsa-sha2-nistp256": |
| 180 | + case "ecdsa-sha2-nistp384": |
| 181 | + case "ecdsa-sha2-nistp521": |
| 182 | + var curve = publicKeyReader.ReadString(Encoding.ASCII); |
| 183 | + var pub = publicKeyReader.ReadBignum2(); |
| 184 | + var prv = privateKeyReader.ReadBignum2(); |
| 185 | + parsedKey = new EcdsaKey(curve, pub, prv); |
| 186 | + break; |
| 187 | + case "ssh-dss": |
| 188 | + var p = publicKeyReader.ReadBignum(); |
| 189 | + var q = publicKeyReader.ReadBignum(); |
| 190 | + var g = publicKeyReader.ReadBignum(); |
| 191 | + var y = publicKeyReader.ReadBignum(); |
| 192 | + var x = privateKeyReader.ReadBignum(); |
| 193 | + parsedKey = new DsaKey(p, q, g, y, x); |
| 194 | + break; |
| 195 | + case "ssh-rsa": |
| 196 | + var exponent = publicKeyReader.ReadBignum(); // e |
| 197 | + var modulus = publicKeyReader.ReadBignum(); // n |
| 198 | + var d = privateKeyReader.ReadBignum(); // d |
| 199 | + p = privateKeyReader.ReadBignum(); // p |
| 200 | + q = privateKeyReader.ReadBignum(); // q |
| 201 | + var inverseQ = privateKeyReader.ReadBignum(); // iqmp |
| 202 | + parsedKey = new RsaKey(modulus, exponent, d, p, q, inverseQ); |
| 203 | + break; |
| 204 | + default: |
| 205 | + throw new SshException("Key type " + keyType + " is not supported for PuTTY key file"); |
| 206 | + } |
| 207 | + |
| 208 | + parsedKey.Comment = _comment; |
| 209 | + return parsedKey; |
| 210 | + } |
| 211 | + |
| 212 | + private static byte[] Argon2(string type, int iterations, int memory, int parallelism, byte[] salt, string passPhrase) |
| 213 | + { |
| 214 | + int param; |
| 215 | + switch (type) |
| 216 | + { |
| 217 | + case "Argon2i": |
| 218 | + param = Argon2Parameters.Argon2i; |
| 219 | + break; |
| 220 | + case "Argon2d": |
| 221 | + param = Argon2Parameters.Argon2d; |
| 222 | + break; |
| 223 | + case "Argon2id": |
| 224 | + param = Argon2Parameters.Argon2id; |
| 225 | + break; |
| 226 | + default: |
| 227 | + throw new SshException("KDF " + type + " is not supported for PuTTY key file"); |
| 228 | + } |
| 229 | + |
| 230 | + var a2p = new Argon2Parameters.Builder(param) |
| 231 | + .WithVersion(Argon2Parameters.Version13) |
| 232 | + .WithIterations(iterations) |
| 233 | + .WithMemoryAsKB(memory) |
| 234 | + .WithParallelism(parallelism) |
| 235 | + .WithSalt(salt).Build(); |
| 236 | + |
| 237 | + var generator = new Argon2BytesGenerator(); |
| 238 | + |
| 239 | + generator.Init(a2p); |
| 240 | + |
| 241 | + var output = new byte[80]; |
| 242 | + var bytes = generator.GenerateBytes(passPhrase.ToCharArray(), output); |
| 243 | + |
| 244 | + if (bytes != output.Length) |
| 245 | + { |
| 246 | + throw new SshException("Failed to generate key via Argon2"); |
| 247 | + } |
| 248 | + |
| 249 | + return output; |
| 250 | + } |
| 251 | + |
| 252 | + private static byte[] V2KDF(string passPhrase) |
| 253 | + { |
| 254 | + var cipherKey = new List<byte>(); |
| 255 | + |
| 256 | + var passPhraseBytes = Encoding.UTF8.GetBytes(passPhrase); |
| 257 | + for (var sequenceNumber = 0; sequenceNumber < 2; sequenceNumber++) |
| 258 | + { |
| 259 | + using (var sha1 = SHA1.Create()) |
| 260 | + { |
| 261 | + var sequence = new byte[] { 0, 0, 0, (byte)sequenceNumber }; |
| 262 | + _ = sha1.TransformBlock(sequence, 0, 4, outputBuffer: null, 0); |
| 263 | + _ = sha1.TransformFinalBlock(passPhraseBytes, 0, passPhraseBytes.Length); |
| 264 | + Debug.Assert(sha1.Hash != null, "Hash is null"); |
| 265 | + cipherKey.AddRange(sha1.Hash); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + return cipherKey.ToArray(); |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | +} |
0 commit comments