private static Object deserializeData(NSData payload, Class<?> elementClass) { if (elementClass == byte.class) { return payload.bytes(); } if (elementClass == Byte.class) { byte[] bytes = payload.bytes(); Object result = Array.newInstance(elementClass, bytes.length); for (int i = 0; i < bytes.length; i++) { Array.set(result, i, bytes[i]); } return result; } throw new IllegalArgumentException("NSData can only be mapped to byte[] or Byte[]."); }
private static NSData fromData(Object object) { int size = Array.getLength(object); byte[] array = new byte[size]; for (int i = 0; i < size; i++) { array[i] = (Byte)(Array.get(object, i)); } return new NSData(array); }
@Override void toXML(StringBuilder xml, int level) { this.indent(xml, level); xml.append("<data>"); xml.append(NSObject.NEWLINE); String base64 = this.getBase64EncodedData(); for (String line : base64.split("\n")) { this.indent(xml, level + 1); xml.append(line); xml.append(NSObject.NEWLINE); } this.indent(xml, level); xml.append("</data>"); }
@Override protected void toASCIIGnuStep(StringBuilder ascii, int level) { this.toASCII(ascii, level); } }
@Override protected void toASCII(StringBuilder ascii, int level) { this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.DATA_BEGIN_TOKEN); int indexOfLastNewLine = ascii.lastIndexOf(NEWLINE); for (int i = 0; i < this.bytes.length; i++) { int b = this.bytes[i] & 0xFF; if (b < 16) ascii.append('0'); ascii.append(Integer.toHexString(b)); if (ascii.length() - indexOfLastNewLine > ASCII_LINE_LENGTH) { ascii.append(NEWLINE); indexOfLastNewLine = ascii.length(); } else if ((i + 1) % 2 == 0 && i != this.bytes.length - 1) { ascii.append(' '); } } ascii.append(ASCIIPropertyListParser.DATA_END_TOKEN); }
/** * Checks whether a given value is contained in this dictionary. * * @param val The value that will be searched for. * @return Whether the key is contained in this dictionary. */ public boolean containsValue(byte[] val) { for (NSObject o : this.dict.values()) { if (o.getClass().equals(NSData.class)) { NSData dat = (NSData) o; if (Arrays.equals(dat.bytes(), val)) return true; } } return false; }
@Override public NSData clone() { return new NSData(this.bytes.clone()); }
static ServiceKeySet backupBagPassword(NSDictionary escrowedData) { byte[] backupBagPassword = PListsLegacy.getAs(escrowedData, "BackupBagPassword", NSData.class).bytes(); logger.debug("-- backupBagPassword() - BackupBagPassword: 0x{}", Hex.toHexString(backupBagPassword)); return DERUtils.parse(backupBagPassword, KeySet::new) .flatMap(ServiceKeySetBuilder::build) .orElseThrow(() -> new IllegalArgumentException("failed to create backup bag key set")); }
public byte[] encoded() { try { NSDictionary dict = new NSDictionary(); dict.put("escrowedKeys", new NSData(escrowedKeys)); dict.put("deviceUuid", new NSString(deviceUuid.toString())); dict.put("deviceHardWareId", new NSString(deviceHardWareId)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PropertyListParser.saveAsBinary(dict, baos); return baos.toByteArray(); } catch (IOException ex) { throw new IllegalStateException(ex); } }
byte[] signaturebytes = signature.bytes(); byte[] plainbytes = values.toString().getBytes(Charset.forName("UTF-8")); try {
return new NSString(getNodeTextContents(n)); } else if (type.equals("data")) { return new NSData(getNodeTextContents(n)); } else if (type.equals("date")) { return new NSDate(getNodeTextContents(n));
static byte[] kPCSMetadataEscrowedKeys(NSDictionary metadata) { NSDictionary clientMetadata = PListsLegacy.getAs(metadata, "ClientMetadata", NSDictionary.class); NSDictionary secureBackupiCloudDataProtection = PListsLegacy.getAs(clientMetadata, "SecureBackupiCloudDataProtection", NSDictionary.class); return PListsLegacy.getAs(secureBackupiCloudDataProtection, "kPCSMetadataEscrowedKeys", NSData.class).bytes(); }
obj = new NSData(bytes);
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(); }
int length = lengthAndOffset[0]; int dataOffset = lengthAndOffset[1]; return new NSData(copyOfRange(this.bytes, offset + dataOffset, offset + dataOffset + length));
static void diagnostic(byte[] metadata) { NSDictionary dictionary = PListsLegacy.parseDictionary(metadata); logger.debug("-- diagnostic() - dictionary: {}", dictionary.toXMLPropertyList()); byte[] backupKeybagDigest = PListsLegacy.getAs(dictionary, "BackupKeybagDigest", NSData.class).bytes(); logger.debug("-- diagnostic() - BackupKeybagDigest: 0x{}", Hex.toHexString(backupKeybagDigest)); Optional<NSString> timestamp = PListsLegacy.optionalAs(dictionary, "com.apple.securebackup.timestamp", NSString.class); logger.debug("-- diagnostic() - com.apple.securebackup.timestamp: {}", timestamp.map(NSString::getContent)); NSDictionary clientMetadata = PListsLegacy.getAs(dictionary, "ClientMetadata", NSDictionary.class); NSDictionary secureBackupiCloudDataProtection = PListsLegacy.getAs(clientMetadata, "SecureBackupiCloudDataProtection", NSDictionary.class); byte[] secureBackupiCloudIdentityPublicData = PListsLegacy.getAs(clientMetadata, "SecureBackupiCloudIdentityPublicData", NSData.class).bytes(); Optional<PublicKeyInfo> optionalPublicKeyInfo = DERUtils.parse(secureBackupiCloudIdentityPublicData, PublicKeyInfo::new); logger.debug("-- diagnostic() - publicKeyInfo: {}", optionalPublicKeyInfo); byte[] kPCSMetadataEscrowedKeys = PListsLegacy.getAs(secureBackupiCloudDataProtection, "kPCSMetadataEscrowedKeys", NSData.class).bytes(); logger.debug("-- diagnostic() - kPCSMetadataEscrowedKeys: 0x{}", Hex.toHexString(kPCSMetadataEscrowedKeys)); } }
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); }
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(); }
return ((NSString)this).getContent(); } else if(this instanceof NSData) { return ((NSData)this).bytes(); } else if(this instanceof NSDate) { return ((NSDate)this).getDate();