/// <summary> /// Intitializes class instance /// </summary> /// <param name="slot">PKCS#11 slot</param> /// <param name="manufacturerId">Token manufacturer</param> /// <param name="model">Token model</param> /// <param name="serialNumber">Token serial number</param> /// <param name="label">Token label</param> internal Token(Slot slot, string manufacturerId, string model, string serialNumber, string label) { if (slot == null) throw new ArgumentNullException("slot"); Slot = slot; _manufacturerId = manufacturerId; _model = model; _serialNumber = serialNumber; _label = label; }
/// <summary> /// Initializes a new instance of the Pkcs11Signature class /// </summary> /// <param name="libraryPath">Path to the unmanaged PCKS#11 library</param> /// <param name="tokenSerial">Serial number of the token (smartcard) that contains signing key. May be null if tokenLabel is specified.</param> /// <param name="tokenLabel">Label of of the token (smartcard) that contains signing key. May be null if tokenSerial is specified.</param> /// <param name="pin">PIN for the token (smartcard)</param> /// <param name="ckaLabel">Label (value of CKA_LABEL attribute) of the private key used for signing. May be null if ckaId is specified.</param> /// <param name="ckaId">Identifier (value of CKA_ID attribute) of the private key used for signing. May be null if ckaLabel is specified.</param> /// <param name="hashAlgorihtm">Hash algorihtm used for the signature creation</param> private void InitializePkcs7RsaSignature(string libraryPath, string tokenSerial, string tokenLabel, byte[] pin, string ckaLabel, byte[] ckaId, HashAlgorithm hashAlgorihtm) { try { if (string.IsNullOrEmpty(libraryPath)) throw new ArgumentNullException("libraryPath"); _pkcs11 = new Pkcs11(libraryPath, true); _slot = FindSlot(tokenSerial, tokenLabel); if (_slot == null) throw new TokenNotFoundException(string.Format("Token with serial \"{0}\" and label \"{1}\" was not found", tokenSerial, tokenLabel)); _session = _slot.OpenSession(true); _session.Login(CKU.CKU_USER, pin); _privateKeyHandle = FindPrivateKey(ckaLabel, ckaId); _ckaLabel = ckaLabel; _ckaId = ckaId; if (!Enum.IsDefined(typeof(HashAlgorithm), hashAlgorihtm)) throw new ArgumentException("Invalid hash algorithm specified"); _hashAlgorihtm = hashAlgorihtm; } catch { if (_session != null) { _session.Dispose(); _session = null; } if (_pkcs11 != null) { _pkcs11.Dispose(); _pkcs11 = null; } throw; } }
/// <summary> /// Disposes object /// </summary> /// <param name="disposing">Flag indicating whether managed resources should be disposed</param> protected virtual void Dispose(bool disposing) { if (!this._disposed) { // Dispose managed objects if (disposing) { _allCertificates = null; _signingCertificate = null; _hashAlgorihtm = HashAlgorithm.SHA512; _ckaId = null; _ckaLabel = null; _privateKeyHandle = null; if (_session != null) { try { _session.Logout(); } catch { // Any exceptions can be safely ignored here } _session.Dispose(); _session = null; } _slot = null; if (_pkcs11 != null) { _pkcs11.Dispose(); _pkcs11 = null; } } // Dispose unmanaged objects _disposed = true; } }