<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Steve Lasker's Web Log - www.SteveLasker.com/Blog : Security</title><link>http://blogs.msdn.com/stevelasker/archive/tags/Security/default.aspx</link><description>Tags: Security</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Security and Encryption for SQL Server Compact</title><link>http://blogs.msdn.com/stevelasker/archive/2008/05/14/security-and-encryption-for-sql-server-compact.aspx</link><pubDate>Wed, 14 May 2008 07:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8502711</guid><dc:creator>Steve.Lasker</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/stevelasker/comments/8502711.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stevelasker/commentrss.aspx?PostID=8502711</wfw:commentRss><description>&lt;P mce_keep="true"&gt;The nice thing about SQL Server Compact is the database can be treated as a document.&amp;nbsp; It's a single file, it's only "open" when being used and can use the standard set of security mechanisms that any other document can.&amp;nbsp; You can set ACLs on the files, and you can encrypt the file with the &lt;A href="http://www.microsoft.com/technet/security/guidance/cryptographyetc/efs.mspx"&gt;Windows Encrypted File System&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Any security expert worth their weight in "keys" will tell you security is a multi-layer process.&amp;nbsp; There's no "one master lock" you can use and be considered secure.&amp;nbsp; It's a layered approach.&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;Here's a set of recommendations you can use for SQL Server Compact, but many of these will apply to any documents. &lt;/P&gt;
&lt;P&gt;&lt;B&gt;Use the Windows Encrypted File System&lt;BR&gt;&lt;/B&gt;Encrypt the directory of all your "document", including the directory of your SQL Server Compact Data File&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'; mso-no-proof: yes; mso-bidi-font-size: 14.0pt"&gt;System.IO.&lt;SPAN style="COLOR: #2b91af"&gt;DirectoryInfo&lt;/SPAN&gt; dirInfo = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; System.IO.&lt;SPAN style="COLOR: #2b91af"&gt;DirectoryInfo&lt;/SPAN&gt;(System.Windows.Forms.&lt;SPAN style="COLOR: #2b91af"&gt;Application&lt;/SPAN&gt;.LocalUserAppDataPath);&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'; mso-no-proof: yes; mso-bidi-font-size: 14.0pt"&gt;dirInfo.Attributes = System.IO.&lt;SPAN style="COLOR: #2b91af"&gt;FileAttributes&lt;/SPAN&gt;.Encrypted;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;This will flip the EFS flag.&lt;/P&gt;
&lt;P&gt;Alternatively, you could simply set the Encrypted flag for just the Compact data file&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Tahoma','sans-serif'; mso-no-proof: yes; mso-bidi-font-size: 14.0pt"&gt;using&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'; mso-no-proof: yes; mso-bidi-font-size: 14.0pt"&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SqlCeConnection&lt;/SPAN&gt; conn = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SqlCeConnection&lt;/SPAN&gt;(Properties.&lt;SPAN style="COLOR: #2b91af"&gt;Settings&lt;/SPAN&gt;.Default.LocalConnectionString)) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'; mso-no-proof: yes; mso-bidi-font-size: 14.0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;System.IO.&lt;SPAN style="COLOR: #2b91af"&gt;File&lt;/SPAN&gt;.Encrypt(conn.Database);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'; mso-no-proof: yes; mso-bidi-font-size: 14.0pt"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;This will encrypt the data file, and the directory of documents to the specific user.&amp;nbsp; This means evil people can't simply map a drive and copy the file.&amp;nbsp; It also means evil people can't just steal the hard drive and log on as an administrator and copy the file.&amp;nbsp; There's some caveats here, and a full study of EFS is recommended, but that's a different conversation.&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;What EFS doesn't do is secure the file while "in flight".&amp;nbsp; If you email the database, copy it to a USB key, copy it to a network share you must remove the EFS flag.&amp;nbsp; So now how do you protect the database while in flight?&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;B&gt;Use the SQL Server Compact File Encryption&lt;BR&gt;&lt;/B&gt;Within the SQL Server Compact Connection String you can tell the engine to encrypt the datafile with a password.&amp;nbsp; By simply specifying a password, the database is automatically encrypted.&amp;nbsp; We used to support an encrypt = true name/value pair, but it was sort of silly to have a password without encryption, and encryption without a password is about as useful as putting a lock on a door but leaving the key in the lock (on the outside of the door).&amp;nbsp; So, we no longer use this name/value pair and will likely throw an error&amp;nbsp; in a future release if the engine sees it.&amp;nbsp; Today Compact just ignores it.&lt;/P&gt;
&lt;P mce_keep="true"&gt;To turn on Encryption, simply set the Password like the following:&lt;/P&gt;
&lt;P&gt;Data Source=|DataDirectory|\Localdatabase.sdf;&lt;B&gt;Password=Foo&lt;/B&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;B&gt;What level of encryption do you want?&amp;nbsp; &lt;BR&gt;&lt;/B&gt;That turns out to almost be a silly question as well.&amp;nbsp; Does anyone want a somewhat secure encryption algorithm?&amp;nbsp; Of course not.&amp;nbsp; But, the evil people keep cracking the encryption algorithms, and the older operating systems don't actually support the newer algorithms.&amp;nbsp; Laxmi posted the different versions supported &lt;A href="http://blogs.msdn.com/laxmi/archive/2008/04/15/sql-server-compact-database-file-security.aspx"&gt;here&lt;/A&gt;.&amp;nbsp; For 4.0 we'll do the latest and greatest at the time we release.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To set the extra bit, simply specify the Encryption Mode like the following:&lt;/P&gt;
&lt;P&gt;Data Source=|DataDirectory|\Localdatabase.sdf;Password=Foo;&lt;B&gt;Encryption Mode=Engine Default&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;This may be needed when creating a database on the server, and streaming it to a PPC 2003 device, or Win2k which uses the same algos as PPC 2003.&lt;/P&gt;
&lt;P mce_keep="true"&gt;In an upcoming updated post on deploying database with scripts, and versioning databases, I'll show how you can incorporate this into your "Health Check" api to assure newly created databases are encrypted.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Steve&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8502711" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stevelasker/archive/tags/Q_2600_amp_3B00_A/default.aspx">Q&amp;amp;A</category><category domain="http://blogs.msdn.com/stevelasker/archive/tags/SQLce/default.aspx">SQLce</category><category domain="http://blogs.msdn.com/stevelasker/archive/tags/SQL+Server+Compact+Edition/default.aspx">SQL Server Compact Edition</category><category domain="http://blogs.msdn.com/stevelasker/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/stevelasker/archive/tags/Encryption/default.aspx">Encryption</category></item></channel></rss>