From b77f20a6e3167339053450c64a8485f8d7f410a8 Mon Sep 17 00:00:00 2001
From: Thomas Lenz <thomas.lenz@egiz.gv.at>
Date: Thu, 16 Aug 2018 16:38:59 +0200
Subject: update SZRClient Test

---
 .../modules/authmodule_eIDASv2/SZRClientTest.java  | 108 +++++++++++++++------
 1 file changed, 80 insertions(+), 28 deletions(-)

(limited to 'eidas_modules')

diff --git a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/test/eidas/specific/modules/authmodule_eIDASv2/SZRClientTest.java b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/test/eidas/specific/modules/authmodule_eIDASv2/SZRClientTest.java
index 926ea2c9..7f9497fc 100644
--- a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/test/eidas/specific/modules/authmodule_eIDASv2/SZRClientTest.java
+++ b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/test/eidas/specific/modules/authmodule_eIDASv2/SZRClientTest.java
@@ -1,11 +1,9 @@
 package at.asitplus.test.eidas.specific.modules.authmodule_eIDASv2;
 
 import java.io.IOException;
-import java.math.BigInteger;
 import java.security.InvalidKeyException;
+import java.security.MessageDigest;
 import java.security.NoSuchProviderException;
-import java.security.PublicKey;
-import java.security.interfaces.RSAPublicKey;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -24,6 +22,7 @@ import org.w3c.dom.Element;
 
 import at.asitplus.eidas.specific.modules.authmodule_eIDASv2.Constants;
 import at.asitplus.eidas.specific.modules.authmodule_eIDASv2.exception.SZRCommunicationException;
+import at.asitplus.eidas.specific.modules.authmodule_eIDASv2.exception.eIDASAuthenticationException;
 import at.asitplus.eidas.specific.modules.authmodule_eIDASv2.szr.SZRClient;
 import at.asitplus.eidas.specific.modules.authmodule_eIDASv2.utils.eIDASResponseUtils;
 import at.gv.e_government.reference.namespace.persondata._20020228.PersonNameType;
