Author: atsushi
Date: 2007-04-26 11:13:29 -0400 (Thu, 26 Apr 2007)
New Revision: 76329
Modified:
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/CardSelectorClient.cs
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/ChangeLog
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/IdentityCardEncryption.cs
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/ChangeLog
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/IdentityCardEncryptionTest.cs
Log:
2007-04-26 Atsushi Enomoto <[EMAIL PROTECTED]>
* IdentityCardEncryption.cs, CardSelectorClient.cs :
implemented store encryption. Changed the reader API a bit too.
* IdentityCardEncryptionTest.cs : added encryption test (roundtrip).
Modified:
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/CardSelectorClient.cs
===================================================================
---
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/CardSelectorClient.cs
2007-04-26 15:05:46 UTC (rev 76328)
+++
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/CardSelectorClient.cs
2007-04-26 15:13:29 UTC (rev 76329)
@@ -65,7 +65,7 @@
protected IdentityCard ProcessImport (string filename, string
password)
{
string xml = new IdentityCardEncryption ().Decrypt (
- filename, password);
+ new StreamReader (filename).ReadToEnd (),
password);
IdentityCard card = new IdentityCard ();
card.Load (XmlReader.Create (new StringReader (xml)));
return card;
Modified:
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/ChangeLog
===================================================================
---
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/ChangeLog
2007-04-26 15:05:46 UTC (rev 76328)
+++
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/ChangeLog
2007-04-26 15:13:29 UTC (rev 76329)
@@ -1,3 +1,8 @@
+2007-04-26 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * IdentityCardEncryption.cs, CardSelectorClient.cs :
+ implemented store encryption. Changed the reader API a bit too.
+
2007-04-25 Atsushi Enomoto <[EMAIL PROTECTED]>
* IdentityCard.cs : implemented Save. It was rather xs:dateTime fixes.
Modified:
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/IdentityCardEncryption.cs
===================================================================
---
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/IdentityCardEncryption.cs
2007-04-26 15:05:46 UTC (rev 76328)
+++
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Mono.ServiceModel.IdentitySelectors/IdentityCardEncryption.cs
2007-04-26 15:13:29 UTC (rev 76329)
@@ -28,6 +28,7 @@
using System;
using System.Globalization;
using System.IO;
+using System.IdentityModel.Selectors;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
@@ -46,43 +47,76 @@
0xc4, 0x01, 0x7b, 0xf1, 0x6b, 0xad, 0x2f, 0x42,
0xaf, 0xf4, 0x97, 0x7d, 0x4, 0x68, 0x3, 0xdb };
- /*
- public void Encrypt (string file, string password)
+ public byte [] Encrypt (string plainText, string password)
{
- string content = new StreamReader (file,
Encoding.UTF8).ReadToEnd ();
+ byte [] salt = new byte [16];
+ RandomNumberGenerator.Create ().GetNonZeroBytes (salt);
+ return Encrypt (plainText, password, salt, null);
+ }
+
+ public byte [] Encrypt (string plainText, string password, byte
[] salt, byte [] iv)
+ {
+ MemoryStream ms = new MemoryStream ();
+ StreamWriter sw = new StreamWriter (ms, new
UTF8Encoding (true));
+ sw.Write (plainText);
+ sw.Close ();
+ byte [] plain = ms.ToArray ();
+
string ns =
"http://schemas.xmlsoap.org/ws/2005/05/identity";
string encNS = EncryptedXml.XmlEncNamespaceUrl;
- byte [] salt = new byte [16];
byte [] encKey = CreateEncryptionKey (password, salt);
byte [] intKey = CreateIntegrityKey (password, salt);
- RijndaelManaged aes = new RijndaelManaged ();
+ RijndaelManaged aes = CreateAES ();
+ if (iv == null)
+ aes.GenerateIV ();
+ else
+ aes.IV = iv;
aes.Key = encKey;
- aes.GenerateIV ();
+ iv = aes.IV;
- byte [] results = null;
+ MemoryStream cms = new MemoryStream ();
+ CryptoStream cs = new CryptoStream (cms,
aes.CreateEncryptor (), CryptoStreamMode.Write);
+ cs.Write (plain, 0, plain.Length);
+ cs.Close ();
+ byte [] results = cms.ToArray ();
- XmlWriter w = XmlWriter.Create (Console.Out);
+ byte [] clearTextBlock = CreateSubArray (plain,
plain.Length - 16, 16);
+ byte [] integrity = SHA256.Create ().ComputeHash
(JoinArray (JoinArray (iv, intKey), clearTextBlock));
+
+ ms = new MemoryStream ();
+ XmlWriter w = XmlWriter.Create (new StreamWriter (ms,
new UTF8Encoding (false))); // no BOM here
w.WriteStartElement ("EncryptedStore", ns);
w.WriteStartElement ("StoreSalt", ns);
- w.WriteString (Convert.ToBase64String (aes.IV));
+ w.WriteString (Convert.ToBase64String (salt));
w.WriteEndElement ();
w.WriteStartElement ("EncryptedData", encNS);
w.WriteStartElement ("CipherData", encNS);
w.WriteStartElement ("CipherValue", encNS);
- w.WriteString (Convert.ToBase64String (results));
+ w.WriteString (Convert.ToBase64String (JoinArray (
+ JoinArray (iv, integrity), results)));
w.WriteEndElement ();
w.WriteEndElement ();
w.WriteEndElement ();
w.WriteEndElement ();
+ w.Close ();
+ return ms.ToArray ();
}
- */
- public string Decrypt (string crdsfile, string password)
+ // content string -> salt and cipherValue
+ // cipherValue -> iv, intExpected, content
+ // iv+content -> bytesToDecrypt
+ // password+salt+encEntropy -> encKey
+ // password+salt+intEntropy -> intKey
+ // AES(iv,encKey)+bytesToDecrypt -> decrypted
+ // decrypted -> clearTextBlock
+ // iv,intKey,clearTextBlock -> intComputed
+
+ public string Decrypt (string crdsxml, string password)
{
XmlDocument doc = new XmlDocument ();
- doc.Load (crdsfile);
+ doc.LoadXml (crdsxml);
byte [] salt = Convert.FromBase64String (
doc.DocumentElement.FirstChild.FirstChild.Value);
@@ -97,14 +131,17 @@
public string Decrypt (string password, byte [] salt, byte []
cipherValue)
{
byte [] iv = CreateSubArray (cipherValue, 0, 16);
- byte [] integrity = CreateSubArray (cipherValue, 16,
32);
+ byte [] intExpected = CreateSubArray (cipherValue, 16,
32);
byte [] content = CreateSubArray (cipherValue, 48,
cipherValue.Length - 48);
// LAMESPEC: the actual content is iv+content.
- content = JoinArray (iv, content);
+ byte [] bytesToDecrypt = JoinArray (iv, content);
EncryptedData ed = new EncryptedData ();
ed.CipherData = new CipherData ();
- ed.CipherData.CipherValue = content;
+ ed.EncryptionMethod = new EncryptionMethod ();
+ ed.EncryptionMethod.KeyAlgorithm =
EncryptedXml.XmlEncAES256Url ;
+ ed.EncryptionMethod.KeySize = 256;
+ ed.CipherData.CipherValue = bytesToDecrypt;
byte [] encKey = CreateEncryptionKey (password, salt);
if (encKey.Length != 32)
@@ -112,29 +149,38 @@
byte [] intKey = CreateIntegrityKey (password, salt);
if (intKey.Length != 32)
throw new Exception ("INTERNAL ERROR:
Unexpected integrity key size: " + intKey.Length);
- RijndaelManaged aes = new RijndaelManaged ();
- aes.BlockSize = 128;
+ RijndaelManaged aes = CreateAES ();
aes.IV = iv;
aes.Key = encKey;
- aes.Padding = PaddingMode.PKCS7;
- aes.Mode = CipherMode.CBC;
- ed.EncryptionMethod = new EncryptionMethod ();
- ed.EncryptionMethod.KeyAlgorithm =
EncryptedXml.XmlEncAES256Url ;
- ed.EncryptionMethod.KeySize = 256;
+ // We need some hook to retrieve TransformFinalBlock
+ // bytes for integrity check.
+ byte [] decrypted = new EncryptedXml ().DecryptData
(ed, aes);
+ byte [] clearTextBlock = CreateSubArray (decrypted,
decrypted.Length - 16, 16);
- // FIXME: actually we need some hook to retrieve
- // TransformFinalBlock bytes for integrity check.
- byte [] bytes = new EncryptedXml ().DecryptData (ed,
aes);
+ byte [] intComputed = SHA256.Create ().ComputeHash
(JoinArray (JoinArray (iv, intKey), clearTextBlock));
+ if (!ArrayEquals (intExpected, intComputed))
+ throw new CardSpaceException (String.Format
("Integrity check failed: expected {0}, actually {1}",
+ Convert.ToBase64String (intExpected),
+ Convert.ToBase64String (intComputed)));
// since the restored bytes contain BOM, they had
// better be stripped by StreamReader (since I'm not
// sure if future versions of this data keep BOM).
- MemoryStream ms = new MemoryStream (bytes);
+ MemoryStream ms = new MemoryStream (decrypted);
string s = new StreamReader (ms,
Encoding.UTF8).ReadToEnd ();
return s;
}
+ RijndaelManaged CreateAES ()
+ {
+ RijndaelManaged aes = new RijndaelManaged ();
+ aes.BlockSize = 128;
+ aes.Padding = PaddingMode.PKCS7;
+ aes.Mode = CipherMode.CBC;
+ return aes;
+ }
+
byte [] CreateEncryptionKey (string password, byte [] salt)
{
return CreateComputedKey (password, salt, encEntropy);
@@ -168,5 +214,15 @@
Array.Copy (a2, 0, ret, a1.Length, a2.Length);
return ret;
}
+
+ public static bool ArrayEquals (byte [] b1, byte [] b2)
+ {
+ if (b1.Length != b2.Length)
+ return false;
+ for (int i = 0; i < b1.Length; i++)
+ if (b1 [i] != b2 [i])
+ return false;
+ return true;
+ }
}
}
Modified:
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/ChangeLog
===================================================================
---
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/ChangeLog
2007-04-26 15:05:46 UTC (rev 76328)
+++
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/ChangeLog
2007-04-26 15:13:29 UTC (rev 76329)
@@ -1,3 +1,7 @@
+2007-04-26 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * IdentityCardEncryptionTest.cs : added encryption test (roundtrip).
+
2007-04-25 Atsushi Enomoto <[EMAIL PROTECTED]>
* IdentityCardTest.cs : added test for Save().
Modified:
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/IdentityCardEncryptionTest.cs
===================================================================
---
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/IdentityCardEncryptionTest.cs
2007-04-26 15:05:46 UTC (rev 76328)
+++
trunk/olive/class/Mono.ServiceModel.IdentitySelectors/Test/Mono.IdentityModel.Selectors/IdentityCardEncryptionTest.cs
2007-04-26 15:13:29 UTC (rev 76329)
@@ -26,6 +26,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
+using System.IO;
+using System.Text;
using System.Xml;
using Mono.ServiceModel.IdentitySelectors;
using NUnit.Framework;
@@ -38,11 +40,28 @@
[Test]
public void Import ()
{
+ string encxml = new StreamReader
("Test/resources/rupert.crds").ReadToEnd ();
string xml = new IdentityCardEncryption ().Decrypt (
- "Test/resources/rupert.crds", "monkeydance");
+ encxml, "monkeydance");
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
}
+
+ [Test]
+ public void Export ()
+ {
+ byte [] salt = Convert.FromBase64String
("ofkHGOy0pioOd7++N2a52w==");
+ byte [] iv = Convert.FromBase64String
("OzFSoAlrfj11g246TM4How==");
+ XmlDocument doc = new XmlDocument ();
+ doc.Load ("Test/resources/rupert.xml");
+ doc.RemoveChild (doc.FirstChild);
+ byte [] result = new IdentityCardEncryption ().Encrypt
(doc.OuterXml, "monkeydance", salt, iv);
+ string resultText = Encoding.UTF8.GetString (result);
+
+ string roundtrip = new IdentityCardEncryption
().Decrypt (resultText, "monkeydance");
+ doc = new XmlDocument ();
+ doc.LoadXml (roundtrip);
+ }
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches