public void RSAKeyValue () { RSA key = RSA.Create (); key.FromXmlString (xmlRSA); RSAKeyValue rsa = new RSAKeyValue (key); info.AddClause (rsa); AssertCrypto.AssertXmlEquals ("rsa", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xmlRSA + "</KeyValue></KeyInfo>", (info.GetXml ().OuterXml)); AssertEquals ("rsa count", 1, info.Count); }
public void InvalidValue2 () { string badKey = "<Test></Test>"; XmlDocument doc = new XmlDocument (); doc.LoadXml (badKey); RSAKeyValue rsa = new RSAKeyValue (); rsa.LoadXml (doc.DocumentElement); }
// // virtual methods // protected virtual AsymmetricAlgorithm GetPublicKey() { if (KeyInfo == null) { throw new CryptographicException(SR.Cryptography_Xml_KeyInfoRequired); } if (_x509Enum != null) { AsymmetricAlgorithm key = GetNextCertificatePublicKey(); if (key != null) { return(key); } } if (_keyInfoEnum == null) { _keyInfoEnum = KeyInfo.GetEnumerator(); } // In our implementation, we move to the next KeyInfo clause which is an RSAKeyValue, DSAKeyValue or KeyInfoX509Data while (_keyInfoEnum.MoveNext()) { RSAKeyValue rsaKeyValue = _keyInfoEnum.Current as RSAKeyValue; if (rsaKeyValue != null) { return(rsaKeyValue.Key); } DSAKeyValue dsaKeyValue = _keyInfoEnum.Current as DSAKeyValue; if (dsaKeyValue != null) { return(dsaKeyValue.Key); } KeyInfoX509Data x509Data = _keyInfoEnum.Current as KeyInfoX509Data; if (x509Data != null) { _x509Collection = Utils.BuildBagOfCerts(x509Data, CertUsageType.Verification); if (_x509Collection.Count > 0) { _x509Enum = _x509Collection.GetEnumerator(); AsymmetricAlgorithm key = GetNextCertificatePublicKey(); if (key != null) { return(key); } } } } return(null); }
public void ImportKey () { string rsaKey = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>ogZ1/O7iks9ncETqNxLDKoPvgrT4nFx1a3lOmpywEmgbc5+8vI5dSzReH4v0YrflY75rIJx13CYWMsaHfQ78GtXvaeshHlQ3lLTuSdYEJceKll/URlBoKQtOj5qYIVSFOIVGHv4Y/0lnLftOzIydem29KKH6lJQlJawBBssR12s=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>"; XmlDocument doc = new XmlDocument (); doc.LoadXml (rsaKey); RSAKeyValue rsa1 = new RSAKeyValue (); rsa1.LoadXml (doc.DocumentElement); string s = (rsa1.GetXml ().OuterXml); AssertEquals ("RSA Key", rsaKey, s); }
public void GeneratedKey () { RSAKeyValue rsa1 = new RSAKeyValue (); AssertNotNull ("Key", rsa1.Key); XmlElement xmlkey = rsa1.GetXml (); RSAKeyValue rsa2 = new RSAKeyValue (); rsa2.LoadXml (xmlkey); Assert ("rsa1==rsa2", (rsa1.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml)); RSA key = rsa1.Key; RSAKeyValue rsa3 = new RSAKeyValue (key); Assert ("rsa3==rsa1", (rsa3.GetXml ().OuterXml) == (rsa1.GetXml ().OuterXml)); Assert ("rsa3==rsa2", (rsa3.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml)); }
public void GeneratedKey () { RSAKeyValue rsa1 = new RSAKeyValue (); Assert.IsNotNull (rsa1.Key, "Key"); XmlElement xmlkey = rsa1.GetXml (); RSAKeyValue rsa2 = new RSAKeyValue (); rsa2.LoadXml (xmlkey); Assert.IsTrue ((rsa1.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml), "rsa1==rsa2"); RSA key = rsa1.Key; RSAKeyValue rsa3 = new RSAKeyValue (key); Assert.IsTrue ((rsa3.GetXml ().OuterXml) == (rsa1.GetXml ().OuterXml), "rsa3==rsa1"); Assert.IsTrue ((rsa3.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml), "rsa3==rsa2"); }
protected virtual AsymmetricAlgorithm GetPublicKey() { if (this.KeyInfo == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_KeyInfoRequired")); } if (this.m_x509Enum != null) { AsymmetricAlgorithm nextCertificatePublicKey = this.GetNextCertificatePublicKey(); if (nextCertificatePublicKey != null) { return(nextCertificatePublicKey); } } if (this.m_keyInfoEnum == null) { this.m_keyInfoEnum = this.KeyInfo.GetEnumerator(); } while (this.m_keyInfoEnum.MoveNext()) { RSAKeyValue current = this.m_keyInfoEnum.Current as RSAKeyValue; if (current != null) { return(current.Key); } DSAKeyValue value3 = this.m_keyInfoEnum.Current as DSAKeyValue; if (value3 != null) { return(value3.Key); } KeyInfoX509Data data = this.m_keyInfoEnum.Current as KeyInfoX509Data; if (data != null) { this.m_x509Collection = System.Security.Cryptography.Xml.Utils.BuildBagOfCerts(data, CertUsageType.Verification); if (this.m_x509Collection.Count > 0) { this.m_x509Enum = this.m_x509Collection.GetEnumerator(); AsymmetricAlgorithm algorithm2 = this.GetNextCertificatePublicKey(); if (algorithm2 != null) { return(algorithm2); } } } } return(null); }
virtual protected void SignWithKeyInfo(String src, String dest, ICipherParameters pk, AsymmetricAlgorithm publicKey, String digestAlgorithm) { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileStream os = new FileStream(dest, FileMode.Create); PdfStamper stamper = PdfStamper.createXmlSignature(reader, os); // Creating the appearance XmlSignatureAppearance appearance = stamper.XmlSignatureAppearance; //Set XfaXmlLocator to control getting and setting Document appearance.SetXmlLocator(new XfaXmlLocator(stamper)); // Creating the signature IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); KeyInfoClause keyInfo; if(publicKey is DSA) keyInfo = new DSAKeyValue((DSA)publicKey); else if(publicKey is RSA) keyInfo = new RSAKeyValue((RSA)publicKey); else throw new ArgumentException("Invalid public key algorithm", "publicKey"); MakeXmlSignature.SignXmlDSig(appearance, pks, keyInfo); }
public void Complex () { KeyInfoName name = new KeyInfoName (); name.Value = "Mono::"; info.AddClause (name); DSA keyDSA = DSA.Create (); keyDSA.FromXmlString (xmlDSA); DSAKeyValue dsa = new DSAKeyValue (keyDSA); info.AddClause (dsa); RSA keyRSA = RSA.Create (); keyRSA.FromXmlString (xmlRSA); RSAKeyValue rsa = new RSAKeyValue (keyRSA); info.AddClause (rsa); KeyInfoRetrievalMethod retrieval = new KeyInfoRetrievalMethod (); retrieval.Uri = "http://www.go-mono.org/"; info.AddClause (retrieval); X509Certificate x509 = new X509Certificate (cert); KeyInfoX509Data x509data = new KeyInfoX509Data (x509); info.AddClause (x509data); string s = "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyName>Mono::</KeyName><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>rjxsMU368YOCTQejWkiuO9e/vUVwkLtq1jKiU3TtJ53hBJqjFRuTa228vZe+BH2su9RPn/vYFWfQDv6zgBYe3eNdu4Afw+Ny0FatX6dl3E77Ra6Tsd3MmLXBiGSQ1mMNd5G2XQGpbt9zsGlUaexXekeMLxIufgfZLwYp67M+2WM=</P><Q>tf0K9rMyvUrU4cIkwbCrDRhQAJk=</Q><G>S8Z+1pGCed00w6DtVcqZLKjfqlCJ7JsugEFIgSy/Vxtu9YGCMclV4ijGEbPo/jU8YOSMuD7E9M7UaopMRcmKQjoKZzoJjkgVFP48Ohxl1f08lERnButsxanx3+OstFwUGQ8XNaGg3KrIoZt1FUnfxN3RHHTvVhjzNSHxMGULGaU=</G><Y>LnrxxRGLYeV2XLtK3SYz8RQHlHFZYrtznDZyMotuRfO5uC5YODhSFyLXvb1qB3WeGtF4h3Eo4KzHgMgfN2ZMlffxFRhJgTtH3ctbL8lfQoDkjeiPPnYGhspdJxr0tyZmiy0gkjJG3vwHYrLnvZWx9Wm/unqiOlGBPNuxJ+hOeP8=</Y><J>9RhE5TycDtdEIXxS3HfxFyXYgpy81zY5lVjwD6E9JP37MWEi80BlX6ab1YPm6xYSEoqReMPP9RgGiW6DuACpgI7+8vgCr4i/7VhzModJAA56PwvTu6UMt9xxKU/fT672v8ucREkMWoc7lEey</J><Seed>HxW3N4RHWVgqDQKuGg7iJTUTiCs=</Seed><PgenCounter>Asw=</PgenCounter></DSAKeyValue></KeyValue>"; s += "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>9DC4XNdQJwMRnz5pP2a6U51MHCODRilaIoVXqUPhCUb0lJdGroeqVYT84ZyIVrcarzD7Tqs3aEOIa3rKox0N1bxQpZPqayVQeLAkjLLtzJW/ScRJx3uEDJdgT1JnM1FH0GZTinmEdCUXdLc7+Y/c/qqIkTfbwHbRZjW0bBJyExM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>"; #if NET_1_0 s += "<RetrievalElement URI=\"http://www.go-mono.org/\" />"; #else s += "<RetrievalMethod URI=\"http://www.go-mono.org/\" />"; #endif s += "<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"; s += "<X509Certificate>MIICHTCCAYYCARQwDQYJKoZIhvcNAQEEBQAwWDELMAkGA1UEBhMCQ0ExHzAdBgNVBAMTFktleXdpdG5lc3MgQ2FuYWRhIEluYy4xKDAmBgorBgEEASoCCwIBExhrZXl3aXRuZXNzQGtleXdpdG5lc3MuY2EwHhcNOTYwNTA3MDAwMDAwWhcNOTkwNTA3MDAwMDAwWjBYMQswCQYDVQQGEwJDQTEfMB0GA1UEAxMWS2V5d2l0bmVzcyBDYW5hZGEgSW5jLjEoMCYGCisGAQQBKgILAgETGGtleXdpdG5lc3NAa2V5d2l0bmVzcy5jYTCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAzSP6KuHtmPTp0JM+13qAAkzMwQKvXLYff/pXQm8w0SDFtSEHQCyphsLzZISuPYUu7YW9VLAYKO9q+BvnCxYfkyVPx/iOw7nKmIQOVdAv73h3xXIoX2C/GSvRcqK32D/glzRaAb0EnMh4Rc2TjRXydhARq7hbLp5S3YE+nGTIKZMCAQMwDQYJKoZIhvcNAQEEBQADgYEAMho1ur9DJ9a01Lh25eObTWzAhsl3NbprFi0TRkqwMlOhW1rpmeIMhogXTg3+gqxOR+/7/zms7jXI+lI3CkmtWa3iiqkcxl8f+G9zfs2gMegMvvVN2bKrihK2MHhoEXwN8UlNo/2y6f8d8JH6VIX/M5Dowb+km6RiRr1hElmYQYk=</X509Certificate></X509Data></KeyInfo>"; AssertCrypto.AssertXmlEquals ("Complex", s, (info.GetXml ().OuterXml)); AssertEquals ("RetrievalMethod count", 5, info.Count); }
/// <summary> /// Creates the key. /// </summary> /// <returns></returns> public static RSAKey CreateKey() { RSAKeyValue rsa = new RSAKeyValue(); return new RSAKey(rsa.Key.ToXmlString(true)); }
public void InvalidValue1 () { RSAKeyValue rsa = new RSAKeyValue (); rsa.LoadXml (null); }