internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials) { PEER_CONTACT pc = new PEER_CONTACT(); pc.pwzDisplayName = peerContact.DisplayName; pc.pwzEmailAddress = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString(); pc.pwzNickname = peerContact.Nickname; pc.pwzPeerName = peerContact.PeerName.ToString(); pc.fWatch = peerContact.IsSubscribed; pc.WatcherPermissions = peerContact.SubscribeAllowed; PEER_DATA pd = new PEER_DATA(); if (peerContact.Credentials != null){ SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2), 0, IntPtr.Zero, 0x00002000/*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001/*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/, IntPtr.Zero); if (certHandle == null || certHandle.IsInvalid){ int win32ErrorCode = Marshal.GetLastWin32Error(); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode); } try{ X509Store certStore = new X509Store(certHandle.DangerousGetHandle()); certStore.Add(peerContact.Credentials as X509Certificate2); bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd, 0); if ((pd.cbData != 0) && (returnCode)){ safeCredentials = new SafeCollabMemory((int)pd.cbData); pd.pbData = safeCredentials.DangerousGetHandle(); returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd,// Clean up memory from here; 0); } else{ pd.cbData = 0; pd.pbData = IntPtr.Zero; } } finally{ if (certHandle != null) certHandle.Dispose(); } } else{ pd.cbData = 0; pd.pbData = IntPtr.Zero; } pc.credentials = pd; return pc; }
internal extern static bool CertSaveStore(SafeCertStore hCertStore, uint dwMsgAndCertEncodingType, uint dwSaveAs, uint dwSaveTo, ref PEER_DATA pvSafeToPara, uint dwFlags);
internal extern static SafeCertStore CertOpenStore(IntPtr lpszStoreProvider, uint dwMsgAndCertEncodingType, IntPtr hCryptProv, uint dwFlags, ref PEER_DATA pvPara);
internal extern static bool CertSaveStore( SafeCertStore hCertStore, uint dwMsgAndCertEncodingType, uint dwSaveAs, uint dwSaveTo, ref PEER_DATA pvSafeToPara, uint dwFlags);
internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials) { PEER_CONTACT pc = new PEER_CONTACT(); pc.pwzDisplayName = peerContact.DisplayName; pc.pwzEmailAddress = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString(); pc.pwzNickname = peerContact.Nickname; pc.pwzPeerName = peerContact.PeerName.ToString(); pc.fWatch = peerContact.IsSubscribed; pc.WatcherPermissions = peerContact.SubscribeAllowed; PEER_DATA pd = new PEER_DATA(); if (peerContact.Credentials != null) { SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2), 0, IntPtr.Zero, 0x00002000 /*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001 /*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/, IntPtr.Zero); if (certHandle == null || certHandle.IsInvalid) { int win32ErrorCode = Marshal.GetLastWin32Error(); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode); } try{ X509Store certStore = new X509Store(certHandle.DangerousGetHandle()); certStore.Add(peerContact.Credentials as X509Certificate2); bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd, 0); if ((pd.cbData != 0) && (returnCode)) { safeCredentials = new SafeCollabMemory((int)pd.cbData); pd.pbData = safeCredentials.DangerousGetHandle(); returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd, // Clean up memory from here; 0); } else { pd.cbData = 0; pd.pbData = IntPtr.Zero; } } finally{ if (certHandle != null) { certHandle.Dispose(); } } } else { pd.cbData = 0; pd.pbData = IntPtr.Zero; } pc.credentials = pd; return(pc); }