summaryrefslogtreecommitdiff
path: root/libdigidoc/DigiDocConfig.h
blob: 7e4d3e4bc17719a0d10cdd209f74eb1a65685aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifndef __DIGI_DOC_CFG_H__
#define __DIGI_DOC_CFG_H__
//==================================================
// FILE:	DigiDocCfonfig.h
// PROJECT:     Digi Doc
// DESCRIPTION: Digi Doc functions for configuration management
// AUTHOR:  Veiko Sinivee, S|E|B IT Partner Estonia
//==================================================
// Copyright (C) AS Sertifitseerimiskeskus
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// GNU Lesser General Public Licence is available at
// http://www.gnu.org/copyleft/lesser.html
//==========< HISTORY >=============================
//      08.01.2004      Veiko Sinivee
//                      Creation
//      20.03.2004      Added functions createOrReplacePrivateConfigItem()
//                      writeConfigFile() and writePrivateConfigFile()
//      20.03.2004      changed function notarizeSignature to check for PKCS12 arguments
//==================================================

#include <libdigidoc/DigiDocDefs.h>
#include <libdigidoc/DigiDocLib.h>
#include <time.h>

#ifdef  __cplusplus
extern "C" {
#endif


#include <openssl/x509.h>


// item type
#define ITEM_TYPE_UNKNOWN   0
#define ITEM_TYPE_GLOBAL    1
#define ITEM_TYPE_PRIVATE   2

// used to mark modified items to then store all together in private config file
#define ITEM_STATUS_UNKNOWN 0
#define ITEM_STATUS_OK      1
#define ITEM_STATUS_MODIFIED 2

  // holds one configuration item
  typedef struct ConfigItem_st {
	char* szKey;		// items key
	char* szValue;		// items value
	int nType;                  // items type (system wide or private)
	int nStatus;                // item status - clean/modified
  } ConfigItem;
    
  // holds one certificate item
  typedef struct CertificateItem_st {
    char* szKey;		// items key
    X509* pCert;		// certificate
  } CertificateItem;

  // array of configration items
  typedef struct ConfigurationStore_st {
	int nItems;
	ConfigItem** pItems;
    int nCerts;
    CertificateItem** pCerts;
  } ConfigurationStore;

  //--------------------------------------------------
  // Returns true (not 0) if config store structure has been inited
  //--------------------------------------------------
  EXP_OPTION int isConfigInited();

  //--------------------------------------------------
  // Initializes configuration store
  // szConfigFile - name of config file. Use NULL for default
  //--------------------------------------------------
  EXP_OPTION int initConfigStore(const char* szConfigFile);

  //--------------------------------------------------
  // Cleans memory of configuration store
  // pConfStore - configuration collection (use NULL for default)
  //--------------------------------------------------
  EXP_OPTION void cleanupConfigStore(ConfigurationStore *pConfStore);

  //--------------------------------------------------
  // Adds a new configration item
  // pConfStore - configuration collection (use NULL for default)
  // key - items key
  // value - items value
  // type - item type
  // status - item status
  // returns ERR_OK on success
  //--------------------------------------------------
  EXP_OPTION int addConfigItem(ConfigurationStore *pConfStore, const char* key, const char* value, int type, int status);
    
  //--------------------------------------------------
  // Read ca and ocsp responder certs from files and cache in memory
  //--------------------------------------------------
  int initCertificateItems();
    
  //--------------------------------------------------
  // Deletes configration item
  // key - items key
  // returns ERR_OK on success
  //--------------------------------------------------
  EXP_OPTION int ConfigItem_delete(const char* key);

  //--------------------------------------------------
  // Adds a new private configration item or modifies
  // pConfStore - configuration collection (use NULL for default)
  // an existing one
  // key - items key
  // value - items value
  // returns ERR_OK on success
  //--------------------------------------------------
  EXP_OPTION int createOrReplacePrivateConfigItem(ConfigurationStore *pConfStore, const char* key, const char* value);

  //--------------------------------------------------
  // Finds a new configration items value by key
  // key - items key
  // returns value of config item or NULL if not found
  //--------------------------------------------------
  EXP_OPTION const char* ConfigItem_lookup(const char* key);

  //--------------------------------------------------
  // Finds a new configration items value by key from the store
  // key - items key
  // pConfStore - store to search in
  // returns value of config item or NULL if not found
  //--------------------------------------------------
  EXP_OPTION const char* ConfigItem_lookup_fromStore(ConfigurationStore *pConfStore, const char* key);

  //--------------------------------------------------
  // Finds a all configration items that start with this prefix
  // pConfStore - collection of found items
  // prefix - item keys prefix
  // returns error code or ERR_OK
  //--------------------------------------------------
  int ConfigItem_findByPrefix(ConfigurationStore *pConfStore, const char* prefix);

  //--------------------------------------------------
  // Finds a numeric configration items value by key
  // key - items key
  // defValue - default value to be returned
  // returns value of config item or defValue if not found
  //--------------------------------------------------
  EXP_OPTION int ConfigItem_lookup_int(const char* key, int defValue);

  //--------------------------------------------------
  // Finds a bolean configration items value by key
  // key - items key
  // defValue - default value to be returned
  // returns value of config item or defValue if not found
  //--------------------------------------------------
  EXP_OPTION int ConfigItem_lookup_bool(const char* key, int defValue);

  //--------------------------------------------------
  // Finds a new configration items value by key
  // key - items key
  // returns value of config item or NULL if not found
  //--------------------------------------------------
  EXP_OPTION const char* ConfigItem_lookup_str(const char* key, const char* defValue);

  //--------------------------------------------------
  // Reads and parses configuration file
  // fileName - configuration file name
  // type - type of config file global/private
  // return error code or 0 for success
  //--------------------------------------------------
  EXP_OPTION int readConfigFile(const char* fileName, int type);

  //--------------------------------------------------
  // Writes a configuration file
  // fileName - configuration file name
  // type - type of config file global/private
  // return error code or 0 for success
  //--------------------------------------------------
  EXP_OPTION int writeConfigFile(const char* fileName, int type);

  //--------------------------------------------------
  // Saves all private config items in correct file
  // return error code or 0 for success
  //--------------------------------------------------
  EXP_OPTION int writePrivateConfigFile();

  //--------------------------------------------------
  // Sets a new name for private config file. Can be
  // used to override default of env(HOME)/.digidoc.conf
  // Use NULL to restore default value
  //--------------------------------------------------
  EXP_OPTION void setPrivateConfigFile(const char* fileName);

  //--------------------------------------------------
  // Finds CA certificate of the given certificate
  // ppCA - address of found CA
  // pCert - certificate whose CA we are looking for
  // return error code or 0 for success
  //  deprecated use findCAForCertificateAndSigTime()
  //--------------------------------------------------
  DIGIDOC_DEPRECATED EXP_OPTION int findCAForCertificate(X509** ppCA, const X509* pCert);

    //--------------------------------------------------
    // Finds CA certificate of the given certificate
    // ppCA - address of found CA
    // pCert - certificate whose CA we are looking for
    // tSigTime - signature timestamp
    // return error code or 0 for success
    //--------------------------------------------------
    EXP_OPTION int findCAForCertificateAndSigTime(X509** ppCA, const X509* pCert, time_t tSigTime);

  //--------------------------------------------------
  // Finds CA certificate by CN
  // ppCA - address of found CA
  // szCN - CA certs common name
  // pHash - authority-key-identifier to search for CA
  // return error code or 0 for success
  // deprecated use findCAForCNAndSigTime()
  //--------------------------------------------------
  DIGIDOC_DEPRECATED EXP_OPTION int findCAForCN(X509** ppCA, const char* szCN, DigiDocMemBuf *pHash);

    //--------------------------------------------------
    // Finds CA certificate by CN
    // ppCA - address of found CA
    // szCN - CA certs common name
    // pHash - authority-key-identifier to search for CA
    // tSigTime - signing time or 0
    // return error code or 0 for success
    //--------------------------------------------------
    EXP_OPTION int findCAForCNAndSigTime(X509** ppCA, const char* szCN, DigiDocMemBuf *pHash, time_t tSigTime);

    //--------------------------------------------------
    // Finds CA chain 
    // ppChain - address of cert pointer array
    // nMaxChain - index of last cert in returned array - 0 based
    // szCN - CN of the first CA cert (not the child cert!)
    // pCert - certificate to search ca-s for
    // return error code or 0 for success
    // deprecated use findCAChainForCNAndSigTime()
    //--------------------------------------------------
    DIGIDOC_DEPRECATED EXP_OPTION int findCAChainForCN(X509** ppChain, int* nMaxChain, const char* szCN, X509* pCert);

    //--------------------------------------------------
    // Finds CA chain 
    // ppChain - address of cert pointer array
    // nMaxChain - index of last cert in returned array - 0 based
    // szCN - CN of the first CA cert (not the child cert!)
    // pCert - certificate to search ca-s for
    // tSigTime - signature timestamp
    // return error code or 0 for success
    //--------------------------------------------------
    EXP_OPTION int findCAChainForCNAndSigTime(X509** ppChain, int* nMaxChain, const char* szCN, X509* pCert, time_t tSigTime);

  //--------------------------------------------------
  // Finds Responders certificate by CN
  // ppResp - address of found cert
  // szCN - Responder certs common name
  // hash - responder certs hash in base64 form
  // szCertSerial - specific serial number to search
  // return error code or 0 for success
  //--------------------------------------------------
  EXP_OPTION int findResponder(X509** ppResp, const char* szCN, 
			       const char* szHash, char* szCertSerial);

  //--------------------------------------------------
  // Finds Responders certificate by CN and index
  // ppResp - address of found cert
  // szCN - Responder certs common name
  // hash - responder certs hash in base64
  // nIdx - index of the certificate for this respnder. Starts at 0
  // return error code or 0 for success
  //--------------------------------------------------
  EXP_OPTION int findResponderByCNAndHashAndIndex(X509** ppResp, const char* szCN, 
						  const char* hash, int nIdx);

  //--------------------------------------------------
  // Finds Responder certificates CA certs CN
  // caCN - buffer for responders CA CN
  // len - length of buffer for CA CN
  // szCN - responder certs common name
  // hash - responder certs hash in base64 form
  // return error code or 0 for success
  //--------------------------------------------------
  EXP_OPTION int findResponderCA(char* caCN, int len, const char* szCN, const char* hash);

  //------------------------------------------
  // Get a notary confirmation for signature
  // pSigDoc - signed document pointer
  // pSigInfo - signature to notarize
  // returns error code
  //------------------------------------------
  EXP_OPTION int notarizeSignature(SignedDoc* pSigDoc, SignatureInfo* pSigInfo);

  //------------------------------------------
  // Get a notary confirmation for signature
  // pSigDoc - signed document pointer
  // pSigInfo - signature to notarize
  // ip - callers ip address if known
  // returns error code
  //------------------------------------------
  EXP_OPTION int notarizeSignatureWithIp(SignedDoc* pSigDoc, SignatureInfo* pSigInfo, unsigned long ip);

  //--------------------------------------------------
  // Signs the document and gets configrmation
  // pSigDoc - signed document pointer
  // ppSigInfo - address of new signature pointer
  // pin - smart card PIN
  // manifest - manifest / resolution (NULL)
  // city - signers city (NULL)
  // state - signers state (NULL)
  // zip - signers postal code (NULL)
  // country - signers country (NULL)
  //--------------------------------------------------
  EXP_OPTION int signDocument(SignedDoc* pSigDoc, SignatureInfo** ppSigInfo,
		   const char* pin, const char* manifest,
		   const char* city, const char* state,
		   const char* zip, const char* country);

  //--------------------------------------------------
  // Signs the document and gets configrmation
  // pSigDoc - signed document pointer
  // ppSigInfo - address of new signature pointer
  // pin - smart card PIN
  // manifest - manifest / resolution (NULL)
  // city - signers city (NULL)
  // state - signers state (NULL)
  // zip - signers postal code (NULL)
  // country - signers country (NULL)
  // signs with PKCS11
  //--------------------------------------------------
  EXP_OPTION int signDocumentWithSlot(SignedDoc* pSigDoc, SignatureInfo** ppSigInfo,
                 const char* pin, const char* manifest,
                 const char* city, const char* state,
                 const char* zip, const char* country, 
				 int nSlot, int nOcsp, int nSigner);
    
  //--------------------------------------------------
  // Signs the document and gets configrmation
  // pSigDoc - signed document pointer
  // ppSigInfo - address of new signature pointer
  // pin - smart card PIN
  // manifest - manifest / resolution (NULL)
  // city - signers city (NULL)
  // state - signers state (NULL)
  // zip - signers postal code (NULL)
  // country - signers country (NULL)
  // nSigner - 1=PKCS11, 2=CNG (Microsoft CAPI), 3=PKCS#12
  // szPkcs12FileName - PKCS#12 file name to be used for signing (required if nSigner=3)
  //--------------------------------------------------
  EXP_OPTION int signDocumentWithSlotAndSigner(SignedDoc* pSigDoc, SignatureInfo** ppSigInfo,
                                                 const char* pin, const char* manifest,
                                                 const char* city, const char* state,
                                                 const char* zip, const char* country, 
                                                 int nSlot, int nOcsp, int nSigner,
                                                 const char* szPkcs12FileName);

  //--------------------------------------------------
  // Verify this notary
  // pSigDoc - signed document pointer
  // pNotInfo - notary to verify
  // returns error code
  //--------------------------------------------------
  int verifyNotary(SignedDoc* pSigDoc, SignatureInfo* pSigInfo, NotaryInfo* pNotInfo);

  //--------------------------------------------------
  // Verify this signature and it's notary
  // pSigDoc - signed document pointer
  // pSigInfo - signature to verify
  // szFileName - input digidoc filename
  // returns error code
  //--------------------------------------------------
  EXP_OPTION int verifySignatureAndNotary(SignedDoc* pSigDoc, SignatureInfo* pSigInfo, const char* szFileName);

  //--------------------------------------------------
  // Extract common name from cert DN or responder id
  // src - DN
  // dest - buffer for CN
  // destLen - size of output buffer in bytes
  //--------------------------------------------------
  int findCN(char* src, char* dest, int destLen);

  //------------------------------------------
  // Verify certificate by OCSP
  // pCert - certificate to check
  // ppResp - address to return OCSP response. Use NULL if
  // you don't want OCSP response to be returned
  // returns error code
  //------------------------------------------
  EXP_OPTION int ddocVerifyCertByOCSP(X509* pCert, OCSP_RESPONSE **ppResp);

  //------------------------------------------
  // Verify certificate by OCSP
  // pCert - certificate to check
  // ppResp - address to return OCSP response. Use NULL if
  // you don't want OCSP response to be returned
  // returns error code
  //------------------------------------------
  EXP_OPTION int ddocVerifyCertByOCSPWithIp(X509* pCert, OCSP_RESPONSE **ppResp, unsigned long ip);

  //------------------------------------------
  // Reads an arbitrary file into memory buffer
  // szFileName - file name and path
  // pData - memory buffer object
  // returns error code
  //------------------------------------------
  EXP_OPTION int ddocReadFile(const char* szFileName, DigiDocMemBuf* pData);

  //------------------------------------------
  // Writes an arbitrary file into memory buffer
  // szFileName - file name and path
  // pData - memory buffer object
  // returns error code
  //------------------------------------------
  EXP_OPTION int ddocWriteFile(const char* szFileName, DigiDocMemBuf* pData);
	

#ifdef  __cplusplus
}
#endif


#endif // __DIGI_DOC_CFG_H__