PC Review


Reply
Thread Tools Rate Thread

Copy SecureString with Length

 
 
Brian Stoop
Guest
Posts: n/a
 
      22nd Apr 2009
Hi, I want to copy the first 6 characters of a SecureString to a new
SecureString, leaving the old one unchanged.

The SecureString constructor takes a char pointer and length, but how do I
get a the char pointer from the old SecureString ?


Thanks, B


 
Reply With Quote
 
 
 
 
Jeroen Mostert
Guest
Posts: n/a
 
      22nd Apr 2009
Brian Stoop wrote:
> Hi, I want to copy the first 6 characters of a SecureString to a new
> SecureString, leaving the old one unchanged.
>

If at all possible, avoid doing this. Change the code that creates the
original SecureString instead to also produce this string. It's very hard to
operate on SecureStrings after the fact (at least while maintaining security).

> The SecureString constructor takes a char pointer and length, but how do I
> get a the char pointer from the old SecureString ?
>

This is quickest, but it requires unsafe code:

SecureString s2;
IntPtr p = IntPtr.Zero;
try {
RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally {
p = Marshal.SecureStringToGlobalAllocUnicode(s);
}
unsafe (char* c = (char*) p) {
s2 = new SecureString(c, 6);
}
} finally {
if (p != IntPtr.Zero) Marshal.ZeroFreeGlobalAllocUnicode(p);
}

Note that allocating is done in a constrained execution region (CER) to
prevent the possibility of an exception occurring between the allocation and
the assignment to p (yes, this is possible, and it would leave a leaked
unsecure copy of the string in memory). This may fire warnings if you're
using MDAs because the CER is technically invalid
(.SecureStringToGlobalAllocUnicode() is not marked with the appropriate
attribute). Still, as far as I can tell it's necessary.

--
J.
 
Reply With Quote
 
Brian Stoop
Guest
Posts: n/a
 
      22nd Apr 2009
thanks.

"Jeroen Mostert" <(E-Mail Removed)> wrote in message
news:49ef61cc$0$199$(E-Mail Removed)...
> Brian Stoop wrote:
>> Hi, I want to copy the first 6 characters of a SecureString to a new
>> SecureString, leaving the old one unchanged.
>>

> If at all possible, avoid doing this. Change the code that creates the
> original SecureString instead to also produce this string. It's very hard
> to operate on SecureStrings after the fact (at least while maintaining
> security).
>
>> The SecureString constructor takes a char pointer and length, but how do
>> I get a the char pointer from the old SecureString ?
>>

> This is quickest, but it requires unsafe code:
>
> SecureString s2;
> IntPtr p = IntPtr.Zero;
> try {
> RuntimeHelpers.PrepareConstrainedRegions();
> try { }
> finally {
> p = Marshal.SecureStringToGlobalAllocUnicode(s);
> }
> unsafe (char* c = (char*) p) {
> s2 = new SecureString(c, 6);
> }
> } finally {
> if (p != IntPtr.Zero) Marshal.ZeroFreeGlobalAllocUnicode(p);
> }
>
> Note that allocating is done in a constrained execution region (CER) to
> prevent the possibility of an exception occurring between the allocation
> and the assignment to p (yes, this is possible, and it would leave a
> leaked unsecure copy of the string in memory). This may fire warnings if
> you're using MDAs because the CER is technically invalid
> (.SecureStringToGlobalAllocUnicode() is not marked with the appropriate
> attribute). Still, as far as I can tell it's necessary.
>
> --
> J.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NetUserSetInfo and SecureString Brian Stoop Microsoft C# .NET 5 21st Apr 2009 01:45 PM
Change length of SecureString Brian Stoop Microsoft C# .NET 2 21st Apr 2009 01:12 AM
SecureString() in framework 1.1 Pietro Pesce Microsoft VB .NET 13 1st Aug 2007 09:54 AM
How to set value of SecureString? Dean Slindee Microsoft VB .NET 1 7th Mar 2007 12:21 AM
Invalid SecureString Error dayspook@hotmail.com Microsoft Dot NET Framework 0 8th Nov 2005 05:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:49 PM.