2013-02-28 14 views
6

Devo abilitare gli utenti delle mie applicazioni a firmare le loro approvazioni con il loro token di sicurezza USB personale.Come ottenere informazioni da un token di sicurezza con C#

Sono riuscito a firmare i dati ma non sono stato in grado di ottenere le informazioni su chi è stato utilizzato per farlo.

Ecco il codice che ho finora:

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'è un campo nella scheda di gettone chiamato "token Nome". Come posso accedere a quel campo per convalidare il quale token è stato utilizzato per firmare l'approvazione?

enter image description here

informazioni Aditional e aggiornamento:

  • "nome Token" corrisponde sempre il nome del proprietario (l'utente che possiede l'usb token)
  • Sembra che non si può fare , forse c'è un servizio web o qualcosa che ho bisogno di chiamare per ottenere direttamente le informazioni dall'autorità cert.
+0

Forse questo è più di una domanda per la sicurezza .stachexchange.com? – MiMo

+0

Sembra che sia necessario estrarre un [Modulo] (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.modulus.aspx) come un'impronta digitale del certificato e quindi confrontarlo con database di tutte le impronte digitali dei certificati disponibili. Probabilmente qualcosa come ['byte [] modulus = rsa.ExportParameters (false) .Modulus;'] (http://attercop.googlecode.com/svn-history/r8/trunk/AttercopClient/SettingsWindow.cs) – oleksii

+0

@oleksii I non ho un database modulo. Devo ottenere il "Nome token" e confrontarlo con il nome utente AD corrente. Grazie per il tuo commento. – daniloquio

risposta

4

Quando ho posto la domanda, la mia comprensione dei certificati digitali era molto semplice, quindi la domanda non è stata posta correttamente. Ora capisco che ho bisogno di accedere a un certificato da un dispositivo smart card, interrogare i suoi attributi e testare se l'utente può inserire il codice PIN per esso.

Ecco il codice che ho usato per farlo:

//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); 

Fonti:

http://stormimon.developpez.com/dotnet/signature-electronique/ (en français) https://www.simple-talk.com/content/print.aspx?article=1713 (in inglese)

+0

Esiste un approccio alternativo per ottenere il nome del token. Dovrai utilizzare l'interfaccia PKCS # 11 o CryptoAPI per rilevare quale tokane è stato inserito. Questo è diverso dal tuo approccio al rilevamento del nome del token basato sul nome del soggetto dei certificati. – Raj

Problemi correlati