/src/nss/lib/smime/cmsmessage.c
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | /* |
6 | | * CMS message methods. |
7 | | */ |
8 | | |
9 | | #include "cmslocal.h" |
10 | | |
11 | | #include "cert.h" |
12 | | #include "secasn1.h" |
13 | | #include "secitem.h" |
14 | | #include "secoid.h" |
15 | | #include "pk11func.h" |
16 | | #include "secerr.h" |
17 | | |
18 | | /* |
19 | | * NSS_CMSMessage_Create - create a CMS message object |
20 | | * |
21 | | * "poolp" - arena to allocate memory from, or NULL if new arena should be created |
22 | | */ |
23 | | NSSCMSMessage * |
24 | | NSS_CMSMessage_Create(PLArenaPool *poolp) |
25 | 16.2k | { |
26 | 16.2k | void *mark = NULL; |
27 | 16.2k | NSSCMSMessage *cmsg; |
28 | 16.2k | PRBool poolp_is_ours = PR_FALSE; |
29 | | |
30 | 16.2k | if (poolp == NULL) { |
31 | 16.2k | poolp = PORT_NewArena(1024); /* XXX what is right value? */ |
32 | 16.2k | if (poolp == NULL) { |
33 | 0 | return NULL; |
34 | 0 | } |
35 | 16.2k | poolp_is_ours = PR_TRUE; |
36 | 16.2k | } |
37 | | |
38 | 16.2k | if (!poolp_is_ours) |
39 | 0 | mark = PORT_ArenaMark(poolp); |
40 | | |
41 | 16.2k | cmsg = (NSSCMSMessage *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSMessage)); |
42 | 16.2k | if (cmsg == NULL || |
43 | 16.2k | NSS_CMSContentInfo_Private_Init(&(cmsg->contentInfo)) != SECSuccess) { |
44 | 0 | if (!poolp_is_ours) { |
45 | 0 | if (mark) { |
46 | 0 | PORT_ArenaRelease(poolp, mark); |
47 | 0 | } |
48 | 0 | } else { |
49 | 0 | PORT_FreeArena(poolp, PR_FALSE); |
50 | 0 | } |
51 | 0 | return NULL; |
52 | 0 | } |
53 | | |
54 | 16.2k | cmsg->poolp = poolp; |
55 | 16.2k | cmsg->poolp_is_ours = poolp_is_ours; |
56 | 16.2k | cmsg->refCount = 1; |
57 | | |
58 | 16.2k | if (mark) { |
59 | 0 | PORT_ArenaUnmark(poolp, mark); |
60 | 0 | } |
61 | | |
62 | 16.2k | return cmsg; |
63 | 16.2k | } |
64 | | |
65 | | /* |
66 | | * NSS_CMSMessage_SetEncodingParams - set up a CMS message object for encoding or decoding |
67 | | * |
68 | | * "cmsg" - message object |
69 | | * "pwfn", pwfn_arg" - callback function for getting token password |
70 | | * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData |
71 | | * "detached_digestalgs", "detached_digests" - digests from detached content |
72 | | */ |
73 | | void |
74 | | NSS_CMSMessage_SetEncodingParams(NSSCMSMessage *cmsg, |
75 | | PK11PasswordFunc pwfn, void *pwfn_arg, |
76 | | NSSCMSGetDecryptKeyCallback decrypt_key_cb, void *decrypt_key_cb_arg, |
77 | | SECAlgorithmID **detached_digestalgs, SECItem **detached_digests) |
78 | 16.2k | { |
79 | 16.2k | if (cmsg == NULL) { |
80 | 0 | return; |
81 | 0 | } |
82 | 16.2k | if (pwfn) { |
83 | 0 | PK11_SetPasswordFunc(pwfn); |
84 | 0 | } |
85 | | |
86 | 16.2k | cmsg->pwfn_arg = pwfn_arg; |
87 | 16.2k | cmsg->decrypt_key_cb = decrypt_key_cb; |
88 | 16.2k | cmsg->decrypt_key_cb_arg = decrypt_key_cb_arg; |
89 | 16.2k | cmsg->detached_digestalgs = detached_digestalgs; |
90 | 16.2k | cmsg->detached_digests = detached_digests; |
91 | 16.2k | } |
92 | | |
93 | | /* |
94 | | * NSS_CMSMessage_Destroy - destroy a CMS message and all of its sub-pieces. |
95 | | */ |
96 | | void |
97 | | NSS_CMSMessage_Destroy(NSSCMSMessage *cmsg) |
98 | 19.4k | { |
99 | 19.4k | if (cmsg == NULL) |
100 | 0 | return; |
101 | | |
102 | 19.4k | PORT_Assert(cmsg->refCount > 0); |
103 | 19.4k | if (cmsg->refCount <= 0) { /* oops */ |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | 19.4k | cmsg->refCount--; /* thread safety? */ |
108 | 19.4k | if (cmsg->refCount > 0) { |
109 | 3.22k | return; |
110 | 3.22k | } |
111 | | |
112 | 16.2k | NSS_CMSContentInfo_Destroy(&(cmsg->contentInfo)); |
113 | | |
114 | | /* if poolp is not NULL, cmsg is the owner of its arena */ |
115 | 16.2k | if (cmsg->poolp_is_ours) { |
116 | 16.2k | PORT_FreeArena(cmsg->poolp, PR_FALSE); /* XXX clear it? */ |
117 | 16.2k | } |
118 | 16.2k | } |
119 | | |
120 | | /* |
121 | | * NSS_CMSMessage_Copy - return a copy of the given message. |
122 | | * |
123 | | * The copy may be virtual or may be real -- either way, the result needs |
124 | | * to be passed to NSS_CMSMessage_Destroy later (as does the original). |
125 | | */ |
126 | | NSSCMSMessage * |
127 | | NSS_CMSMessage_Copy(NSSCMSMessage *cmsg) |
128 | 8.10k | { |
129 | 8.10k | if (cmsg == NULL) { |
130 | 4.88k | return NULL; |
131 | 4.88k | } |
132 | | |
133 | 3.22k | PORT_Assert(cmsg->refCount > 0); |
134 | | |
135 | 3.22k | cmsg->refCount++; /* XXX chrisk thread safety? */ |
136 | 3.22k | return cmsg; |
137 | 8.10k | } |
138 | | |
139 | | /* |
140 | | * NSS_CMSMessage_GetContentInfo - return a pointer to the top level contentInfo |
141 | | */ |
142 | | NSSCMSContentInfo * |
143 | | NSS_CMSMessage_GetContentInfo(NSSCMSMessage *cmsg) |
144 | 11.3k | { |
145 | 11.3k | if (cmsg == NULL) { |
146 | 4.88k | return NULL; |
147 | 4.88k | } |
148 | | |
149 | 6.44k | return &(cmsg->contentInfo); |
150 | 11.3k | } |
151 | | |
152 | | /* |
153 | | * Return a pointer to the actual content. |
154 | | * In the case of those types which are encrypted, this returns the *plain* content. |
155 | | * In case of nested contentInfos, this descends and retrieves the innermost content. |
156 | | */ |
157 | | SECItem * |
158 | | NSS_CMSMessage_GetContent(NSSCMSMessage *cmsg) |
159 | 8.10k | { |
160 | 8.10k | if (cmsg == NULL) { |
161 | 4.88k | return NULL; |
162 | 4.88k | } |
163 | | |
164 | | /* this is a shortcut */ |
165 | 3.22k | NSSCMSContentInfo *cinfo = NSS_CMSMessage_GetContentInfo(cmsg); |
166 | 3.22k | SECItem *pItem = NSS_CMSContentInfo_GetInnerContent(cinfo); |
167 | 3.22k | return pItem; |
168 | 8.10k | } |
169 | | |
170 | | /* |
171 | | * NSS_CMSMessage_ContentLevelCount - count number of levels of CMS content objects in this message |
172 | | * |
173 | | * CMS data content objects do not count. |
174 | | */ |
175 | | int |
176 | | NSS_CMSMessage_ContentLevelCount(NSSCMSMessage *cmsg) |
177 | 8.10k | { |
178 | 8.10k | int count = 0; |
179 | 8.10k | NSSCMSContentInfo *cinfo; |
180 | | |
181 | 8.10k | if (cmsg == NULL) { |
182 | 4.88k | return 0; |
183 | 4.88k | } |
184 | | |
185 | | /* walk down the chain of contentinfos */ |
186 | 9.75k | for (cinfo = &(cmsg->contentInfo); cinfo != NULL;) { |
187 | 6.53k | count++; |
188 | 6.53k | cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo); |
189 | 6.53k | } |
190 | 3.22k | return count; |
191 | 8.10k | } |
192 | | |
193 | | /* |
194 | | * NSS_CMSMessage_ContentLevel - find content level #n |
195 | | * |
196 | | * CMS data content objects do not count. |
197 | | */ |
198 | | NSSCMSContentInfo * |
199 | | NSS_CMSMessage_ContentLevel(NSSCMSMessage *cmsg, int n) |
200 | 6.53k | { |
201 | 6.53k | int count = 0; |
202 | 6.53k | NSSCMSContentInfo *cinfo; |
203 | | |
204 | 6.53k | if (cmsg == NULL) { |
205 | 0 | return NULL; |
206 | 0 | } |
207 | | |
208 | | /* walk down the chain of contentinfos */ |
209 | 10.9k | for (cinfo = &(cmsg->contentInfo); cinfo != NULL && count < n; |
210 | 6.53k | cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo)) { |
211 | 4.37k | count++; |
212 | 4.37k | } |
213 | | |
214 | 6.53k | return cinfo; |
215 | 6.53k | } |
216 | | |
217 | | /* |
218 | | * NSS_CMSMessage_IsEncrypted - see if message contains a encrypted submessage |
219 | | */ |
220 | | PRBool |
221 | | NSS_CMSMessage_IsEncrypted(NSSCMSMessage *cmsg) |
222 | 8.10k | { |
223 | 8.10k | NSSCMSContentInfo *cinfo; |
224 | | |
225 | 8.10k | if (cmsg == NULL) { |
226 | 4.88k | return PR_FALSE; |
227 | 4.88k | } |
228 | | |
229 | | /* walk down the chain of contentinfos */ |
230 | 9.69k | for (cinfo = &(cmsg->contentInfo); cinfo != NULL; |
231 | 6.50k | cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo)) { |
232 | 6.50k | switch (NSS_CMSContentInfo_GetContentTypeTag(cinfo)) { |
233 | 24 | case SEC_OID_PKCS7_ENVELOPED_DATA: |
234 | 26 | case SEC_OID_CMS_AUTH_ENVELOPED_DATA: |
235 | 31 | case SEC_OID_PKCS7_ENCRYPTED_DATA: |
236 | 31 | return PR_TRUE; |
237 | 6.47k | default: |
238 | | /* callback here for generic wrappers? */ |
239 | 6.47k | break; |
240 | 6.50k | } |
241 | 6.50k | } |
242 | 3.19k | return PR_FALSE; |
243 | 3.22k | } |
244 | | |
245 | | /* |
246 | | * NSS_CMSMessage_IsSigned - see if message contains a signed submessage |
247 | | * |
248 | | * If the CMS message has a SignedData with a signature (not just a SignedData) |
249 | | * return true; false otherwise. This can/should be called before calling |
250 | | * VerifySignature, which will always indicate failure if no signature is |
251 | | * present, but that does not mean there even was a signature! |
252 | | * Note that the content itself can be empty (detached content was sent |
253 | | * another way); it is the presence of the signature that matters. |
254 | | */ |
255 | | PRBool |
256 | | NSS_CMSMessage_IsSigned(NSSCMSMessage *cmsg) |
257 | 8.10k | { |
258 | 8.10k | NSSCMSContentInfo *cinfo; |
259 | | |
260 | 8.10k | if (cmsg == NULL) { |
261 | 4.88k | return PR_FALSE; |
262 | 4.88k | } |
263 | | |
264 | | /* walk down the chain of contentinfos */ |
265 | 8.11k | for (cinfo = &(cmsg->contentInfo); cinfo != NULL; |
266 | 5.72k | cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo)) { |
267 | 5.72k | switch (NSS_CMSContentInfo_GetContentTypeTag(cinfo)) { |
268 | 3.19k | case SEC_OID_PKCS7_SIGNED_DATA: |
269 | 3.19k | if (cinfo->content.signedData == NULL) { |
270 | 13 | return PR_FALSE; |
271 | 13 | } |
272 | 3.18k | if (!NSS_CMSArray_IsEmpty((void **)cinfo->content.signedData->signerInfos)) { |
273 | 811 | return PR_TRUE; |
274 | 811 | } |
275 | 2.37k | break; |
276 | 2.52k | default: |
277 | | /* callback here for generic wrappers? */ |
278 | 2.52k | break; |
279 | 5.72k | } |
280 | 5.72k | } |
281 | 2.39k | return PR_FALSE; |
282 | 3.22k | } |