2013-02-28 35 views
6

Tôi cần phải cho phép người dùng ứng dụng của mình ký vào các chấp thuận của họ bằng mã thông báo bảo mật USB cá nhân của họ.Cách nhận thông tin từ mã thông báo bảo mật với C#

Tôi đã quản lý để đăng nhập dữ liệu nhưng tôi chưa thể nhận được thông tin về mã thông báo của ai đã được sử dụng để làm như vậy.

Dưới đây là đoạn code tôi có cho đến nay:

CspParameters csp = new CspParameters(1, "SafeNet RSA CSP"); 
csp.Flags = CspProviderFlags.UseDefaultKeyContainer;    
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp); 
// Create some data to sign. 
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }; 
Console.WriteLine("Data   : " + BitConverter.ToString(data)); 
// Sign the data using the Smart Card CryptoGraphic Provider.    
byte[] sig = rsa.SignData(data, "SHA1");    
Console.WriteLine("Signature : " + BitConverter.ToString(sig)); 

Có một lĩnh vực thông tin thẻ được gọi là "Mã Name". Làm cách nào để truy cập vào trường đó để xác thực mã thông báo đã được sử dụng để ký duyệt?

enter image description here

thông tin aditional và cập nhật:

  • "tên Mã" luôn luôn phù hợp với tên của chủ sở hữu (người dùng những người sở hữu usb token)
  • Nó có vẻ như nó không thể được thực hiện , có thể có dịch vụ web hoặc điều tôi cần gọi để nhận thông tin trực tiếp từ cơ quan cấp chứng chỉ.
+0

Có lẽ đây là chi tiết của một câu hỏi về an ninh .stachexchange.com? – MiMo

+0

Có vẻ như bạn sẽ cần trích xuất [Modulus] (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.modulus.aspx) dưới dạng vân tay chứng chỉ và sau đó so sánh với cơ sở dữ liệu của tất cả các dấu vân tay của chứng chỉ có sẵn. Có lẽ giống như ['byte [] modulus = rsa.ExportParameters (sai) .Modulus;'] (http://attercop.googlecode.com/svn-history/r8/trunk/AttercopClient/SettingsWindow.cs) – oleksii

+0

@oleksii I không có cơ sở dữ liệu modulus. Tôi cần lấy tên "Token" và so sánh nó với tên người dùng AD hiện tại. Cám ơn bạn đã góp ý. – daniloquio

Trả lời

4

Khi tôi đặt câu hỏi, việc tôi thiếu các chứng chỉ kỹ thuật số rất cơ bản, do đó câu hỏi không được hỏi đúng. Bây giờ tôi hiểu rằng tôi cần truy cập chứng chỉ từ thiết bị thẻ thông minh, truy vấn thuộc tính của nó và kiểm tra xem người dùng có thể nhập mã PIN cứng cho nó hay không.

Đây là mã tôi đã sử dụng để làm như vậy:

//Prompt the user with the list of certificates on the local store. 
//The user have to select the certificate he wants to use for signing. 
//Note: All certificates form the USB device are automatically copied to the local store as soon the device is plugged in. 
X509Store store = new X509Store(StoreLocation.CurrentUser); 
store.Open(OpenFlags.ReadOnly); 
X509CertificateCollection certificates = X509Certificate2UI.SelectFromCollection(store.Certificates, 
                       "Certificados conocidos", 
                       "Por favor seleccione el certificado con el cual desea firmar", 
                       X509SelectionFlag.SingleSelection 
                       ); 
store.Close(); 
X509Certificate2 certificate = null; 
if (certificates.Count != 0) 
{ 
    //The selected certificate 
    certificate = (X509Certificate2)certificates[0]; 
} 
else 
{ 
    //The user didn't select a certificate 
    return "El usuario canceló la selección de un certificado"; 
} 
//Check certificate's atributes to identify the type of certificate (censored) 
if (certificate.Issuer != "CN=............................., OU=................., O=..., C=US") 
{ 
    //The selected certificate is not of the needed type 
    return "El certificado seleccionado no corresponde a un token ..."; 
} 
//Check if the certificate is issued to the current user 
if (!certificate.Subject.ToUpper().Contains(("E=" + pUserADLogin + "@censoreddomain.com").ToUpper())) 
{ 
    return "El certificado seleccionado no corresponde al usuario actual"; 
} 
//Check if the token is currently plugged in 
XmlDocument xmlDoc = new XmlDocument(); 
XmlElement element = xmlDoc.CreateElement("Content", SignedXml.XmlDsigNamespaceUrl.ToString()); 
element.InnerText = "comodin"; 
xmlDoc.AppendChild(element); 
SignedXml signedXml = new SignedXml(); 
try 
{ 
    signedXml.SigningKey = certificate.PrivateKey; 
} 
catch 
{ 
    //USB Token is not plugged in 
    return "El token no se encuentra conectado al equipo"; 
} 
DataObject dataObject = new DataObject(); 
dataObject.Data = xmlDoc.ChildNodes; 
dataObject.Id = "CONTENT"; 
signedXml.AddObject(dataObject); 
Reference reference = new Reference(); 
reference.Uri = "#CONTENT"; 
signedXml.AddReference(reference); 
//Attempt to sign the data. The user will be prompted to enter his PIN 
try 
{ 
    signedXml.ComputeSignature(); 
} 
catch 
{ 
    //User didn't enter the correct PIN 
    return "Hubo un error confirmando la identidad del usuario"; 
} 
//The user has signed with the correct token 
return String.Format("El usuario {0} ha firmado exitosamente usando el token con serial {1}", pUserADLogin, certificate.SerialNumber); 

Nguồn:

http://stormimon.developpez.com/dotnet/signature-electronique/ (en Francais) https://www.simple-talk.com/content/print.aspx?article=1713 (bằng tiếng Anh)

+0

Có một cách tiếp cận khác để lấy tên mã thông báo. Bạn sẽ phải sử dụng giao diện PKCS # 11 hoặc CryptoAPI để phát hiện thẻ nào đã được chèn vào. Điều này khác với cách tiếp cận của bạn để xóa tên mã thông báo dựa trên tên chủ đề của chứng chỉ. – Raj

Các vấn đề liên quan