Debian Codegraph @

src/lib/object_store/ObjectFile.h

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 ObjectFile.h
29
30 This class represents object files
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_OBJECTFILE_H
34 #define _SOFTHSM_V2_OBJECTFILE_H
35
36 #include "config.h"
37 #include "File.h"
38 #include "Generation.h"
39 #include "ByteString.h"
40 #include "OSAttribute.h"
41 #include "MutexFactory.h"
42 #include <string>
43 #include <map>
44 #include <time.h>
45 #include "cryptoki.h"
46 #include "OSObject.h"
47
48 // OSToken forward declaration
49 class OSToken;
50
51 class ObjectFile : public OSObject
52 {
53 public:
54 // Constructor
55 ObjectFile(OSToken* parent, const std::string inPath, const std::string inLockpath, bool isNew = false);
56
57 // Destructor
58 virtual ~ObjectFile();
59
60 // Check if the specified attribute exists
61 virtual bool attributeExists(CK_ATTRIBUTE_TYPE type);
62
63 // Retrieve the specified attribute
64 virtual OSAttribute getAttribute(CK_ATTRIBUTE_TYPE type);
65 virtual bool getBooleanValue(CK_ATTRIBUTE_TYPE type, bool val);
66 virtual unsigned long getUnsignedLongValue(CK_ATTRIBUTE_TYPE type, unsigned long val);
67 virtual ByteString getByteStringValue(CK_ATTRIBUTE_TYPE type);
68
69 // Retrieve the next attribute type
70 virtual CK_ATTRIBUTE_TYPE nextAttributeType(CK_ATTRIBUTE_TYPE type);
71
72 // Set the specified attribute
73 virtual bool setAttribute(CK_ATTRIBUTE_TYPE type, const OSAttribute& attribute);
74
75 // Delete the specified attribute
76 virtual bool deleteAttribute(CK_ATTRIBUTE_TYPE type);
77
78 // The validity state of the object (refresh from disk as a side effect)
79 virtual bool isValid();
80
81 // Invalidate the object file externally; this method is normally
82 // only called by the OSToken class in case an object file has
83 // been deleted.
84 void invalidate();
85
86 // Returns the file name of the object
87 std::string getFilename() const;
88
89 // Returns the file name of the lock
90 std::string getLockname() const;
91
92 // Start an attribute set transaction; this method is used when - for
93 // example - a key is generated and all its attributes need to be
94 // persisted in one go.
95 //
96 // N.B.: Starting a transaction locks the object!
97 //
98 // Function returns false in case a transaction is already in progress
99 virtual bool startTransaction(Access access);
100
101 // Commit an attribute transaction; returns false if no transaction is in progress
102 virtual bool commitTransaction();
103
104 // Abort an attribute transaction; loads back the previous version of the object from disk;
105 // returns false if no transaction was in progress
106 virtual bool abortTransaction();
107
108 // Destroys the object; WARNING: pointers to the object become invalid after this
109 // call!
110 virtual bool destroyObject();
111
112 private:
113 // OSToken instances can read valid (vs calling IsValid() from index())
114 friend class OSToken;
115
116 // Refresh the object if necessary
117 void refresh(bool isFirstTime = false);
118
119 // Write the object to background storage
120 void store(bool isCommit = false);
121
122 // Store subroutine
123 bool writeAttributes(File &objectFile);
124
125 // Discard the cached attributes
126 void discardAttributes();
127
128 // The path to the file
129 std::string path;
130
131 // The Generation object that is used to detect changes in the
132 // object file from other SoftHSM instances
133 Generation* gen;
134
135 // The object's raw attributes
136 std::map<CK_ATTRIBUTE_TYPE, OSAttribute*> attributes;
137
138 // The object's validity state
139 bool valid;
140
141 // The token this object is associated with
142 OSToken* token;
143
144 // Mutex object for thread-safeness
145 Mutex* objectMutex;
146
147 // Is the object undergoing an attribute transaction?
148 bool inTransaction;
149 File* transactionLockFile;
150 std::string lockpath;
151 };
152
153 #endif // !_SOFTHSM_V2_OBJECTFILE_H
154