예제 #1
0
 private static AS4EncryptedKey GetEncryptedKey(
     byte[] symmetricKey,
     KeyEncryptionConfiguration keyEncryptionConfig)
 {
     return
         (AS4EncryptedKey.CreateEncryptedKeyBuilderForKey(symmetricKey, keyEncryptionConfig)
          .Build());
 }
예제 #2
0
            public void ThenCreateAS4EncryptedKeySucceeds()
            {
                byte[] encryptionKey = GenerateEncryptionKey();

                AS4EncryptedKey key =
                    AS4EncryptedKey.CreateEncryptedKeyBuilderForKey(encryptionKey, new KeyEncryptionConfiguration(GetCertificate())).Build();

                Assert.Equal(EncryptionStrategy.XmlEncRSAOAEPUrlWithMgf, key.GetEncryptionAlgorithm());
                Assert.Equal(EncryptionStrategy.XmlEncSHA256Url, key.GetDigestAlgorithm());
            }
예제 #3
0
            public void ThenLoadEncryptedKeySucceeds()
            {
                // Arrange
                var xmlDocument = new XmlDocument();

                xmlDocument.LoadXml(Properties.Resources.as4_encrypted_envelope);

                // Act
                AS4EncryptedKey as4EncryptedKey = AS4EncryptedKey.LoadFromXmlDocument(xmlDocument);

                // Assert
                Assert.Equal("EK-501d4b2b-5d8459ed-c0c0-45a5-a0c4-4bde7cf06a38", as4EncryptedKey.GetReferenceId());
            }
예제 #4
0
        /// <summary>
        /// Decrypts the <see cref="AS4Message"/>, replacing the encrypted content with the decrypted content.
        /// </summary>
        public void DecryptMessage()
        {
            IEnumerable <EncryptedData> encryptedDatas =
                new EncryptedDataSerializer(_soapEnvelope).SerializeEncryptedDatas();

            var as4EncryptedKey = AS4EncryptedKey.LoadFromXmlDocument(_soapEnvelope);

            byte[] key = DecryptEncryptedKey(as4EncryptedKey, _certificate);

            foreach (EncryptedData encryptedData in encryptedDatas)
            {
                DecryptEncryptedData(encryptedData, key);
            }
        }
예제 #5
0
        /// <summary>
        /// Encrypts the <see cref="AS4Message"/> and its attachments.
        /// </summary>
        public void EncryptMessage()
        {
            _encryptedDatas.Clear();

            byte[]          encryptionKey   = GenerateSymmetricKey(_dataEncryptionConfig.AlgorithmKeySize);
            AS4EncryptedKey as4EncryptedKey = GetEncryptedKey(encryptionKey, _keyEncryptionConfig);

            _as4EncryptedKey = as4EncryptedKey;

            using (SymmetricAlgorithm encryptionAlgorithm =
                       CreateSymmetricAlgorithm(_dataEncryptionConfig.EncryptionMethod, encryptionKey))
            {
                EncryptAttachmentsWithAlgorithm(as4EncryptedKey, encryptionAlgorithm);
            }
        }
예제 #6
0
        private void EncryptAttachmentsWithAlgorithm(
            AS4EncryptedKey encryptedKey,
            SymmetricAlgorithm encryptionAlgorithm)
        {
            foreach (Attachment attachment in _attachments)
            {
                Stream        encrypted     = EncryptData(attachment.Content, encryptionAlgorithm);
                EncryptedData encryptedData = CreateEncryptedDataForAttachment(attachment, encryptedKey);

                _encryptedDatas.Add(encryptedData);

                encryptedKey.AddDataReference(encryptedData.Id);
                attachment.UpdateContent(encrypted, "application/octet-stream");
            }
        }
예제 #7
0
            public void ThenGetReferenceIdSucceeds(string id)
            {
                // Arrange
                var encryptedKey = new EncryptedKey {
                    Id = id
                };

                AS4EncryptedKey as4EncryptedKey = AS4EncryptedKey.FromEncryptedKey(encryptedKey);

                // Act
                string referenceId = as4EncryptedKey.GetReferenceId();

                // Assert
                Assert.Equal(id, referenceId);
            }
            public void ThenCreateCorrectEncoding()
            {
                // Arrange
                var xmlDocument = new XmlDocument();

                xmlDocument.LoadXml(Properties.Resources.EncryptedKeyWithMGFSpec);
                AS4EncryptedKey as4EncryptedKey = AS4EncryptedKey.LoadFromXmlDocument(xmlDocument);

                // Act
                OaepEncoding encoding = EncodingFactory.Instance.Create(
                    as4EncryptedKey.GetDigestAlgorithm(),
                    as4EncryptedKey.GetMaskGenerationFunction());

                // Assert
                AssertMgf1Hash(encoding, "SHA-256");
            }
