2 // PKCS8.cs: PKCS #8 - Private-Key Information Syntax Standard
3 // ftp://ftp.rsasecurity.com/pub/pkcs/doc/pkcs-8.doc
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Security.Cryptography;
36 using Mono.Security.X509;
38 namespace Mono.Security.Cryptography {
57 static public KeyInfo GetType (byte[] data)
60 throw new ArgumentNullException ("data");
62 KeyInfo ki = KeyInfo.Unknown;
64 ASN1 top = new ASN1 (data);
65 if ((top.Tag == 0x30) && (top.Count > 0)) {
66 ASN1 firstLevel = top [0];
67 switch (firstLevel.Tag) {
69 ki = KeyInfo.PrivateKey;
72 ki = KeyInfo.EncryptedPrivateKey;
78 throw new CryptographicException ("invalid ASN.1 data");
84 * PrivateKeyInfo ::= SEQUENCE {
86 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
87 * privateKey PrivateKey,
88 * attributes [0] IMPLICIT Attributes OPTIONAL
93 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
95 * PrivateKey ::= OCTET STRING
97 * Attributes ::= SET OF Attribute
99 public class PrivateKeyInfo {
101 private int _version;
102 private string _algorithm;
104 private ArrayList _list;
106 public PrivateKeyInfo ()
109 _list = new ArrayList ();
112 public PrivateKeyInfo (byte[] data) : this ()
119 public string Algorithm {
120 get { return _algorithm; }
121 set { _algorithm = value; }
124 public ArrayList Attributes {
125 get { return _list; }
128 public byte[] PrivateKey {
132 return (byte[]) _key.Clone ();
136 throw new ArgumentNullException ("PrivateKey");
137 _key = (byte[]) value.Clone ();
142 get { return _version; }
145 throw new ArgumentOutOfRangeException ("negative version");
152 private void Decode (byte[] data)
154 ASN1 privateKeyInfo = new ASN1 (data);
155 if (privateKeyInfo.Tag != 0x30)
156 throw new CryptographicException ("invalid PrivateKeyInfo");
158 ASN1 version = privateKeyInfo [0];
159 if (version.Tag != 0x02)
160 throw new CryptographicException ("invalid version");
161 _version = version.Value [0];
163 ASN1 privateKeyAlgorithm = privateKeyInfo [1];
164 if (privateKeyAlgorithm.Tag != 0x30)
165 throw new CryptographicException ("invalid algorithm");
167 ASN1 algorithm = privateKeyAlgorithm [0];
168 if (algorithm.Tag != 0x06)
169 throw new CryptographicException ("missing algorithm OID");
170 _algorithm = ASN1Convert.ToOid (algorithm);
172 ASN1 privateKey = privateKeyInfo [2];
173 _key = privateKey.Value;
175 // attributes [0] IMPLICIT Attributes OPTIONAL
176 if (privateKeyInfo.Count > 3) {
177 ASN1 attributes = privateKeyInfo [3];
178 for (int i=0; i < attributes.Count; i++) {
179 _list.Add (attributes [i]);
184 public byte[] GetBytes ()
186 ASN1 privateKeyAlgorithm = new ASN1 (0x30);
187 privateKeyAlgorithm.Add (ASN1Convert.FromOid (_algorithm));
188 privateKeyAlgorithm.Add (new ASN1 (0x05)); // ASN.1 NULL
190 ASN1 pki = new ASN1 (0x30);
191 pki.Add (new ASN1 (0x02, new byte [1] { (byte) _version }));
192 pki.Add (privateKeyAlgorithm);
193 pki.Add (new ASN1 (0x04, _key));
195 if (_list.Count > 0) {
196 ASN1 attributes = new ASN1 (0xA0);
197 foreach (ASN1 attribute in _list) {
198 attributes.Add (attribute);
200 pki.Add (attributes);
203 return pki.GetBytes ();
208 static private byte[] RemoveLeadingZero (byte[] bigInt)
211 int length = bigInt.Length;
212 if (bigInt [0] == 0x00) {
216 byte[] bi = new byte [length];
217 Buffer.BlockCopy (bigInt, start, bi, 0, length);
221 static private byte[] Normalize (byte[] bigInt, int length)
223 if (bigInt.Length == length)
225 else if (bigInt.Length > length)
226 return RemoveLeadingZero (bigInt);
229 byte[] bi = new byte [length];
230 Buffer.BlockCopy (bigInt, 0, bi, (length - bigInt.Length), bigInt.Length);
236 * RSAPrivateKey ::= SEQUENCE {
238 * modulus INTEGER, -- n
239 * publicExponent INTEGER, -- e
240 * privateExponent INTEGER, -- d
241 * prime1 INTEGER, -- p
242 * prime2 INTEGER, -- q
243 * exponent1 INTEGER, -- d mod (p-1)
244 * exponent2 INTEGER, -- d mod (q-1)
245 * coefficient INTEGER, -- (inverse of q) mod p
246 * otherPrimeInfos OtherPrimeInfos OPTIONAL
249 static public RSA DecodeRSA (byte[] keypair)
251 ASN1 privateKey = new ASN1 (keypair);
252 if (privateKey.Tag != 0x30)
253 throw new CryptographicException ("invalid private key format");
255 ASN1 version = privateKey [0];
256 if (version.Tag != 0x02)
257 throw new CryptographicException ("missing version");
259 if (privateKey.Count < 9)
260 throw new CryptographicException ("not enough key parameters");
262 RSAParameters param = new RSAParameters ();
263 // note: MUST remove leading 0 - else MS wont import the key
264 param.Modulus = RemoveLeadingZero (privateKey [1].Value);
265 int keysize = param.Modulus.Length;
266 int keysize2 = (keysize >> 1); // half-size
267 // size must be normalized - else MS wont import the key
268 param.D = Normalize (privateKey [3].Value, keysize);
269 param.DP = Normalize (privateKey [6].Value, keysize2);
270 param.DQ = Normalize (privateKey [7].Value, keysize2);
271 param.Exponent = RemoveLeadingZero (privateKey [2].Value);
272 param.InverseQ = Normalize (privateKey [8].Value, keysize2);
273 param.P = Normalize (privateKey [4].Value, keysize2);
274 param.Q = Normalize (privateKey [5].Value, keysize2);
276 RSA rsa = RSA.Create ();
277 rsa.ImportParameters (param);
282 * RSAPrivateKey ::= SEQUENCE {
284 * modulus INTEGER, -- n
285 * publicExponent INTEGER, -- e
286 * privateExponent INTEGER, -- d
287 * prime1 INTEGER, -- p
288 * prime2 INTEGER, -- q
289 * exponent1 INTEGER, -- d mod (p-1)
290 * exponent2 INTEGER, -- d mod (q-1)
291 * coefficient INTEGER, -- (inverse of q) mod p
292 * otherPrimeInfos OtherPrimeInfos OPTIONAL
295 static public byte[] Encode (RSA rsa)
297 RSAParameters param = rsa.ExportParameters (true);
299 ASN1 rsaPrivateKey = new ASN1 (0x30);
300 rsaPrivateKey.Add (new ASN1 (0x02, new byte [1] { 0x00 }));
301 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.Modulus));
302 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.Exponent));
303 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.D));
304 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.P));
305 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.Q));
306 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.DP));
307 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.DQ));
308 rsaPrivateKey.Add (ASN1Convert.FromUnsignedBigInteger (param.InverseQ));
310 return rsaPrivateKey.GetBytes ();
313 // DSA only encode it's X private key inside an ASN.1 INTEGER (Hint: Tag == 0x02)
314 // which isn't enough for rebuilding the keypair. The other parameters
315 // can be found (98% of the time) in the X.509 certificate associated
316 // with the private key or (2% of the time) the parameters are in it's
317 // issuer X.509 certificate (not supported in the .NET framework).
318 static public DSA DecodeDSA (byte[] privateKey, DSAParameters dsaParameters)
320 ASN1 pvk = new ASN1 (privateKey);
322 throw new CryptographicException ("invalid private key format");
324 // X is ALWAYS 20 bytes (no matter if the key length is 512 or 1024 bits)
325 dsaParameters.X = Normalize (privateKey, 20);
326 DSA dsa = DSA.Create ();
327 dsa.ImportParameters (dsaParameters);
331 static public byte[] Encode (DSA dsa)
333 DSAParameters param = dsa.ExportParameters (true);
334 return ASN1Convert.FromUnsignedBigInteger (param.X).GetBytes ();
337 static public byte[] Encode (AsymmetricAlgorithm aa)
340 return Encode ((RSA)aa);
342 return Encode ((DSA)aa);
344 throw new CryptographicException ("Unknown asymmetric algorithm {0}", aa.ToString ());
349 * EncryptedPrivateKeyInfo ::= SEQUENCE {
350 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
351 * encryptedData EncryptedData
354 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
356 * EncryptedData ::= OCTET STRING
359 * AlgorithmIdentifier ::= SEQUENCE {
360 * algorithm OBJECT IDENTIFIER,
361 * parameters ANY DEFINED BY algorithm OPTIONAL
365 * PBEParameter ::= SEQUENCE {
366 * salt OCTET STRING SIZE(8),
367 * iterationCount INTEGER
370 public class EncryptedPrivateKeyInfo {
372 private string _algorithm;
373 private byte[] _salt;
374 private int _iterations;
375 private byte[] _data;
377 public EncryptedPrivateKeyInfo () {}
379 public EncryptedPrivateKeyInfo (byte[] data) : this ()
386 public string Algorithm {
387 get { return _algorithm; }
388 set { _algorithm = value; }
391 public byte[] EncryptedData {
392 get { return (_data == null) ? null : (byte[]) _data.Clone (); }
393 set { _data = (value == null) ? null : (byte[]) value.Clone (); }
399 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
400 _salt = new byte [8];
401 rng.GetBytes (_salt);
403 return (byte[]) _salt.Clone ();
405 set { _salt = (byte[]) value.Clone (); }
408 public int IterationCount {
409 get { return _iterations; }
412 throw new ArgumentOutOfRangeException ("IterationCount", "Negative");
419 private void Decode (byte[] data)
421 ASN1 encryptedPrivateKeyInfo = new ASN1 (data);
422 if (encryptedPrivateKeyInfo.Tag != 0x30)
423 throw new CryptographicException ("invalid EncryptedPrivateKeyInfo");
425 ASN1 encryptionAlgorithm = encryptedPrivateKeyInfo [0];
426 if (encryptionAlgorithm.Tag != 0x30)
427 throw new CryptographicException ("invalid encryptionAlgorithm");
428 ASN1 algorithm = encryptionAlgorithm [0];
429 if (algorithm.Tag != 0x06)
430 throw new CryptographicException ("invalid algorithm");
431 _algorithm = ASN1Convert.ToOid (algorithm);
432 // parameters ANY DEFINED BY algorithm OPTIONAL
433 if (encryptionAlgorithm.Count > 1) {
434 ASN1 parameters = encryptionAlgorithm [1];
435 if (parameters.Tag != 0x30)
436 throw new CryptographicException ("invalid parameters");
438 ASN1 salt = parameters [0];
439 if (salt.Tag != 0x04)
440 throw new CryptographicException ("invalid salt");
443 ASN1 iterationCount = parameters [1];
444 if (iterationCount.Tag != 0x02)
445 throw new CryptographicException ("invalid iterationCount");
446 _iterations = ASN1Convert.ToInt32 (iterationCount);
449 ASN1 encryptedData = encryptedPrivateKeyInfo [1];
450 if (encryptedData.Tag != 0x04)
451 throw new CryptographicException ("invalid EncryptedData");
452 _data = encryptedData.Value;
455 // Note: PKCS#8 doesn't define how to generate the key required for encryption
456 // so you're on your own. Just don't try to copy the big guys too much ;)
457 // Netscape: http://www.cs.auckland.ac.nz/~pgut001/pubs/netscape.txt
458 // Microsoft: http://www.cs.auckland.ac.nz/~pgut001/pubs/breakms.txt
459 public byte[] GetBytes ()
461 if (_algorithm == null)
462 throw new CryptographicException ("No algorithm OID specified");
464 ASN1 encryptionAlgorithm = new ASN1 (0x30);
465 encryptionAlgorithm.Add (ASN1Convert.FromOid (_algorithm));
467 // parameters ANY DEFINED BY algorithm OPTIONAL
468 if ((_iterations > 0) || (_salt != null)) {
469 ASN1 salt = new ASN1 (0x04, _salt);
470 ASN1 iterations = ASN1Convert.FromInt32 (_iterations);
472 ASN1 parameters = new ASN1 (0x30);
473 parameters.Add (salt);
474 parameters.Add (iterations);
475 encryptionAlgorithm.Add (parameters);
478 // encapsulates EncryptedData into an OCTET STRING
479 ASN1 encryptedData = new ASN1 (0x04, _data);
481 ASN1 encryptedPrivateKeyInfo = new ASN1 (0x30);
482 encryptedPrivateKeyInfo.Add (encryptionAlgorithm);
483 encryptedPrivateKeyInfo.Add (encryptedData);
485 return encryptedPrivateKeyInfo.GetBytes ();