2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
12 * The Original Code is the Netscape security libraries.
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
35 * CMS message methods.
38 #include <Security/SecCmsMessage.h>
40 #include <Security/SecCmsContentInfo.h>
41 #include <Security/SecCmsSignedData.h>
48 #include <security_asn1/secasn1.h>
49 #include <security_asn1/secerr.h>
52 * SecCmsMessageCreate - create a CMS message object
54 * "poolp" - arena to allocate memory from, or NULL if new arena should be created
57 SecCmsMessageCreate(SecArenaPoolRef pool
)
59 PLArenaPool
*poolp
= (PLArenaPool
*)pool
;
61 SecCmsMessageRef cmsg
= NULL
;
62 Boolean poolp_is_ours
= PR_FALSE
;
65 poolp
= PORT_NewArena (1024); /* XXX what is right value? */
68 poolp_is_ours
= PR_TRUE
;
72 mark
= PORT_ArenaMark(poolp
);
74 cmsg
= (SecCmsMessageRef
)PORT_ArenaZAlloc (poolp
, sizeof(SecCmsMessage
));
78 PORT_ArenaRelease(poolp
, mark
);
81 PORT_FreeArena(poolp
, PR_FALSE
);
86 cmsg
->poolp_is_ours
= poolp_is_ours
;
90 PORT_ArenaUnmark(poolp
, mark
);
96 * SecCmsMessageSetEncodingParams - set up a CMS message object for encoding or decoding
98 * "cmsg" - message object
99 * "pwfn", pwfn_arg" - callback function for getting token password
100 * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData
101 * "detached_digestalgs", "detached_digests" - digests from detached content
104 SecCmsMessageSetEncodingParams(SecCmsMessageRef cmsg
,
105 PK11PasswordFunc pwfn
, void *pwfn_arg
,
106 SecCmsGetDecryptKeyCallback decrypt_key_cb
, void *decrypt_key_cb_arg
,
107 SECAlgorithmID
**detached_digestalgs
, CSSM_DATA_PTR
*detached_digests
)
110 // @@@ Deal with password stuff.
112 PK11_SetPasswordFunc(pwfn
);
114 cmsg
->pwfn_arg
= pwfn_arg
;
115 cmsg
->decrypt_key_cb
= decrypt_key_cb
;
116 cmsg
->decrypt_key_cb_arg
= decrypt_key_cb_arg
;
117 cmsg
->detached_digestalgs
= detached_digestalgs
;
118 cmsg
->detached_digests
= detached_digests
;
122 * SecCmsMessageDestroy - destroy a CMS message and all of its sub-pieces.
125 SecCmsMessageDestroy(SecCmsMessageRef cmsg
)
127 PORT_Assert (cmsg
->refCount
> 0);
128 if (cmsg
->refCount
<= 0) /* oops */
131 cmsg
->refCount
--; /* thread safety? */
132 if (cmsg
->refCount
> 0)
135 SecCmsContentInfoDestroy(&(cmsg
->contentInfo
));
137 /* if poolp is not NULL, cmsg is the owner of its arena */
138 if (cmsg
->poolp_is_ours
&& cmsg
->poolp
) {
139 PORT_FreeArena (cmsg
->poolp
, PR_TRUE
);
144 * SecCmsMessageCopy - return a copy of the given message.
146 * The copy may be virtual or may be real -- either way, the result needs
147 * to be passed to SecCmsMessageDestroy later (as does the original).
150 SecCmsMessageCopy(SecCmsMessageRef cmsg
)
155 PORT_Assert (cmsg
->refCount
> 0);
157 cmsg
->refCount
++; /* XXX chrisk thread safety? */
162 * SecCmsMessageGetArena - return a pointer to the message's arena pool
165 SecCmsMessageGetArena(SecCmsMessageRef cmsg
)
167 return (SecArenaPoolRef
)cmsg
->poolp
;
171 * SecCmsMessageGetContentInfo - return a pointer to the top level contentInfo
174 SecCmsMessageGetContentInfo(SecCmsMessageRef cmsg
)
176 return &(cmsg
->contentInfo
);
180 * Return a pointer to the actual content.
181 * In the case of those types which are encrypted, this returns the *plain* content.
182 * In case of nested contentInfos, this descends and retrieves the innermost content.
185 SecCmsMessageGetContent(SecCmsMessageRef cmsg
)
187 /* this is a shortcut */
188 SecCmsContentInfoRef cinfo
= SecCmsMessageGetContentInfo(cmsg
);
189 CSSM_DATA_PTR pItem
= SecCmsContentInfoGetInnerContent(cinfo
);
194 * SecCmsMessageContentLevelCount - count number of levels of CMS content objects in this message
196 * CMS data content objects do not count.
199 SecCmsMessageContentLevelCount(SecCmsMessageRef cmsg
)
202 SecCmsContentInfoRef cinfo
;
204 /* walk down the chain of contentinfos */
205 for (cinfo
= &(cmsg
->contentInfo
); cinfo
!= NULL
; ) {
207 cinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
);
213 * SecCmsMessageContentLevel - find content level #n
215 * CMS data content objects do not count.
218 SecCmsMessageContentLevel(SecCmsMessageRef cmsg
, int n
)
221 SecCmsContentInfoRef cinfo
;
223 /* walk down the chain of contentinfos */
224 for (cinfo
= &(cmsg
->contentInfo
); cinfo
!= NULL
&& count
< n
; cinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
)) {
232 * SecCmsMessageContainsCertsOrCrls - see if message contains certs along the way
235 SecCmsMessageContainsCertsOrCrls(SecCmsMessageRef cmsg
)
237 SecCmsContentInfoRef cinfo
;
239 /* descend into CMS message */
240 for (cinfo
= &(cmsg
->contentInfo
); cinfo
!= NULL
; cinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
)) {
241 if (SecCmsContentInfoGetContentTypeTag(cinfo
) != SEC_OID_PKCS7_SIGNED_DATA
)
242 continue; /* next level */
244 if (SecCmsSignedDataContainsCertsOrCrls(cinfo
->content
.signedData
))
251 * SecCmsMessageIsEncrypted - see if message contains a encrypted submessage
254 SecCmsMessageIsEncrypted(SecCmsMessageRef cmsg
)
256 SecCmsContentInfoRef cinfo
;
258 /* walk down the chain of contentinfos */
259 for (cinfo
= &(cmsg
->contentInfo
); cinfo
!= NULL
; cinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
))
261 switch (SecCmsContentInfoGetContentTypeTag(cinfo
)) {
262 case SEC_OID_PKCS7_ENVELOPED_DATA
:
263 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
273 * SecCmsMessageIsSigned - see if message contains a signed submessage
275 * If the CMS message has a SignedData with a signature (not just a SignedData)
276 * return true; false otherwise. This can/should be called before calling
277 * VerifySignature, which will always indicate failure if no signature is
278 * present, but that does not mean there even was a signature!
279 * Note that the content itself can be empty (detached content was sent
280 * another way); it is the presence of the signature that matters.
283 SecCmsMessageIsSigned(SecCmsMessageRef cmsg
)
285 SecCmsContentInfoRef cinfo
;
287 /* walk down the chain of contentinfos */
288 for (cinfo
= &(cmsg
->contentInfo
); cinfo
!= NULL
; cinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
))
290 switch (SecCmsContentInfoGetContentTypeTag(cinfo
)) {
291 case SEC_OID_PKCS7_SIGNED_DATA
:
292 if (!SecCmsArrayIsEmpty((void **)cinfo
->content
.signedData
->signerInfos
))
303 * SecCmsMessageIsContentEmpty - see if content is empty
305 * returns PR_TRUE is innermost content length is < minLen
306 * XXX need the encrypted content length (why?)
309 SecCmsMessageIsContentEmpty(SecCmsMessageRef cmsg
, unsigned int minLen
)
311 CSSM_DATA_PTR item
= NULL
;
316 item
= SecCmsContentInfoGetContent(SecCmsMessageGetContentInfo(cmsg
));
320 } else if(item
->Length
<= minLen
) {
328 * SecCmsMessageContainsTSTInfo - see if message contains a TimeStamping info block
331 SecCmsMessageContainsTSTInfo(SecCmsMessageRef cmsg
)
333 SecCmsContentInfoRef cinfo
;
335 /* walk down the chain of contentinfos */
336 for (cinfo
= &(cmsg
->contentInfo
); cinfo
!= NULL
; cinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
))
338 switch (SecCmsContentInfoGetContentTypeTag(cinfo
))
340 case SEC_OID_PKCS9_ID_CT_TSTInfo
:
341 // TSTInfo is in cinfo->rawContent->Data
351 SecCmsMessageSetTSACallback(SecCmsMessageRef cmsg
, SecCmsTSACallback tsaCallback
)
354 cmsg
->tsaCallback
= tsaCallback
;
358 SecCmsMessageSetTSAContext(SecCmsMessageRef cmsg
, const void *tsaContext
) //CFTypeRef
361 cmsg
->tsaContext
= tsaContext
;