public FormRmsFileWatcher()
        {
            InitializeComponent();

            buttonCollapseLog.Tag = false;
            policyList = null;
            currentProtectionPolicy = null;

            fileWatchEngine = new FileWatchEngine();
            fileWatchEngine.MillisecondsBeforeProcessing = waitPeriodToProcess;
            fileWatchEngine.EngineEvent += fileWatchEngine_EngineEvent;

            SafeNativeMethods.IpcInitialize();
            populatePolicyList();
            
            setFormStateFromConfiguration();
        }
Exemplo n.º 2
0
        public static byte[] Encrypt(Uri uri, string templateString, string data)
        {
            MSIPC.ConnectionInfo connectionInfo = new MSIPC.ConnectionInfo(uri, uri);

            object cred = GetOAuthContext(null);
            // object cred = GetCertData();

            Collection <MSIPC.TemplateInfo> templates = MSIPC.SafeNativeMethods.IpcGetTemplateList(
                connectionInfo,
                forceDownload: false,
                suppressUI: true,
                offline: false,
                hasUserConsent: true,
                parentWindow: null,
                cultureInfo: null,
                credentialType: cred);

            MSIPC.TemplateInfo templateToUse = templates[0];
            foreach (MSIPC.TemplateInfo info in templates)
            {
                if (info.TemplateId == templateString)
                {
                    templateToUse = info;
                    break;
                }
            }

            templateString = templateToUse.TemplateId;
            if (!string.IsNullOrEmpty(templateString))
            {
                MSIPC.SafeInformationProtectionKeyHandle keyHandle = null;
                byte[] license = MSIPC.SafeNativeMethods.IpcSerializeLicense(
                    templateString,
                    MSIPC.SerializeLicenseFlags.KeyNoPersist,
                    suppressUI: true,
                    offline: false,
                    hasUserConsent: true,
                    parentWindow: null,
                    keyHandle: out keyHandle,
                    credentialType: cred);

                using (MemoryStream outputStream = new MemoryStream())
                {
                    int    blockSize = MSIPC.SafeNativeMethods.IpcGetKeyBlockSize(keyHandle);
                    byte[] bytes     = Encoding.UTF8.GetBytes(data);

                    MSIPC.SafeNativeMethods.IpcEncrypt(keyHandle, 0, true, ref bytes);

                    byte[] lengthBytes = BitConverter.GetBytes(license.Length);
                    int    length      = 0;
                    while (length + lengthBytes.Length < 4)
                    {
                        outputStream.WriteByte(0);
                        length++;
                    }

                    outputStream.Write(lengthBytes, 0, lengthBytes.Length);
                    outputStream.Write(license, 0, license.Length);
                    outputStream.Write(bytes, 0, bytes.Length);

                    return(outputStream.ToArray());
                }
            }

            return(null);
        }
        // IPC_LI_DESCRIPTOR - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
        public static void IpcSetLicenseDescriptor(SafeInformationProtectionLicenseHandle licenseHandle, TemplateInfo templateInfo)
        {
            int hr = 0;
            IntPtr LicenseInfoPtr = IntPtr.Zero;

            if (null == templateInfo)
            {
                hr = UnsafeNativeMethods.IpcSetLicenseProperty(
                                    licenseHandle,
                                    true,
                                    (uint)LicensePropertyType.Descriptor,
                                    LicenseInfoPtr);
                ThrowOnErrorCode(hr);
            }
            else
            {
                LicenseInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IpcTemplateInfo)));
                try
                {
                    IpcTemplateInfo ipcTemplateInfo = TemplateInfoToIpcTemplateInfo(templateInfo);

                    Marshal.StructureToPtr(ipcTemplateInfo, LicenseInfoPtr, false);
                    hr = UnsafeNativeMethods.IpcSetLicenseProperty(
                                    licenseHandle,
                                    false,
                                    (uint)LicensePropertyType.Descriptor,
                                    LicenseInfoPtr);
                    ThrowOnErrorCode(hr);
                }
                finally
                {
                    Marshal.FreeHGlobal(LicenseInfoPtr);
                }
            }
        }
 /// <summary>
 /// Handle protection policy selection change to track policy to apply to files.
 /// </summary>
 private void comboBoxTemplates_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (comboBoxTemplates.SelectedIndex != -1)
     {
         currentProtectionPolicy = findTemplate((string)comboBoxTemplates.SelectedItem);
     }
 }
        private static IpcTemplateInfo TemplateInfoToIpcTemplateInfo(TemplateInfo templateInfo)
        {
            IpcTemplateInfo ipcTemplateInfo = new IpcTemplateInfo();
            
            ipcTemplateInfo.templateID = templateInfo.TemplateId;
            ipcTemplateInfo.lcid = (uint)templateInfo.CultureInfo.LCID;
            ipcTemplateInfo.templateName = templateInfo.Name;
            ipcTemplateInfo.templateDescription = templateInfo.Description;
            ipcTemplateInfo.issuerDisplayName = templateInfo.IssuerDisplayName;
            ipcTemplateInfo.fromTemplate = templateInfo.FromTemplate;

            return ipcTemplateInfo;
        }