@@ -49,8 +48,12 @@ public class SZRClientTest {
 	
 	private static final String givenName = "Franz";
 	private static final String familyName = "Mustermann";
-	private static final String dateOfBirth = "1987-05-05";
-	private static final String eIDASeID = "IS/AT/123456789ABCDE";
+	private static final String dateOfBirth = "1988-05-05";
+	private static final String eIDASeID = "IS/AT/123456789ABCDEF";
+	
+	//Dummy public RSA Key
+	private static final String PUBKEY_EXPONENT = "AQAB";
+	private static final String PUBKEY_MODULUS = "AJZyj/+sdCMDRq9RkvbFcgSTVn/OfS8EUE81ddwP8MNuJ1kd1SWBUJPaQX2JLJHrL54mkOhrkhH2M/zcuOTu8nW9TOEgXGjrRB/0HpiYKpV+VDJViyyc/GacNLxN4Anw4pima6gHYaJIw9hQkL/nuO2hyh8PGJd7rxeFXJmbLy+X";
 	
 	private static final String DUMMY_TARGET = EAAFConstants.URN_PREFIX_CDID + "ZP";
 	
@@ -60,10 +63,31 @@ public class SZRClientTest {
 	} 
 	
 	
+	/*
+	 * getIdentityLink without RAW mode does not contain a valid signature
+	 */
 	//@Test
-	public void getIdentityLink() throws SZRException_Exception, EAAFParserException, NoSuchProviderException, IOException, InvalidKeyException, SZRCommunicationException {
+//	public void getIdentityLink() throws SZRException_Exception, EAAFParserException, NoSuchProviderException, IOException, InvalidKeyException, eIDASAuthenticationException {
+//		log.debug("Starting connecting SZR Gateway");											
+//		IdentityLinkType result = szrClient.getIdentityLink(
+//									getPersonInfo(), 
+//									dummyCodeForKeys(), 
+//									basicConfig.getBasicMOAIDConfigurationBoolean(
+//											Constants.CONIG_PROPS_EIDAS_SZRCLIENT_DEBUG_INSERTERNB, 
+//											true)
+//									);
+//
+//		Element idlFromSZR = (Element)result.getAssertion();			
+//		IIdentityLink identityLink = new SimpleIdentityLinkAssertionParser(idlFromSZR).parseIdentityLink();
+//		if (identityLink == null)
+//			throw new SZRCommunicationException("ernb.00", new Object[] {"bPK is null or empty"});
+//			
+//	}
+	
+//	@Test
+	public void getIdentityLinkRawMode() throws SZRException_Exception, EAAFParserException, NoSuchProviderException, IOException, InvalidKeyException, eIDASAuthenticationException {
 		log.debug("Starting connecting SZR Gateway");											
-		IdentityLinkType result = szrClient.getIdentityLink(
+		IdentityLinkType result = szrClient.getIdentityLinkInRawMode(
 									getPersonInfo(), 
 									dummyCodeForKeys(), 
 									basicConfig.getBasicMOAIDConfigurationBoolean(
@@ -73,13 +97,39 @@ public class SZRClientTest {
 
 		Element idlFromSZR = (Element)result.getAssertion();			
 		IIdentityLink identityLink = new SimpleIdentityLinkAssertionParser(idlFromSZR).parseIdentityLink();
+		
 		if (identityLink == null)
-			throw new SZRCommunicationException("ernb.00", new Object[] {"bPK is null or empty"});
-			
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO IDL object"});
+
+		System.out.println(identityLink.getSerializedSamlAssertion());
+		
+		if (StringUtils.isEmpty(identityLink.getFamilyName()))
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO FamilyName from IDL"});
+		
+		if (StringUtils.isEmpty(identityLink.getGivenName()))
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO GivenName from IDL"});
+		
+		if (StringUtils.isEmpty(identityLink.getDateOfBirth()))
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO DateOfBirthName from IDL"});
+		
+		if (StringUtils.isEmpty(identityLink.getIdentificationType()))
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO baseIdType from IDL"});
+		
+		if (StringUtils.isEmpty(identityLink.getIdentificationValue()))
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO baseId from IDL"});
+		
+		if (StringUtils.isEmpty(identityLink.getSerializedSamlAssertion()))
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO serialized IDL"});
+		
+		if (identityLink.getSamlAssertion() == null )
+			throw new SZRCommunicationException("ernb.00", new Object[] {"NO raw IDL"});
+		
+
+		
 	}
 	
-	//@Test
-	public void getbPKTest() throws SZRException_Exception, SZRCommunicationException {
+//	@Test
+	public void getbPKTest() throws SZRException_Exception, eIDASAuthenticationException {
 		String bPK = szrClient.getBPK(getPersonInfo(), DUMMY_TARGET, 
 				basicConfig.getBasicConfiguration(
 				Constants.CONIG_PROPS_EIDAS_SZRCLIENT_PARAMS_VKZ, 
@@ -91,7 +141,20 @@ public class SZRClientTest {
 		
 	}
 	
-	private PersonInfoType getPersonInfo() {
+	private String createHashFromUniqueId(String uniqueId) throws eIDASAuthenticationException {
+		try {
+            MessageDigest md = MessageDigest.getInstance("SHA-256");
+            byte[] hash = md.digest(uniqueId.getBytes("UTF-8"));
+            String hashBase64 = new String(Base64Utils.encode(hash), "UTF-8").replaceAll("\r\n", "");
+            return hashBase64;
+            
+        } catch (Exception ex) {
+            throw new eIDASAuthenticationException("internal.03", new Object[]{},  ex);
+            
+        }
+	}
+	
+	private PersonInfoType getPersonInfo() throws eIDASAuthenticationException {
 		PersonInfoType personInfo = new PersonInfoType();
 		PersonNameType personName = new PersonNameType();
 		PhysicalPersonType naturalPerson = new PhysicalPersonType();
@@ -104,7 +167,7 @@ public class SZRClientTest {
 		//parse some eID attributes
 		Trible<String, String, String> eIdentifier = 
 				eIDASResponseUtils.parseEidasPersonalIdentifier((String)eIDASeID);
-		String uniqueId = (String)eIDASeID;
+		String uniqueId = createHashFromUniqueId(eIdentifier.getThird());
 		String citizenCountry = eIdentifier.getFirst();
 					
 		//person information
@@ -137,31 +200,20 @@ public class SZRClientTest {
 				false)) {
 			List<KeyValueType> keyvalueList = new ArrayList<KeyValueType>();
 			try {
-				//Security.addProvider(new BouncyCastleProvider());
-				//PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Constants.SZR_CONSTANTS_DEFAULT_PUBL_KEY);
-				//KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
-										
-				//PublicKey pb = kf.generatePublic(spec);
-				PublicKey pb = new iaik.security.rsa.RSAPublicKey(Constants.SZR_CONSTANTS_DEFAULT_PUBL_KEY);
-				
-				RSAPublicKey rsapb = (RSAPublicKey)pb;	        
-				BigInteger modulus = rsapb.getModulus();
-				BigInteger exponent = rsapb.getPublicExponent();
-		           	           
 				// set key values
 				RSAKeyValueType rsa = new RSAKeyValueType();
-				rsa.setExponent(new String(Base64Utils.encode(exponent.toByteArray())));
-				rsa.setModulus(new String(Base64Utils.encode(modulus.toByteArray())));
+				rsa.setExponent(PUBKEY_EXPONENT);
+				rsa.setModulus(PUBKEY_MODULUS);
 						
 				KeyValueType key = new KeyValueType();
-				key.setRSAKeyValue(rsa);
-							
+				key.setRSAKeyValue(rsa);							
 				keyvalueList.add(key);
 			
 				return keyvalueList;
 			} catch (Exception e) {
 				log.error("TestCode has an internal ERROR", e);
 				throw e;
+				
 			}
 			
 		}
-- 
cgit v1.2.3