Estonian ID Card C-library
DigiDocLib.h
1 #ifndef __DIGI_DOC_LIB_H__
2 #define __DIGI_DOC_LIB_H__
3 //==================================================
4 // FILE: DigiDocLib.h
5 // PROJECT: Digi Doc
6 // DESCRIPTION: Digi Doc functions for creating
7 // and reading signed documents.
8 // AUTHOR: Veiko Sinivee, S|E|B IT Partner Estonia
9 //==================================================
10 // Copyright (C) AS Sertifitseerimiskeskus
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.ode
19 // GNU Lesser General Public Licence is available at
20 // http://www.gnu.org/copyleft/lesser.html
21 //==========< HISTORY >=============================
22 // 10.02.2004 Integrated
23 // 02.01.2004 Veiko Sinivee
24 // Added support for format 1.3
25 // 29.10.2003 Aare Amenberg
26 // Added some function headers
27 // (needed for compiling COM)
28 // 1.0 09.04.2002 Veiko Sinivee
29 // Supports XML format (Type: SK-XML-1.0)
30 //==================================================
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <openssl/x509.h>
37 #include <openssl/ocsp.h>
38 #include <libdigidoc/DigiDocMem.h>
39 #include <libdigidoc/DigiDocDefs.h>
40 #include <libdigidoc/DigiDocError.h>
41 #include <libdigidoc/DigiDocObj.h>
42 #include <libdigidoc/DigiDocVerify.h>
43 
44 
45 //==========< data structures >====================
46 
47 // supported format and version combination
48 typedef struct FormatAndVer_st {
49  char* szFormat;
50  char* szVersion;
51 } FormatAndVer;
52 
53 
54 //==========< admin functions >====================
55 
56 // returns the library name
57 EXP_OPTION const char* getLibName();
58 // returns the library version
59 EXP_OPTION const char* getLibVersion();
60 // returns an array of supported formats terminated by NULL
61 EXP_OPTION const char** getSupportedFormats();
62 // returns an array of supported formats and versions terminated by NULL
63 EXP_OPTION FormatAndVer* getSupportedFormatsAndVersions();
64 // returns the GUI version
65 EXP_OPTION const char* getGUIVersion();
66 // sets the GUI version
67 EXP_OPTION void setGUIVersion(const char* szVer);
68 
69 // initializes error library
70 EXP_OPTION void initDigiDocLib();
71 // cleanup of error library
72 EXP_OPTION void finalizeDigiDocLib();
73 
74 // trim leading and trailing whitespace
75 EXP_OPTION char* trim(char* src);
76 
77 // Sets a string element of a struct to a new value
78 EXP_OPTION int setString(char** dest, const char* value, int valLen);
79 
80 EXP_OPTION int checkFileExists(const char* szFileName);
81 
82 int ddocPullUrl(const char* url, DigiDocMemBuf* pSendData, DigiDocMemBuf* pRecvData,
83  const char* proxyHost, const char* proxyPort);
84 
85 // Cheks a combination of format and version for validity
86 int ddocCheckFormatAndVer(const char* format, const char* version);
87 
88 
89 //--------------------------------------------------
90 // Converts timestamp (time_t) value to a struct
91 // tm value. Caller must provide address of tm struct.
92 // This function is used because loacltime() is not
93 // thread-safe and win32 has no equvalent of localtime_r().
94 // pTime - time_t value address
95 // pTmStruct - struct tm address
96 // bLocal - 1=localtime_r, 0=gmtime_r
97 // returns error code or ERR_OK
98 //--------------------------------------------------
99 EXP_OPTION int ddocLocalTime(time_t* pTime, struct tm* pTmStruct, int bLocal);
100 
101 EXP_OPTION int getTempFileName(char* szFileName, int len);
102 
103 //==========< general crypto fucntions >============
104 
105 // calculates input files digest.
106 // supports only SHA1
107 EXP_OPTION int calculateFileDigest(const char* szFileName, int nDigestType,
108  byte* pDigestBuf, int* nDigestLen, long* lFileLen);
109 
110 // calculate file size
111 EXP_OPTION int calculateFileSize(const char* szFileName, long* lFileLen);
112 
113 // calculates files RSA+SHA1 signature
114 EXP_OPTION int calculateFileSignature(const char* szFileName, int nDigestType,
115  byte* pSigBuf, int* nSigLen,
116  const char *keyfile, const char* passwd);
117 
118 // sign some arbitrary daya
119 EXP_OPTION int signData(const byte* data, int dlen, byte* pSigBuf, int* nSigLen,
120  int nDigestType, const char *keyfile, const char* passwd);
121 
122 // calculate digest over some arbitrary data
123 EXP_OPTION int calculateDigest(const byte* data, int nDataLen, int nDigestType,
124  byte* pDigestBuf, int* nDigestLen);
125 
126 
127 
128 //--------------------------------------------------
129 // Prepares a new signature for signing and calculates
130 // the final hash value to sign.
131 // pSigDoc - signed document object
132 // ppSigInfo - pointer for address of newly allocated signature
133 // manifest - manifest or role
134 // city - signers address , city
135 // state - signers address , state or province
136 // zip - signers address , postal code
137 // country - signers address , country name
138 // id - id for new signature. Optional, use NULL for default
139 
140 // return returns error code or ERR_OK
141 //--------------------------------------------------
142 EXP_OPTION int ddocPrepareSignature(SignedDoc* pSigDoc, SignatureInfo** ppSigInfo,
143  const char* manifest, const char* city,
144  const char* state, const char* zip,
145  const char* country, X509* pCert, const char* id);
146 
147 //--------------------------------------------------
148 // Returns the hash to be signed with or without ASN1
149 // prefix and with or without base64 encoding
150 // pSigInfo - signature address
151 // pBuf - buffer for hash value with or without prefix
152 // pBufLen - pointer to buffer length
153 // enc - return 0=unencoded, 1=base64, 2=hex
154 // bWithAsn1Prefix - return with or without ASN1 prefix 1/0
155 // return returns error code or ERR_OK
156 //--------------------------------------------------
157 EXP_OPTION int ddocGetSignedHash(SignatureInfo* pSigInfo, char* pBuf, int* pBufLen, int enc, int bWithAsn1Prefix);
158 
159 
160 //==========< XML generating functions >=================
161 // thse functions generate certain XML form info, that
162 // is used as input data for either signing or digesting
163 
164 // creates XML <SignedInfo> block (actually signed info)
165 EXP_OPTION char* createXMLSignedInfo(const SignedDoc* pSigDoc, const SignatureInfo* pSigInfo);
166 // creates XML mime type <SinatureProperty> block for signing
167 EXP_OPTION int createMimeType(char* buf, const char* mime,
168  const char* sigId, const char* docId);
169 
170 //==========< memory mgmt functions >=================
171 
172 // Get the filename part of full file name
173 EXP_OPTION const char* getSimpleFileName(const char* szFileName);
174 // Get the absolute filename with path
175 EXP_OPTION int getFullFileName(const char* szFileName, char* szDest, int len);
176 // Get the path part of full file name
177  EXP_OPTION int getFileNamePath(const char* szFileName, char* szPath, int len);
178 
179 
180 //------------------------ ??
181 
182 // Sets the signatures certificate and calculates
183 EXP_OPTION int setSignatureCertFile(SignatureInfo* pSigInfo, const char* certFile);
184 // Sets the signatures certificate and calculates it's digest
185 EXP_OPTION int setSignatureCert(SignatureInfo* pSigInfo, X509* cert);
186 
187 // Sets the signature value
188 EXP_OPTION int setSignatureValue(SignatureInfo* pSigInfo, byte* szSignature, int sigLen);
189 
190 // Sets the signature value from a file that contains
191 // the base64 encoded signature value
192 EXP_OPTION int setSignatureValueFromFile(SignatureInfo* pSigInfo, char* szSigFile);
193 
194 
195 //================< certficate info functions> =================================
196 
197 // Decodes a signature value
198 void decodeSignatureValue(const char* b64val, int b64len, char* value, int* vlen);
199 
200 // Returns the user signatures certificate data
201 EXP_OPTION X509* getSignCertData(const SignatureInfo* pSignInfo);
202 // Returns the notary signatures certificate data
203 //EXP_OPTION X509* getNotCertData(const NotaryInfo* pNotInfo);
204 
205 
206 
207 //================< deprecated functions> =================================
208 // these functions are deprecated. Use the replacements in DigiDocCert.h
209 // these functions will be removed in future releases!
210 #ifdef WITH_DEPRECATED_FUNCTIONS
211 
212 // get signers id-code
213 // USE: ddocCertGetSubjectPerCode(getSignCertData(pSignInfo), pMemBuf);
214 EXP_OPTION int getSignerCode(const SignatureInfo* pSigInfo, char* buf);
215 
216 // get signers first name
217 // USE: ddocCertGetSubjectFirstName(getSignCertData(pSignInfo), pMemBuf);
218 EXP_OPTION int getSignerFirstName(const SignatureInfo* pSigInfo, char* buf);
219 
220 // get signers last name
221 // USE: ddocCertGetSubjectLastName(getSignCertData(pSignInfo), pMemBuf);
222 EXP_OPTION int getSignerLastName(const SignatureInfo* pSigInfo, char* buf);
223 
224 // get the whole signers CN in desired charset
225 // USE: ddocCertGetSubjectCN(getSignCertData(pSignInfo), pMemBuf);
226 EXP_OPTION int getSignerCN(const SignatureInfo* pSigInfo, char* buf, int bUTF8);
227 
228 #endif // WITH_DEPRECATED_FUNCTIONS
229 
230 //==================================================================================
231 void CorrectCharacters(char *psText);
232 
233 #ifdef __cplusplus
234 }
235 #endif
236 
237 
238 #endif // __DIGI_DOC_LIB_H__
239 
Definition: DigiDocMem.h:32
Definition: DigiDocLib.h:48
Definition: DigiDocObj.h:154
Definition: DigiDocObj.h:177