public SymmetricKeyCredential GetSymmetricKey()
 {
     SymmetricKeyCredential symKey = new SymmetricKeyCredential()
     {
         BposTenantId = TenantId,
         AppPrincipalId = AppId,
         Base64Key = Key
     };
     return symKey;
 }
        public static string IpcfEncryptFile(
            string inputFile,
            string templateId,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey = null,
            string outputDirectory = null)
        {
            int hr = 0;
            IntPtr encryptedFileName = IntPtr.Zero;
            string outputFileName = null;

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                    offline,
                    hasUserConsent,
                    parentForm,
                    symmKey);

            IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId);

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFile(
                    inputFile,
                    licenseInfoPtr,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_TEMPLATE_ID,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    outputDirectory,
                    out encryptedFileName);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                outputFileName = Marshal.PtrToStringUni(encryptedFileName);
                if (null == outputFileName || 0 == outputFileName.Length)
                {
                    outputFileName = inputFile;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(licenseInfoPtr);
                UnsafeFileApiMethods.IpcFreeMemory(encryptedFileName);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return outputFileName;
        }
예제 #3
0
 public static string IpcfEncryptFileStream(
     Stream inputStream,
     string inputFilePath,
     SafeInformationProtectionLicenseHandle licenseHandle,
     EncryptFlags flags,
     bool suppressUI,
     bool offline,
     bool hasUserConsent,
     Form parentWindow,
     SymmetricKeyCredential symmKey,
     ref Stream outputStream)
 {
     return(IpcfEncryptFileStream(
                inputStream,
                inputFilePath,
                licenseHandle,
                flags,
                suppressUI,
                offline,
                hasUserConsent,
                IpcWindow.Create(parentWindow).Handle,
                symmKey,
                ref outputStream));
 }
