]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_pkcs12/lib/pkcs12BagAttrs.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_pkcs12 / lib / pkcs12BagAttrs.cpp
1 /*
2 * Copyright (c) 2003-2004,2011-2014 Apple 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 <security_utilities/simulatecrash_assert.h>
35 #include <Security/SecBase.h>
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);
87 uint32 numVals = (uint32)CFArrayGetCount(attrValues);
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()) {
107 MacOSError::throwMe(errSecParam);
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 CFRelease(val);
122 }
123 *attrValues = vals;
124 }
125
126 #pragma mark --- private methods ---
127
128 /*
129 * Alloc/realloc attr array.
130 * Returns ptr to new empty NSS_Attribute for insertion.
131 */
132 NSS_Attribute *P12BagAttrs::reallocAttrs(
133 size_t numNewAttrs)
134 {
135 unsigned curSize = numAttrs();
136 assert(numNewAttrs > curSize);
137 NSS_Attribute **newAttrs =
138 (NSS_Attribute **)p12NssNullArray((uint32)numNewAttrs, mCoder);
139 for(unsigned dex=0; dex<curSize; dex++) {
140 newAttrs[dex] = mAttrs[dex];
141 }
142 mAttrs = newAttrs;
143
144 /* allocate new NSS_Attributes */
145 for(unsigned dex=curSize; dex<numNewAttrs; dex++) {
146 mAttrs[dex] = mCoder.mallocn<NSS_Attribute>();
147 memset(mAttrs[dex], 0, sizeof(NSS_Attribute));
148 }
149 return mAttrs[curSize];
150 }
151
152 void P12BagAttrs::copyAttr(
153 const NSS_Attribute &src,
154 NSS_Attribute &dst)
155 {
156 mCoder.allocCopyItem(src.attrType, dst.attrType);
157 unsigned numVals = nssArraySize((const void **)src.attrValue);
158 dst.attrValue = (CSSM_DATA **)p12NssNullArray(numVals, mCoder);
159 for(unsigned dex=0; dex<numVals; dex++) {
160 CSSM_DATA *dstVal = mCoder.mallocn<CSSM_DATA>();
161 memset(dstVal, 0, sizeof(CSSM_DATA));
162 dst.attrValue[dex] = dstVal;
163 mCoder.allocCopyItem(*src.attrValue[dex], *dstVal);
164 }
165 }