public String createChallangeNAClaims(String req, String claimDefs, int size) throws Exception { ObjectMapper mapper = new ObjectMapper(); ArrayNode claimDefNodes = (ArrayNode) mapper.readTree(claimDefs); req = req.replaceAll("\"", ""); byte[] reqElemBytes = Base64.decode(req); Element reqElem = null; ArrayList<IdentityClaimDefinition> icds = new ArrayList<IdentityClaimDefinition>(); for (int i = 0; i < size; i++) { String onVal = claimDefNodes.get(i).getTextValue(); ObjectNode claimDefOn = (ObjectNode) mapper.readTree(onVal); IdentityClaimDefinition idClaimDef = new IdentityClaimDefinition(claimDefOn); icds.add(idClaimDef); if (reqElem == null) { Pairing pairing = idClaimDef.getParams().getPairing(); reqElem = pairing.getG1().newElement(); reqElem.setFromBytes(reqElemBytes); // System.out.println(reqElem); } } Pairing pairing = icds.get(0).getParams().getPairing(); Field gt = pairing.getGT(); Element sessionKey = gt.newRandomElement().getImmutable(); Element sessionKeyOrig = sessionKey.getImmutable(); // System.out.println("Key: " + sessionKey); JsonNode rootNode = mapper.createObjectNode(); ObjectNode on = (ObjectNode) rootNode; Encrypt encrypt = new Encrypt(); for (int i = 0; i < size; i++) { IdentityClaimDefinition claimDef = icds.get(i); Element share = null; if (i < (size - 1)) { share = gt.newRandomElement().getImmutable(); sessionKey = sessionKey.sub(share).getImmutable(); } else { // Last one should be the remaining part of session key share = sessionKey; } encrypt.init(claimDef.getParams()); // System.out.println("Part : " + i + " : " + share); AECipherTextBlock ct = encrypt.doEncrypt(share, reqElem); on.put(claimDef.getName(), ct.serializeJSON()); } // System.out.println(sessionKeyOrig); String sk = new String(Base64.encode(sessionKeyOrig.toBytes())); sk = sk.replaceAll(" ", ""); on.put("SessionKey", sk); return on.toString(); }
/** * Create a session key, encrypt it with the given request and claimDef. Return a serialized JSON * node with the session key and challenge. * * @param request Request from user: Base64 encoded bytes of the request element. * @param claimDef Claim definition to use: JSON encoded * @return * @throws Exception */ public String createChallange(String request, String claimDef) throws Exception { ObjectMapper mapper = new ObjectMapper(); ObjectNode claimDefOn = (ObjectNode) mapper.readTree(claimDef); IdentityClaimDefinition idClaimDef = new IdentityClaimDefinition(claimDefOn); byte[] reqElemBytes = Base64.decode(request); Element reqElem = idClaimDef.getParams().getPairing().getG1().newElement(); reqElem.setFromBytes(reqElemBytes); Element sessionKey = idClaimDef.getParams().getPairing().getGT().newRandomElement().getImmutable(); // Encrypt session key Encrypt encrypt = new Encrypt(); encrypt.init(idClaimDef.getParams()); AECipherTextBlock ct = encrypt.doEncrypt(sessionKey, reqElem); JsonNode rootNode = mapper.createObjectNode(); ObjectNode on = (ObjectNode) rootNode; on.put(idClaimDef.getName(), ct.serializeJSON()); String sk = new String(Base64.encode(sessionKey.toBytes())); sk = sk.replaceAll(" ", ""); on.put("SessionKey", sk); return on.toString(); }
/** * @param request * @param claimDefs Array of claim defs * @return * @throws Exception */ public String createChallangeNClaimsThreads(String requests, String claimDefs) throws Exception { ObjectMapper mapper = new ObjectMapper(); // System.out.println(requests); String[] split = requests.split(","); ArrayNode claimDefNodes = (ArrayNode) mapper.readTree(claimDefs); ArrayList<IdentityClaimDefinition> icds = new ArrayList<IdentityClaimDefinition>(); ArrayList<Element> reqs = new ArrayList<Element>(); for (int i = 0; i < split.length; i++) { String onVal = claimDefNodes.get(i).getTextValue(); ObjectNode claimDefOn = (ObjectNode) mapper.readTree(onVal); IdentityClaimDefinition idClaimDef = new IdentityClaimDefinition(claimDefOn); icds.add(idClaimDef); Pairing pairing = idClaimDef.getParams().getPairing(); // System.out.println(idClaimDef.serializeJSON()); String tmpReq = split[i].replaceAll("\"", ""); byte[] reqElemBytes = Base64.decode(tmpReq); // System.out.println(reqElemBytes.length); Element reqElem = pairing.getG1().newElement(); reqElem.setFromBytes(reqElemBytes); // System.out.println(reqElem.getImmutable()); reqs.add(reqElem); } Pairing pairing = icds.get(0).getParams().getPairing(); Field gt = pairing.getGT(); Element sessionKey = gt.newRandomElement().getImmutable(); Element sessionKeyOrig = sessionKey.getImmutable(); // System.out.println("Key: " + sessionKey); JsonNode rootNode = mapper.createObjectNode(); ObjectNode on = (ObjectNode) rootNode; ArrayList<EncrypterThread> ets = new ArrayList<ServiceProvider.EncrypterThread>(); for (int i = 0; i < split.length; i++) { IdentityClaimDefinition claimDef = icds.get(i); Element share = null; if (i < (split.length - 1)) { share = gt.newRandomElement().getImmutable(); sessionKey = sessionKey.sub(share).getImmutable(); } else { // Last one should be the remaining part of session key share = sessionKey; } EncrypterThread t = new EncrypterThread(claimDef.getName(), claimDef.getParams(), share, reqs.get(i), on); t.start(); ets.add(t); } for (EncrypterThread t : ets) { t.join(); } String sk = new String(Base64.encode(sessionKeyOrig.toBytes())); sk = sk.replaceAll(" ", ""); on.put("SessionKey", sk); return on.toString(); }