1- using  System ; 
1+ using  ITfoxtec . Identity . Saml2 . Schemas ; 
2+ using  System ; 
23using  System . Security . Cryptography ; 
34using  System . Security . Cryptography . Xml ; 
45using  System . Xml ; 
@@ -7,9 +8,20 @@ namespace ITfoxtec.Identity.Saml2.Cryptography
78{ 
89    public  class  Saml2EncryptedXml  :  EncryptedXml 
910    { 
11+         public  const  string  XmlEncKeyAlgorithmRSAOAEPUrl  =  "http://www.w3.org/2009/xmlenc11#rsa-oaep" ; 
12+ 
1013        public  RSA  EncryptionPublicKey  {  get ;  set ;  } 
1114        public  RSA  EncryptionPrivateKey  {  get ;  set ;  } 
1215
16+ #if ! NETFULL 
17+         static Saml2EncryptedXml ( ) 
18+         { 
19+             // Register AES-GCM wrapper on .NET Core targets where AES-GCM algorithm is available 
20+             CryptoConfig . AddAlgorithm ( typeof ( AesGcmAlgorithm ) ,  AesGcmAlgorithm . AesGcm256Identifier ) ; 
21+             CryptoConfig . AddAlgorithm ( typeof ( AesGcmAlgorithm ) ,  AesGcmAlgorithm . AesGcm128Identifier ) ; 
22+         } 
23+ #endif
24+ 
1325        public  Saml2EncryptedXml ( RSA  encryptionPublicKey )  :  base ( ) 
1426        { 
1527            EncryptionPublicKey  =  encryptionPublicKey ; 
@@ -29,20 +41,20 @@ public Saml2EncryptedXml(XmlDocument document, RSA encryptionPrivateKey) : this(
2941
3042        public  virtual  XmlElement  EncryptAassertion ( XmlElement  assertionElement ) 
3143        { 
32-             using  ( var  encryptionAlgorithm  =  new   AesCryptoServiceProvider ( ) ) 
44+             using  ( var  encryptionAlgorithm  =  Aes . Create ( ) ) 
3345            { 
3446                encryptionAlgorithm . KeySize  =  256 ; 
3547
3648                var  encryptedData  =  new  EncryptedData 
3749                { 
38-                     Type  =  EncryptedXml . XmlEncElementUrl , 
39-                     EncryptionMethod  =  new  EncryptionMethod ( EncryptedXml . XmlEncAES256Url ) , 
50+                     Type  =  XmlEncElementUrl , 
51+                     EncryptionMethod  =  new  EncryptionMethod ( XmlEncAES256Url ) , 
4052                    KeyInfo  =  new  KeyInfo ( ) 
4153                } ; 
4254                encryptedData . KeyInfo . AddClause ( new  KeyInfoEncryptedKey ( new  EncryptedKey 
4355                { 
44-                     EncryptionMethod  =  new  EncryptionMethod ( EncryptedXml . XmlEncRSAOAEPUrl ) , 
45-                     CipherData  =  new  CipherData ( EncryptedXml . EncryptKey ( encryptionAlgorithm . Key ,  EncryptionPublicKey ,  true ) ) 
56+                     EncryptionMethod  =  new  EncryptionMethod ( XmlEncRSAOAEPUrl ) , 
57+                     CipherData  =  new  CipherData ( EncryptKey ( encryptionAlgorithm . Key ,  EncryptionPublicKey ,  true ) ) 
4658                } ) ) ; 
4759
4860                var  encryptedXml  =  new  EncryptedXml ( ) ; 
@@ -52,9 +64,64 @@ public virtual XmlElement EncryptAassertion(XmlElement assertionElement)
5264            } 
5365        } 
5466
67+         public  override  byte [ ]  GetDecryptionIV ( EncryptedData  encryptedData ,  string  symmetricAlgorithmUri ) 
68+         { 
69+             if  ( encryptedData  is  null ) 
70+             { 
71+                 throw  new  ArgumentNullException ( nameof ( encryptedData ) ) ; 
72+             } 
73+ 
74+ #if ! NETFULL 
75+ 
76+             var  aesGcmSymmetricAlgorithmUri  =  symmetricAlgorithmUri  ??  encryptedData . EncryptionMethod ? . KeyAlgorithm ; 
77+             if  ( aesGcmSymmetricAlgorithmUri  ==  AesGcmAlgorithm . AesGcm128Identifier  ||  aesGcmSymmetricAlgorithmUri  ==  AesGcmAlgorithm . AesGcm256Identifier ) 
78+             { 
79+                 int  initBytesSize  =  12 ; 
80+                 byte [ ]  iv  =  new  byte [ initBytesSize ] ; 
81+                 Buffer . BlockCopy ( encryptedData . CipherData . CipherValue ,  0 ,  iv ,  0 ,  iv . Length ) ; 
82+                 return  iv ; 
83+             } 
84+ #endif
85+ 
86+             return  base . GetDecryptionIV ( encryptedData ,  symmetricAlgorithmUri ) ; 
87+         } 
88+ 
5589        public  override  byte [ ]  DecryptEncryptedKey ( EncryptedKey  encryptedKey ) 
5690        { 
57-             return  DecryptKey ( encryptedKey . CipherData . CipherValue ,  EncryptionPrivateKey ,  ( encryptedKey . EncryptionMethod  !=  null )  &&  ( encryptedKey . EncryptionMethod . KeyAlgorithm  ==  XmlEncRSAOAEPUrl ) ) ; 
91+             if  ( encryptedKey . EncryptionMethod . KeyAlgorithm  ==  XmlEncKeyAlgorithmRSAOAEPUrl ) 
92+             { 
93+                 return  EncryptionPrivateKey . Decrypt ( encryptedKey . CipherData . CipherValue ,  GetEncryptionPadding ( encryptedKey ) ) ; 
94+             } 
95+             else 
96+             { 
97+                 return  DecryptKey ( encryptedKey . CipherData . CipherValue ,  EncryptionPrivateKey ,  ( encryptedKey . EncryptionMethod  !=  null )  &&  ( encryptedKey . EncryptionMethod . KeyAlgorithm  ==  XmlEncRSAOAEPUrl ) ) ; 
98+             } 
99+         } 
100+ 
101+         private  static RSAEncryptionPadding  GetEncryptionPadding ( EncryptedKey  encryptedKey ) 
102+         { 
103+             var  xmlElement  =  encryptedKey . GetXml ( ) ; 
104+             var  nsm  =  new  XmlNamespaceManager ( xmlElement . OwnerDocument . NameTable ) ; 
105+             nsm . AddNamespace ( "enc" ,  XmlEncNamespaceUrl ) ; 
106+             nsm . AddNamespace ( "ds" ,  SignedXml . XmlDsigNamespaceUrl ) ; 
107+             var  digestMethodElement  =  xmlElement . SelectSingleNode ( "enc:EncryptionMethod/ds:DigestMethod" ,  nsm )  as  XmlElement ; 
108+             if  ( digestMethodElement  !=  null ) 
109+             { 
110+                 var  method  =  digestMethodElement . GetAttribute ( "Algorithm" ) ; 
111+                 switch  ( method ) 
112+                 { 
113+                     case  Saml2SecurityAlgorithms . Sha1Digest : 
114+                         return  RSAEncryptionPadding . OaepSHA1 ; 
115+                     case  Saml2SecurityAlgorithms . Sha256Digest : 
116+                         return  RSAEncryptionPadding . OaepSHA256 ; 
117+                     case  Saml2SecurityAlgorithms . Sha384Digest : 
118+                         return  RSAEncryptionPadding . OaepSHA384 ; 
119+                     case  Saml2SecurityAlgorithms . Sha512Digest : 
120+                         return  RSAEncryptionPadding . OaepSHA512 ; 
121+                 } 
122+             } 
123+ 
124+             return  RSAEncryptionPadding . OaepSHA256 ; 
58125        } 
59126    } 
60127} 
0 commit comments