1 /* -*- Mode: C; tab-width: 8 -*-*/
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape security libraries.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1994-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
45 CRMF_CertReqMsgGetPOPType(CRMFCertReqMsg
*inCertReqMsg
)
47 PORT_Assert(inCertReqMsg
!= NULL
);
48 if (inCertReqMsg
!= NULL
&& inCertReqMsg
->pop
!= NULL
) {
49 return inCertReqMsg
->pop
->popUsed
;
51 return crmfNoPOPChoice
;
55 crmf_destroy_validity(CRMFOptionalValidity
*inValidity
, PRBool freeit
)
57 if (inValidity
!= NULL
){
58 if (inValidity
->notBefore
.data
!= NULL
) {
59 PORT_Free(inValidity
->notBefore
.data
);
61 if (inValidity
->notAfter
.data
!= NULL
) {
62 PORT_Free(inValidity
->notAfter
.data
);
65 PORT_Free(inValidity
);
72 crmf_copy_cert_request_validity(PRArenaPool
*poolp
,
73 CRMFOptionalValidity
**destValidity
,
74 CRMFOptionalValidity
*srcValidity
)
76 CRMFOptionalValidity
*myValidity
= NULL
;
79 *destValidity
= myValidity
= (poolp
== NULL
) ?
80 PORT_ZNew(CRMFOptionalValidity
) :
81 PORT_ArenaZNew(poolp
, CRMFOptionalValidity
);
82 if (myValidity
== NULL
) {
85 if (srcValidity
->notBefore
.data
!= NULL
) {
86 rv
= SECITEM_CopyItem(poolp
, &myValidity
->notBefore
,
87 &srcValidity
->notBefore
);
88 if (rv
!= SECSuccess
) {
92 if (srcValidity
->notAfter
.data
!= NULL
) {
93 rv
= SECITEM_CopyItem(poolp
, &myValidity
->notAfter
,
94 &srcValidity
->notAfter
);
95 if (rv
!= SECSuccess
) {
101 if (myValidity
!= NULL
&& poolp
== NULL
) {
102 crmf_destroy_validity(myValidity
, PR_TRUE
);
108 crmf_copy_extensions(PRArenaPool
*poolp
,
109 CRMFCertTemplate
*destTemplate
,
110 CRMFCertExtension
**srcExt
)
113 CRMFCertExtension
**myExtArray
= NULL
;
115 while (srcExt
[numExt
] != NULL
) {
119 /*No extensions to copy.*/
120 destTemplate
->extensions
= NULL
;
121 destTemplate
->numExtensions
= 0;
124 destTemplate
->extensions
= myExtArray
=
125 PORT_NewArray(CRMFCertExtension
*, numExt
+1);
126 if (myExtArray
== NULL
) {
130 for (i
=0; i
<numExt
; i
++) {
131 myExtArray
[i
] = crmf_copy_cert_extension(poolp
, srcExt
[i
]);
132 if (myExtArray
[i
] == NULL
) {
136 destTemplate
->numExtensions
= numExt
;
137 myExtArray
[numExt
] = NULL
;
140 if (myExtArray
!= NULL
) {
142 for (i
=0; myExtArray
[i
] != NULL
; i
++) {
143 CRMF_DestroyCertExtension(myExtArray
[i
]);
146 PORT_Free(myExtArray
);
148 destTemplate
->extensions
= NULL
;
149 destTemplate
->numExtensions
= 0;
154 crmf_copy_cert_request_template(PRArenaPool
*poolp
,
155 CRMFCertTemplate
*destTemplate
,
156 CRMFCertTemplate
*srcTemplate
)
160 if (srcTemplate
->version
.data
!= NULL
) {
161 rv
= SECITEM_CopyItem(poolp
, &destTemplate
->version
,
162 &srcTemplate
->version
);
163 if (rv
!= SECSuccess
) {
167 if (srcTemplate
->serialNumber
.data
!= NULL
) {
168 rv
= SECITEM_CopyItem(poolp
, &destTemplate
->serialNumber
,
169 &srcTemplate
->serialNumber
);
170 if (rv
!= SECSuccess
) {
174 if (srcTemplate
->signingAlg
!= NULL
) {
175 rv
= crmf_template_copy_secalg(poolp
, &destTemplate
->signingAlg
,
176 srcTemplate
->signingAlg
);
177 if (rv
!= SECSuccess
) {
181 if (srcTemplate
->issuer
!= NULL
) {
182 rv
= crmf_copy_cert_name(poolp
, &destTemplate
->issuer
,
183 srcTemplate
->issuer
);
184 if (rv
!= SECSuccess
) {
188 if (srcTemplate
->validity
!= NULL
) {
189 rv
= crmf_copy_cert_request_validity(poolp
, &destTemplate
->validity
,
190 srcTemplate
->validity
);
191 if (rv
!= SECSuccess
) {
195 if (srcTemplate
->subject
!= NULL
) {
196 rv
= crmf_copy_cert_name(poolp
, &destTemplate
->subject
,
197 srcTemplate
->subject
);
198 if (rv
!= SECSuccess
) {
202 if (srcTemplate
->publicKey
!= NULL
) {
203 rv
= crmf_template_add_public_key(poolp
, &destTemplate
->publicKey
,
204 srcTemplate
->publicKey
);
205 if (rv
!= SECSuccess
) {
209 if (srcTemplate
->issuerUID
.data
!= NULL
) {
210 rv
= crmf_make_bitstring_copy(poolp
, &destTemplate
->issuerUID
,
211 &srcTemplate
->issuerUID
);
212 if (rv
!= SECSuccess
) {
216 if (srcTemplate
->subjectUID
.data
!= NULL
) {
217 rv
= crmf_make_bitstring_copy(poolp
, &destTemplate
->subjectUID
,
218 &srcTemplate
->subjectUID
);
219 if (rv
!= SECSuccess
) {
223 if (srcTemplate
->extensions
!= NULL
) {
224 rv
= crmf_copy_extensions(poolp
, destTemplate
,
225 srcTemplate
->extensions
);
226 if (rv
!= SECSuccess
) {
236 crmf_copy_control(PRArenaPool
*poolp
, CRMFControl
*srcControl
)
238 CRMFControl
*newControl
;
241 newControl
= (poolp
== NULL
) ? PORT_ZNew(CRMFControl
) :
242 PORT_ArenaZNew(poolp
, CRMFControl
);
243 if (newControl
== NULL
) {
246 newControl
->tag
= srcControl
->tag
;
247 rv
= SECITEM_CopyItem(poolp
, &newControl
->derTag
, &srcControl
->derTag
);
248 if (rv
!= SECSuccess
) {
251 rv
= SECITEM_CopyItem(poolp
, &newControl
->derValue
, &srcControl
->derValue
);
252 if (rv
!= SECSuccess
) {
255 /* We only handle PKIArchiveOptions Control right now. But if in
256 * the future, more controls that are part of the union are added,
257 * then they need to be handled here as well.
259 switch (newControl
->tag
) {
260 case SEC_OID_PKIX_REGCTRL_PKI_ARCH_OPTIONS
:
261 rv
= crmf_copy_pkiarchiveoptions(poolp
,
262 &newControl
->value
.archiveOptions
,
263 &srcControl
->value
.archiveOptions
);
268 if (rv
!= SECSuccess
) {
274 if (poolp
== NULL
&& newControl
!= NULL
) {
275 CRMF_DestroyControl(newControl
);
281 crmf_copy_cert_request_controls(PRArenaPool
*poolp
,
282 CRMFCertRequest
*destReq
,
283 CRMFCertRequest
*srcReq
)
286 CRMFControl
**myControls
= NULL
;
288 numControls
= CRMF_CertRequestGetNumControls(srcReq
);
289 if (numControls
== 0) {
290 /* No Controls To Copy*/
293 myControls
= destReq
->controls
= PORT_NewArray(CRMFControl
*,
295 if (myControls
== NULL
) {
298 for (i
=0; i
<numControls
; i
++) {
299 myControls
[i
] = crmf_copy_control(poolp
, srcReq
->controls
[i
]);
300 if (myControls
[i
] == NULL
) {
304 myControls
[numControls
] = NULL
;
307 if (myControls
!= NULL
) {
309 for (i
=0; myControls
[i
] != NULL
; i
++) {
310 CRMF_DestroyControl(myControls
[i
]);
313 PORT_Free(myControls
);
320 crmf_copy_cert_request(PRArenaPool
*poolp
, CRMFCertRequest
*srcReq
)
322 CRMFCertRequest
*newReq
= NULL
;
325 if (srcReq
== NULL
) {
328 newReq
= (poolp
== NULL
) ? PORT_ZNew(CRMFCertRequest
) :
329 PORT_ArenaZNew(poolp
, CRMFCertRequest
);
330 if (newReq
== NULL
) {
333 rv
= SECITEM_CopyItem(poolp
, &newReq
->certReqId
, &srcReq
->certReqId
);
334 if (rv
!= SECSuccess
) {
337 rv
= crmf_copy_cert_request_template(poolp
, &newReq
->certTemplate
,
338 &srcReq
->certTemplate
);
339 if (rv
!= SECSuccess
) {
342 rv
= crmf_copy_cert_request_controls(poolp
, newReq
, srcReq
);
343 if (rv
!= SECSuccess
) {
348 if (newReq
!= NULL
&& poolp
== NULL
) {
349 CRMF_DestroyCertRequest(newReq
);
356 CRMF_DestroyGetValidity(CRMFGetValidity
*inValidity
)
358 PORT_Assert(inValidity
!= NULL
);
359 if (inValidity
!= NULL
) {
360 if (inValidity
->notAfter
) {
361 PORT_Free(inValidity
->notAfter
);
362 inValidity
->notAfter
= NULL
;
364 if (inValidity
->notBefore
) {
365 PORT_Free(inValidity
->notBefore
);
366 inValidity
->notBefore
= NULL
;
373 crmf_make_bitstring_copy(PRArenaPool
*arena
, SECItem
*dest
, SECItem
*src
)
379 origLenBits
= src
->len
;
380 bytesToCopy
= CRMF_BITS_TO_BYTES(origLenBits
);
381 src
->len
= bytesToCopy
;
382 rv
= SECITEM_CopyItem(arena
, dest
, src
);
383 src
->len
= origLenBits
;
384 if (rv
!= SECSuccess
) {
387 dest
->len
= origLenBits
;
392 CRMF_CertRequestGetNumberOfExtensions(CRMFCertRequest
*inCertReq
)
394 CRMFCertTemplate
*certTemplate
;
397 certTemplate
= &inCertReq
->certTemplate
;
398 if (certTemplate
->extensions
) {
399 while (certTemplate
->extensions
[count
] != NULL
)
406 CRMF_CertExtensionGetOidTag(CRMFCertExtension
*inExtension
)
408 PORT_Assert(inExtension
!= NULL
);
409 if (inExtension
== NULL
) {
410 return SEC_OID_UNKNOWN
;
412 return SECOID_FindOIDTag(&inExtension
->id
);
416 CRMF_CertExtensionGetIsCritical(CRMFCertExtension
*inExt
)
418 PORT_Assert(inExt
!= NULL
);
422 return inExt
->critical
.data
!= NULL
;
426 CRMF_CertExtensionGetValue(CRMFCertExtension
*inExtension
)
428 PORT_Assert(inExtension
!= NULL
);
429 if (inExtension
== NULL
) {
433 return SECITEM_DupItem(&inExtension
->value
);
438 CRMF_DestroyPOPOSigningKey(CRMFPOPOSigningKey
*inKey
)
440 PORT_Assert(inKey
!= NULL
);
442 if (inKey
->derInput
.data
!= NULL
) {
443 SECITEM_FreeItem(&inKey
->derInput
, PR_FALSE
);
445 if (inKey
->algorithmIdentifier
!= NULL
) {
446 SECOID_DestroyAlgorithmID(inKey
->algorithmIdentifier
, PR_TRUE
);
448 if (inKey
->signature
.data
!= NULL
) {
449 SECITEM_FreeItem(&inKey
->signature
, PR_FALSE
);
457 CRMF_DestroyPOPOPrivKey(CRMFPOPOPrivKey
*inPrivKey
)
459 PORT_Assert(inPrivKey
!= NULL
);
460 if (inPrivKey
!= NULL
) {
461 SECITEM_FreeItem(&inPrivKey
->message
.thisMessage
, PR_FALSE
);
462 PORT_Free(inPrivKey
);
468 CRMF_CertRequestGetNumControls(CRMFCertRequest
*inCertReq
)
472 PORT_Assert(inCertReq
!= NULL
);
473 if (inCertReq
== NULL
) {
476 if (inCertReq
->controls
) {
477 while (inCertReq
->controls
[count
] != NULL
)