]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/clAppUtils/BlobList.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / SecurityTests / clxutils / clAppUtils / BlobList.cpp
1 /*** BlobList class for managing groups of raw certs and CRLs ***/
2
3 #include "BlobList.h"
4 #include <utilLib/fileIo.h>
5 #include <utilLib/common.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <security_cdsa_utils/cuPem.h>
10
11 BlobList::~BlobList()
12 {
13 for(uint32 dex=0; dex<mNumBlobs; dex++) {
14 free(mBlobList[dex].Data); // mallocd by readFile()
15 }
16 free(mBlobList);
17 mBlobList = NULL;
18 mNumBlobs = 0;
19 }
20
21 /* blob is mallocd & copied; its referent is not copied */
22 void BlobList::addBlob(const CSSM_DATA &blob, CSSM_BOOL copyBlob /* = CSSM_FALSE */)
23 {
24 ++mNumBlobs;
25 mBlobList = (CSSM_DATA_PTR)realloc(mBlobList, mNumBlobs * sizeof(CSSM_DATA));
26 CSSM_DATA_PTR dst = &mBlobList[mNumBlobs - 1];
27 if(copyBlob) {
28 /* can't use appCopyCssmData since we free with free(), not appFree() */
29 dst->Length = blob.Length;
30 dst->Data = (uint8 *)malloc(dst->Length);
31 memmove(dst->Data, blob.Data, dst->Length);
32 }
33 else {
34 *dst = blob;
35 }
36 }
37
38
39 int BlobList::addFile(const char *fileName,
40 const char *dirName /* = NULL */)
41 {
42 CSSM_DATA blob;
43 int rtn;
44 char *fullName;
45 unsigned blobDataLen;
46 unsigned char *blobData;
47
48 if(dirName) {
49 int len = strlen(dirName) + strlen(fileName) + 2;
50 fullName = (char *)malloc(len);
51 sprintf(fullName, "%s/%s", dirName, fileName);
52 }
53 else {
54 fullName = (char *)fileName;
55 }
56 rtn = cspReadFile(fullName, &blobData, &blobDataLen);
57 if(rtn) {
58 printf("***Error reading file %s\n", fullName);
59 if(dirName) {
60 free(fullName);
61 }
62 return rtn;
63 }
64
65 /* convert from PEM to DER if appropriate */
66 if(isPem(blobData, blobDataLen)) {
67 unsigned derDataLen;
68 unsigned char *derData;
69
70 if(pemDecode(blobData, blobDataLen, &derData, &derDataLen)) {
71 printf("***Error PEM-decoding file %s; using as raw data\n", fileName);
72 blob.Data = blobData;
73 blob.Length = blobDataLen;
74 }
75 else {
76 blob.Data = derData;
77 blob.Length = derDataLen;
78 free(blobData);
79 }
80 }
81 else {
82 /* raw file data is the stuff we'll use */
83 blob.Data = blobData;
84 blob.Length = blobDataLen;
85 }
86 addBlob(blob);
87 if(dirName) {
88 free(fullName);
89 }
90 return 0;
91 }
92