public RmsPropertyParser(byte[] fileLicense, SafeInformationProtectionKeyHandle keyHandle)
        {
            FileLicense = fileLicense;
            KeyHandle = keyHandle;

            DocumentProperties = new RmsDocumentProperties();
            DocumentProperties.ConnectionInfo = getConnectionInfo();
            DocumentProperties.ContentId = getSimpleProperty(LicensePropertyType.ContentId);
            DocumentProperties.Descriptor = getDescriptor();
            DocumentProperties.IntervalTime = getIntervalTime();
            DocumentProperties.Owner = getSimpleProperty(LicensePropertyType.Owner);
            DocumentProperties.ReferralInfoUrl = getSimpleProperty(LicensePropertyType.ReferralInfoUrl);
            DocumentProperties.UserRightsList = getRightsList();
            DocumentProperties.ValidityTime = getValidityTime();
        }
        // IpcGetSerializedLicenseProperty - http://msdn.microsoft.com/en-us/library/windows/desktop/hh995038(v=vs.85).aspx

        // IPC_LI_VALIDITY_TIME - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
        public static Term IpcGetSerializedLicenseValidityTime(byte[] license, SafeInformationProtectionKeyHandle keyHandle)
        {
            IpcTerm ipcValidityTime = null;
            int hr = 0;

            IntPtr licenseInfoPtr = IntPtr.Zero;
            SafeIpcBuffer ipcBuffer = MarshalIpcBufferToNative(license);
            try
            {
                hr = UnsafeNativeMethods.IpcGetSerializedLicenseProperty(
                                (IpcBuffer)ipcBuffer,
                                (uint)LicensePropertyType.ValidityTime,
                                keyHandle,
                                0,
                                out licenseInfoPtr);
                ThrowOnErrorCode(hr);

                ipcValidityTime = (IpcTerm)Marshal.PtrToStructure(licenseInfoPtr, typeof(IpcTerm));
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licenseInfoPtr);
                ipcBuffer.Dispose();
            }

            return IpcTermToTerm(ipcValidityTime);
        }
        // IPC_LI_CONTENT_KEY - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
        public static SafeInformationProtectionKeyHandle IpcGetLicenseContentKey(SafeInformationProtectionLicenseHandle licenseHandle)
        {
            SafeInformationProtectionKeyHandle keyHandle = null;
            int hr = 0;

            IntPtr licenseInfoPtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcGetLicenseProperty(
                                licenseHandle,
                                (uint)LicensePropertyType.ReferralInfoUrl,
                                0,
                                out licenseInfoPtr);
                ThrowOnErrorCode(hr);

                keyHandle = new SafeInformationProtectionKeyHandle(licenseInfoPtr);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licenseInfoPtr);
            }

            return keyHandle;
        }
 // IPC_LI_CONTENT_KEY - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
 public static void IpcSetLicenseContentKey(SafeInformationProtectionLicenseHandle licenseHandle, SafeInformationProtectionKeyHandle hKey)
 {
     int hr = 0;
     hr = UnsafeNativeMethods.IpcSetLicenseProperty(
                     licenseHandle,
                     false,
                     (uint)LicensePropertyType.ContentKey,
                     hKey.Value);
     ThrowOnErrorCode(hr);
 }
        // IpcSerializeLicense() using License Handle - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535269(v=vs.85).aspx
        public static byte[] IpcSerializeLicense(
                                SafeInformationProtectionLicenseHandle licenseHandle,
                                SerializeLicenseFlags flags,
                                bool suppressUI,
                                bool offline,
                                bool hasUserConsent,
                                System.Windows.Forms.Form parentForm,
                                out SafeInformationProtectionKeyHandle keyHandle)
        {
            byte[] license = null;
            int hr = 0;

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

            IntPtr licensePtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcSerializeLicense(
                                        licenseHandle.Value,
                                        SerializationInputType.License,
                                        (uint)flags,
                                        (IpcPromptContext)ipcPromptContext,
                                        out keyHandle,
                                        out licensePtr);

                ThrowOnErrorCode(hr);

                license = MarshalIpcBufferToManaged(licensePtr);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licensePtr);
            }

            return license;
        }
 internal static extern int IpcAccessCheck(
     [In] SafeInformationProtectionKeyHandle hKey,
     [In, MarshalAs(UnmanagedType.LPWStr)]
     string wszRequestedRight,
     [Out, MarshalAs(UnmanagedType.Bool)]
     out bool bAccessGranted);
        // IpcDecrypt() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535258(v=vs.85).aspx
        public static byte[] IpcDecrypt(SafeInformationProtectionKeyHandle keyHandle,
                                        UInt32 blockNumber,
                                        bool final,
                                        byte[] data)
        {
            int hr = 0;

            uint inputDataSize = (uint)data.Length;
            uint decryptedDataSize = 0;

            if (0 != data.Length)
            {
                hr = UnsafeNativeMethods.IpcDecrypt(
                                    keyHandle,
                                    blockNumber,
                                    final,
                                    data,
                                    inputDataSize,
                                    null,
                                    0,
                                    out decryptedDataSize);
                ThrowOnErrorCode(hr);
            }

            byte[] decryptedData = new byte[decryptedDataSize];
            if (0 < decryptedDataSize)
            {
                hr = UnsafeNativeMethods.IpcDecrypt(
                                    keyHandle,
                                    blockNumber,
                                    final,
                                    data,
                                    inputDataSize,
                                    decryptedData,
                                    decryptedDataSize,
                                    out decryptedDataSize);
                ThrowOnErrorCode(hr);
            }
            Array.Resize(ref decryptedData, (int)decryptedDataSize);
            return decryptedData;
        }
 public static NameValueCollection IpcGetSerializedLicenseAppSpecificDataNoEncryption(byte[] license, SafeInformationProtectionKeyHandle keyHandle)
 {
     return IpcGetSerializedLicenseAppSpecificData(license, keyHandle, LicensePropertyType.AppSpecificDataNoEncryption);
 }
 // IpcAccessCheck() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535253(v=vs.85).aspx
 public static bool IpcAccessCheck(SafeInformationProtectionKeyHandle keyHandle, string right)
 {
     bool accessGranted = false;
     int hr = UnsafeNativeMethods.IpcAccessCheck(
                         keyHandle,
                         right,
                         out accessGranted);
     ThrowOnErrorCode(hr);
     return accessGranted;
 }
        // IpcDecrypt() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535258(v=vs.85).aspx
        public static void IpcDecrypt(SafeInformationProtectionKeyHandle keyHandle,
                                    UInt32 blockNumber,
                                    bool final,
                                    ref byte[] data)
        {
            int hr = 0;

            uint inputDataSize = (uint)data.Length;
            uint decryptedDataSize = 0;

            hr = UnsafeNativeMethods.IpcDecrypt(
                                keyHandle,
                                blockNumber,
                                final,
                                data,
                                inputDataSize,
                                null,
                                0,
                                out decryptedDataSize);
            ThrowOnErrorCode(hr);

            if (decryptedDataSize > inputDataSize)
            {
                Array.Resize(ref data, (int)decryptedDataSize);
            }

            hr = UnsafeNativeMethods.IpcDecrypt(
                                keyHandle,
                                blockNumber,
                                final,
                                data,
                                inputDataSize,
                                data,
                                decryptedDataSize,
                                out decryptedDataSize);
            ThrowOnErrorCode(hr);

            Array.Resize(ref data, (int)decryptedDataSize);
        }
        // IPC_LI_DESCRIPTOR - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
        public static TemplateInfo IpcGetSerializedLicenseDescriptor(byte[] license,
                                                                       SafeInformationProtectionKeyHandle keyHandle,
                                                                       CultureInfo cultureInfo)
        {
            IpcTemplateInfo ipcTemplateInfo = null;
            int hr = 0;

            IntPtr licenseInfoPtr = IntPtr.Zero;
            SafeIpcBuffer ipcBuffer = MarshalIpcBufferToNative(license);
            try
            {
                uint lcid = 0;
                if (null != cultureInfo)
                {
                    lcid = (uint)(cultureInfo.LCID);
                }

                hr = UnsafeNativeMethods.IpcGetSerializedLicenseProperty(
                                (IpcBuffer)ipcBuffer,
                                (uint)LicensePropertyType.Descriptor,
                                keyHandle,
                                lcid,
                                out licenseInfoPtr);
                ThrowOnErrorCode(hr);

                ipcTemplateInfo = (IpcTemplateInfo)Marshal.PtrToStructure(licenseInfoPtr, typeof(IpcTemplateInfo));
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licenseInfoPtr);
                ipcBuffer.Dispose();
            }

            return IpcTemplateInfoToTemplateInfo(ipcTemplateInfo);
        }
 internal static extern int IpcGetSerializedLicenseProperty(
     [In, MarshalAs(UnmanagedType.LPStruct)] IpcBuffer pvLicense,
     [In, MarshalAs(UnmanagedType.U4)] uint dwPropID,
     [In] SafeInformationProtectionKeyHandle hKey,
     [In, MarshalAs(UnmanagedType.U4)] uint lcid,
     [Out] out IntPtr ppvProperty);
 internal static extern int IpcGetKeyProperty(
     [In] SafeInformationProtectionKeyHandle hKey,
     [In, MarshalAs(UnmanagedType.U4)] uint dwPropID,
     [In] IntPtr pvReserved,
     [Out] out IntPtr ppvProperty);
        // IPC_LI_INTERVAL_TIME - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
        public static uint IpcGetSerializedLicenseIntervalTime(byte[] license, SafeInformationProtectionKeyHandle keyHandle)
        {
            uint intervalTime = 0;
            int hr = 0;

            IntPtr licenseInfoPtr = IntPtr.Zero;
            SafeIpcBuffer ipcBuffer = MarshalIpcBufferToNative(license);
            try
            {
                hr = UnsafeNativeMethods.IpcGetSerializedLicenseProperty(
                                (IpcBuffer)ipcBuffer,
                                (uint)LicensePropertyType.IntervalTime,
                                keyHandle,
                                0,
                                out licenseInfoPtr);
                ThrowOnErrorCode(hr);

                intervalTime = (uint)Marshal.ReadInt32(licenseInfoPtr);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licenseInfoPtr);
                ipcBuffer.Dispose();
            }

            return intervalTime;
        }
        // IpcGetKeyProperty() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535264(v=vs.85).aspx

        // IPC_KI_BLOCK_SIZE - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535264(v=vs.85).aspx
        public static int IpcGetKeyBlockSize(SafeInformationProtectionKeyHandle keyHandle)
        {
            int blockSize = 0;
            int hr = 0;

            IntPtr keyInfoPtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcGetKeyProperty(
                                keyHandle,
                                (uint)KeyPropertyType.BlockSize,
                                IntPtr.Zero,
                                out keyInfoPtr);
                ThrowOnErrorCode(hr);

                blockSize = Marshal.ReadInt32(keyInfoPtr);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(keyInfoPtr);
            }

            return blockSize;
        }
        // IPC_LI_USER_RIGHTS_LIST - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535287(v=vs.85).aspx
        public static Collection<UserRights> IpcGetSerializedLicenseUserRightsList(byte[] license, SafeInformationProtectionKeyHandle keyHandle)
        {
            Collection<UserRights> userRightsList = new Collection<UserRights>();
            int hr = 0;

            IntPtr licenseInfoPtr = IntPtr.Zero;
            SafeIpcBuffer ipcBuffer = MarshalIpcBufferToNative(license);
            try
            {
                hr = UnsafeNativeMethods.IpcGetSerializedLicenseProperty(
                                (IpcBuffer)ipcBuffer,
                                (uint)LicensePropertyType.UserRightsList,
                                keyHandle,
                                0,
                                out licenseInfoPtr);
                ThrowOnErrorCode(hr);

                MarshalUserRightsListToManaged(licenseInfoPtr, userRightsList);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licenseInfoPtr);
                ipcBuffer.Dispose();
            }

            return userRightsList;
        }
        // IPC_KI_LICENSE - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535264(v=vs.85).aspx
        public static byte[] IpcGetKeyLicense(SafeInformationProtectionKeyHandle keyHandle)
        {
            byte[] license = null;
            int hr = 0;

            IntPtr keyInfoPtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcGetKeyProperty(
                                keyHandle,
                                (uint)KeyPropertyType.License,
                                IntPtr.Zero,
                                out keyInfoPtr);
                ThrowOnErrorCode(hr);

                license = MarshalIpcBufferToManaged(keyInfoPtr);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(keyInfoPtr);
            }

            return license;
        }
        private static NameValueCollection IpcGetSerializedLicenseAppSpecificData(byte[] license, SafeInformationProtectionKeyHandle keyHandle, LicensePropertyType type)
        {
            NameValueCollection applicationSpecificData = new NameValueCollection();
            int hr = 0;

            IntPtr licenseInfoPtr = IntPtr.Zero;
            SafeIpcBuffer ipcBuffer = MarshalIpcBufferToNative(license);
            try
            {
                hr = UnsafeNativeMethods.IpcGetSerializedLicenseProperty(
                                (IpcBuffer)ipcBuffer,
                                (uint)type,
                                keyHandle,
                                0,
                                out licenseInfoPtr);
                ThrowOnErrorCode(hr);

                MarshalNameValueListToManaged(licenseInfoPtr, applicationSpecificData);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(licenseInfoPtr);
                ipcBuffer.Dispose();
            }

            return applicationSpecificData;
        }
        // IPC_KI_USER_DISPLAYNAME - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535264(v=vs.85).aspx
        public static string IpcGetKeyUserDisplayName(SafeInformationProtectionKeyHandle keyHandle)
        {
            string userDisplayName = null;
            int hr = 0;

            IntPtr keyInfoPtr = IntPtr.Zero;
            try
            {
                hr = UnsafeNativeMethods.IpcGetKeyProperty(
                                keyHandle,
                                (uint)KeyPropertyType.UserDisplayName,
                                IntPtr.Zero,
                                out keyInfoPtr);
                ThrowOnErrorCode(hr);

                userDisplayName = Marshal.PtrToStringUni(keyInfoPtr);
            }
            finally
            {
                UnsafeNativeMethods.IpcFreeMemory(keyInfoPtr);
            }

            return userDisplayName;
        }
예제 #20
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);
        }
 internal static extern int IpcGetKey(
     [In, MarshalAs(UnmanagedType.LPStruct)] IpcBuffer pvLicense,
     [In, MarshalAs(UnmanagedType.U4)] uint dwFlags,
     [In, MarshalAs(UnmanagedType.LPStruct)] IpcPromptContext pContext,
     [In] IntPtr pvReserved,
     [Out] out SafeInformationProtectionKeyHandle phKey);