5c3ba582b5e86deafcfc977224df88fa84932473
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * pkcs12BagAttrs.cpp : internal representation of P12 SafeBag
26 * attribute, OTHER THAN friendlyName and localKeyId.
27 * This corresponds to a SecPkcs12AttrsRef at the
31 #include "pkcs12BagAttrs.h"
32 #include "pkcs12Utils.h"
33 #include <security_asn1/nssUtils.h>
35 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
38 * Copying constructor used by P12SafeBag during encoding
40 P12BagAttrs::P12BagAttrs(
41 const P12BagAttrs
*otherAttrs
,
43 : mAttrs(NULL
), mCoder(coder
)
45 if(otherAttrs
== NULL
) {
46 /* empty copy, done */
49 unsigned num
= otherAttrs
->numAttrs();
51 for(unsigned dex
=0; dex
<num
; dex
++) {
52 copyAttr(*otherAttrs
->mAttrs
[dex
], *mAttrs
[dex
]);
56 unsigned P12BagAttrs::numAttrs() const
58 return nssArraySize((const void **)mAttrs
);
61 NSS_Attribute
*P12BagAttrs::getAttr(
64 assert(attrNum
< numAttrs());
65 return mAttrs
[attrNum
];
70 * Add an attr during decode.
72 void P12BagAttrs::addAttr(
73 const NSS_Attribute
&attr
)
75 NSS_Attribute
*newAttr
= reallocAttrs(numAttrs() + 1);
76 copyAttr(attr
, *newAttr
);
80 * Add an attr during encode.
82 void P12BagAttrs::addAttr(
83 const CFDataRef attrOid
,
84 const CFArrayRef attrValues
)
86 NSS_Attribute
*newAttr
= reallocAttrs(numAttrs() + 1);
87 p12CfDataToCssm(attrOid
, newAttr
->attrType
, mCoder
);
88 unsigned numVals
= CFArrayGetCount(attrValues
);
89 newAttr
->attrValue
= (CSSM_DATA
**)p12NssNullArray(numVals
, mCoder
);
90 for(unsigned dex
=0; dex
<numVals
; dex
++) {
91 CSSM_DATA
*dstVal
= (CSSM_DATA
*)mCoder
.malloc(sizeof(CSSM_DATA
));
92 newAttr
->attrValue
[dex
] = dstVal
;
93 CFDataRef srcVal
= (CFDataRef
)CFArrayGetValueAtIndex(attrValues
, dex
);
94 assert(CFGetTypeID(srcVal
) == CFDataGetTypeID());
95 p12CfDataToCssm(srcVal
, *dstVal
, mCoder
);
100 * getter, public API version
102 void P12BagAttrs::getAttr(
104 CFDataRef
*attrOid
, // RETURNED
105 CFArrayRef
*attrValues
) // RETURNED
107 if(attrNum
>= numAttrs()) {
108 MacOSError::throwMe(paramErr
);
110 NSS_Attribute
*attr
= mAttrs
[attrNum
];
111 *attrOid
= p12CssmDataToCf(attr
->attrType
);
112 unsigned numVals
= nssArraySize((const void **)attr
->attrValue
);
114 /* maybe should return empty array...? */
118 CFMutableArrayRef vals
= CFArrayCreateMutable(NULL
, numVals
, NULL
);
119 for(unsigned dex
=0; dex
<numVals
; dex
++) {
120 CFDataRef val
= p12CssmDataToCf(*attr
->attrValue
[dex
]);
121 CFArrayAppendValue(vals
, val
);
126 #pragma mark --- private methods ---
129 * Alloc/realloc attr array.
130 * Returns ptr to new empty NSS_Attribute for insertion.
132 NSS_Attribute
*P12BagAttrs::reallocAttrs(
135 unsigned curSize
= numAttrs();
136 assert(numNewAttrs
> curSize
);
137 NSS_Attribute
**newAttrs
=
138 (NSS_Attribute
**)p12NssNullArray(numNewAttrs
, mCoder
);
139 for(unsigned dex
=0; dex
<curSize
; dex
++) {
140 newAttrs
[dex
] = mAttrs
[dex
];
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
));
149 return mAttrs
[curSize
];
152 void P12BagAttrs::copyAttr(
153 const NSS_Attribute
&src
,
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
);