]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utilities/lib/AuthorizationData.cpp
2 * Copyright (c) 2000-2006,2011-2012,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@
24 #include <security_cdsa_utilities/AuthorizationData.h>
25 #include <security_cdsa_utilities/AuthorizationWalkers.h>
26 #include <security_cdsa_utilities/walkers.h>
27 #include <Security/checkpw.h>
32 // checkpw() that uses provided struct passwd
35 int checkpw_internal( const struct passwd
*pw
, const char* password
);
39 namespace Authorization
{
42 AuthValueRef::AuthValueRef(const AuthValue
&value
) :
43 RefPointer
<AuthValue
>(new AuthValue(value
)) {}
45 AuthValueRef::AuthValueRef(const AuthorizationValue
&value
) :
46 RefPointer
<AuthValue
>(new AuthValue(value
)) {}
48 AuthValue::AuthValue(const AuthorizationValue
&value
) :
51 mValue
.length
= value
.length
;
52 mValue
.data
= value
.data
;
55 AuthValueRef::AuthValueRef(UInt32 length
, void *data
) :
56 RefPointer
<AuthValue
>(new AuthValue(length
, data
)) {}
58 AuthValue::AuthValue(UInt32 length
, void *data
) :
61 mValue
.length
= length
;
62 mValue
.data
= new uint8_t[length
];
64 memcpy(mValue
.data
, data
, length
);
67 AuthValue::~AuthValue()
71 memset(mValue
.data
, 0, mValue
.length
);
72 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
77 AuthValue::operator = (const AuthValue
&other
)
81 memset(mValue
.data
, 0 , mValue
.length
);
82 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
85 mValue
= other
.mValue
;
86 mOwnsValue
= other
.mOwnsValue
;
87 other
.mOwnsValue
= false;
92 AuthValue::fillInAuthorizationValue(AuthorizationValue
&value
)
94 value
.length
= mValue
.length
;
95 value
.data
= mValue
.data
;
99 AuthValueVector::operator = (const AuthorizationValueVector
& valueVector
)
102 for (unsigned int i
=0; i
< valueVector
.count
; i
++)
103 push_back(AuthValueRef(valueVector
.values
[i
]));
107 AuthItem::AuthItem(const AuthorizationItem
&item
) :
113 MacOSError::throwMe(errAuthorizationInternal
);
114 size_t nameLen
= strlen(item
.name
) + 1;
115 mName
= new char[nameLen
];
116 memcpy(const_cast<char *>(mName
), item
.name
, nameLen
);
118 mValue
.length
= item
.valueLength
;
119 mValue
.data
= new uint8_t[item
.valueLength
];
121 memcpy(mValue
.data
, item
.value
, item
.valueLength
);
125 AuthItem::AuthItem(AuthorizationString name
) :
135 AuthItem::AuthItem(AuthorizationString name
, AuthorizationValue value
, AuthorizationFlags flags
) :
141 MacOSError::throwMe(errAuthorizationInternal
);
142 size_t nameLen
= strlen(name
) + 1;
143 mName
= new char[nameLen
];
144 memcpy(const_cast<char *>(mName
), name
, nameLen
);
146 mValue
.length
= value
.length
;
147 mValue
.data
= new uint8_t[value
.length
];
149 memcpy(mValue
.data
, value
.data
, value
.length
);
152 AuthItem::~AuthItem()
158 memset(mValue
.data
, 0, mValue
.length
);
159 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
164 AuthItem::operator < (const AuthItem
&other
) const
166 return strcmp(mName
, other
.mName
) < 0;
170 AuthItem::operator = (const AuthItem
&other
)
176 memset(mValue
.data
, 0, mValue
.length
);
177 delete[] reinterpret_cast<uint8_t*>(mValue
.data
);
181 mValue
= other
.mValue
;
182 mFlags
= other
.mFlags
;
183 mOwnsName
= other
.mOwnsName
;
184 other
.mOwnsName
= false;
185 mOwnsValue
= other
.mOwnsValue
;
186 other
.mOwnsValue
= false;
191 AuthItem::getString(string
&value
)
193 // if terminating NUL is included, ignore it
194 size_t len
= mValue
.length
;
195 if (len
> 0 && (static_cast<char*>(mValue
.data
)[len
- 1] == 0))
197 value
= string(static_cast<char*>(mValue
.data
), len
);
202 AuthItem::getCssmData(CssmAutoData
&value
)
204 value
= CssmData(static_cast<uint8_t*>(mValue
.data
), mValue
.length
);
209 AuthItemRef::AuthItemRef(const AuthorizationItem
&item
) : RefPointer
<AuthItem
>(new AuthItem(item
)) {}
211 AuthItemRef::AuthItemRef(AuthorizationString name
) : RefPointer
<AuthItem
>(new AuthItem(name
)) {}
213 AuthItemRef::AuthItemRef(AuthorizationString name
, AuthorizationValue value
, AuthorizationFlags flags
) : RefPointer
<AuthItem
>(new AuthItem(name
, value
, flags
)) {}
219 AuthItemSet::AuthItemSet()
223 AuthItemSet::~AuthItemSet()
228 AuthItemSet::operator = (const AuthorizationItemSet
& itemSet
)
232 for (unsigned int i
=0; i
< itemSet
.count
; i
++)
233 insert(AuthItemRef(itemSet
.items
[i
]));
239 AuthItemSet::operator=(const AuthItemSet
& itemSet
)
241 std::set
<AuthItemRef
>::operator=(itemSet
);
246 AuthItemSet::AuthItemSet(const AuthorizationItemSet
*itemSet
)
248 if (NULL
!= itemSet
&& NULL
!= itemSet
->items
)
250 for (unsigned int i
=0; i
< itemSet
->count
; i
++)
251 insert(AuthItemRef(itemSet
->items
[i
]));
255 AuthItemSet::AuthItemSet(const AuthItemSet
& itemSet
)
256 : std::set
<AuthItemRef
>(itemSet
)
261 AuthItemSet::find(const char *name
)
263 AuthItemSet::const_iterator found
= find_if(this->begin(), this->end(), FindAuthItemByRightName(name
) );
264 if (found
!= this->end())
270 } // end namespace Authorization