]> git.saurik.com Git - apple/security.git/blob - AppleX509CL/clNssUtils.h
Security-163.tar.gz
[apple/security.git] / AppleX509CL / clNssUtils.h
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18 /*
19 * clNssUtils.h - support for libnssasn1-based ASN1 encode/decode
20 */
21
22 #ifndef _CL_NSS_UTILS_H_
23 #define _CL_NSS_UTILS_H_
24
25 #include <SecurityNssAsn1/SecNssCoder.h>
26 #include <SecurityNssAsn1/certExtensionTemplates.h>
27 #include <Security/cssmalloc.h>
28 #include "DecodedCert.h"
29
30 /*
31 * A CssmAllocator which is actually based upon a PLArenaPool. This only
32 * mallocs, it doesn't have a free - all memory allocated with this
33 * object is freed when the SecNssCoder associated with this object is
34 * freed. It's used to malloc the fields in DecodedCert.mCert and
35 * DecodedCrl.mCrl.
36 */
37 class ArenaAllocator : public Security::CssmAllocator
38 {
39 NOCOPY(ArenaAllocator)
40 public:
41 ArenaAllocator(SecNssCoder &coder)
42 : mCoder(coder) { }
43 ~ArenaAllocator() { }
44 void *malloc(size_t) throw(std::bad_alloc) ;
45 void free(void *) throw() ;
46 void *realloc(void *, size_t) throw(std::bad_alloc);
47 private:
48 SecNssCoder &mCoder;
49 };
50
51 /*
52 * Misc. alloc/copy with arbitrary CssmAllocator
53 */
54
55 /* malloc d.Data, set d.Length */
56 void clAllocData(
57 CssmAllocator &alloc,
58 CSSM_DATA &dst,
59 size_t len);
60
61 /* malloc and copy */
62 void clAllocCopyData(
63 CssmAllocator &alloc,
64 const CSSM_DATA &src,
65 CSSM_DATA &dst);
66
67 /* return true if two CSSM_DATAs (or two CSSM_OIDs) compare equal */
68 bool clCompareCssmData(
69 const CSSM_DATA *data1,
70 const CSSM_DATA *data2);
71
72 /*
73 * CSSM_DATA --> uint32
74 */
75 uint32 clDataToInt(
76 const CSSM_DATA &cdata,
77 CSSM_RETURN toThrow = CSSMERR_CL_INVALID_CERT_POINTER);
78 void clIntToData(
79 uint32 num,
80 CSSM_DATA &cdata,
81 CssmAllocator &alloc);
82
83 /* CSSM_BOOL <--> CSSM_DATA */
84 CSSM_BOOL clNssBoolToCssm(
85 const CSSM_DATA &nssBool);
86 void clCssmBoolToNss(
87 CSSM_BOOL cBool,
88 CSSM_DATA &nssBool,
89 CssmAllocator &alloc);
90
91 /* Bit String */
92 void clCssmBitStringToNss(
93 CSSM_DATA &b);
94 void clNssBitStringToCssm(
95 CSSM_DATA &b);
96
97 /* How many items in a NULL-terminated array of pointers? */
98 unsigned clNssArraySize(
99 const void **array);
100
101 /* malloc a NULL-ed array of pointers of size num+1 */
102 void **clNssNullArray(
103 uint32 num,
104 SecNssCoder &coder);
105
106 CE_KeyUsage clBitStringToKeyUsage(
107 const CSSM_DATA &cdata);
108
109 CSSM_ALGORITHMS CL_oidToAlg(
110 const CSSM_OID &oid);
111
112 void CL_copyAlgId(
113 const CSSM_X509_ALGORITHM_IDENTIFIER &srcAlgId,
114 CSSM_X509_ALGORITHM_IDENTIFIER &destAlgId,
115 CssmAllocator &alloc);
116 void CL_freeCssmAlgId(
117 CSSM_X509_ALGORITHM_IDENTIFIER *cdsaObj, // optional
118 CssmAllocator &alloc);
119
120
121 bool CL_nssTimeToCssm(
122 const NSS_Time &derTime,
123 CSSM_X509_TIME &cssmObj,
124 CssmAllocator &alloc);
125 void CL_cssmTimeToNss(
126 const CSSM_X509_TIME &cssmTime,
127 NSS_Time &nssTime,
128 SecNssCoder &coder);
129 void CL_freeCssmTime(
130 CSSM_X509_TIME *cssmTime,
131 CssmAllocator &alloc);
132
133 void CL_nullAlgParams(
134 CSSM_X509_ALGORITHM_IDENTIFIER &algId);
135
136 void CL_copySubjPubKeyInfo(
137 const CSSM_X509_SUBJECT_PUBLIC_KEY_INFO &srcInfo,
138 bool srcInBits,
139 CSSM_X509_SUBJECT_PUBLIC_KEY_INFO &dstInfo,
140 bool dstInBits,
141 CssmAllocator &alloc);
142 CSSM_KEY_PTR CL_extractCSSMKeyNSS(
143 const CSSM_X509_SUBJECT_PUBLIC_KEY_INFO &keyInfo,
144 CssmAllocator &alloc,
145 const DecodedCert *decodedCert); // optional
146 void CL_CSSMKeyToSubjPubKeyInfoNSS(
147 const CSSM_KEY &cssmKey,
148 CSSM_X509_SUBJECT_PUBLIC_KEY_INFO &nssKeyInfo,
149 SecNssCoder &coder);
150 void CL_freeCSSMKey(
151 CSSM_KEY_PTR cssmKey,
152 CssmAllocator &alloc,
153 bool freeTop = true); // delete the actual key
154 // as well as contents
155
156 void CL_cssmAuthorityKeyIdToNss(
157 const CE_AuthorityKeyID &cdsaObj,
158 NSS_AuthorityKeyId &nssObj,
159 SecNssCoder &coder);
160 void CL_nssAuthorityKeyIdToCssm(
161 const NSS_AuthorityKeyId &nssObj,
162 CE_AuthorityKeyID &cdsaObj,
163 SecNssCoder &coder, // for temp decoding
164 CssmAllocator &alloc);
165
166 void CL_decodeDistributionPointName(
167 const CSSM_DATA &nssBlob,
168 CE_DistributionPointName &cssmDpn,
169 SecNssCoder &coder,
170 CssmAllocator &alloc);
171 void CL_encodeDistributionPointName(
172 CE_DistributionPointName &cpoint,
173 CSSM_DATA &npoint,
174 SecNssCoder &coder);
175 void CL_cssmDistPointsToNss(
176 const CE_CRLDistPointsSyntax &cdsaObj,
177 NSS_CRLDistributionPoints &nssObj,
178 SecNssCoder &coder);
179 void CL_nssDistPointsToCssm(
180 const NSS_CRLDistributionPoints &nssObj,
181 CE_CRLDistPointsSyntax &cdsaObj,
182 SecNssCoder &coder, // for temp decoding
183 CssmAllocator &alloc);
184
185 void CL_nssIssuingDistPointToCssm(
186 NSS_IssuingDistributionPoint *nssIdp,
187 CE_IssuingDistributionPoint *cssmIdp,
188 SecNssCoder &coder,
189 CssmAllocator &alloc);
190
191 void CL_certCrlDecodeComponents(
192 const CssmData &signedItem, // DER-encoded cert or CRL
193 CssmOwnedData &tbsBlob, // still DER-encoded
194 CssmOwnedData &algId, // ditto
195 CssmOwnedData &rawSig); // raw bits (not an encoded AsnBits)
196 void
197 CL_certEncodeComponents(
198 const CssmData &TBSCert, // DER-encoded
199 const CssmData &algId, // ditto
200 const CssmData &rawSig, // raw bits, not encoded
201 CssmOwnedData &signedCert); // DER-encoded
202
203 #endif /* _CL_NSS_UTILS_H_ */