/// <summary> /// Asynchronously reads the pair record for a device. /// </summary> /// <param name="udid"> /// The UDID of the device for which to read the pair record. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation. /// </param> /// <returns> /// A <see cref="Task"/> which represents the asynchronous operation, and which returns the pairing /// record. /// </returns> public async virtual Task <PairingRecord> ReadPairingRecordAsync(string udid, CancellationToken cancellationToken) { if (udid == null) { throw new ArgumentNullException(nameof(udid)); } await using (var protocol = await this.TryConnectToMuxerAsync(cancellationToken).ConfigureAwait(false)) { // Send the read ReadPairRecord message await protocol.WriteMessageAsync( new ReadPairingRecordMessage() { MessageType = MuxerMessageType.ReadPairRecord, PairRecordID = udid, }, cancellationToken).ConfigureAwait(false); var response = (ResultMessage)await protocol.ReadMessageAsync(cancellationToken).ConfigureAwait(false); if (response.Number == MuxerError.BadDevice) { return(null); } else if (response.Number != MuxerError.Success) { throw new MuxerException($"An error occurred while reading the pairing record for device {udid}: {response.Number}.", response.Number); } var pairingRecordResponse = (PairingRecordDataMessage)response; var pairingRecord = PairingRecord.Read(pairingRecordResponse.PairRecordData); return(pairingRecord); } }
public void CopyWithPrivateKeyForSsl_ThrowsOnNull() { var pairingRecord = PairingRecord.Read(File.ReadAllBytes("Lockdown/0123456789abcdef0123456789abcdef01234567.plist")); Assert.Throws <ArgumentNullException>(() => X509Certificate2Extensions.CopyWithPrivateKeyForSsl(pairingRecord.HostCertificate, null)); Assert.Throws <ArgumentNullException>(() => X509Certificate2Extensions.CopyWithPrivateKeyForSsl(null, pairingRecord.HostPrivateKey)); }
public void ToString_ValidPairingRecord() { var raw = File.ReadAllText("Lockdown/0123456789abcdef0123456789abcdef01234567.plist"); var dict = (NSDictionary)XmlPropertyListParser.ParseString(raw); var pairingRecord = PairingRecord.Read(dict); Assert.Equal("HostId: 01234567-012345678901234567, SystemBUID: 01234567890123456789012345, Host certificate: EE63391AA1FBA937E2784CC7DAAA9C22BA223B54 (expires: 2026-11-28 11:20:17Z)", pairingRecord.ToString()); }
public void ToPropertyList_ExcludesPrivateKeysIfRequired() { var raw = File.ReadAllText("Lockdown/0123456789abcdef0123456789abcdef01234567.plist"); var dict = (NSDictionary)XmlPropertyListParser.ParseString(raw); var pairingRecord = PairingRecord.Read(dict); var serializedDict = pairingRecord.ToPropertyList(includePrivateKeys: false); Assert.False(serializedDict.ContainsKey("HostPrivateKey")); Assert.False(serializedDict.ContainsKey("RootPrivateKey")); }
public void ToPropertyList_NoPrivateKeys_Works() { var raw = File.ReadAllText("Lockdown/0123456789abcdef0123456789abcdef01234567.plist"); var dict = (NSDictionary)XmlPropertyListParser.ParseString(raw); var pairingRecord = PairingRecord.Read(dict); pairingRecord.HostPrivateKey = null; pairingRecord.RootPrivateKey = null; var serializedDict = pairingRecord.ToPropertyList(); Assert.Equal(dict.Keys.Count - 2, serializedDict.Keys.Count); }
public void CopyWithPrivateKeyForSsl_Works() { var pairingRecord = PairingRecord.Read(File.ReadAllBytes("Lockdown/0123456789abcdef0123456789abcdef01234567.plist")); using (var certificate = pairingRecord.HostCertificate.CopyWithPrivateKeyForSsl(pairingRecord.HostPrivateKey)) { Assert.NotNull(certificate.PrivateKey); // On Windows, because of a SCHANNEL bug (https://github.com/dotnet/runtime/issues/23749#issuecomment-485947319), private keys // for certificates cannot be marked as ephemeral because the in-memory TLS client certificate private key is not marshaled // between SCHANNEL and LSASS. Make sure the private key is not ephemeral. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var key = Assert.IsType <RSACng>(certificate.PrivateKey); Assert.False(key.Key.IsEphemeral); } } }
public void ToPropertyList_Works() { var raw = File.ReadAllText("Lockdown/0123456789abcdef0123456789abcdef01234567.plist"); var dict = (NSDictionary)XmlPropertyListParser.ParseString(raw); var pairingRecord = PairingRecord.Read(dict); var serializedDict = pairingRecord.ToPropertyList(); Assert.Equal(dict.Keys, serializedDict.Keys); foreach (var key in dict.Keys) { Assert.Equal(dict[key], serializedDict[key]); } var xml = serializedDict.ToXmlPropertyList(); Assert.Equal(raw, xml, ignoreLineEndingDifferences: true); }
public void Read_Works() { var dict = (NSDictionary)PropertyListParser.Parse("Lockdown/0123456789abcdef0123456789abcdef01234567.plist"); var pairingRecord = PairingRecord.Read(dict); Assert.NotNull(pairingRecord.DeviceCertificate); Assert.Equal("879D15EC44D67A89BF0AC3C0311DA035FDD56D0E", pairingRecord.DeviceCertificate.Thumbprint); Assert.Equal("MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA=", Convert.ToBase64String(pairingRecord.EscrowBag)); Assert.NotNull(pairingRecord.HostCertificate); Assert.Equal("EE63391AA1FBA937E2784CC7DAAA9C22BA223B54", pairingRecord.HostCertificate.Thumbprint); Assert.Equal("01234567-012345678901234567", pairingRecord.HostId); Assert.NotNull(pairingRecord.HostPrivateKey); Assert.Equal(2048, pairingRecord.HostPrivateKey.KeySize); Assert.NotNull(pairingRecord.RootCertificate); Assert.Equal("DB0F6BAA694FA99879281A388D170CCE1412AC92", pairingRecord.RootCertificate.Thumbprint); Assert.NotNull(pairingRecord.RootPrivateKey); Assert.Equal(2048, pairingRecord.RootPrivateKey.KeySize); Assert.Equal("01234567890123456789012345", pairingRecord.SystemBUID); Assert.Equal("01:23:45:67:89:ab", pairingRecord.WiFiMacAddress); }