]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_pkcs12/lib/pkcs12BagAttrs.cpp
2 * Copyright (c) 2003-2004,2011-2014 Apple 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>
34 #include <security_utilities/simulatecrash_assert.h>
35 #include <Security/SecBase.h>
37 * Copying constructor used by P12SafeBag during encoding
39 P12BagAttrs::P12BagAttrs(
40 const P12BagAttrs
*otherAttrs
,
42 : mAttrs(NULL
), mCoder(coder
)
44 if(otherAttrs
== NULL
) {
45 /* empty copy, done */
48 unsigned num
= otherAttrs
->numAttrs();
50 for(unsigned dex
=0; dex
<num
; dex
++) {
51 copyAttr(*otherAttrs
->mAttrs
[dex
], *mAttrs
[dex
]);
55 unsigned P12BagAttrs::numAttrs() const
57 return nssArraySize((const void **)mAttrs
);
60 NSS_Attribute
*P12BagAttrs::getAttr(
63 assert(attrNum
< numAttrs());
64 return mAttrs
[attrNum
];
69 * Add an attr during decode.
71 void P12BagAttrs::addAttr(
72 const NSS_Attribute
&attr
)
74 NSS_Attribute
*newAttr
= reallocAttrs(numAttrs() + 1);
75 copyAttr(attr
, *newAttr
);
79 * Add an attr during encode.
81 void P12BagAttrs::addAttr(
82 const CFDataRef attrOid
,
83 const CFArrayRef attrValues
)
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
);
99 * getter, public API version
101 void P12BagAttrs::getAttr(
103 CFDataRef
*attrOid
, // RETURNED
104 CFArrayRef
*attrValues
) // RETURNED
106 if(attrNum
>= numAttrs()) {
107 MacOSError::throwMe(errSecParam
);
109 NSS_Attribute
*attr
= mAttrs
[attrNum
];
110 *attrOid
= p12CssmDataToCf(attr
->attrType
);
111 unsigned numVals
= nssArraySize((const void **)attr
->attrValue
);
113 /* maybe should return empty array...? */
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
);
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((uint32
)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
);