11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

818 <strong>Improving</strong> <strong>Web</strong> <strong>Application</strong> <strong>Security</strong>: <strong>Threats</strong> <strong>and</strong> CountermeasuresWithout further modification, you can only call the managed DPAPI wrapper in thereferenced How To article from full trust code. To be able to call the DPAPI wrapperfrom partial trust code, such as a medium trust ASP.NET <strong>Web</strong> application, you musts<strong>and</strong>box the calls to the unmanaged DPAPI functions. To do this, make the followingmodifications:● Assert the unmanaged code permission in the DPAPI wrapper code. This meansthat any calling code does not require the unmanaged code permission.● Authorize the calling code inside the wrapper by dem<strong>and</strong>ing the customEncryptionPermission. The Dem<strong>and</strong> call occurs before the Assert call to, inaccordance with the Dem<strong>and</strong>/Assert usage pattern. For more information aboutusing Assert safely, see “Assert <strong>and</strong> RevertAssert,” in Chapter 8, “Code Access<strong>Security</strong> in Practice.” To modify the DPAPI managed wrapper1. Build the DPAPI managed wrapper by following the instructions in “How To:Create a DPAPI Library.”2. Add a reference to the CustomPermission assembly.3. Open dataprotection.cs from the managed wrapper library, <strong>and</strong> add the followingusing statements beneath the existing using statements at the top of the file.using System.<strong>Security</strong>;using System.<strong>Security</strong>.Permissions;using CustomPermissions;4. Locate the Encrypt method in dataprotection.cs, <strong>and</strong> add the following code at thetop of the outer try block in the Encrypt method.// Set the storeFlag depending on how the caller uses// the managed DPAPI wrapper.StorePermissionFlag storeFlag;if(Store.USE_MACHINE_STORE == store){storeFlag = StorePermissionFlag.Machine;}else{storeFlag = StorePermissionFlag.User;}// Dem<strong>and</strong> custom EncryptionPermission.(new EncryptionPermission(EncryptionPermissionFlag.Encrypt, storeFlag)).Dem<strong>and</strong>();// Assert the unmanaged code permission.(new <strong>Security</strong>Permission(<strong>Security</strong>PermissionFlag.UnmanagedCode)).Assert();// Now use P/Invoke to call the unmanaged DPAPI functions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!