]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSAccountDer.c
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSAccountDer.c
1 /*
2 * Copyright (c) 2013-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 #include "SOSAccountPriv.h"
26
27 //
28 // DER Encoding utilities
29 //
30
31 static const uint8_t* ccder_decode_null(const uint8_t* der, const uint8_t *der_end)
32 {
33 if (NULL == der)
34 return NULL;
35
36 size_t payload_size = 0;
37 const uint8_t *payload = ccder_decode_tl(CCDER_NULL, &payload_size, der, der_end);
38
39 if (NULL == payload || payload_size != 0) {
40 return NULL;
41 }
42
43 return payload + payload_size;
44 }
45
46
47 static size_t ccder_sizeof_null(void)
48 {
49 return ccder_sizeof(CCDER_NULL, 0);
50 }
51
52
53 static uint8_t* ccder_encode_null(const uint8_t *der, uint8_t *der_end)
54 {
55 return ccder_encode_tl(CCDER_NULL, 0, der, der_end);
56 }
57
58
59 //
60 // Encodes data or a zero length data
61 //
62 size_t der_sizeof_data_or_null(CFDataRef data, CFErrorRef* error)
63 {
64 if (data) {
65 return der_sizeof_data(data, error);
66 } else {
67 return der_sizeof_null(kCFNull, error);
68 }
69 }
70
71 uint8_t* der_encode_data_or_null(CFDataRef data, CFErrorRef* error, const uint8_t* der, uint8_t* der_end)
72 {
73 if (data) {
74 return der_encode_data(data, error, der, der_end);
75 } else {
76 return der_encode_null(kCFNull, error, der, der_end);
77 }
78 }
79
80
81 const uint8_t* der_decode_data_or_null(CFAllocatorRef allocator, CFDataRef* data,
82 CFErrorRef* error,
83 const uint8_t* der, const uint8_t* der_end)
84 {
85 CFTypeRef value = NULL;
86 der = der_decode_plist(allocator, 0, &value, error, der, der_end);
87 if (value && CFGetTypeID(value) != CFDataGetTypeID()) {
88 CFReleaseNull(value);
89 }
90 if (data) {
91 *data = value;
92 }
93 return der;
94 }
95
96 //
97 // Encodes data or a zero length data
98 //
99 size_t der_sizeof_fullpeer_or_null(SOSFullPeerInfoRef full_peer, CFErrorRef* error)
100 {
101 if (full_peer) {
102 return SOSFullPeerInfoGetDEREncodedSize(full_peer, error);
103 } else {
104 return ccder_sizeof_null();
105 }
106 }
107
108 uint8_t* der_encode_fullpeer_or_null(SOSFullPeerInfoRef full_peer, CFErrorRef* error, const uint8_t* der, uint8_t* der_end)
109 {
110 if (full_peer) {
111 return SOSFullPeerInfoEncodeToDER(full_peer, error, der, der_end);
112 } else {
113 return ccder_encode_null(der, der_end);
114 }
115 }
116
117
118 const uint8_t* der_decode_fullpeer_or_null(CFAllocatorRef allocator, SOSFullPeerInfoRef* full_peer,
119 CFErrorRef* error,
120 const uint8_t* der, const uint8_t* der_end)
121 {
122 ccder_tag tag;
123
124 require_action_quiet(ccder_decode_tag(&tag, der, der_end), fail, der = NULL);
125
126 require_action_quiet(full_peer, fail, der = NULL);
127
128 if (tag == CCDER_NULL) {
129 der = ccder_decode_null(der, der_end);
130 } else {
131 *full_peer = SOSFullPeerInfoCreateFromDER(kCFAllocatorDefault, error, &der, der_end);
132 }
133
134 fail:
135 return der;
136 }
137
138
139 //
140 // Mark: public_bytes encode/decode
141 //
142
143 size_t der_sizeof_public_bytes(SecKeyRef publicKey, CFErrorRef* error)
144 {
145 CFDataRef publicData = NULL;
146
147 if (publicKey)
148 SecKeyCopyPublicBytes(publicKey, &publicData);
149
150 size_t size = der_sizeof_data_or_null(publicData, error);
151
152 CFReleaseNull(publicData);
153
154 return size;
155 }
156
157 uint8_t* der_encode_public_bytes(SecKeyRef publicKey, CFErrorRef* error, const uint8_t* der, uint8_t* der_end)
158 {
159 CFDataRef publicData = NULL;
160
161 if (publicKey)
162 SecKeyCopyPublicBytes(publicKey, &publicData);
163
164 uint8_t *result = der_encode_data_or_null(publicData, error, der, der_end);
165
166 CFReleaseNull(publicData);
167
168 return result;
169 }
170
171 const uint8_t* der_decode_public_bytes(CFAllocatorRef allocator, CFIndex algorithmID, SecKeyRef* publicKey, CFErrorRef* error, const uint8_t* der, const uint8_t* der_end)
172 {
173 CFDataRef dataFound = NULL;
174 der = der_decode_data_or_null(allocator, &dataFound, error, der, der_end);
175
176 if (der && dataFound && publicKey) {
177 *publicKey = SecKeyCreateFromPublicData(allocator, algorithmID, dataFound);
178 }
179 CFReleaseNull(dataFound);
180
181 return der;
182 }
183