]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
1 | /* |
2 | * Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /* | |
25 | * pkcs12BagAttrs.cpp : internal representation of P12 SafeBag | |
26 | * attribute, OTHER THAN friendlyName and localKeyId. | |
27 | * This corresponds to a SecPkcs12AttrsRef at the | |
28 | * public API layer. | |
29 | */ | |
30 | ||
31 | #include "pkcs12BagAttrs.h" | |
32 | #include "pkcs12Utils.h" | |
33 | #include <security_asn1/nssUtils.h> | |
34 | #include <assert.h> | |
427c49bc | 35 | #include <Security/SecBase.h> |
b1ab9ed8 A |
36 | /* |
37 | * Copying constructor used by P12SafeBag during encoding | |
38 | */ | |
39 | P12BagAttrs::P12BagAttrs( | |
40 | const P12BagAttrs *otherAttrs, | |
41 | SecNssCoder &coder) | |
42 | : mAttrs(NULL), mCoder(coder) | |
43 | { | |
44 | if(otherAttrs == NULL) { | |
45 | /* empty copy, done */ | |
46 | return; | |
47 | } | |
48 | unsigned num = otherAttrs->numAttrs(); | |
49 | reallocAttrs(num); | |
50 | for(unsigned dex=0; dex<num; dex++) { | |
51 | copyAttr(*otherAttrs->mAttrs[dex], *mAttrs[dex]); | |
52 | } | |
53 | } | |
54 | ||
55 | unsigned P12BagAttrs::numAttrs() const | |
56 | { | |
57 | return nssArraySize((const void **)mAttrs); | |
58 | } | |
59 | ||
60 | NSS_Attribute *P12BagAttrs::getAttr( | |
61 | unsigned attrNum) | |
62 | { | |
63 | assert(attrNum < numAttrs()); | |
64 | return mAttrs[attrNum]; | |
65 | } | |
66 | ||
67 | ||
68 | /* | |
69 | * Add an attr during decode. | |
70 | */ | |
71 | void P12BagAttrs::addAttr( | |
72 | const NSS_Attribute &attr) | |
73 | { | |
74 | NSS_Attribute *newAttr = reallocAttrs(numAttrs() + 1); | |
75 | copyAttr(attr, *newAttr); | |
76 | } | |
77 | ||
78 | /* | |
79 | * Add an attr during encode. | |
80 | */ | |
81 | void P12BagAttrs::addAttr( | |
82 | const CFDataRef attrOid, | |
83 | const CFArrayRef attrValues) | |
84 | { | |
85 | NSS_Attribute *newAttr = reallocAttrs(numAttrs() + 1); | |
86 | p12CfDataToCssm(attrOid, newAttr->attrType, mCoder); | |
427c49bc | 87 | uint32 numVals = (uint32)CFArrayGetCount(attrValues); |
b1ab9ed8 A |
88 | newAttr->attrValue = (CSSM_DATA **)p12NssNullArray(numVals, mCoder); |
89 | for(unsigned dex=0; dex<numVals; dex++) { | |
90 | CSSM_DATA *dstVal = (CSSM_DATA *)mCoder.malloc(sizeof(CSSM_DATA)); | |
91 | newAttr->attrValue[dex] = dstVal; | |
92 | CFDataRef srcVal = (CFDataRef)CFArrayGetValueAtIndex(attrValues, dex); | |
93 | assert(CFGetTypeID(srcVal) == CFDataGetTypeID()); | |
94 | p12CfDataToCssm(srcVal, *dstVal, mCoder); | |
95 | } | |
96 | } | |
97 | ||
98 | /* | |
99 | * getter, public API version | |
100 | */ | |
101 | void P12BagAttrs::getAttr( | |
102 | unsigned attrNum, | |
103 | CFDataRef *attrOid, // RETURNED | |
104 | CFArrayRef *attrValues) // RETURNED | |
105 | { | |
106 | if(attrNum >= numAttrs()) { | |
427c49bc | 107 | MacOSError::throwMe(errSecParam); |
b1ab9ed8 A |
108 | } |
109 | NSS_Attribute *attr = mAttrs[attrNum]; | |
110 | *attrOid = p12CssmDataToCf(attr->attrType); | |
111 | unsigned numVals = nssArraySize((const void **)attr->attrValue); | |
112 | if(numVals == 0) { | |
113 | /* maybe should return empty array...? */ | |
114 | *attrValues = NULL; | |
115 | return; | |
116 | } | |
117 | CFMutableArrayRef vals = CFArrayCreateMutable(NULL, numVals, NULL); | |
118 | for(unsigned dex=0; dex<numVals; dex++) { | |
119 | CFDataRef val = p12CssmDataToCf(*attr->attrValue[dex]); | |
120 | CFArrayAppendValue(vals, val); | |
121 | } | |
122 | *attrValues = vals; | |
123 | } | |
124 | ||
125 | #pragma mark --- private methods --- | |
126 | ||
127 | /* | |
128 | * Alloc/realloc attr array. | |
129 | * Returns ptr to new empty NSS_Attribute for insertion. | |
130 | */ | |
131 | NSS_Attribute *P12BagAttrs::reallocAttrs( | |
132 | size_t numNewAttrs) | |
133 | { | |
134 | unsigned curSize = numAttrs(); | |
135 | assert(numNewAttrs > curSize); | |
136 | NSS_Attribute **newAttrs = | |
427c49bc | 137 | (NSS_Attribute **)p12NssNullArray((uint32)numNewAttrs, mCoder); |
b1ab9ed8 A |
138 | for(unsigned dex=0; dex<curSize; dex++) { |
139 | newAttrs[dex] = mAttrs[dex]; | |
140 | } | |
141 | mAttrs = newAttrs; | |
142 | ||
143 | /* allocate new NSS_Attributes */ | |
144 | for(unsigned dex=curSize; dex<numNewAttrs; dex++) { | |
145 | mAttrs[dex] = mCoder.mallocn<NSS_Attribute>(); | |
146 | memset(mAttrs[dex], 0, sizeof(NSS_Attribute)); | |
147 | } | |
148 | return mAttrs[curSize]; | |
149 | } | |
150 | ||
151 | void P12BagAttrs::copyAttr( | |
152 | const NSS_Attribute &src, | |
153 | NSS_Attribute &dst) | |
154 | { | |
155 | mCoder.allocCopyItem(src.attrType, dst.attrType); | |
156 | unsigned numVals = nssArraySize((const void **)src.attrValue); | |
157 | dst.attrValue = (CSSM_DATA **)p12NssNullArray(numVals, mCoder); | |
158 | for(unsigned dex=0; dex<numVals; dex++) { | |
159 | CSSM_DATA *dstVal = mCoder.mallocn<CSSM_DATA>(); | |
160 | memset(dstVal, 0, sizeof(CSSM_DATA)); | |
161 | dst.attrValue[dex] = dstVal; | |
162 | mCoder.allocCopyItem(*src.attrValue[dex], *dstVal); | |
163 | } | |
164 | } |