// tries to parse all document properties, getting authorization if we can, but otherwise // gracefully falls back to just getting public properties private void collectDocumentProperties(string file) { byte[] fileLicense; SafeInformationProtectionKeyHandle keyHandle; fileLicense = SafeFileApiNativeMethods.IpcfGetSerializedLicenseFromFile(file); keyHandle = null; try { keyHandle = SafeNativeMethods.IpcGetKey(fileLicense, false, false, true, this); } catch { } propertyParser = new RmsPropertyParser(fileLicense, keyHandle); }
/// <summary> /// Load pdf file by stream /// </summary> /// <param name="fileName"></param> /// <returns></returns> private bool LoadFileByStream(string fileName) { bool isrmsProtect = true; Stream stream = null; byte[] license = null; string rmsUserPassword = string.Empty; if (rmsUserpEncrypt) { try { //RMS化PDFファイルから、RMSライセンス情報と、暗号化された本文情報を分割する //RMS署名情報から、RMSサーバー情報を抽出する //RMSサーバーでの認証 //RMSサーバーからRMSライセンスの取得 license = SafeFileApiNativeMethods.IpcfGetSerializedLicenseFromFile(fileName); } catch (Exception ex) { isrmsProtect = false; } } if (isrmsProtect && rmsUserpEncrypt) { try { rmsUserPassword = GenerateRandom(32); //RMSライセンスから、復号鍵の抽出 SafeInformationProtectionKeyHandle keyHandle = SafeNativeMethods.IpcGetKey(license, false, false, true, this); //RMSライセンスから、権利リストの抽出 //Collection<UserRights> userRights = new Collection<UserRights>(); //userRights = SafeNativeMethods.IpcGetSerializedLicenseUserRightsList(license, keyHandle); bool accessGranted = SafeNativeMethods.IpcAccessCheck(keyHandle, "VIEW"); //本文情報を復号鍵で、復号 tempFile = GenerateRandom(10); //一時フォルダ作成 add kondo System.IO.Directory.CreateDirectory(Path.GetTempPath() + @"PDFViewer\"); tempFile = Path.GetTempPath() + @"PDFViewer\" + tempFile; Stream outPutRmsStream = new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); stream = new FileStream(fileName, FileMode.Open); if (accessGranted) { SafeFileApiNativeMethods.IpcfDecryptFileStream(stream, fileName, SafeFileApiNativeMethods.DecryptFlags.IPCF_DF_FLAG_DEFAULT, false, false, false, this, ref outPutRmsStream); } outPutRmsStream.Close(); outPutRmsStream.Dispose(); PdfReader reader = new PdfReader(tempFile); outPutStream = new FileStream(tempFile + ".tmp", FileMode.Create, FileAccess.ReadWrite, FileShare.None); PdfEncryptor.Encrypt(reader, outPutStream, false, rmsUserPassword, "", 0); rmsUserpEncrypt = false; reader.Close(); reader.Dispose(); File.Delete(tempFile); } catch (InformationProtectionException ex) { //DirectoryDelete MSIPC DeleteDirectorySelect(true); isrmsProtect = false; MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK); } catch (Exception ex) { isrmsProtect = false; } } try { if (isrmsProtect) { pdfDoc.LoadPDF(tempFile + ".tmp"); } else { pdfDoc.LoadPDF(fileName); } if (stream != null) { stream.Close(); stream.Dispose(); } return(true); } catch (System.Security.SecurityException sex) { if (stream != null) { stream.Close(); stream.Dispose(); } if (pdfDoc != null) { pdfDoc.Dispose(); pdfDoc = null; } pdfDoc = new PDFWrapper(); if (!rmsUserpEncrypt) { pdfDoc.UserPassword = rmsUserPassword; } else { String password = Interaction.InputBox("Please enter the document password:"******"Document Password", ""); if (password.Equals(string.Empty)) { return(false); } pdfDoc.UserPassword = password; } return(LoadFileByStream(fileName)); } catch (Exception ex) { //MessageBox.Show(ex.ToString()); return(false); } }
public bool ReadEncryptedContent(string inputFile, out string encryptContent) { SafeInformationProtectionKeyHandle _keyHandle = null; // RMSServerURL _rmsURLs; TemplateInfo _template; Collection <UserRights> _rights; Term _term; byte[] _license; int i = 1; int j = 1; int days; string _info = ""; _info += "=============== 擷取檔案【" + inputFile.Trim() + "】資訊 ===============\r\n"; _info += "加密狀態:已加密\r\n"; _license = SafeFileApiNativeMethods.IpcfGetSerializedLicenseFromFile(inputFile.Trim()); // 自加密檔案取得憑證資訊 _keyHandle = SafeNativeMethods.IpcGetKey(_license, false, false, true, this); // // 從憑證中取得加密金鑰之處理指標(非金鑰內容), 只是它的 pointer。 _info += "憑證作者: " + SafeNativeMethods.IpcGetSerializedLicenseOwner(_license) + "\r\n"; // 自加密憑證中 取得憑證擁有者資訊 _info += "加密內容 ID: " + SafeNativeMethods.IpcGetSerializedLicenseContentId(_license, _keyHandle) + "\r\n"; // 自加密憑證中取得憑證內容 ID _info += "金鑰(Key)擁有者: " + SafeNativeMethods.IpcGetKeyUserDisplayName(_keyHandle) + "\r\n"; // 取得加密作者資訊 _term = SafeNativeMethods.IpcGetSerializedLicenseValidityTime(_license, _keyHandle); // 取得此範本的有效期 if (_term.From.Year != 1601) { _info += "\t 有效期: 自 " + _term?.From.ToString() + " 開始,可使用" + _term?.Duration.TotalDays.ToString() + " 天\r\n"; } else { _info += "\t 有效期: 可永久使用。\r\n"; } try { days = (int)SafeNativeMethods.IpcGetSerializedLicenseIntervalTime(_license, _keyHandle); } catch { days = -1; } _info += "\t 更新頻率: " + ((days != -1) ? ("每 " + days.ToString() + "天更新") : ("【未設定】")) + "\r\n"; try { _template = SafeNativeMethods.IpcGetSerializedLicenseDescriptor(_license, _keyHandle, null); // 得憑證的各項敘述內容 _info += "權限原則範本資訊(Template Info):\r\n"; _info += "\t 權限資訊來自範本: " + _template.FromTemplate + "\r\n"; _info += "\t 範本代號: " + _template.TemplateId + "\r\n"; _info += "\t 範本名稱: " + _template.Name + "\r\n"; _info += "\t 範本發行者(RMS Server): " + _template.IssuerDisplayName + "\r\n"; _info += "\t 範本說明:" + _template.Description + "\r\n"; } catch { _info += "權限原則範本資訊(Template Info):無 (此檔案由使用者自定之權限原則所加密)\r\n"; } _rights = SafeNativeMethods.IpcGetSerializedLicenseUserRightsList(_license, _keyHandle); // 取得序列化憑證中的使用者權限內容 _info += "\t 權限列表:\r\n\t\t 授權人數:" + _rights.Count.ToString() + " 人\r\n"; foreach (var u in _rights) { _info += "\t\t(" + j.ToString() + ") " + u.UserId + "\r\n"; j++; _info += "\t\t 權限:"; foreach (var r in u.Rights) { _info += r + ", "; i++; if (i > 6) { _info += "\r\n\t\t"; i = 1; } } i = 1; _info = _info.Substring(0, (_info.Length - 1)); _info += "\r\n"; } _keyHandle.Dispose(); _info += "==================================================\r\n"; encryptContent = _info; return(true); }
/// <summary> /// Load pdf file /// </summary> /// <param name="fileName"></param> /// <returns></returns> private bool LoadFile(string fileName) { bool isrmsProtect = true; byte[] license = null; try { //RMS化PDFファイルから、RMSライセンス情報と、暗号化された本文情報を分割する //RMS署名情報から、RMSサーバー情報を抽出する //RMSサーバーでの認証 //RMSサーバーからRMSライセンスの取得 license = SafeFileApiNativeMethods.IpcfGetSerializedLicenseFromFile(fileName); } catch (Exception ex) { isrmsProtect = false; } if (isrmsProtect) { try { //SymmetricKeyCredential symmkey = new SymmetricKeyCredential(); //symmkey.AppPrincipalId = "0C5BDABD-CF4D-4FBB-BF4A-DD62BCF7E976"; //symmkey.Base64Key = "P@ssw0rd"; //symmkey.BposTenantId = "*****@*****.**"; SymmetricKeyCredential symmkey = null; //RMSライセンスから、復号鍵の抽出 SafeInformationProtectionKeyHandle keyHandle = SafeNativeMethods.IpcGetKey(license, false, false, true, this); //symmkey = (SymmetricKeyCredential)keyHandle; //RMSライセンスから、権利リストの抽出 //Collection<UserRights> userRights = new Collection<UserRights>(); //userRights = SafeNativeMethods.IpcGetSerializedLicenseUserRightsList(license, keyHandle); bool accessGranted = SafeNativeMethods.IpcAccessCheck(keyHandle, "VIEW"); if (accessGranted) { SafeFileApiNativeMethods.IpcfDecryptFile(fileName, SafeFileApiNativeMethods.DecryptFlags.IPCF_DF_FLAG_DEFAULT, false, false, true, this, symmkey); } //使用権限が正しく設定されていません //ConnectionInfo connectionInfo = SafeNativeMethods.IpcGetSerializedLicenseConnectionInfo(license); //System.Collections.ObjectModel.Collection<TemplateIssuer> templateIssuerList = SafeNativeMethods.IpcGetTemplateIssuerList(connectionInfo, false, false, false, false, this, symmkey); //TemplateIssuer templateIssuer = templateIssuerList[0]; //SafeInformationProtectionLicenseHandle licenseHandle = SafeNativeMethods.IpcCreateLicenseFromScratch(templateIssuer); //SafeFileApiNativeMethods.IpcfEncryptFile(fileName, licenseHandle, SafeFileApiNativeMethods.EncryptFlags.IPCF_EF_FLAG_DEFAULT, false, false, false, this, symmkey); //テンプレートは管理者によって作成されていません //TemplateInfo templateInfo = SafeNativeMethods.IpcGetSerializedLicenseDescriptor(license, keyHandle, System.Globalization.CultureInfo.CurrentCulture); //SafeFileApiNativeMethods.IpcfEncryptFile(fileName, templateInfo.TemplateId, SafeFileApiNativeMethods.EncryptFlags.IPCF_EF_FLAG_DEFAULT, false, false, true, this, null); } catch (InformationProtectionException ex) { isrmsProtect = false; MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK); } catch (Exception ex) { isrmsProtect = false; } } try { pdfDoc.LoadPDF(fileName); return(true); } catch (System.Security.SecurityException sex) { String password = Interaction.InputBox("Please enter the document password:"******"Document Password", ""); if (password.Equals(string.Empty)) { return(false); } if (pdfDoc != null) { pdfDoc.Dispose(); pdfDoc = null; } pdfDoc = new PDFWrapper(); pdfDoc.UserPassword = password; return(LoadFile(fileName)); } catch (Exception ex) { return(false); } }