Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

364 lines
12 KiB

  1. // protect this file against multiple inclusion
  2. #ifndef _KEYRINGOBJECTS_
  3. #define _KEYRINGOBJECTS_
  4. /* STARTING
  5. When creating your dll using this api, you will be mostly concerned
  6. the CService and CKey object classes. You are expected to override
  7. both of these and provide functionality for storing/retrieving the
  8. keys and maintaining any service specific properites.
  9. Your dll needs only one exported routine "LoadService" defined below.
  10. This routine creates your overridden service object, populates it
  11. with retrieve keys, and connects it to its host machine. The Machine
  12. object is passed in to this routine and the service is returned.
  13. If the host machine does not have your service on it, simply return
  14. from LoadService without attaching a service object to it.
  15. PROPERTIES
  16. You can enable the properties item in the context menu for either your
  17. keys or your service by overridding the classes' OnUpdateProperties
  18. and OnProperties routines. These are very similar to MFC command
  19. handlers. In fact, they are just passed in from a command handler.
  20. You can do whatever you feel like in the OnProperties routine, although
  21. some sort of dialog is probably appropriate;
  22. INFO STRING
  23. Services and Keys also have the option of displaying a one-line
  24. information string in the right-hand pand of the keyring application,
  25. To do this, override the GetInfoString methode and return
  26. something.
  27. KEY NAMES
  28. All keys have names and you are expected to store/retrieve them. The
  29. name is automatically editable in the right-hand pane of the main app.
  30. The name, however, can be different from the caption in the tree view.
  31. To do this, override the UpdateCaption routine and use it to call
  32. FSetCaption with a modified string name. An example can be seen in the
  33. W3 server, which displays the name of the key followed by the ip
  34. address it is attached to in brackets. MyKey<100.200.150.250>
  35. CUSTOM ICONS IN TREEVIEW
  36. You can add your own custom icons to the tree view in addition to the
  37. standard machine, key, unfinished key icons. To do this, get the
  38. CTreeCtrl object by calling PGetTreeCtrl. Then use that to get the
  39. CImageList. From there, you can add your own icons (making sure to note
  40. down the starting index). See CTreeCtrl and CImageList docs for details.
  41. */
  42. // basic icon numbers
  43. enum
  44. {
  45. TREE_ICON_MACHINE = 0,
  46. TREE_ICON_KEY_OK,
  47. TREE_ICON_KEY_IMMATURE,
  48. TREE_ICON_KEY_EXPIRED
  49. };
  50. // declare the correct dllexport definitions
  51. #ifdef _EXE_
  52. // we are exporting the classes - this is the main application
  53. #define DLL_SHARE __declspec( dllexport )
  54. #else
  55. // we are importing the classes - this is your dll
  56. #define DLL_SHARE __declspec( dllimport )
  57. #endif _EXE_
  58. //====================== Forward class declarations
  59. class DLL_SHARE CMachine;
  60. //====================== Template for the exported routine
  61. extern BOOL _cdecl LoadService( CMachine* pMachine );
  62. //---------------------------------------------------------------
  63. // CTreeItem
  64. // This is the base class for all objects that can be in the tree view.
  65. // This includes machines, services, keys and key folders. Note that each
  66. // tree item object can contain other tree item objects. This interface
  67. // allows you to access the item's handle in the tree.
  68. class DLL_SHARE CTreeItem : public CObject
  69. {
  70. public:
  71. // constructors
  72. CTreeItem();
  73. // get the parent object
  74. CTreeItem* PGetParent( void );
  75. // remove this item from the tree
  76. BOOL FRemoveFromTree();
  77. // access the name of the item
  78. // Must be added to parent first!
  79. virtual void UpdateCaption( void ) {;}
  80. BOOL FSetCaption( CString& szName );
  81. // a informational string that is displayed in the right-hand
  82. // pane of the main application. Override to actually show something
  83. virtual void GetInfoString( CString& szInfo )
  84. { szInfo.Empty(); }
  85. // access the image shown in the tree view
  86. // Must be added to parent first!
  87. WORD IGetImage( void ) { return m_iImage; }
  88. BOOL FSetImage( WORD i );
  89. // get the grandparental ctreectrl object
  90. CTreeCtrl* PGetTreeCtrl( void );
  91. // add the item to the tree
  92. BOOL FAddToTree( CTreeItem* pParent );
  93. // how many children does this item have?
  94. WORD GetChildCount();
  95. // get the HTREEITEM handle
  96. HTREEITEM HGetTreeItem() { return m_hTreeItem; }
  97. // do you want the properties item in the context menu?
  98. // the default is NO - Override these in your subclasses
  99. // to provide specific properties dialogs
  100. virtual void OnUpdateProperties(CCmdUI* pCmdUI)
  101. {pCmdUI->Enable(FALSE);}
  102. // your properties item has been selected
  103. virtual void OnProperties() {ASSERT(FALSE);}
  104. // helpful utilities for scanning the
  105. // children contained by a object
  106. CTreeItem* GetFirstChild();
  107. CTreeItem* GetNextChild( CTreeItem* pKid );
  108. // access to the dirty flag
  109. // setting dirty affects parents too (in the default method)
  110. virtual void SetDirty( BOOL fDirty );
  111. virtual BOOL FGetDirty()
  112. { return m_fDirty; }
  113. protected:
  114. // DO declare all this stuff DYNCREATE
  115. DECLARE_DYNCREATE(CTreeItem);
  116. // the name of the item. In the case of keys, you should
  117. // store this name and retrieve it later
  118. CString m_szItemName;
  119. // index of the item's image in the image list
  120. // Note: if you wish to have a special icon different from
  121. // the standard icons enumerated above, (e.g. for a service)
  122. // you get the tree control, then use that to get its CImageList
  123. // object. Then you call the Add member of the image list.
  124. // That call does return the index of your first added image.
  125. WORD m_iImage;
  126. // the dirty flag
  127. BOOL m_fDirty;
  128. private:
  129. // the item's reference handle in the tree
  130. // access it using the api above
  131. HTREEITEM m_hTreeItem;
  132. };
  133. //---------------------------------------------------------------
  134. // CKey
  135. // This class is what its all about. This is a key. You should override
  136. // this class. You are expected to provide storage and retrieval of this
  137. // key. You are also expected to provide any properties dialogs and such.
  138. // basic SSL functionality has already been built in.
  139. class DLL_SHARE CKey : public CTreeItem
  140. {
  141. public:
  142. CKey();
  143. ~CKey();
  144. // override the update caption so the name is automatically shown
  145. virtual void UpdateCaption( void )
  146. {
  147. FSetCaption(m_szItemName);
  148. UpdateIcon();
  149. }
  150. // update the currently shown icon
  151. virtual void UpdateIcon( void );
  152. // the private key - keep this safe! // must store!
  153. DWORD m_cbPrivateKey;
  154. PVOID m_pPrivateKey;
  155. // the certificate // must store!
  156. DWORD m_cbCertificate;
  157. PVOID m_pCertificate;
  158. // the certificate request // must store!
  159. DWORD m_cbCertificateRequest;
  160. PVOID m_pCertificateRequest;
  161. // the password. Be careful where you
  162. // store this.
  163. CString m_szPassword;
  164. // make a copy of the key
  165. virtual CKey* PClone( void );
  166. // checks that the key, certificate and password all match
  167. BOOL FVerifyValidPassword( CString szPassword );
  168. // routine for installing the certificate
  169. virtual BOOL FInstallCertificate( CString szPath, CString szPass );
  170. virtual BOOL FInstallCertificate( PVOID pCert, DWORD cbCert, CString &szPass );
  171. // write out the request file
  172. virtual BOOL FOutputRequestFile( CString szFile, BOOL fMime = FALSE, PVOID privData = NULL );
  173. // copy the members from a key into this key
  174. virtual void CopyDataFrom( CKey* pKey );
  175. // called by the right-hand dialog pane
  176. virtual void SetName( CString &szNewName );
  177. virtual CString GetName()
  178. { return m_szItemName; }
  179. // import/export routines
  180. virtual BOOL FImportKeySetFiles( CString szPrivate, CString szPublic, CString &szPass );
  181. BOOL FImportExportBackupFile( CString szFile, BOOL fImport );
  182. protected:
  183. // DO declare all this stuff DYNCREATE
  184. DECLARE_DYNCREATE(CKey);
  185. private:
  186. void OutputHeader( CFile* pFile, PVOID privData1, PVOID privData2 );
  187. };
  188. //---------------------------------------------------------------
  189. // CService
  190. // This class MUST be overridden in your dll! It is your main link to the app.
  191. // It resides on a machine and contains keys
  192. class DLL_SHARE CService : public CTreeItem
  193. {
  194. public:
  195. // create a new key. You can override to
  196. // create a key of your own class type
  197. virtual CKey* PNewKey() {return new CKey;}
  198. // load the existing keys
  199. virtual void LoadKeys( CMachine* pMachine ) {;}
  200. // the order in which things happen is that you are responsible
  201. // for creating this service object and populating it with key
  202. // objects that you retrieve from whatever storage medium you want
  203. // to use. Then, if that is successful, you attach this service
  204. // to the machine that is passed in to you through the LoadService
  205. // routine. - NOTE that routine is a direct export of your DLL;
  206. // see the definition of that routine above.
  207. // CommitChanges is where you write out any and all changes in
  208. // the service's key list to some storage facility. The storage
  209. // facility and the manner in which you access it is up to you.
  210. virtual BOOL FCommitChangesNow() {return FALSE;}
  211. // CloseConnection is called before disconnecting a machine from
  212. // the tree, or when application is exiting.
  213. virtual void CloseConnection();
  214. protected:
  215. // DO declare all this stuff DYNCREATE
  216. DECLARE_DYNCREATE(CService);
  217. private:
  218. };
  219. //---------------------------------------------------------------
  220. // CKeyCrackedData
  221. // This is a special purpose class. You give it a key object (must have a
  222. // valid certificate attached to it) and it will crack the certificate.
  223. // you can then use the supplied methods to access the data in the cert.
  224. // This uses a two-step construction. First, declare the object, then
  225. // crack it using the CrackKey command, which returns an error code
  226. class DLL_SHARE CKeyCrackedData : public CObject
  227. {
  228. public:
  229. // constructor
  230. CKeyCrackedData();
  231. ~CKeyCrackedData();
  232. // give it a key to crack. If this object was previously used to
  233. // crack a key, cleanup is automatically done and the new key is
  234. // cracked. - NOTE: The target key MUST have either a certificate
  235. // or a certificate request. Those are what get cracked. A return
  236. // value of 0 indicates success
  237. WORD CrackKey( CKey* pKey );
  238. // The rest of the methods access the data in the cracked certificate
  239. DWORD GetVersion();
  240. DWORD* PGetSerialNumber(); // returns a pointer to a DWORD[4]
  241. int GetSignatureAlgorithm();
  242. FILETIME GetValidFrom();
  243. FILETIME GetValidUntil();
  244. PVOID PSafePublicKey();
  245. DWORD GetBitLength();
  246. void GetIssuer( CString &sz );
  247. void GetSubject( CString &sz );
  248. void GetDNCountry( CString &sz );
  249. void GetDNState( CString &sz );
  250. void GetDNLocality( CString &sz );
  251. void GetDNNetAddress( CString &sz );
  252. void GetDNOrganization( CString &sz );
  253. void GetDNUnit( CString &sz );
  254. protected:
  255. private:
  256. void GetDN( CString &szDN, LPCSTR szKey );
  257. CKey* m_pKey;
  258. PVOID m_pData;
  259. };
  260. //---------------------------------------------------------------
  261. // CMachine
  262. // This class is almost always used just by the application. It is the
  263. // machine that the services and keys reside on. It is very simple and
  264. // is to be used just to attach the services to something. Otherwise it
  265. // maintains where the machine is.
  266. class DLL_SHARE CMachine : public CTreeItem
  267. {
  268. public:
  269. // the machine objects are always created and maintained by the
  270. // application. This interface is provided just so that you can
  271. // attach and detach services to it.
  272. // query this method to see if this is the local machine or
  273. // a remote machine on the net.
  274. virtual BOOL FLocal();
  275. // NOTE: when you add the service to the machine it is also added
  276. // to the tree view. The machine is always added to the tree view
  277. // before you are asked to load your service. Immediately after
  278. // adding your service to the machine, don't forget to set the
  279. // service's caption string.
  280. virtual void GetMachineName(CString& sz);
  281. protected:
  282. // DO declare all this stuff DYNCREATE
  283. DECLARE_DYNCREATE(CMachine);
  284. // The name of the machine. - This MAY be different from the caption
  285. // in the tree view. This is the name you use to link to the machine
  286. // over the net. In the case of the local machine, this string will
  287. // be empty. Use SZGetMachineName() above to access it.
  288. CString m_szNetMachineName;
  289. private:
  290. };
  291. // end inclusion protection
  292. #endif //_KEYRINGOBJECTS_