예제 #9
0
            public void ThenGetCipherDataSucceeds()
            {
                // Arrange
                var cipherData = new CipherData {
                    CipherValue = new byte[] { 20 }
                };
                var encryptedKey = new EncryptedKey {
                    CipherData = cipherData
                };

                AS4EncryptedKey as4EncryptedKey = AS4EncryptedKey.FromEncryptedKey(encryptedKey);

                // Act
                CipherData as4CipherData = as4EncryptedKey.GetCipherData();

                // Assert
                Assert.Equal(cipherData, as4CipherData);
            }
예제 #10
0
            public void ThenAppendEncryptedKeySucceeds()
            {
                // Arrange
                var xmlDocument = new XmlDocument();

                xmlDocument.LoadXml(Properties.Resources.as4_encrypted_envelope);
                AS4EncryptedKey as4EncryptedKey = AS4EncryptedKey.LoadFromXmlDocument(xmlDocument);

                xmlDocument = new XmlDocument();
                XmlElement securityElement = xmlDocument.CreateElement(
                    "wsse",
                    "Security",
                    Constants.Namespaces.WssSecuritySecExt);

                // Act
                as4EncryptedKey.AppendEncryptedKey(securityElement);

                // Assert
                Assert.Equal("EncryptedKey", securityElement.FirstChild.LocalName);
            }
예제 #11
0
            public void ThenCreateAS4EncryptedKeySucceeds(string algorithm, string digest, string mgf)
            {
                byte[] encryptionKey = GenerateEncryptionKey();

                var keyEncryption = new KeyEncryption
                {
                    TransportAlgorithm = algorithm,
                    DigestAlgorithm    = digest,
                    MgfAlgorithm       = mgf
                };

                var keyEncryptionConfiguration = new KeyEncryptionConfiguration(GetCertificate(), keyEncryption);

                AS4EncryptedKey key =
                    AS4EncryptedKey.CreateEncryptedKeyBuilderForKey(encryptionKey, keyEncryptionConfiguration)
                    .Build();

                Assert.Equal(algorithm, key.GetEncryptionAlgorithm());
                Assert.Equal(digest, key.GetDigestAlgorithm());
                Assert.Equal(mgf, key.GetMaskGenerationFunction());
            }
예제 #12
0
        private static byte[] DecryptEncryptedKey(AS4EncryptedKey encryptedKey, X509Certificate2 certificate)
        {
            OaepEncoding encoding = EncodingFactory.Instance
                                    .Create(encryptedKey.GetDigestAlgorithm(), encryptedKey.GetMaskGenerationFunction());

            // We do not look at the KeyInfo element in here, but rather decrypt it with the certificate provided as argument.
            // Call GetRSAPrivateKey to avoid KeySet does not exist exceptions that might be thrown.
            RSA privateKey = certificate.GetRSAPrivateKey();

            if (privateKey == null)
            {
                throw new CryptographicException("The decryption certificate does not contain a private key.");
            }

            AsymmetricCipherKeyPair encryptionCertificateKeyPair =
                DotNetUtilities.GetRsaKeyPair(privateKey);

            encoding.Init(false, encryptionCertificateKeyPair.Private);

            CipherData cipherData = encryptedKey.GetCipherData();

            return(encoding.ProcessBlock(
                       inBytes: cipherData.CipherValue, inOff: 0, inLen: cipherData.CipherValue.Length));
        }
예제 #13
0
 private EncryptedData CreateEncryptedDataForAttachment(Attachment attachment, AS4EncryptedKey encryptedKey)
 {
     return(new EncryptedDataBuilder()
            .WithDataEncryptionConfiguration(_dataEncryptionConfig)
            .WithMimeType(attachment.ContentType)
            .WithEncryptionKey(encryptedKey)
            .WithUri(attachment.Id)
            .Build());
 }
예제 #14
0
 public EncryptedDataBuilder WithEncryptionKey(AS4EncryptedKey encryptionKey)
 {
     _encryptionKey = encryptionKey;
     return(this);
 }