public void ExportDataObject(Pkcs11DataObjectInfo objectInfo, out string fileName, out byte[] fileContent) { if (this._disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (objectInfo == null) { throw new ArgumentNullException("objectInfo"); } using (Session session = _slot.OpenSession(SessionType.ReadWrite)) { List <ulong> attributes = new List <ulong>(); attributes.Add((ulong)CKA.CKA_LABEL); attributes.Add((ulong)CKA.CKA_VALUE); List <ObjectAttribute> objectAttributes = session.GetAttributeValue(objectInfo.ObjectHandle, attributes); fileName = objectAttributes[0].GetValueAsString(); fileName = (!string.IsNullOrEmpty(fileName)) ? Utils.NormalizeFileName(fileName) : "data_object"; fileContent = objectAttributes[1].GetValueAsByteArray(); } }
private List <Pkcs11DataObjectInfo> ReadDataObjects() { List <Pkcs11DataObjectInfo> infos = new List <Pkcs11DataObjectInfo>(); using (Session session = _slot.OpenSession(SessionType.ReadWrite)) { List <ObjectAttribute> searchTemplate = new List <ObjectAttribute>(); searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA)); List <ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate); foreach (ObjectHandle foundObject in foundObjects) { // Read attributes required for sane object presentation List <ulong> attributes = new List <ulong>(); attributes.Add((ulong)CKA.CKA_PRIVATE); attributes.Add((ulong)CKA.CKA_LABEL); attributes.Add((ulong)CKA.CKA_APPLICATION); List <ObjectAttribute> requiredAttributes = session.GetAttributeValue(foundObject, attributes); // Read attributes configured for specific object class attributes = new List <ulong>(); foreach (ClassAttribute classAttribute in Pkcs11Admin.Instance.Config.DataObjectAttributes.CommonAttributes) { attributes.Add(classAttribute.Value); } List <ObjectAttribute> configuredAttributes = session.GetAttributeValue(foundObject, attributes); // Read object storage size ulong?storageSize = ReadObjectSize(session, foundObject); // Construct info object Pkcs11DataObjectInfo info = new Pkcs11DataObjectInfo(foundObject, configuredAttributes, storageSize) { CkaPrivate = requiredAttributes[0].GetValueAsBool(), CkaLabel = requiredAttributes[1].GetValueAsString(), CkaApplication = requiredAttributes[2].GetValueAsString(), StorageSize = storageSize }; infos.Add(info); } } return(infos); }