예제 #4
0
 public static string IpcfEncryptFile(
     string inputFile,
     string templateId,
     EncryptFlags flags,
     bool suppressUI,
     bool offline,
     bool hasUserConsent,
     Form parentWindow,
     SymmetricKeyCredential symmKey    = null,
     string outputDirectory            = null,
     WaitHandle cancelCurrentOperation = null)
 {
     return(IpcfEncryptFile(
                inputFile,
                templateId,
                flags,
                suppressUI,
                offline,
                hasUserConsent,
                IpcWindow.Create(parentWindow).Handle,
                symmKey,
                outputDirectory,
                cancelCurrentOperation));
 }
 private RmsContentPublisher(SymmetricKeyCredential _servicePrincipalTuple)
 {
     symmetricKey = _servicePrincipalTuple;
 }
        public static string IpcfEncryptFile(
            string inputFile,
            SafeInformationProtectionLicenseHandle licenseHandle,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey,
            string outputDirectory = null)
        {
            int hr = 0;
            IntPtr encryptedFileName = IntPtr.Zero;
            string outputFileName = null;

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                    offline,
                    hasUserConsent,
                    parentForm,
                    symmKey);

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFile(
                    inputFile,
                    licenseHandle.Value,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_LICENSE_HANDLE,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    outputDirectory,
                    out encryptedFileName);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                outputFileName = Marshal.PtrToStringUni(encryptedFileName);
                if (null == outputFileName || 0 == outputFileName.Length)
                {
                    outputFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(encryptedFileName);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return outputFileName;
        }
        public static string IpcfDecryptFile(
            string inputFile,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey,
            string outputDirectory = null)
        {
            int hr = 0;
            IntPtr decryptedFileNamePtr = IntPtr.Zero;
            string decryptedFileName = null;

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                    offline,
                    hasUserConsent,
                    parentForm,
                    symmKey);

            try
            {
                hr = UnsafeFileApiMethods.IpcfDecryptFile(
                    inputFile,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    outputDirectory,
                    out decryptedFileNamePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return decryptedFileName;
        }
        // IpcGetTemplateIssuerList() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535266(v=vs.85).aspx
        public static Collection<TemplateIssuer> IpcGetTemplateIssuerList(
                            ConnectionInfo connectionInfo,
                            bool defaultServerOnly,
                            bool suppressUI,
                            bool offline,
                            bool hasUserConsent,
                            System.Windows.Forms.Form parentForm,
                            SymmetricKeyCredential symmKey = null)
        {
            Collection<TemplateIssuer> templateIssuerList = null;
            int hr = 0;

            uint flags = 0;
            if (defaultServerOnly)
            {
                flags |= Convert.ToUInt32(GetTemplateIssuerListFlags.DefaultServerOnly);
            }

            IpcConnectionInfo ipcConnectionInfo = ConnectionInfoToIpcConnectionInfo(connectionInfo);

            SafeIpcPromptContext ipcPromptContext = CreateIpcPromptContext(suppressUI, offline, hasUserConsent, parentForm, symmKey);
            
            IntPtr ipcTemplateIssuerListPtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcGetTemplateIssuerList(
                                ipcConnectionInfo,
                                flags,
                                (IpcPromptContext)ipcPromptContext,
                                IntPtr.Zero,
                                out ipcTemplateIssuerListPtr);
                ThrowOnErrorCode(hr);

                templateIssuerList = new Collection<TemplateIssuer>();

                MarshalIpcTemplateIssuerListToManaged(ipcTemplateIssuerListPtr, templateIssuerList);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(ipcTemplateIssuerListPtr);
                ReleaseIpcPromptContext(ipcPromptContext);
            }

            return templateIssuerList;
        }
        // Private Helpers
        public static SafeIpcPromptContext CreateIpcPromptContext(bool suppressUI, bool offline, bool hasUserConsent, System.Windows.Forms.Form parentForm, SymmetricKeyCredential symmKey = null)
        {
            SafeIpcPromptContext ipcPromptContext = new SafeIpcPromptContext(
                ((null != parentForm) ? parentForm.Handle : IntPtr.Zero),
                ((null != symmKey) ? new IpcCredential(IpcCredentialType.SymmetricKey, symmKey) : null),
                //We don't support the cancel event
                IntPtr.Zero);

            IpcPromptContext context = (IpcPromptContext)ipcPromptContext;
            context.flags = 0;
            if (suppressUI)
            {
                context.flags |= (uint)PromptContextFlag.Slient;
            }

            if (offline)
            {
                context.flags |= (uint)PromptContextFlag.Offline;
            }

            if (hasUserConsent)
            {
                context.flags |= (uint)PromptContextFlag.HasUserConsent;
            }
            return ipcPromptContext;
        }
        // IpcGetTemplateList() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535267(v=vs.85).aspx
        public static Collection<TemplateInfo> IpcGetTemplateList(
                            ConnectionInfo connectionInfo,
                            bool forceDownload,
                            bool suppressUI,
                            bool offline,
                            bool hasUserConsent,
                            System.Windows.Forms.Form parentForm,
                            CultureInfo cultureInfo,
                            SymmetricKeyCredential symmKey = null)
        {
            Collection<TemplateInfo> templateList = null;
            int hr = 0;

            uint flags = 0;
            if (forceDownload)
            {
                flags |= Convert.ToUInt32(GetTemplateListFlags.ForceDownload);
            }

            uint lcid = 0;
            if (null != cultureInfo)
            {
                lcid = (uint)(cultureInfo.LCID);
            }

            IpcConnectionInfo ipcConnectionInfo = ConnectionInfoToIpcConnectionInfo(connectionInfo);

            SafeIpcPromptContext ipcPromptContext = CreateIpcPromptContext(suppressUI, offline, hasUserConsent, parentForm, symmKey);

            IntPtr ipcTilPtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcGetTemplateList(
                                ipcConnectionInfo,
                                flags,
                                lcid,
                                (IpcPromptContext)ipcPromptContext,
                                IntPtr.Zero,
                                out ipcTilPtr);
                ThrowOnErrorCode(hr);

                templateList = new Collection<TemplateInfo>();

                MarshalIpcTilToManaged(ipcTilPtr, templateList);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(ipcTilPtr);
                ReleaseIpcPromptContext(ipcPromptContext);
            }

            return templateList;

        }