]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/inc/sm_vdasnacc.h
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-lib / inc / sm_vdasnacc.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /* @(#) sm_vdasnacc.h 1.21 5/1/98 09:59:08 */
20 // vdasnacc.h
21 //
22 #ifndef _SM_VDASNACC_H_
23 #define _SM_VDASNACC_H_
24
25 #include "asn-incl.h"
26
27 #include <stdio.h>
28 #ifndef SM_SIZE_T
29 #define SM_SIZE_T size_t
30 #endif
31
32 #ifdef WIN32
33 #include <stdlib.h>
34 #define SM_FOPEN_WRITE "wb"
35 #define SM_FOPEN_READ "rb"
36 #define SM_FOPEN_APPEND "ab"
37 #else
38 #define SM_FOPEN_WRITE "w"
39 #define SM_FOPEN_READ "r"
40 #define SM_FOPEN_APPEND "a"
41 #endif
42
43
44 //////////////////////////////////////////////////////////////////////////
45 // CSM_Buffer is the general purpose buffer used throughout the SFL
46 class CSM_Buffer: public AsnType
47 {
48 private:
49 SM_SIZE_T m_lSize;
50 char *m_pMemory;
51 #if !defined(macintosh) && !defined(__APPLE__)
52 char *m_pszFN;
53 FILE *m_pFP;
54 #endif
55 char *m_pMemFP;
56 char *m_pCache;
57 SM_SIZE_T m_lCacheSize;
58
59 #if !defined(macintosh) && !defined(__APPLE__)
60 // returns bool value indicating if the buffer is in a file
61 bool InFile() { if (m_pszFN == NULL) return false; else return true; }
62 #endif
63
64 // AllocMoreMem allocates specified more bytes for mem buffer
65 void AllocMoreMem(SM_SIZE_T lSize);
66
67 public:
68 // CONSTRUCTORS
69 // use this constructor to create a complete empty buffer
70 CSM_Buffer();
71 // use this constructor to create a memory buffer of size lSize
72 CSM_Buffer(size_t lSize);
73 // use this constructor to create a buffer in file pszFileName
74 //CSM_Buffer(char *pszFileName);
75 // use this constructor to init the memory buffer with a ptr and size
76 CSM_Buffer(const char *pBuf, SM_SIZE_T lSize);
77 // use this constructor to make a copy of the provided buffer
78 // and put it into this buffer
79 CSM_Buffer(const CSM_Buffer &b);
80
81 virtual ~CSM_Buffer(); // DESTRUCTOR
82
83 // Inheirited from AsnType.
84 virtual AsnType *Clone() const;
85 virtual AsnType *Copy() const;
86
87 virtual AsnLen BEnc (BUF_TYPE b);
88 void Print (ostream &os) const;
89
90 // CONTENT MODIFYING MEMBERS
91 void Clear();
92
93 // ATTRIBUTE MEMBERS
94 // return size of the buffer
95 SM_SIZE_T Length() const;
96 // copy the provided null terminated memory in memory buffer
97 void Set(const char *psz);
98 // copy the provided memory of size lSize in memory buffer
99 void Set(const char *p, SM_SIZE_T lSize);
100 // set the length of the buffer
101 void SetLength(SM_SIZE_T lSize) { m_lSize = lSize; }
102 #if !defined(macintosh) && !defined(__APPLE__)
103 // copy the provided file name into m_pszFN
104 void SetFileName(char *pszFN)
105 {
106 #ifdef HAVE_STRDUP
107 strdup(pszFN);
108 #else
109 m_pszFN = (char *)malloc (strlen (pszFN) + 1);
110 strcpy (m_pszFN, pszFN);
111 #endif
112 }
113 #endif
114 // allocate memory in the buffer and return ptr to it
115 char* Alloc(SM_SIZE_T lSize);
116 // compare this with b, return 0 if match
117 long Compare(const CSM_Buffer &b);
118 // ReSet copies b into this
119 long ReSet(const CSM_Buffer &b);
120
121 // BUFFER DATA ACCESS MEMBERS
122 // return a pointer to the actual data, if in file, call CopyAll
123 const char* Access() const;
124 // return a copy of the actual data and return the size
125 char* Get(SM_SIZE_T &l) const;
126 // return a copy of the actual data
127 char* Get() const { SM_SIZE_T l; return Get(l); }
128
129 // COMPARISON OPERATORS
130 bool operator == (/*const*/ CSM_Buffer &b) {
131 if (Compare(b) == 0) return true; else return false; }
132 bool operator != (/*const*/ CSM_Buffer &b) {
133 if (Compare(b) == 0) return false; else return true; }
134
135 // ASSIGNMENT OPERATOR
136 CSM_Buffer &operator = (/*const*/ CSM_Buffer &b) {
137 ReSet(b); return *this; }
138
139 #if !defined(macintosh) && !defined(__APPLE__)
140 // BUFFER CONVERSION MEMBERS
141 long ConvertFileToMemory();
142 long ConvertMemoryToFile(char *pszFN);
143 #endif
144
145 // STREAMING MEMBERS
146 long Open(char *pszMode);
147 long Seek(SM_SIZE_T lOffset, SM_SIZE_T lOrigin);
148 void Close();
149
150 // STREAMING MEMBERS
151 long cRead(char *pBuffer, SM_SIZE_T lSize);
152 long Write(const char *pBuffer, SM_SIZE_T lSize);
153 char* nRead(SM_SIZE_T lSize, SM_SIZE_T &lBytesRead);
154 void Flush();
155 };
156
157 long vdasnacc_sortSet(CSM_Buffer *pEncBuf[], int icount);
158 long vdasnacc_sortSetOf(CSM_Buffer **&pEncBuf, int icount);
159 long SM_WriteToAsnBuf(CSM_Buffer *&pCBuf, AsnBuf &SNACCinputBuf);
160 long SM_WriteToAsnBuf(CSM_Buffer &CBuf, AsnBuf &SNACCoutputBuf);
161 long SM_ReadFromAsnBuf(CSM_Buffer *&pCBuf, // OUT,copied data.
162 AsnBuf &SNACCinputBuf, // IN, input SNACC buffer
163 long length, // IN, length of data to read.
164 CSM_Buffer *preLoad); // IN, optional data to be pre-loaded;
165 // (for SNACC support)
166 // no alloc version of SM_ReadFromAsnBuf
167 long SM_ReadFromAsnBuf(
168 AsnBuf &SNACCinputBuf, // IN, input SNACC buffer
169 CSM_Buffer *pCBuf, // OUT,copied data.
170 long length, // IN, length of data to read.
171 CSM_Buffer *preLoad); // IN, optional data to be pre-loaded;
172 // (for SNACC support)
173 // function to convert an AsnBits to a CSM_Buffer
174 long SM_AsnBits2Buffer(AsnBits *pBits, CSM_Buffer *pBuffer);
175 long SM_Buffer2AsnBits(CSM_Buffer *pBuffer, AsnBits *pBits, size_t lBits);
176 long SM_BufferReverseBits(CSM_Buffer *pBuffer);
177
178 class BigIntegerStr;
179 #define SM_BUF_2_BIG_INT_STR 0
180 #if SM_BUF_2_BIG_INT_STR
181 // FIXME - why doesn't this link properly?
182 // prototypes for converting to and from BigIntegerStr and CSM_Buffer.
183 long SM_Buffer2BigIntegerStr( CSM_Buffer *asn1Data,
184 BigIntegerStr &pSnaccBigIntStr,
185 bool unsignedFlag);
186
187 long SM_Buffer2BigIntegerStr( CSM_Buffer *asn1Data,
188 BigIntegerStr *&pSnaccBigIntStr,
189 bool unsignedFlag);
190 #endif /* SM_BUF_2_BIG_INT_STR */
191
192 // VDASNACC_ENCDEC_BUFSIZE is the number of bytes in the global
193 // buffer used for encoding and decoding
194 #define VDASNACC_ENCDEC_BUFSIZE 100000
195
196 //typedef struct
197 //{
198 // long lgth; /* Number of characters in string */
199 // unsigned char *str; /* Pointer to character string */
200 //} Str_struct;
201
202 #define NULL_STR (Str_struct *) NULL
203
204
205 //extern "C" {
206 //#include <stdio.h> /**** Standard I/O includes ****/
207 //long vdasnacc_sortSetOf(Str_struct **strEnc, int icount);
208 //long vdasnacc_sortSet(Str_struct **strEnc, int icount);
209 //void free_Str(Str_struct *str);
210 //void free_Str_content(Str_struct *str);
211 //}
212
213 #define ENCODE_ANY(encodedData,asnAny)\
214 {\
215 CSM_Buffer *blob=new CSM_Buffer;\
216 \
217 if ((encodedData) && (asnAny))\
218 {\
219 ENCODE_BUF((encodedData), blob)\
220 (asnAny)->value = (AsnType *)blob;\
221 }\
222 }
223
224 #define DECODE_ANY(decodeData,asnAny)\
225 {\
226 CSM_Buffer *blob;\
227 if ((asnAny))\
228 blob=(CSM_Buffer *)(asnAny)->value;\
229 \
230 if (blob)\
231 DECODE_BUF((decodeData), blob)\
232 }
233
234 // This macro is usually only necessary if a SNACC AsnBuf is used
235 // immediately after being loaded by an application (e.g. consecutive
236 // encode decode operations).
237 #define SNACC_BUFRESET_READ(pSnaccBuf) (pSnaccBuf)->ResetInReadMode();
238 #define SNACC_BUFRESET_WRITE(pSnaccBuf) (pSnaccBuf)->ResetInWriteRvsMode();
239
240 #define ENCODE_BUF_NO_ALLOC(encodeData, blob)\
241 {\
242 char *pchBuffer = (char *)calloc(1, \
243 VDASNACC_ENCDEC_BUFSIZE);\
244 size_t encodedLen;\
245 AsnBuf outputBuf;\
246 int status=0;\
247 \
248 outputBuf.Init(pchBuffer, VDASNACC_ENCDEC_BUFSIZE);\
249 outputBuf.ResetInWriteRvsMode();\
250 status = (encodeData)->BEncPdu (outputBuf, encodedLen);\
251 outputBuf.ResetInReadMode();\
252 SM_ReadFromAsnBuf(outputBuf, (blob), outputBuf.DataLen(),NULL);\
253 free(pchBuffer);\
254 }
255
256 #define ENCODE_BUF(encodeData, blob)\
257 {\
258 char *pchBuffer = (char *)calloc(1, \
259 VDASNACC_ENCDEC_BUFSIZE);\
260 size_t encodedLen;\
261 AsnBuf outputBuf;\
262 int status=0;\
263 \
264 outputBuf.Init(pchBuffer, VDASNACC_ENCDEC_BUFSIZE);\
265 outputBuf.ResetInWriteRvsMode();\
266 if((status = (encodeData)->BEncPdu (outputBuf, encodedLen))==false)\
267 SME_THROW(33, "BAD SNACC Encode", NULL);\
268 outputBuf.ResetInReadMode();\
269 SM_ReadFromAsnBuf((blob), outputBuf, outputBuf.DataLen(),NULL);\
270 free(pchBuffer);\
271 }
272
273 #define DECODE_BUF(decodeData, blob)\
274 {\
275 char *pchBuffer = (char *)calloc(1, \
276 VDASNACC_ENCDEC_BUFSIZE);\
277 size_t encodedLen;\
278 AsnBuf outputBuf;\
279 int nDecStatus = 0;\
280 \
281 outputBuf.Init(pchBuffer, VDASNACC_ENCDEC_BUFSIZE);\
282 outputBuf.ResetInWriteRvsMode();\
283 SM_WriteToAsnBuf((blob), outputBuf);\
284 outputBuf.ResetInReadMode();\
285 if ((nDecStatus = (decodeData)->BDecPdu(outputBuf, encodedLen)) == false)\
286 SME_THROW(34, "BAD SNACC Decode", NULL);\
287 free(pchBuffer);\
288 }
289
290 #define SM_ASSIGN_ANYBUF(lpBuf, asnAny)\
291 {\
292 (asnAny)->value = (AsnType *)new CSM_Buffer(*(lpBuf));\
293 }
294
295 /* don't know if this actually works... dave */
296 #define SM_EXTRACT_ANYBUF(pSS, asnAny)\
297 {\
298 (pSS) = new CSM_Buffer(*(CSM_Buffer *)(asnAny)->value);\
299 }
300
301 #define ENCODE_BUF1(encodeContent, encodeLen)\
302 {\
303 AsnBuf outputBuf;\
304 char *lpszBuf;\
305 \
306 lpszBuf = (char *)calloc(1, VDASNACC_ENCDEC_BUFSIZE/2);\
307 outputBuf.Init(lpszBuf, VDASNACC_ENCDEC_BUFSIZE/2);\
308 outputBuf.ResetInWriteRvsMode();\
309 (encodeLen) = encodeContent(outputBuf);
310
311 #define ENCODE_BUF2(blob)\
312 outputBuf.ResetInReadMode();\
313 SM_ReadFromAsnBuf((blob), outputBuf, outputBuf.DataLen(),NULL);\
314 free(lpszBuf);\
315 }
316
317 // RWC; The following macro defines the ASN ANY load for "BEnc...()"
318 // RWC; operations into the final output buffers. NO ERROR checking
319 // RWC; is performed to be sure the buffer is ASN decodable.
320 // RWC; this convention for loading ANY results is only valid for
321 // RWC; the SMIME/MSP library loads, where previous logic has
322 // RWC; loaded the "AsnType *value" element with a "CSM_Buffer *"
323 // RWC; containing the encoded ANY result.
324 // RWC; The "Str_struct *" needs to be freed when class destroyed.
325 // RWC; Place encoded ASN directly into buffer.
326 #if defined(macintosh) || defined(__APPLE__)
327 #define ENC_LOAD_ANYBUF(asnType, Bbuf, l) \
328 if ((asnType)->value != NULL)\
329 {\
330 l = (asnType)->value->BEnc(Bbuf);\
331 }
332 #else
333 #define ENC_LOAD_ANYBUF(asnType, Bbuf, l) \
334 if ((CSM_Buffer *)(asnType)->value != NULL)\
335 {\
336 SM_WriteToAsnBuf(((CSM_Buffer *&)(asnType)->value), Bbuf);\
337 l = ((CSM_Buffer *)(asnType)->value)->Length();\
338 }
339 #endif
340
341 // RWC; The following macro decodes the ANY buffer tag and length to
342 // RWC; allocate a "CSM_Buffer", then copies the unencoded results.
343 // RWC; The assumption is that the "readloc" buffer will still be intact
344 // RWC; even after the decode of the tag and length. (HOPEFULLY!)
345 // RWC; Once the data for this ANY is copied, unencoded into the CSM_Buffer
346 // RWC; then we set the buffer "readloc" pointer to after this element.
347 // RWC; "bBuf.GetSeg(elmtLen)"
348 #define DEC_LOAD_ANYBUF(asnType, Bbuf, l, env) \
349 {\
350 size_t len = (size_t) 0; \
351 AsnLen bytesDecoded = 0L; \
352 size_t elmtLen = (size_t) 0; \
353 int tag = 0 ; \
354 char *readloc = NULL; \
355 CSM_Buffer *blob; \
356 CSM_Buffer *preLoad;\
357 \
358 readloc = Bbuf.GetSeg (&len);\
359 tag = BDecTag (Bbuf, bytesDecoded, env);\
360 elmtLen = BDecLen (Bbuf, bytesDecoded, env);\
361 len = bytesDecoded;\
362 preLoad = new CSM_Buffer(readloc, len);\
363 SM_ReadFromAsnBuf(blob, (Bbuf), elmtLen,preLoad);\
364 (asnType)->value = blob;\
365 delete preLoad;\
366 l += len + elmtLen;\
367 }
368
369
370 // RWC; Correctly process our OID values, the "char *" "asnOid->Set()" function
371 // directly loads the "->oid" private variable, no processing!!!
372 //int SM_STR_TO_OID(char *lpStrOid, AsnOid *asnOid);
373 //int SM_OID_TO_STR(char *lpStrOid, AsnOid *asnOid);
374
375 #ifdef BOB
376 #define SNACC_OID_FIX(asnOid, long_arr4) \
377 {\
378 unsigned long int a[11];\
379 int i;\
380 for (i=0; i < (long_arr4)->lgth; i++) a[i] = (long_arr4)->int_arr[i];\
381 for (i=(long_arr4)->lgth; i < 11; i++) a[i] = -1;\
382 (asnOid)->Set(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],\
383 a[10]);\
384 }
385 #endif
386
387 #endif // _SM_VDASNACC_H_
388
389 // EOF vdasnacc.h