static String string(NSDictionary dictionary, String key, String defaultValue) { NSObject object = dictionary.get(key); return object == null ? defaultValue : object.toString(); } }
public static Optional<NSObject> optional(NSDictionary dictionary, String key) { return dictionary.containsKey(key) ? Optional.of(dictionary.get(key)) : Optional.empty(); }
static <T extends NSObject> T cast(String key, NSDictionary dictionary) throws BadDataException { try { return (T) dictionary.get(key); } catch (ClassCastException ex) { throw new BadDataException("Bad type for key:" + key, ex); } }
static String string(NSDictionary dictionary, String key) throws BadDataException { NSObject object = dictionary.get(key); if (object == null) { throw new BadDataException("Missing key: " + key); } return object.toString(); }
public static Optional<NSObject> get(NSDictionary dictionary, String key) { return dictionary.containsKey(key) ? Optional.of(dictionary.get(key)) : Optional.empty(); }
public static Optional<InflatableData> from(byte[] bs) { InflatableData data; try { NSDictionary parse = (NSDictionary) PropertyListParser.parse(bs); byte[] escrowedKeys = ((NSData) parse.get("escrowedKeys")).bytes(); UUID deviceUuid = UUID.fromString(((NSString) parse.get("deviceUuid")).getContent()); String deviceHardWareId = ((NSString) parse.get("deviceHardWareId")).getContent(); data = new InflatableData(escrowedKeys, deviceUuid, deviceHardWareId); } catch (ClassCastException | IllegalArgumentException | IOException | NullPointerException | PropertyListFormatException | ParseException | ParserConfigurationException | SAXException ex) { logger.warn("-- from() - exception: ", ex); data = null; } return Optional.ofNullable(data); }
static <T> T get(NSDictionary dictionary, String key) throws BadDataException { NSObject object = dictionary.get(key); if (object == null) { throw new BadDataException("Missing key: " + key); } try { return (T) object; } catch (ClassCastException ex) { throw new BadDataException("Bad data type", ex); } }
private PairSetupPin3Response doPairSetupPin3(Socket socket, final byte[] sessionKeyHashK) throws Exception { MessageDigest sha512Digest = MessageDigest.getInstance("SHA-512"); sha512Digest.update("Pair-Setup-AES-Key".getBytes(StandardCharsets.UTF_8)); sha512Digest.update(sessionKeyHashK); byte[] aesKey = Arrays.copyOfRange(sha512Digest.digest(), 0, 16); sha512Digest.update("Pair-Setup-AES-IV".getBytes(StandardCharsets.UTF_8)); sha512Digest.update(sessionKeyHashK); byte[] aesIV = Arrays.copyOfRange(sha512Digest.digest(), 0, 16); int lengthB; int lengthA = lengthB = aesIV.length - 1; for (; lengthB >= 0 && 256 == ++aesIV[lengthA]; lengthA = lengthB += -1) ; Cipher aesGcm128Encrypt = Cipher.getInstance("AES/GCM/NoPadding"); SecretKeySpec secretKey = new SecretKeySpec(aesKey, "AES"); aesGcm128Encrypt.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(128, aesIV)); final byte[] aesGcm128ClientLTPK = aesGcm128Encrypt.doFinal(authKey.getAbyte()); byte[] pairSetupPinRequestData = AuthUtils.createPList(new HashMap<String, byte[]>() {{ put("epk", Arrays.copyOfRange(aesGcm128ClientLTPK, 0, aesGcm128ClientLTPK.length - 16)); put("authTag", Arrays.copyOfRange(aesGcm128ClientLTPK, aesGcm128ClientLTPK.length - 16, aesGcm128ClientLTPK.length)); }}); byte[] pairSetupPin3ResponseBytes = AuthUtils.postData(socket, "/pair-setup-pin", "application/x-apple-binary-plist", pairSetupPinRequestData); NSDictionary pairSetupPin3Response = (NSDictionary) PropertyListParser.parse(pairSetupPin3ResponseBytes); if (pairSetupPin3Response.containsKey("epk") && pairSetupPin3Response.containsKey("authTag")) { byte[] epk = ((NSData) pairSetupPin3Response.get("epk")).bytes(); byte[] authTag = ((NSData) pairSetupPin3Response.get("authTag")).bytes(); return new PairSetupPin3Response(epk, authTag); } throw new Exception(); }
private PairSetupPin1Response doPairSetupPin1(Socket socket) throws Exception { byte[] pairSetupPinRequestData = AuthUtils.createPList(new HashMap<String, String>() {{ put("method", "pin"); put("user", clientId); }}); byte[] pairSetupPin1ResponseBytes = AuthUtils.postData(socket, "/pair-setup-pin", "application/x-apple-binary-plist", pairSetupPinRequestData); NSDictionary pairSetupPin1Response = (NSDictionary) PropertyListParser.parse(pairSetupPin1ResponseBytes); if (pairSetupPin1Response.containsKey("pk") && pairSetupPin1Response.containsKey("salt")) { byte[] pk = ((NSData) pairSetupPin1Response.get("pk")).bytes(); byte[] salt = ((NSData) pairSetupPin1Response.get("salt")).bytes(); return new PairSetupPin1Response(pk, salt); } throw new Exception(); }
private PairSetupPin2Response doPairSetupPin2(Socket socket, final byte[] publicClientValueA, final byte[] clientEvidenceMessageM1) throws Exception { byte[] pairSetupPinRequestData = AuthUtils.createPList(new HashMap<String, byte[]>() {{ put("pk", publicClientValueA); put("proof", clientEvidenceMessageM1); }}); byte[] pairSetupPin2ResponseBytes = AuthUtils.postData(socket, "/pair-setup-pin", "application/x-apple-binary-plist", pairSetupPinRequestData); NSDictionary pairSetupPin2Response = (NSDictionary) PropertyListParser.parse(pairSetupPin2ResponseBytes); if (pairSetupPin2Response.containsKey("proof")) { byte[] proof = ((NSData) pairSetupPin2Response.get("proof")).bytes(); return new PairSetupPin2Response(proof); } throw new Exception(); }