<?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>Laurentiu Cristofor's blog @microsoft.com : SQL Server</title><link>http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx</link><description>Tags: SQL Server</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Finding information about which account xp_cmdshell is running as</title><link>http://blogs.msdn.com/lcris/archive/2009/10/27/finding-information-about-which-account-xp-cmdshell-is-running-as.aspx</link><pubDate>Tue, 27 Oct 2009 21:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9913721</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/9913721.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=9913721</wfw:commentRss><description>&lt;P&gt;If you ever needed to debug a permission related issue when using xp_cmdshell, you have probably realized that a crucial piece of information is about what particular account xp_cmdshell is executing under. If you are the administrator of the database, you already know the context used by xp_cmdshell, but otherwise you may not have that information. Here are some tips on how to find more.&lt;/P&gt;
&lt;P&gt;First, if you have a command line tool that displays the current context, like whoami or a utility for dumping the security contex, you can just execute that under xp_cmdshell. That's pretty easy. But what if there is no such tool available? One thing you can try in this case is to loop back into SQL (assuming the xp_cmdshell account has access to the database - it usually does) and just ask SQL for more information with queries like the following:&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;EXEC&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;xp_cmdshell&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'osql -E -Q"select suser_sname()"'&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;EXEC&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;xp_cmdshell&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'osql -E -Q"select * from sys.login_token"'&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;Don't forget to pass in the server/instance name using the -S option, if you are not dealing with a default instance. These should give you plenty of information about the xp_cmdshell context and should help you figure out any access permission issue.&lt;/P&gt;
&lt;P&gt;Given that I am on the topic of xp_cmdshell, here's how the command can be enabled using T-SQL:&lt;/P&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;
&lt;P&gt;sp_configure&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'show advanced options'&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;reconfigure&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#800000 size=2&gt;&lt;FONT color=#800000 size=2&gt;sp_configure&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#ff0000 size=2&gt;'xp_cmdshell'&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;reconfigure&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9913721" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+general/default.aspx">SQL Server - general</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Injection watch blog</title><link>http://blogs.msdn.com/lcris/archive/2009/08/14/sql-injection-watch-blog.aspx</link><pubDate>Sat, 15 Aug 2009 00:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9870586</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/9870586.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=9870586</wfw:commentRss><description>I was looking for information on a new SQL injection attack when I stumbled upon this very useful blog: &lt;A href="http://s3cwatch.wordpress.com/"&gt;http://s3cwatch.wordpress.com/&lt;/A&gt;. It's worth a look from time to time, to get an idea of what attacks are going on in the wild.&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9870586" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/computer+security/default.aspx">computer security</category></item><item><title>Basic SQL Server Security concepts: ownership, CONTROL, TAKE OWNERSHIP</title><link>http://blogs.msdn.com/lcris/archive/2009/08/11/basic-sql-server-security-concepts-ownership-control-take-ownership.aspx</link><pubDate>Wed, 12 Aug 2009 01:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9865191</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/9865191.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=9865191</wfw:commentRss><description>&lt;P&gt;I realized today that while I have discussed earlier object&amp;nbsp;&lt;A href="http://blogs.msdn.com/lcris/archive/2007/09/12/basic-sql-server-security-concepts-permissions-and-special-principals-sa-dbo-guest.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2007/09/12/basic-sql-server-security-concepts-permissions-and-special-principals-sa-dbo-guest.aspx"&gt;permissions&lt;/A&gt;, I have not gone into the details of object ownership.&amp;nbsp;I want to cover the following here: ownership of objects,&amp;nbsp;how it can be changed, and the relatively new permission CONTROL (introduced in SQL Server 2005).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Ownership:&lt;/STRONG&gt; This should&amp;nbsp;be&amp;nbsp;pretty clear - the owner of an object is usually its creator, but a different owner can be&amp;nbsp;specified at creation time using the AUTHORIZATION clause. The owner of an object has all possible permissions on that object and, most importantly, cannot be denied those permissions while he continues to be an owner.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CONTROL permission:&lt;/STRONG&gt; The CONTROL permission can be used to easily grant all permissions on an entity to some principal. It's the next best thing after ownership of the entity, but it's not quite as powerful as ownership. The main difference is that a grantee of CONTROL can still be denied some other permissions on the entity. For example, I can be granted CONTROL on a table, while at the same time I can be denied SELECT on that table, preventing me from selecting from it - this can never happen to the owner, because the owner cannot be granted or denied permissions.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Changing ownership:&lt;/STRONG&gt; &amp;nbsp;To change the owner of an entity, we can use the &lt;A href="http://msdn.microsoft.com/en-us/library/ms187359.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms187359.aspx"&gt;ALTER AUTHORIZATION&lt;/A&gt; statement. Changing ownership of an object creates some interesting scenarios in terms of what should be allowed and what should not be. Basically, owners should not be able to "dump" their objects to other users who may not want them and users should not be able to "steal" objects from those that own them. To accomplish this, two&amp;nbsp;checks are needed: one controls to whom&amp;nbsp;ownership can be given&amp;nbsp;to - we verify that the performer of the change has some right on the new owner (we check for IMPERSONATE for users and logins, ALTER or membership for roles, etc);&amp;nbsp;the other&amp;nbsp;check controls from whom&amp;nbsp;ownership can be taken away: we require the performer of the change to have TAKE OWNERSHIP permission on the object.&lt;/P&gt;
&lt;P&gt;Note&amp;nbsp;as a corollary that a grantee of CONTROL permission on an object has the ability to take ownership of the object&amp;nbsp;because CONTROL implies the TAKE OWNERSHIP permission as well - the way to prevent this would be to grant CONTROL and deny TAKE OWNERSHIP.&lt;/P&gt;
&lt;P&gt;TAKE OWNERSHIP is thus used to &lt;EM&gt;selectively&lt;/EM&gt; grant someone the ability to &lt;EM&gt;willingly&lt;/EM&gt; become the owner of an object or to facilitate the change of ownership of that object.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Side effect of ownership change:&lt;/STRONG&gt; A potentially surprising side effect of changing ownership of an object is that all permissions granted on that object will be lost. As always, it's a good idea to script all permissions granted on an entity before changing its ownership, so that the grants can be re-executed by the new owner, if appropriate.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9865191" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/computer+security/default.aspx">computer security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/basic+SQL+Server+security+concepts/default.aspx">basic SQL Server security concepts</category></item><item><title>SQL Server: Windows Groups, default schemas, and other properties</title><link>http://blogs.msdn.com/lcris/archive/2008/08/22/sql-server-windows-groups-default-schemas-and-other-properties.aspx</link><pubDate>Sat, 23 Aug 2008 02:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8889391</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/8889391.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=8889391</wfw:commentRss><description>&lt;P&gt;Exceptions are dangerous because people like to&amp;nbsp;simplify their thinking process using&amp;nbsp;rules, so exceptions always carry the risk of being overlooked. In security, exceptions are a bad thing because they make the model more complex and complex systems can break in more ways than simple systems, thus being harder to analyze and secure.&lt;/P&gt;
&lt;P&gt;Windows Groups are an exception in the SQL Server security&amp;nbsp;model. You can quickly infer my opinion about them from the above paragraph. At Windows OS level they behave similarly to how roles behave in SQL Server - they are simply containers of permissions and privileges. But bringing them in the SQL Server world creates a hybrid - an entity&amp;nbsp;that can not only be granted permissions, but can also own objects as well - a mix of roles and logins/users. In SQL Server, Windows Groups are a secondary identity with additional capabilities that are traditionally reserved only for primary identities. Suddenly, Windows Groups require handling not seen in any other security system.&lt;/P&gt;
&lt;P&gt;From a usability point of view, it is convenient to get access to a system via membership to a Windows Group, but from a security perspective, the actual user identity needs to be always available and must not be allowed to disappear behind the group anonymity. Imagine having to explain who deleted an important table - "Builtin\Users did it!" This leads to tradeoffs that are either not noticed or that surprise those that pay attention. For example, even though groups can own objects, an object created by someone that connected via a group membership will be owned by a login or user created under the cover - this is called &lt;EM&gt;implicit login/user creation&lt;/EM&gt; and is done so that the identity of the object creator is not lost.&lt;/P&gt;
&lt;P&gt;As far as groups work like they work in the Windows OS or like roles work in SQL Server, their behavior is easy to understand. This is because the semantics of permissions allows&amp;nbsp;them&amp;nbsp;to be added together and still make sense. So, if I belong to two different groups and&amp;nbsp;I&amp;nbsp;inherit from them two different sets of permissions,&amp;nbsp;there is no confusion about what permissions I have - I simply have all the GRANT's and DENY's that those groups provide me with.&lt;/P&gt;
&lt;P&gt;But the situation gets trickier when we want to endow groups with properties similar to what "real" principals have. The earliest situation consisted of the default language and default database settings that were added to logins. Because groups were supposed to act as logins, a solution was needed for them too. I was not around at the time these options were introduced, but I can see the result: the options were simply added to Windows Groups as well. However, the problem with having these options available&amp;nbsp;for groups is that they really reflect properties&amp;nbsp;and properties, unlike permissions, are not additive. If I am a member of two groups and each has a different default database, which one do I end up connecting to?&amp;nbsp;Having these&amp;nbsp;properties for&amp;nbsp;groups&amp;nbsp;was&amp;nbsp;a bad choice, but now this cannot be fixed because of backward compatibility.&lt;/P&gt;
&lt;P&gt;A new problem these days is that schemas were introduced in SQL Server 2005 and with them, a new property was added to users: DEFAULT_SCHEMA. However, the bad choice made earlier was not repeated&amp;nbsp;this time&amp;nbsp;and users mapped to Windows Groups&amp;nbsp;were not allowed&amp;nbsp;to have this property set.&amp;nbsp;This causes problems with the use of Windows Groups as it is indeed desirable to be able to specify default schemas for Windows Groups, but this must be done in an unambiguous way and there is no mechanism allowing this today. Addressing this problem is currently an open issue.&lt;/P&gt;
&lt;P&gt;Some take-offs from this discussion:&lt;/P&gt;
&lt;P&gt;Windows Groups can simplify management but due to their hybrid nature, they come with some restrictions. If you want to use them to avoid managing a large set of logins and users, then make sure you don't provide them with permissions to create objects - object creation will trigger implicit login/user creation, defeating the initial reason to use them. If you avoid the object creation, there is still the issue of not being able to control the default schema - until a solution is provided, access to data via Windows Group membership needs to be done&amp;nbsp;using schema qualified object names, Finally, Windows Groups are useful today because they provide a way to authenticate to SQL Server and can carry permissions, but they are not yet ready to be used as carriers of properties.&lt;/P&gt;
&lt;P&gt;Additional links:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=79418&amp;amp;SiteID=1" mce_href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=79418&amp;amp;SiteID=1"&gt;This topic in the SQL Server Security Forum&lt;/A&gt;&lt;BR&gt;&lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=328585" mce_href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=328585"&gt;The customer feedback item requesting the capability of setting a default schema for Windows Groups&lt;/A&gt; - you can use this to rate the importance of fixing the&amp;nbsp;default schema&amp;nbsp;issue.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8889391" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Server 2005: How to debug login failures (18456, anyone?)</title><link>http://blogs.msdn.com/lcris/archive/2008/02/21/sql-server-2005-how-to-debug-login-failures-18456-anyone.aspx</link><pubDate>Fri, 22 Feb 2008 03:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7843609</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/7843609.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=7843609</wfw:commentRss><description>&lt;P&gt;In my series of new posts on old topics, I decided to gather today several pieces of information that I think will help in debugging SQL Server login failures. Although most information should remain useful for future versions as well, some of it may become outdated, so I tagged this article as 2005 specific.&lt;/P&gt;
&lt;P&gt;Login failures can be broadly divided in two categories: failures to connect to SQL Server and failures to authenticate with SQL Server - I will refer to the first as protocol failures and to the second as security failures. Security failures can only&amp;nbsp;occur after successfully establishing connection to the database server. Most security failures result in the 18456 error, which will always be logged by SQL Server. This means that to determine if you are likely hitting a security error, you should&amp;nbsp;check the current ERRORLOG file&amp;nbsp;to see if an 18456 error was logged there - the absence of such an error usually indicates the problem is elsewhere - you either&amp;nbsp;failed connecting or there was some application error that was reported as a login error.&lt;/P&gt;
&lt;P&gt;The ERRORLOG file is a text file located in the LOG folder, usually situated in the same place as the DATA folder containing the system databases. You can always find the actual location by checking the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.&lt;EM&gt;OOO&lt;/EM&gt;\CPE\ErrorDumpDir, where&amp;nbsp;&lt;EM&gt;OOO&lt;/EM&gt; should be replaced by&amp;nbsp;the proper database instance number.&lt;/P&gt;
&lt;P&gt;For protocol errors, I recommend two resources:&lt;/P&gt;
&lt;P&gt;&lt;A class="" href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=87&amp;amp;SiteID=1" mce_href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=87&amp;amp;SiteID=1"&gt;MSDN Forum for the SQL Server Protocols Team&lt;/A&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/sql_protocols/default.aspx" mce_href="http://blogs.msdn.com/sql_protocols/default.aspx"&gt;SQL Server Protocols Team Blog&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;In what follows, I will focus more on discussing the 18456 error. This error&amp;nbsp;can be raised for a variety of reasons and the most important information in debugging it is to know the state value for the error. This is not obvious because the error state is always displayed to clients with a value of 1, to prevent information disclosure to unauthorized parties. The real error state value, however, is always logged in the ERRORLOG file, as in the examples below:&lt;/P&gt;
&lt;P&gt;2008-01-28 10:34:19.02 Logon&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Error: 18456, Severity: 14, &lt;STRONG&gt;State: 8&lt;/STRONG&gt;.&lt;BR&gt;2008-01-28 10:34:19.02 Logon&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Login failed for user 'sa'. [CLIENT: &amp;lt;local machine&amp;gt;]&lt;BR&gt;...&lt;BR&gt;2008-02-14 18:23:10.10 Logon&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Error: 18456, Severity: 14, &lt;STRONG&gt;State: 11&lt;/STRONG&gt;.&lt;BR&gt;2008-02-14 18:23:10.10 Logon&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Login failed for user 'Domain\User'. [CLIENT: &amp;lt;local machine&amp;gt;]&lt;/P&gt;
&lt;P&gt;Some of the most obvious states are described in this &lt;A class="" href="http://blogs.msdn.com/sql_protocols/archive/2006/02/21/536201.aspx" mce_href="http://blogs.msdn.com/sql_protocols/archive/2006/02/21/536201.aspx"&gt;classic 18456 post from the Protocols Team Blog&lt;/A&gt;, which pretty much already covered this topic. The comments to the post uncover a few more states. With this knowledge, the first failure above is easily explained as being&amp;nbsp;due to the use of an incorrect password,&amp;nbsp;while the second failure is due to the account not&amp;nbsp;having been granted&amp;nbsp;access to the server.&lt;/P&gt;
&lt;P&gt;Knowledge of the error state is crucial in finding out the possible reason of an 18456 failure, so it should be the first piece of information you gather before reporting the problem.&lt;/P&gt;
&lt;P&gt;For security issues, the main resources are:&lt;/P&gt;
&lt;P&gt;&lt;A class="" href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=92&amp;amp;SiteID=1" mce_href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=92&amp;amp;SiteID=1"&gt;MSDN Forum for SQL Server Security Team&lt;/A&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/sqlsecurity/" mce_href="http://blogs.msdn.com/sqlsecurity/"&gt;SQL Server Security Team Blog&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;So, when dealing with some sort of login failure, try to determine where it happened: in the application, in establishing a connection to the server, or in authenticating with the server. Checking the SQL Server error log can help you determine if you reached the server or not,&amp;nbsp;and it can also provide&amp;nbsp;you with additional information about the error,&amp;nbsp;which is not available elsewhere. If you are hitting an 18456 error and the state is described in the&amp;nbsp;Protocols&amp;nbsp;post, then you should have a good idea of what the problem is; if the state is not listed, then you should post the state when you are reporting the error - I suggest to try the SQL Server Security Forum first.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7843609" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>How to request features in Microsoft products</title><link>http://blogs.msdn.com/lcris/archive/2008/02/07/how-to-request-features-in-microsoft-products.aspx</link><pubDate>Fri, 08 Feb 2008 00:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7528458</guid><dc:creator>lcris</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/lcris/comments/7528458.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=7528458</wfw:commentRss><description>&lt;P&gt;I want to address the topic of requesting feature changes in Microsoft products, to point to some tools that can help, and to describe ways to use those tools more effectively. This post is based on my experience working on customer requests while being a member of the Microsoft SQL Server team, but you may find the information I provide here&amp;nbsp;useful for other Microsoft products than SQL Server.&lt;/P&gt;
&lt;P&gt;So, basically, the problem I am discussing here is that you are working on some project using Microsoft technology and you&amp;nbsp;find out that your work might be much easier if only some feature would be available. How can you&amp;nbsp;ask Microsoft to add that feature&amp;nbsp;to their product? One way to do this is to contact technical support, but there are other ways that I want to&amp;nbsp;address here.&lt;/P&gt;
&lt;P&gt;Before I start, I should mention that if the problem you are confronting appears to be a security vulnerability, then there is a dedicated site on which you can provide a description of the problem to the &lt;A class="" href="http://www.microsoft.com/technet/security/bulletin/alertus.aspx" mce_href="http://www.microsoft.com/technet/security/bulletin/alertus.aspx"&gt;Microsoft Security Response Center&lt;/A&gt;. Needless to say, security vulnerabilities are addressed with higher priority than the addition of new features and&amp;nbsp;the rest of this post deals with the latter.&lt;/P&gt;
&lt;P&gt;Also, while I am talking mainly about requesting features, the steps I present would be similar if you what you need is a bug fix.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 1 - Information&amp;nbsp;gathering.&lt;/STRONG&gt; Before a feature should be requested, it is important to do a bit of research to find answers to the following questions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- Does the feature allow you to achieve something that is worth achieving?&lt;BR&gt;&amp;nbsp;- Is the feature absolutely needed or are there existing&amp;nbsp;features that can be used to achieve the same goal with approximately the same effort?&lt;BR&gt;&amp;nbsp;- Has the feature been requested already?&lt;/P&gt;
&lt;P&gt;The easiest way to find the answers to these questions is to ask around, and the places you can ask these questions online can be divided in two categories:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- public forums, such as the microsoft.public.* forums or forums hosted by various sites focusing on the use of a specific Microsoft product&lt;BR&gt;&amp;nbsp;- Microsoft forums, such as the MSDN hosted&amp;nbsp;forums at &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/categories/"&gt;http://social.msdn.microsoft.com/Forums/en-US/categories/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;It is worth keeping in mind the differences between these categories of forums. Each offers specific advantages and posting on the right forum is a great step toward getting a quick and appropriate answer. An advantage of public forums is that they can be used to ask a wider array of questions - for example, you can ask questions related to the interactions between Microsoft products and other products. Microsoft forums have the advantage that they are monitored by members of the product units, but they are usually more focused on discussing specific features of a Microsoft product.&lt;/P&gt;
&lt;P&gt;As usual, before posting on any forum, you should take the time to search for existing threads that may already answer your questions. This is&amp;nbsp;good forum etiquette&amp;nbsp;that&amp;nbsp;is expected on&amp;nbsp;most forums&amp;nbsp;and that their frequent&amp;nbsp;contributors will often&amp;nbsp;suggest, because it reduces the redundancy of information in the forum and thus increases its quality.&lt;/P&gt;
&lt;P&gt;Once you have the answers to the above questions, several things can happen:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- you may find that the request is not necessary either because it does not really help solving a problem (in security it is often the case that a mechanism can be perceived as improving security when it really does not do so)&amp;nbsp;or because there are other alternatives of solving the problem&lt;BR&gt;&amp;nbsp;- you may find that&amp;nbsp;a request already exists and you may be provided with a way to track its progress or with a status update about the plans for developing and releasing it&lt;BR&gt;&amp;nbsp;- you may find that you may be the first customer to&amp;nbsp;request the feature&lt;/P&gt;
&lt;P&gt;If you are in the latter case, the next step is to ask formally for the feature. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 2 - Filing a request.&lt;/STRONG&gt; Depending on the product, there may be various ways to ask for features. One way could be to just ask a member of the product team that you contacted online or at a professional conference&amp;nbsp;to open an item for the request. But some products allow you to directly open requests online&amp;nbsp;using the &lt;A class="" href="https://connect.microsoft.com/default.aspx" mce_href="https://connect.microsoft.com/default.aspx"&gt;MSDN Product Feedback Center&lt;/A&gt;. The Feedback Center is a very powerful tool, because the item that you open is directly viewable by the product team. When opening a new feedback item, you should include all the information needed to describe the problem you are facing. You should also update the forum thread that you started at the previous step, to include a link to the feedback report that you opened. This&amp;nbsp;will help you by helping others notice your report&amp;nbsp;so they can&amp;nbsp;vote for it - the more votes your proposal gathers, the higher the chance that it will be assigned a higher priority that will allow it to go ahead of other requests.&lt;/P&gt;
&lt;P&gt;While in the SQL Server team, I have been on the receiving end of such requests and I have solved many of these (solving them meaning&amp;nbsp;that I made the required changes to&amp;nbsp;the SQL Server code); I also saw requests for existing features or for features that wouldn't accomplish anything useful, which is why I am emphasizing the importance of the information gathering first step. Some of the&amp;nbsp;features I added were important enough to ship in the next service pack (like some of the features mentioned &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2007/02/19/sql-server-2005-some-new-security-features-in-sp2.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2007/02/19/sql-server-2005-some-new-security-features-in-sp2.aspx"&gt;here&lt;/A&gt;), while others were postponed for the next release, but the important thing to note is that in all cases the requests got directly to the development team. In most cases, I also&amp;nbsp;provided feedback back to the customer about the progress made and the plans for releasing the changes. My point here is that this process works better than you may expect. As far as I know, no other company provides a way for customers to directly contact the development team.&lt;/P&gt;
&lt;P&gt;What next after filing a request?&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 3 - Monitoring your request.&lt;/STRONG&gt; After you made your request, the next thing to do is to monitor it, to provide additional information if needed, and to check for any developments. Feedback items have&amp;nbsp;a great&amp;nbsp;feature in&amp;nbsp;that they allow a two way communication with the product team. As long as the state of your request does not change, there isn't much that you can do other than making sure that other people that ask for the same thing know about your request and vote for it. Each product will have its own schedule for releasing new features. Service packs usually contain high priority fixes so if your issue is not&amp;nbsp;high priority, you can only expect it to show up in&amp;nbsp;a&amp;nbsp;future release. Bugs have a better chance of being resolved than new features have to be added, because they usually involve less code changes. Also, bugs usually get fixed all the time, while features tend to compete against other features for&amp;nbsp;a larger set&amp;nbsp;of&amp;nbsp;resources and they only get&amp;nbsp;worked on&amp;nbsp;during specific periods in the development cycle. Finally, a problem for which a workaround exists will get a lesser priority than a problem with no workaround.&lt;/P&gt;
&lt;P&gt;This is&amp;nbsp;basically it - I hope this information helps you figure out how to make use of the resources I listed.&amp;nbsp;I'll end this post with a summary of all these steps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- Search to see if your problem has already been discussed. If yes, then join the existing discussion.&lt;BR&gt;&amp;nbsp;- Open a new request if your search has not uncovered one. Provide all information someone would need to understand your problem.&lt;BR&gt;&amp;nbsp;- Track your request, to provide additional information, if needed, and to check for status updates.&lt;/P&gt;
&lt;P&gt;Finally, keep in mind that forums are great sources of information, but they don't help that much with tracking feature requests - the best way to do that is offered by sites like the Microsoft Product Feedback Center. So once you have a feedback item opened, make sure that other people&amp;nbsp;having the same problem&amp;nbsp;will vote for your feedback item.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7528458" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/Microsoft+products/default.aspx">Microsoft products</category></item><item><title>SQL Server 2005: How to debug errors in code that does encryption</title><link>http://blogs.msdn.com/lcris/archive/2008/01/31/sql-server-2005-how-to-debug-errors-in-code-that-does-encryption.aspx</link><pubDate>Thu, 31 Jan 2008 23:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7358699</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/7358699.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=7358699</wfw:commentRss><description>&lt;P&gt;Encryption builtin functions in SQL Server have no known issues and, if used properly, they will produce the expected results. However, if they are used incorrectly, it can be hard to figure out what exactly is the problem, so in this post I am going to collect some hints about what to look for in the malfunctioning code.&lt;/P&gt;
&lt;P&gt;There are two main types of encryption code related issues that I have seen popping up in forums; they usually are described as "encryption errors", which is not what they really are.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Issue 1:&amp;nbsp;Corruption of encrypted value&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This issue happens when the output of encryption is corrupted when stored or passed around by code. This happens because the encryption blob is truncated during a conversion or because the column in which it is stored is shorter or has the &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms187403.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms187403.aspx"&gt;ANSI_PADDING&lt;/A&gt; option set to OFF. For determining the size of a column that should hold encrypted data, see &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2008/01/11/sql-server-2005-how-to-determine-the-size-of-a-column-that-will-hold-encrypted-data.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2008/01/11/sql-server-2005-how-to-determine-the-size-of-a-column-that-will-hold-encrypted-data.aspx"&gt;this post&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Symptom:&lt;/STRONG&gt; Decryption of corrupted encryption blob will return NULL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;How to debug this?&lt;/STRONG&gt; Print the output of the encryption function, before it is processed in any way - this is the original encrypted blob value. Then also print the blob after each conversion or assignment; if it is inserted into a column, select back the inserted value. Any difference from the original blob value will indicate where the error happened. Most often, you don't even need to do a byte by byte comparison and you can instead just compare the lengths of the blob at different stages - you can get the length&amp;nbsp;using the &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms173486.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms173486.aspx"&gt;datalength()&lt;/A&gt; builtin.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Issue 2: Incorrect conversion of the decrypted value&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This issue happens when encrypting a Unicode string and when later, after decryption, we try to&amp;nbsp;convert the binary&amp;nbsp;decryption output to a non-Unicode value. Or vice-versa. There is nothing wrong with the encryption or decryption in this case, which can be determined by following the debugging steps for Issue 1. It is easy to get this issue because the difference between &lt;STRONG&gt;n&lt;/STRONG&gt;varchar (Unicode) and varchar (ASCII) is the single letter &lt;EM&gt;&lt;STRONG&gt;n&lt;/STRONG&gt;&lt;/EM&gt;, which you may fail to type in.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Symptom:&lt;/STRONG&gt; Decrypted data is displayed as garbage.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;How to debug this?&lt;/STRONG&gt; Compare the type of the data you compress and verify that the decompressed blob is appropriately converted back to that type.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7358699" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item><item><title>SQL Server 2005: A great post by Aaron Morton about using MARS to access opened keys</title><link>http://blogs.msdn.com/lcris/archive/2008/01/17/sql-server-2005-a-great-post-by-aaron-morton-about-using-mars-to-access-opened-keys.aspx</link><pubDate>Fri, 18 Jan 2008 09:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7146652</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/7146652.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=7146652</wfw:commentRss><description>&lt;P&gt;&lt;A class="" href="http://www.thelastpickle.com/" mce_href="http://www.thelastpickle.com/"&gt;Aaron Morton&lt;/A&gt; has a &lt;A class="" href="http://www.thelastpickle.com/2008/01/17/how-to-read-encrypted-data-and-the-problem-of-mars/" mce_href="http://www.thelastpickle.com/2008/01/17/how-to-read-encrypted-data-and-the-problem-of-mars/"&gt;very interesting post and demo&lt;/A&gt; that show how MARS can be used to access keys temporarily opened by a procedure. This is a must-read for anyone that is interested in implementing custom restrictions around the use of encryption keys.&amp;nbsp;Some time ago, I wrote a &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/01/13/sql-server-2005-example-for-how-to-allow-a-user-to-encrypt-but-not-decrypt.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/01/13/sql-server-2005-example-for-how-to-allow-a-user-to-encrypt-but-not-decrypt.aspx"&gt;post&lt;/A&gt; about restricting the use of an encryption key just for encryption. Aaron's demo goes further and demonstrates a pitfall if one would attempt to restrict the use of an encryption key for specific data decryption. The issue happens because MARS allows interleaving around SELECT statements, and a way to prevent it is to use auto-decryption routines, which open the key in the context of the transaction rather than the session context.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7146652" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item><item><title>SQL Server 2005: Why you should not encrypt data with certificates</title><link>http://blogs.msdn.com/lcris/archive/2008/01/11/sql-server-2005-why-you-should-not-encrypt-data-with-certificates.aspx</link><pubDate>Fri, 11 Jan 2008 23:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7080575</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/7080575.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=7080575</wfw:commentRss><description>&lt;P&gt;I often recommended to only encrypt data in SQL Server using symmetric keys and to reserve the use of asymmetric encryption for protection of symmetric keys and for signing.&amp;nbsp;In this post, I will go in more detail about why asymmetric encryption is not appropriate for protecting data.&lt;/P&gt;
&lt;P&gt;There are three reasons why asymmetric encryption is not suitable for encrypting data:&lt;/P&gt;
&lt;P&gt;(1) Asymmetric encryption is much slower than symmetric encryption.&lt;/P&gt;
&lt;P&gt;(2) For small data like SSNs and CCNs, asymmetric encryption will result in a blob that will be significantly larger than what you would get from symmetric encryption - about 2 times larger or more.&lt;/P&gt;
&lt;P&gt;(3) While for symmetric encryption, the largest data that can be compressed is around 8000 bytes, because of limitations in the SQL Server encryption interfaces,&amp;nbsp;for asymmetric key encryption, the limit is much smaller and is due to limitations of the&amp;nbsp;technology itself. The limits are: a 512 bit RSA key can encrypt up to 53 bytes, a 1024 bit key can encrypt&amp;nbsp;up to 117 bytes, and a 2048 bit key can encrypt&amp;nbsp;up to 245 bytes (reminder: in SQL Server, both certificates and asymmetric keys are wrappers over RSA keys).&lt;/P&gt;
&lt;P&gt;So, by using asymmetric encryption to encrypt data,&amp;nbsp;you&amp;nbsp;pay additional&amp;nbsp;time&amp;nbsp;and space&amp;nbsp;costs, and on top of that you are limited&amp;nbsp;about how large a piece of data you can encrypt.&lt;/P&gt;
&lt;P&gt;All these points are unrelated to the actual implementation of the algorithms; they are simply derived from the properties of RSA encryption, which is the only form of asymmetric encryption supported in SQL Server 2005.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Related to (3), also see &lt;A class="" href="http://blogs.msdn.com/yukondoit/archive/2005/11/24/496521.aspx" mce_href="http://blogs.msdn.com/yukondoit/archive/2005/11/24/496521.aspx"&gt;this post&lt;/A&gt; and &lt;A class="" href="http://groups.google.com/group/microsoft.public.sqlserver.security/browse_thread/thread/29fa470a60d03fc9/f8034886d9da6420?hl=en&amp;amp;lnk=gst&amp;amp;q=encryption+size#f8034886d9da6420" mce_href="http://groups.google.com/group/microsoft.public.sqlserver.security/browse_thread/thread/29fa470a60d03fc9/f8034886d9da6420?hl=en&amp;amp;lnk=gst&amp;amp;q=encryption+size#f8034886d9da6420"&gt;this post&lt;/A&gt;, which contain information that can help you determine the limits for RSA keys of different sizes than I enumerated above (or you can just determine those limits by trial and error).&lt;/P&gt;
&lt;P mce_keep="true"&gt;Also related is this post about the &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2007/10/04/sql-server-2005-a-note-about-the-use-of-certificates.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2007/10/04/sql-server-2005-a-note-about-the-use-of-certificates.aspx"&gt;use of certificates&lt;/A&gt; in SQL Server and this post about the difference between &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/03/13/550904.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/03/13/550904.aspx"&gt;certificates and asymmetric keys&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7080575" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item><item><title>SQL Server 2005: How to determine the size of a column that will hold encrypted data</title><link>http://blogs.msdn.com/lcris/archive/2008/01/11/sql-server-2005-how-to-determine-the-size-of-a-column-that-will-hold-encrypted-data.aspx</link><pubDate>Fri, 11 Jan 2008 22:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7078373</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/7078373.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=7078373</wfw:commentRss><description>&lt;P&gt;This issue has been addressed before on forums, but with the heavy traffic, it can be hard to find the proper post. So, I'll provide some explanations here as well.&lt;/P&gt;
&lt;P&gt;Note:&amp;nbsp;This article is written with symmetric encryption in mind, but the actual technique would work for&amp;nbsp;asymmetric encryption&amp;nbsp;too.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When encrypting a piece of data, the encrypted version will always be larger than the original data. This happens because encryption doesn't compress the data and, in addition to that, SQL Server prefixes the encrypted blob with some data, the most significant being the &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/07/06/658409.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/07/06/658409.aspx"&gt;GUID of the symmetric key used for encryption&lt;/A&gt;. It is possible to compute in advance what will be the length of the encryption for a given piece of data, but rather than working with a formula, a safer and simpler method is to just perform the encryption using EncryptByKey and pass the result to the datalength() builtin function. While the contents of the encrypted blob are non deterministic due to the use of a random IV, the length of the blob will be constant if the same&amp;nbsp;method is used for encryption; in fact, the length of the encryption doesn't even depend on the data that is encrypted - it only depends on the length of that data.&lt;/P&gt;
&lt;P&gt;So, when trying to decide the size of a column that is supposed to hold encrypted data, you only need to determine, as you would normally do in the absence of encryption, what is the maximum size of unencrypted data that you are willing to store; once you know this, you can just generate a piece of data of that size, encrypt it, and get the length of the result using datalength()&amp;nbsp;- that will be the maximum length that you should define for the encrypted column.&lt;/P&gt;
&lt;P&gt;Although I remember several posts on this topic, I could only find one with a quick search. Fortunately, it's a very good post from &lt;A class="" href="http://blogs.msdn.com/raulga/" mce_href="http://blogs.msdn.com/raulga/"&gt;Raul&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=908990&amp;amp;SiteID=1"&gt;http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=908990&amp;amp;SiteID=1&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;Update (later same day):&lt;/STRONG&gt; This is the &lt;A class="" href="http://blogs.msdn.com/yukondoit/archive/2005/11/24/496521.aspx" mce_href="http://blogs.msdn.com/yukondoit/archive/2005/11/24/496521.aspx"&gt;original post&lt;/A&gt; from Raul that explains in more detail the formula you could use to determine the length of an encrypted blob without actually encrypting. The blog on which it is posted has been discontinued, but there are some good posts made to it that are worth checking out. And if you are wondering about the blog name, Yukondoit was one of the slogans used on T-shirts around the launch of SQL Server 2005, whose code name was&amp;nbsp;"Yukon".&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7078373" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item><item><title>SQL Server 2005: Restoring the backup of a database that uses encryption</title><link>http://blogs.msdn.com/lcris/archive/2007/11/16/sql-server-2005-restoring-the-backup-of-a-database-that-uses-encryption.aspx</link><pubDate>Fri, 16 Nov 2007 22:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6314934</guid><dc:creator>lcris</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/lcris/comments/6314934.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=6314934</wfw:commentRss><description>&lt;P&gt;I have addressed this topic in previous threads and comments (&lt;A class="" href="http://blogs.msdn.com/lcris/archive/2005/07/08/sql-server-2005-a-look-at-the-master-keys.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2005/07/08/sql-server-2005-a-look-at-the-master-keys.aspx"&gt;here&lt;/A&gt;, &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2005/09/30/475822.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2005/09/30/475822.aspx"&gt;here&lt;/A&gt;,&amp;nbsp;and&amp;nbsp;&lt;A class="" href="http://blogs.msdn.com/lcris/archive/2007/11/14/sql-server-2005-how-to-recover-when-the-service-master-key-smk-is-not-accessible.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2007/11/14/sql-server-2005-how-to-recover-when-the-service-master-key-smk-is-not-accessible.aspx"&gt;here&lt;/A&gt;, for example), both on this blog&amp;nbsp;and on various forums, but it looks like when you need the answer, it can be hard to dig out. So I'm hoping that&amp;nbsp;by placing these steps in a dedicated post, they will become easier to find.&lt;/P&gt;
&lt;P&gt;When you restore a database that uses encryption features, there is only one thing you need to take care off - if the database master key (DbMK)&amp;nbsp;needs a service master key (SMK)&amp;nbsp;encryption,&amp;nbsp;you need to regenerate this encryption.&amp;nbsp;Note that this encryption is&amp;nbsp;made&amp;nbsp;by default when you create the DbMK, but it may be intentionally dropped, if you want&amp;nbsp;tighter control of&amp;nbsp;access to the encrypted data. Anyway, if you did have such SMK encryption for the DbMK, the steps to regenerate it are the following:&lt;/P&gt;
&lt;P&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms174433.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms174433.aspx"&gt;OPEN MASTER KEY&lt;/A&gt; DECRYPTION BY PASSWORD = 'password'&lt;BR&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms186937.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms186937.aspx"&gt;ALTER MASTER KEY&lt;/A&gt; ADD ENCRYPTION BY SERVICE MASTER KEY&lt;BR&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms188387.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms188387.aspx"&gt;CLOSE MASTER KEY&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;That's it - the database encryption features should now work as when the backup was taken. Also note that it doesn't matter if you restore the database on the server where the backup was taken or elsewhere. The only thing that matters for this procedure&amp;nbsp;is that you know one of the passwords protecting&amp;nbsp;the DbMK (yes, there can be more than one - see &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2005/09/23/473464.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2005/09/23/473464.aspx"&gt;this post&lt;/A&gt;).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6314934" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item><item><title>SQL Server 2005: How to recover when the service master key (SMK) is not accessible</title><link>http://blogs.msdn.com/lcris/archive/2007/11/14/sql-server-2005-how-to-recover-when-the-service-master-key-smk-is-not-accessible.aspx</link><pubDate>Thu, 15 Nov 2007 00:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6231099</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/6231099.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=6231099</wfw:commentRss><description>&lt;P&gt;I wrote earlier today a reply on this topic on the public forums, but now that I checked, the reply appears to have got lost, although I still entertain the hope it may only have got delayed and will appear there in 24 hours. Anyway, this is the reason why I prefer to write longer posts on this blog rather than on forums - they don't get lost as easily, either due to forum bugs or to threads going old and forgotten.&lt;/P&gt;
&lt;P&gt;So, the problem I want to discuss is about what can be done if the SMK, for some reason or another, becomes inaccessible. I had previously touched on this in a &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/04/10/572678.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/04/10/572678.aspx"&gt;previous article&lt;/A&gt;, but this time I want to&amp;nbsp;go in&amp;nbsp;more details.&lt;/P&gt;
&lt;P&gt;First, let's look into why such a thing could happen. For the SMK to become inaccessible, something out of the ordinary must happen - either a disk corruption, which would most likely impact more data than just the SMK, or a change in the system configuration that would invalidate the DPAPI encryption of the SMK, or some other problem that I don't know yet about - maybe some obscure software error we have yet to see. In the years since SQL Server 2005 has shipped, I have&amp;nbsp;seen only one case where the SMK became inaccessible and the reason for that was that the master database had been restored on a different system, such that the DPAPI encryptions of the SMK were invalid - even this issue had happened in an internal testing environment and not in a real production scenario. So, there is a&amp;nbsp;way you can realistically end up with an inaccessible SMK, but&amp;nbsp;it is not an ordinary scenario.&lt;/P&gt;
&lt;P&gt;The following discussion assumes that something happened to the SMK and just to the SMK&amp;nbsp;- I don't make any assumption on what happened, I just assume that this SMK&amp;nbsp;problem is not compounded by the existence of other problems affecting other areas than the SMK. Also, before launching yourself into a recovery operation, make sure that you can at least recover from that - you should backup your database files such that you don't get in a worse situation than when you started.&lt;/P&gt;
&lt;P&gt;Let's review the encryption hierarchy around the SMK, because this helps us understand what we need to fix. On one hand, the SMK has two DPAPI encryptions:&amp;nbsp;using the service account and the machine account credentials, so for the SMK to become inaccessible, the system must be incapable of decrypting &lt;STRONG&gt;both&lt;/STRONG&gt; these encryptions (also see&amp;nbsp;&lt;A class="" href="http://blogs.msdn.com/lcris/archive/2005/09/30/475822.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2005/09/30/475822.aspx"&gt;this article&lt;/A&gt;). On the other hand, the SMK is used for encrypting three different classes of entities: credentials, linked server passwords, and database master keys (also see &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/04/10/572678.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/04/10/572678.aspx"&gt;this article&lt;/A&gt;). This information allows us to determine what must have happened for the SMK to become inaccessible -&amp;nbsp;both DPAPI encryptions&amp;nbsp;must be undecryptable,&amp;nbsp;and what is the effect of the SMK becoming inaccessible -&amp;nbsp;credentials, linked server passwords, and database master keys&amp;nbsp;become undecryptable by the system. (If you're reading this article while working with future versions of SQL Server, keep in mind that the list of entities encrypted by the SMK may change.) We can also use this information to verify that the SMK is indeed inaccessible; to do this, we could&amp;nbsp;attempt to use the SMK&amp;nbsp;to encrypt a new entity, such as a new database master key (in a database created just for the purpose of this test) or a credential secret - if such operations fail, a final test can be made by attempting to regenerate the SMK - if this fails as well, it becomes pretty clear that the problem we're dealing with is related to the SMK.&lt;/P&gt;
&lt;P&gt;So, the best fix for the problem would be to fix the DPAPI encryptions of the SMK.&amp;nbsp;We can do this in two ways:&lt;/P&gt;
&lt;P&gt;(A) If&amp;nbsp;we have a backup of the SMK (this is why such backups are recommended), then&amp;nbsp;we can restore that backup.&amp;nbsp;We will need to use the FORCE option because the current SMK cannot be decrypted. This should fix our SMK&amp;nbsp;and we can check this using the tests I mentioned above.&lt;/P&gt;
&lt;P&gt;(B) If we don't have a backup of the SMK and if the issue happened because we moved the databases from a machine to another and if that machine is still available, then all is not lost. We can either make a backup of the SMK on the original machine (if it still has the database system on it) and then apply it according to the (A) solution, or we can copy the database files back to the original machine, to make such a backup. Either way, the purpose would be that we want to make&amp;nbsp;a backup of the SMK and then restore it on the machine that has the SMK problem.&lt;/P&gt;
&lt;P&gt;If neither of these apply - we don't have a backup of the SMK and there is no original system where we can make one, then we're in some trouble. Not a lot, but we'll have some non-trivial cleanup to do. So let's assess the situation in this case: we don't have a SMK backup and we can't access the SMK, so let's face it: we've lost the SMK; this means we've lost the credentials and linked server passwords encrypted by the SMK - we'll need to regenerate all of these with help from whoever created them. The good part is that we didn't loose the database master keys - unless we managed to also forget the passwords protecting them - that would be quite unfortunate. Because DbMKs are always encrypted by a password, besides a default SMK encryption, the latter can always be fixed as long as we know the DbMK password. So, let's enumerate the steps we need to follow to cleanup the situation:&lt;/P&gt;
&lt;P&gt;(a) We need to regenerate the SMK, because&amp;nbsp;the current one is now unrecoverable. So we need to use REGENERATE and the FORCE option to create a new and valid SMK.&lt;/P&gt;
&lt;P&gt;(b) For each&amp;nbsp;DbMK that had a SMK encryption, we need to open the DbMK using its password encryption and then we need to re-encrypt the DbMK using the SMK. This is the same kind of step that we would need to do when moving a database from one system to another. Here is the TSQL for it:&lt;/P&gt;
&lt;P&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms174433.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms174433.aspx"&gt;OPEN MASTER KEY&lt;/A&gt; DECRYPTION BY PASSWORD = 'password'&lt;BR&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms186937.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms186937.aspx"&gt;ALTER MASTER KEY&lt;/A&gt; ADD ENCRYPTION BY SERVICE MASTER KEY&lt;BR&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms188387.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms188387.aspx"&gt;CLOSE MASTER KEY&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;(c) We need now to recreate all credentials and linked server passwords.&lt;/P&gt;
&lt;P&gt;The steps above should bring our database system back to normal operational state as far as the SMK is concerned. It's going to be painful to do (c), but that's the price to pay because we didn't have a SMK backup. (b) and&amp;nbsp;(c)&amp;nbsp;need to be done after (a) because&amp;nbsp;they need a valid SMK, but their order can be swapped, i.e. we can do (c) before (b).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6231099" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item><item><title>SQL Server undocumented password hashing builtins: pwdcompare and pwdencrypt</title><link>http://blogs.msdn.com/lcris/archive/2007/10/31/sql-server-undocumented-password-hashing-builtins-pwdcompare-and-pwdencrypt.aspx</link><pubDate>Wed, 31 Oct 2007 21:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5802629</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/5802629.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=5802629</wfw:commentRss><description>&lt;P&gt;First, I must say that I don't know why these exist in an undocumented form. They have been around for a long time&amp;nbsp;and a search on their names gets me back pages of hits. Being undocumented means that their actual implementation may change slightly from one version of SQL Server to another, mainly because the password hash format&amp;nbsp;could change - and it has changed for the last three major versions of SQL Server. They could also disappear entirely&amp;nbsp;in a future&amp;nbsp;release. So, you shouldn't write applications relying on these functions, but they may come in handy&amp;nbsp;for ad-hoc queries, when you know they're available.&lt;/P&gt;
&lt;P&gt;So here's a short description of the two functions:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;pwdencrypt&lt;/STRONG&gt;: takes a varchar value as parameter and returns a varbin value that is the SQL Server password hash of the input value. I have seen people talking about the use of this function for hashing passwords when implementing custom application authentication. &lt;STRONG&gt;Do not use this!&lt;/STRONG&gt; Starting with SQL Server 2005, you can use instead the &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms174415.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms174415.aspx"&gt;HashBytes&lt;/A&gt; function, to implement any custom hashing scheme that you want. HashBytes provides you with direct access to several hashing algorithms, so there is no point in using pwdencrypt. In fact, I can't come up with any useful need for pwdencrypt and the only reason I included it here is to warn you against using it.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;pwdcompare&lt;/STRONG&gt;: takes two arguments - a varchar value that is a cleartext password and a varbin value that is a SQL Server password hash, and returns 1 if they match and 0 if they don't. There is a third optional parameter that can be set to 1 if the second parameter represents a pre-SQL Server 2000 password&amp;nbsp;hash value. This third parameter will most likely be dropped in a future SQL Server version.&lt;/P&gt;
&lt;P&gt;pwdcompare is useful if you are an administrator looking for accounts that have weak passwords. For example, to query for logins that have an empty password, the following queries can be used (first one will work on SQL Server 2000 and the second one uses the SQL Server 2005 new catalogs):&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;select&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;name&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;from&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;sys.syslogins&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;where&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;pwdcompare&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;''&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;password&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;select&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;name&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;from&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;sys.sql_logins&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;where&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;pwdcompare&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;''&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; password_hash&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;/P&gt;
&lt;P&gt;Obviously, these queries can be used to search for other weak passwords than empty ones. Does this constitute a threat against the strength of password hashes by allowing a TSQL brute-force attack? Not really, because such an approach would be&amp;nbsp;very slow&amp;nbsp;- it would be much&amp;nbsp;more efficient&amp;nbsp;to attempt such an attack using a compiled program, and even such approach would only have a good chance of success against a short or weak password. So, the pwdcompare function doesn't make an attacker's job easier. Other than helping administrators with checking for weak passwords, I can't think of another use for this function.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5802629" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/computer+security/default.aspx">computer security</category></item><item><title>Basic SQL Server Security concepts: SIDs, orphaned users, and loginless users</title><link>http://blogs.msdn.com/lcris/archive/2007/10/25/basic-sql-server-security-concepts-sids-orphaned-users-and-loginless-users.aspx</link><pubDate>Fri, 26 Oct 2007 03:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5683564</guid><dc:creator>lcris</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/lcris/comments/5683564.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=5683564</wfw:commentRss><description>&lt;P&gt;I am grouping here two topics (orphaned users and&amp;nbsp;loginless users)&amp;nbsp;that are actually very different, but I have often seen confusion between them, so I am covering them together in an attempt to dispel that confusion.&lt;/P&gt;
&lt;P&gt;In a &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2007/03/23/basic-sql-server-security-concepts-logins-users-and-principals.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2007/03/23/basic-sql-server-security-concepts-logins-users-and-principals.aspx"&gt;previous discussion&lt;/A&gt; of logins and users, I pointed out that the way a login gets mapped to users in databases is via&amp;nbsp;its SID value. This mapping is a one to many mapping - a login maps to several users (each one in a different database), but a user maps back to a single login.&amp;nbsp;When a Windows login is created,&amp;nbsp;its SID value is taken to be the SID of the corresponding Windows principal, but for&amp;nbsp;SQL Server specific logins the SID values are generated at creation time, unless they are explicitly provided via the SID clause of &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms189751.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms189751.aspx"&gt;CREATE LOGIN&lt;/A&gt;. The SID value represents the identity of the login and is invariant for the life of the login - this is why the SID clause is not available for &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms189828.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms189828.aspx"&gt;ALTER LOGIN&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The SID link between a login and a user&amp;nbsp;can be severed&amp;nbsp;when databases are moved from one server to another - this is because on the new server, there may be no login having the same SID as that user. When such a situation happens, we say that&amp;nbsp;the user was &lt;EM&gt;orphaned&lt;/EM&gt; - it becomes an &lt;EM&gt;orphan user&lt;/EM&gt;. Orphan users can also result from the deletion of a login - in previous versions, SQL Server attempted to warn about the existence of corresponding users, but even that check would fail to detect users in databases that were offline, so SQL Server 2005 no longer performs&amp;nbsp;it&amp;nbsp;(it was also an expensive check, as it required accessing each database). The bottom line is that orphaned users can appear in various ways and we have to either&amp;nbsp;fix them when they occur or we have to&amp;nbsp;prevent&amp;nbsp;them from occurring at all. The&amp;nbsp;classic method for restoring the SID link&amp;nbsp;was to use the &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms189828.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms189828.aspx"&gt;sp_change_users_login&lt;/A&gt; procedure, but starting with SQL Server 2005 SP2, a new LOGIN clause of &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms176060.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms176060.aspx"&gt;ALTER USER&lt;/A&gt; has also been made available, which helps fix Windows&amp;nbsp;users in addition to SQL users - a feature that sp_change_users_login was lacking. These methods will fix the SID of a user to match that of a login, effectively remapping the user to&amp;nbsp;match the login.&amp;nbsp;For the preventive approach, the main tool we have available is to create logins with an explicit SID value - this will help when moving a database between two or more&amp;nbsp;servers, by allowing&amp;nbsp;us to create in advance SQL Server logins that not only have&amp;nbsp;the same name, but&amp;nbsp;the same SID value as well. This will also help with recreating a login that was inadvertently dropped.&lt;/P&gt;
&lt;P&gt;Some people seem to think that having the possibility to change a login's SID to match that of a user&amp;nbsp;would be a great&amp;nbsp;feature. That is not true. Changing a login's SID would be wrong because it would attack the problem on the wrong side of the login-user relationship. When fixing the user SID, only that SID needs to be changed and this would be a database scoped operation, but when fixing a login SID, all corresponding user SIDs would have to be changed as well, otherwise we would generate more orphaned users instead of fixing them - and fixing all users mapped to a login isn't a task that can be easily performed,&amp;nbsp;because it would require the availability of all databases in which the login is mapped - something that cannot be guaranteed. There is a good reason why this issue is referred to as orphaned users and not as childless logins.&lt;/P&gt;
&lt;P&gt;So, orphaned users are an issue that normally&amp;nbsp;requires some fixing, when it occurs. Loginless users, on the other hand, are an intentional construct. They are created using the WITHOUT LOGIN clause of &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms173463.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms173463.aspx"&gt;CREATE USER&lt;/A&gt;&amp;nbsp;and they represent standalone users that &lt;STRONG&gt;cannot&lt;/STRONG&gt; map to a login (they have special&amp;nbsp;SID values preventing this), so you cannot connect to a database as a loginless user, although you can impersonate one via EXECUTE AS.&amp;nbsp;A loginless user is valuable in many situations where an application needs to have users that don't necessarily&amp;nbsp;correspond to a human user. For example, an application may be in need to create objects from time to time and to designate an owner for them - it could do this in the caller's context, but that would mean the caller would end up owning objects he may not be aware of - it is better for the application to designate a loginless user as the owner of those objects. One of my favorite uses of loginless users is to break ownership chains (see &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2007/09/13/basic-sql-server-security-concepts-ownership-chaining-good-and-evil-schemas.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2007/09/13/basic-sql-server-security-concepts-ownership-chaining-good-and-evil-schemas.aspx"&gt;previous discussion&lt;/A&gt;) by&amp;nbsp;assigning different loginless users as owners. Raul has a &lt;A class="" href="http://blogs.msdn.com/raulga/archive/2006/07/03/655587.aspx" mce_href="http://blogs.msdn.com/raulga/archive/2006/07/03/655587.aspx"&gt;lengthier&amp;nbsp;post&lt;/A&gt; on the topic of loginless users that offers&amp;nbsp;further examples of use, including some TSQL sample code.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5683564" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+security/default.aspx">SQL Server - security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/computer+security/default.aspx">computer security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/basic+SQL+Server+security+concepts/default.aspx">basic SQL Server security concepts</category></item><item><title>SQL Server 2005: A note about the use of certificates</title><link>http://blogs.msdn.com/lcris/archive/2007/10/04/sql-server-2005-a-note-about-the-use-of-certificates.aspx</link><pubDate>Fri, 05 Oct 2007 06:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5285355</guid><dc:creator>lcris</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/lcris/comments/5285355.aspx</comments><wfw:commentRss>http://blogs.msdn.com/lcris/commentrss.aspx?PostID=5285355</wfw:commentRss><description>&lt;P&gt;To avoid any confusion, this post is not about the use of certificates for securing the communication between a client machine and the server; instead, this&amp;nbsp;refers&amp;nbsp;to the use of certificates created via the CREATE CERTIFICATE DDL.&lt;/P&gt;
&lt;P&gt;I am prompted in writing this post by a recent question I just saw, which I know I answered&amp;nbsp;many times before, but I now realized I never wrote about it here. The question is simply around the differences between how certificates are used elsewhere and how certificates are used in SQL Server. I'll answer this question here and I'll add some additional information and references, which I hope you will find useful.&lt;/P&gt;
&lt;P&gt;The main use of certificates that we are familiar with from daily browsing activities is about the signing of code on the Internet. This signing is done as a way to prove the authenticity of a piece of software, because a signature can help&amp;nbsp;validate the manufacturer of a piece of software. I say &lt;EM&gt;"can help"&lt;/EM&gt;, because not everyone checks carefully the details of a digital signature to ensure it is indeed&amp;nbsp;trustworthy. For more details around this type of use scenario, you can look into &lt;A class="" href="http://en.wikipedia.org/wiki/Public_key_infrastructure" mce_href="http://en.wikipedia.org/wiki/Public_key_infrastructure"&gt;PKI - Public Key Infrastructure&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Certificates can be used in many ways, because asymmetric key encryption is a&amp;nbsp;very powerful&amp;nbsp;tool, so, when certificates were introduced in SQL Server, one of&amp;nbsp;the design goals was to build a generic store for encryption keys, without putting any unnecessary restrictions on their use. Because of this, expiry settings or certificate revocation&amp;nbsp;are not enforced.&amp;nbsp;SQL Server&amp;nbsp;just regards the certificate as a key container and&amp;nbsp;keeps track of all of its settings, but&amp;nbsp;it doesn't restrict its use - it is up to&amp;nbsp;you to decide&amp;nbsp;what&amp;nbsp;to do with&amp;nbsp;it. The main use scenarios within SQL Server do not require any restrictions, so the only thing that&amp;nbsp;is done is to give some warnings at the time the certificate is imported - if it was expired, for example. I should also note that features within SQL Server may do additional checks on a certificates's settings: for example, the Service Broker feature does check for expiration of its certificates. If your intent is to plug into PKI, that is also possible through CLR.&lt;/P&gt;
&lt;P&gt;So, what are the main use scenarios for having certificates in SQL Server? Here they are:&lt;/P&gt;
&lt;P&gt;1. You can use certificates to sign T-SQL code.&lt;BR&gt;2. You can use certificates to encrypt symmetric encryption keys or small pieces of data.&lt;/P&gt;
&lt;P&gt;Let's discuss these a little. Signing T-SQL code is the most powerful security feature shipped with SQL Server 2005 (my opinion, of course, as is any other subjective evaluation you may read on this blog). But, really, think about it: signing allows you to grant permissions to code instead of granting them to principals calling the code -&amp;nbsp;this opens a world of possibilities in terms of how you can manage permissions and how you can restrict access to objects. So that you don't get any wrong idea at my enthusiasm for this feature, I will add that I made no significant contributions to it, so I am not trying to push forward my work ;) For a simple example of the things you can do with signing, you can have a look at this &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/01/13/512829.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/01/13/512829.aspx"&gt;example&lt;/A&gt;. &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/01/13/512829.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/01/13/512829.aspx"&gt;Raul's blog&lt;/A&gt;&amp;nbsp;has additional examples and information on this feature.&lt;/P&gt;
&lt;P&gt;You can also use special builtin functions (SignByCert)&amp;nbsp;to sign your own data, but unfortunately, due to&amp;nbsp;a bug, their implementation uses the MD5 algorithm instead of the SHA1 algorithm used&amp;nbsp;in T-SQL signing, so they are not interoperable - you cannot use these functions to simulate the internal code signing, so that you could manually compute the signature of a function, for example; instead, you would need to actually create the function and sign it via ADD SIGNATURE, then read the signature blob from the system catalogs.&lt;/P&gt;
&lt;P&gt;The second use of certificates is quite natural for an encryption key&amp;nbsp;- we would definitely&amp;nbsp;like to encrypt something with it. The reason why certificates are more convenient to use for protecting symmetric keys than other methods is simply because they can be backed up individually. This also gives them an edge over their cryptographic sibling - the asymmetric keys. For a discussion of why there are two objects encapsulating RSA keys in SQL Server, see this &lt;A class="" href="http://blogs.msdn.com/lcris/archive/2006/03/13/550904.aspx" mce_href="http://blogs.msdn.com/lcris/archive/2006/03/13/550904.aspx"&gt;explanation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Some people think that certificate encryption is inherently safer than symmetric key encryption - it is not. There is no significant gain in strength from securing your data with an RSA key instead of using an AES one, as long as you are using equivalent key lengths - not equal, but&amp;nbsp;equivalent - more on this below. The RSA encryption is slower than any symmertric key encryption and its larger key length is simply due to&amp;nbsp;it being a fundamentally different technique and requiring longer key lengths than symmetric key algorithms, to offer the same strength of encryption. For a&amp;nbsp;discussion of key lengths and a comparison of key lengths across symmetric and asymmetric algorithms,&amp;nbsp;you can check&amp;nbsp;this &lt;A class="" href="http://en.wikipedia.org/wiki/Key_length" mce_href="http://en.wikipedia.org/wiki/Key_length"&gt;article&lt;/A&gt;&amp;nbsp;or a cryptography book. This means that certificates are not appropriate for data encryption and their use should be restricted to the encryption of symmetric keys (which are small enough that the encryption performance is not a concern) and to the application of digital signatures (when the key is only used to encrypt what is basically a hash of the signed data, hence, again,&amp;nbsp;performance is not a concern&amp;nbsp;given that&amp;nbsp;the length of a&amp;nbsp;SHA1 hash value&amp;nbsp;is 20 bytes/160 bits).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5285355" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server+-+cryptography/default.aspx">SQL Server - cryptography</category><category domain="http://blogs.msdn.com/lcris/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/lcris/archive/tags/computer+security/default.aspx">computer security</category><category domain="http://blogs.msdn.com/lcris/archive/tags/encryption/default.aspx">encryption</category></item></channel></rss>