Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get certificate public key from SQL Server?

I have imported certificate into a SQL Server database.

 create certificate MyCertificate from file = 'c:\certificate.cer';

Now I want to retrieve public key of that certificate. How to do it ?

I can do

select * from sys.certificates

but there is no public key column.

I know I can save it to disk by

backup certificate MyCertificate to file = 'c:\MyCertificate.cer';

but that's not what I want. I just need database to tell me public key or somehow get me whole certificate but I dont want to use files.

EDIT:

I wanted to verify digital signature of a row in database table using SQL Server and certificate. But since function VerifySignedByCert doesn't check certificate expiration date (according to this note: Built-in functions for encryption and signing do not check the expiration dates of certificates. Users of these functions must decide when to check certificate expiration. in http://msdn.microsoft.com/en-US/library/ms187798%28v=SQL.90%29.aspx) I have to do it manually in C# code.

That's why I wanted to get certificate public key and expiration date (which can be found in sys.certificates). But it seems that storing certificate in a varbinary column in a table is the best option. Or are there other better ways how to achieve this ?

Thank you for your help

like image 831
Harlsten Avatar asked Nov 24 '22 08:11

Harlsten


1 Answers

The information that you are looking for resides in the master database in the syscerts table. However, there is no easy way to get at that data. If you are trying to get to the keys, perhaps you are not trying to use the certificate for the purpose in which certificates are intended to be used by SQL Server?

If you are trying to store certificates in SQL Server but not use them for the security of SQL Server then you might want to try storing them in a varbinary column in a table. Would something like that work for you?

If you give a little more information about what you are trying to accomplish, I might be able to help you a little more.

Here is a good overview of certificates in SQL Server: http://www.mssqltips.com/sqlservertip/1319/sql-server-2005-encryption-certificates-overview/

like image 111
John Avatar answered Dec 05 '22 13:12

John