2 * Copyright 2007 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
23 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/exception.h"
31 #include "crypt32_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
35 /* Called when a message's ref count reaches zero. Free any message-specific
38 typedef void (*CryptMsgCloseFunc
)(HCRYPTMSG msg
);
40 typedef BOOL (*CryptMsgGetParamFunc
)(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
41 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
);
43 typedef BOOL (*CryptMsgUpdateFunc
)(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
44 DWORD cbData
, BOOL fFinal
);
46 typedef BOOL (*CryptMsgControlFunc
)(HCRYPTMSG hCryptMsg
, DWORD dwFlags
,
47 DWORD dwCtrlType
, const void *pvCtrlPara
);
49 static BOOL
CRYPT_DefaultMsgControl(HCRYPTMSG hCryptMsg
, DWORD dwFlags
,
50 DWORD dwCtrlType
, const void *pvCtrlPara
)
52 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg
, dwFlags
, dwCtrlType
, pvCtrlPara
);
53 SetLastError(E_INVALIDARG
);
57 typedef enum _CryptMsgState
{
60 MsgStateDataFinalized
,
64 typedef struct _CryptMsgBase
69 CMSG_STREAM_INFO stream_info
;
71 CryptMsgCloseFunc close
;
72 CryptMsgUpdateFunc update
;
73 CryptMsgGetParamFunc get_param
;
74 CryptMsgControlFunc control
;
77 static inline void CryptMsgBase_Init(CryptMsgBase
*msg
, DWORD dwFlags
,
78 PCMSG_STREAM_INFO pStreamInfo
, CryptMsgCloseFunc close
,
79 CryptMsgGetParamFunc get_param
, CryptMsgUpdateFunc update
,
80 CryptMsgControlFunc control
)
83 msg
->open_flags
= dwFlags
;
87 msg
->stream_info
= *pStreamInfo
;
91 msg
->streamed
= FALSE
;
92 memset(&msg
->stream_info
, 0, sizeof(msg
->stream_info
));
95 msg
->get_param
= get_param
;
97 msg
->control
= control
;
98 msg
->state
= MsgStateInit
;
101 typedef struct _CDataEncodeMsg
104 DWORD bare_content_len
;
108 static const BYTE empty_data_content
[] = { 0x04,0x00 };
110 static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg
)
112 CDataEncodeMsg
*msg
= hCryptMsg
;
114 if (msg
->bare_content
!= empty_data_content
)
115 LocalFree(msg
->bare_content
);
118 static BOOL WINAPI
CRYPT_EncodeContentLength(DWORD dwCertEncodingType
,
119 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
120 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
122 DWORD dataLen
= *(DWORD
*)pvStructInfo
;
126 /* Trick: report bytes needed based on total message length, even though
127 * the message isn't available yet. The caller will use the length
128 * reported here to encode its length.
130 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
132 *pcbEncoded
= 1 + lenBytes
+ dataLen
;
135 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
136 pcbEncoded
, 1 + lenBytes
)))
138 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
139 pbEncoded
= *(BYTE
**)pbEncoded
;
140 *pbEncoded
++ = ASN_OCTETSTRING
;
141 CRYPT_EncodeLen(dataLen
, pbEncoded
,
148 static BOOL
CRYPT_EncodeDataContentInfoHeader(const CDataEncodeMsg
*msg
,
149 CRYPT_DATA_BLOB
*header
)
153 if (msg
->base
.streamed
&& msg
->base
.stream_info
.cbContent
== 0xffffffff)
155 static const BYTE headerValue
[] = { 0x30,0x80,0x06,0x09,0x2a,0x86,0x48,
156 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x80,0x24,0x80 };
158 header
->pbData
= LocalAlloc(0, sizeof(headerValue
));
161 header
->cbData
= sizeof(headerValue
);
162 memcpy(header
->pbData
, headerValue
, sizeof(headerValue
));
170 struct AsnConstructedItem constructed
= { 0,
171 &msg
->base
.stream_info
.cbContent
, CRYPT_EncodeContentLength
};
172 struct AsnEncodeSequenceItem items
[2] = {
173 { szOID_RSA_data
, CRYPT_AsnEncodeOid
, 0 },
174 { &constructed
, CRYPT_AsnEncodeConstructed
, 0 },
177 ret
= CRYPT_AsnEncodeSequence(X509_ASN_ENCODING
, items
,
178 ARRAY_SIZE(items
), CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
179 (LPBYTE
)&header
->pbData
, &header
->cbData
);
182 /* Trick: subtract the content length from the reported length,
183 * as the actual content hasn't come yet.
185 header
->cbData
-= msg
->base
.stream_info
.cbContent
;
191 static BOOL
CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
192 DWORD cbData
, BOOL fFinal
)
194 CDataEncodeMsg
*msg
= hCryptMsg
;
197 if (msg
->base
.state
== MsgStateFinalized
)
198 SetLastError(CRYPT_E_MSG_ERROR
);
199 else if (msg
->base
.streamed
)
203 if (msg
->base
.state
!= MsgStateUpdated
)
205 CRYPT_DATA_BLOB header
;
207 ret
= CRYPT_EncodeDataContentInfoHeader(msg
, &header
);
210 ret
= msg
->base
.stream_info
.pfnStreamOutput(
211 msg
->base
.stream_info
.pvArg
, header
.pbData
, header
.cbData
,
213 LocalFree(header
.pbData
);
216 /* Curiously, every indefinite-length streamed update appears to
217 * get its own tag and length, regardless of fFinal.
219 if (msg
->base
.stream_info
.cbContent
== 0xffffffff)
224 ret
= CRYPT_EncodeContentLength(X509_ASN_ENCODING
, NULL
,
225 &cbData
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, (BYTE
*)&header
,
229 ret
= msg
->base
.stream_info
.pfnStreamOutput(
230 msg
->base
.stream_info
.pvArg
, header
, headerLen
,
237 ret
= msg
->base
.stream_info
.pfnStreamOutput(
238 msg
->base
.stream_info
.pvArg
, (BYTE
*)pbData
, cbData
,
240 msg
->base
.state
= MsgStateUpdated
;
244 msg
->base
.state
= MsgStateFinalized
;
245 if (msg
->base
.stream_info
.cbContent
== 0xffffffff)
247 BYTE indefinite_trailer
[6] = { 0 };
249 ret
= msg
->base
.stream_info
.pfnStreamOutput(
250 msg
->base
.stream_info
.pvArg
, (BYTE
*)pbData
, cbData
,
253 ret
= msg
->base
.stream_info
.pfnStreamOutput(
254 msg
->base
.stream_info
.pvArg
, indefinite_trailer
,
255 sizeof(indefinite_trailer
), TRUE
);
258 ret
= msg
->base
.stream_info
.pfnStreamOutput(
259 msg
->base
.stream_info
.pvArg
, (BYTE
*)pbData
, cbData
, TRUE
);
264 SetLastError(STATUS_ACCESS_VIOLATION
);
273 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
274 SetLastError(E_INVALIDARG
);
276 SetLastError(CRYPT_E_MSG_ERROR
);
280 CRYPT_DATA_BLOB blob
= { cbData
, (LPBYTE
)pbData
};
282 msg
->base
.state
= MsgStateFinalized
;
283 /* non-streamed data messages don't allow non-final updates,
284 * don't bother checking whether data already exist, they can't.
286 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_OCTET_STRING
,
287 &blob
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &msg
->bare_content
,
288 &msg
->bare_content_len
);
294 static BOOL
CRYPT_CopyParam(void *pvData
, DWORD
*pcbData
, const void *src
,
301 else if (*pcbData
< len
)
304 SetLastError(ERROR_MORE_DATA
);
310 memcpy(pvData
, src
, len
);
315 static BOOL
CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
316 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
318 CDataEncodeMsg
*msg
= hCryptMsg
;
323 case CMSG_CONTENT_PARAM
:
324 if (msg
->base
.streamed
)
325 SetLastError(E_INVALIDARG
);
328 CRYPT_CONTENT_INFO info
;
329 char rsa_data
[] = "1.2.840.113549.1.7.1";
331 info
.pszObjId
= rsa_data
;
332 info
.Content
.cbData
= msg
->bare_content_len
;
333 info
.Content
.pbData
= msg
->bare_content
;
334 ret
= CryptEncodeObject(X509_ASN_ENCODING
, PKCS_CONTENT_INFO
, &info
,
338 case CMSG_BARE_CONTENT_PARAM
:
339 if (msg
->base
.streamed
)
340 SetLastError(E_INVALIDARG
);
342 ret
= CRYPT_CopyParam(pvData
, pcbData
, msg
->bare_content
,
343 msg
->bare_content_len
);
346 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
351 static HCRYPTMSG
CDataEncodeMsg_Open(DWORD dwFlags
, const void *pvMsgEncodeInfo
,
352 LPSTR pszInnerContentObjID
, PCMSG_STREAM_INFO pStreamInfo
)
358 SetLastError(E_INVALIDARG
);
361 msg
= CryptMemAlloc(sizeof(CDataEncodeMsg
));
364 CryptMsgBase_Init((CryptMsgBase
*)msg
, dwFlags
, pStreamInfo
,
365 CDataEncodeMsg_Close
, CDataEncodeMsg_GetParam
, CDataEncodeMsg_Update
,
366 CRYPT_DefaultMsgControl
);
367 msg
->bare_content_len
= sizeof(empty_data_content
);
368 msg
->bare_content
= (LPBYTE
)empty_data_content
;
373 typedef struct _CHashEncodeMsg
378 CRYPT_DATA_BLOB data
;
381 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg
)
383 CHashEncodeMsg
*msg
= hCryptMsg
;
385 CryptMemFree(msg
->data
.pbData
);
386 CryptDestroyHash(msg
->hash
);
387 if (msg
->base
.open_flags
& CMSG_CRYPT_RELEASE_CONTEXT_FLAG
)
388 CryptReleaseContext(msg
->prov
, 0);
391 static BOOL
CRYPT_EncodePKCSDigestedData(CHashEncodeMsg
*msg
, void *pvData
,
396 DWORD size
= sizeof(algID
);
398 ret
= CryptGetHashParam(msg
->hash
, HP_ALGID
, (BYTE
*)&algID
, &size
, 0);
401 CRYPT_DIGESTED_DATA digestedData
= { 0 };
402 char oid_rsa_data
[] = szOID_RSA_data
;
404 digestedData
.version
= CMSG_HASHED_DATA_PKCS_1_5_VERSION
;
405 digestedData
.DigestAlgorithm
.pszObjId
= (LPSTR
)CertAlgIdToOID(algID
);
406 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
407 /* Quirk: OID is only encoded messages if an update has happened */
408 if (msg
->base
.state
!= MsgStateInit
)
409 digestedData
.ContentInfo
.pszObjId
= oid_rsa_data
;
410 if (!(msg
->base
.open_flags
& CMSG_DETACHED_FLAG
) && msg
->data
.cbData
)
412 ret
= CRYPT_AsnEncodeOctets(0, NULL
, &msg
->data
,
413 CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
414 (LPBYTE
)&digestedData
.ContentInfo
.Content
.pbData
,
415 &digestedData
.ContentInfo
.Content
.cbData
);
417 if (msg
->base
.state
== MsgStateFinalized
)
419 size
= sizeof(DWORD
);
420 ret
= CryptGetHashParam(msg
->hash
, HP_HASHSIZE
,
421 (LPBYTE
)&digestedData
.hash
.cbData
, &size
, 0);
424 digestedData
.hash
.pbData
= CryptMemAlloc(
425 digestedData
.hash
.cbData
);
426 ret
= CryptGetHashParam(msg
->hash
, HP_HASHVAL
,
427 digestedData
.hash
.pbData
, &digestedData
.hash
.cbData
, 0);
431 ret
= CRYPT_AsnEncodePKCSDigestedData(&digestedData
, pvData
,
433 CryptMemFree(digestedData
.hash
.pbData
);
434 LocalFree(digestedData
.ContentInfo
.Content
.pbData
);
439 static BOOL
CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
440 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
442 CHashEncodeMsg
*msg
= hCryptMsg
;
445 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg
, dwParamType
, dwIndex
,
450 case CMSG_BARE_CONTENT_PARAM
:
451 if (msg
->base
.streamed
)
452 SetLastError(E_INVALIDARG
);
454 ret
= CRYPT_EncodePKCSDigestedData(msg
, pvData
, pcbData
);
456 case CMSG_CONTENT_PARAM
:
458 CRYPT_CONTENT_INFO info
;
460 ret
= CryptMsgGetParam(hCryptMsg
, CMSG_BARE_CONTENT_PARAM
, 0, NULL
,
461 &info
.Content
.cbData
);
464 info
.Content
.pbData
= CryptMemAlloc(info
.Content
.cbData
);
465 if (info
.Content
.pbData
)
467 ret
= CryptMsgGetParam(hCryptMsg
, CMSG_BARE_CONTENT_PARAM
, 0,
468 info
.Content
.pbData
, &info
.Content
.cbData
);
471 char oid_rsa_hashed
[] = szOID_RSA_hashedData
;
473 info
.pszObjId
= oid_rsa_hashed
;
474 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
,
475 PKCS_CONTENT_INFO
, &info
, 0, NULL
, pvData
, pcbData
);
477 CryptMemFree(info
.Content
.pbData
);
484 case CMSG_COMPUTED_HASH_PARAM
:
485 ret
= CryptGetHashParam(msg
->hash
, HP_HASHVAL
, pvData
, pcbData
, 0);
487 case CMSG_VERSION_PARAM
:
488 if (msg
->base
.state
!= MsgStateFinalized
)
489 SetLastError(CRYPT_E_MSG_ERROR
);
492 DWORD version
= CMSG_HASHED_DATA_PKCS_1_5_VERSION
;
494 /* Since the data are always encoded as octets, the version is
495 * always 0 (see rfc3852, section 7)
497 ret
= CRYPT_CopyParam(pvData
, pcbData
, &version
, sizeof(version
));
501 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
506 static BOOL
CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
507 DWORD cbData
, BOOL fFinal
)
509 CHashEncodeMsg
*msg
= hCryptMsg
;
512 TRACE("(%p, %p, %d, %d)\n", hCryptMsg
, pbData
, cbData
, fFinal
);
514 if (msg
->base
.state
== MsgStateFinalized
)
515 SetLastError(CRYPT_E_MSG_ERROR
);
516 else if (msg
->base
.streamed
|| (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
))
518 /* Doesn't do much, as stream output is never called, and you
519 * can't get the content.
521 ret
= CryptHashData(msg
->hash
, pbData
, cbData
, 0);
522 msg
->base
.state
= fFinal
? MsgStateFinalized
: MsgStateUpdated
;
527 SetLastError(CRYPT_E_MSG_ERROR
);
530 ret
= CryptHashData(msg
->hash
, pbData
, cbData
, 0);
533 msg
->data
.pbData
= CryptMemAlloc(cbData
);
534 if (msg
->data
.pbData
)
536 memcpy(msg
->data
.pbData
+ msg
->data
.cbData
, pbData
, cbData
);
537 msg
->data
.cbData
+= cbData
;
542 msg
->base
.state
= MsgStateFinalized
;
548 static HCRYPTMSG
CHashEncodeMsg_Open(DWORD dwFlags
, const void *pvMsgEncodeInfo
,
549 LPSTR pszInnerContentObjID
, PCMSG_STREAM_INFO pStreamInfo
)
552 const CMSG_HASHED_ENCODE_INFO
*info
= pvMsgEncodeInfo
;
556 if (info
->cbSize
!= sizeof(CMSG_HASHED_ENCODE_INFO
))
558 SetLastError(E_INVALIDARG
);
561 if (!(algID
= CertOIDToAlgId(info
->HashAlgorithm
.pszObjId
)))
563 SetLastError(CRYPT_E_UNKNOWN_ALGO
);
566 if (info
->hCryptProv
)
567 prov
= info
->hCryptProv
;
570 prov
= I_CryptGetDefaultCryptProv(algID
);
573 SetLastError(E_INVALIDARG
);
576 dwFlags
&= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG
;
578 msg
= CryptMemAlloc(sizeof(CHashEncodeMsg
));
581 CryptMsgBase_Init((CryptMsgBase
*)msg
, dwFlags
, pStreamInfo
,
582 CHashEncodeMsg_Close
, CHashEncodeMsg_GetParam
, CHashEncodeMsg_Update
,
583 CRYPT_DefaultMsgControl
);
585 msg
->data
.cbData
= 0;
586 msg
->data
.pbData
= NULL
;
587 if (!CryptCreateHash(prov
, algID
, 0, 0, &msg
->hash
))
596 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
599 PCERT_INFO pCertInfo
;
600 HCRYPTPROV hCryptProv
;
602 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm
;
605 PCRYPT_ATTRIBUTE rgAuthAttr
;
607 PCRYPT_ATTRIBUTE rgUnauthAttr
;
609 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm
;
610 void *pvHashEncryptionAuxInfo
;
611 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS
;
613 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
617 CMSG_SIGNER_ENCODE_INFO_WITH_CMS
*rgSigners
;
619 PCERT_BLOB rgCertEncoded
;
621 PCRL_BLOB rgCrlEncoded
;
622 DWORD cAttrCertEncoded
;
623 PCERT_BLOB rgAttrCertEncoded
;
624 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS
;
626 static BOOL
CRYPT_IsValidSigner(const CMSG_SIGNER_ENCODE_INFO_WITH_CMS
*signer
)
628 if (signer
->cbSize
!= sizeof(CMSG_SIGNER_ENCODE_INFO
) &&
629 signer
->cbSize
!= sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS
))
631 SetLastError(E_INVALIDARG
);
634 if (signer
->cbSize
== sizeof(CMSG_SIGNER_ENCODE_INFO
))
636 if (!signer
->pCertInfo
->SerialNumber
.cbData
)
638 SetLastError(E_INVALIDARG
);
641 if (!signer
->pCertInfo
->Issuer
.cbData
)
643 SetLastError(E_INVALIDARG
);
647 else if (signer
->cbSize
== sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS
))
649 switch (signer
->SignerId
.dwIdChoice
)
652 if (!signer
->pCertInfo
->SerialNumber
.cbData
)
654 SetLastError(E_INVALIDARG
);
657 if (!signer
->pCertInfo
->Issuer
.cbData
)
659 SetLastError(E_INVALIDARG
);
663 case CERT_ID_ISSUER_SERIAL_NUMBER
:
664 if (!signer
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
.cbData
)
666 SetLastError(E_INVALIDARG
);
669 if (!signer
->SignerId
.u
.IssuerSerialNumber
.Issuer
.cbData
)
671 SetLastError(E_INVALIDARG
);
675 case CERT_ID_KEY_IDENTIFIER
:
676 if (!signer
->SignerId
.u
.KeyId
.cbData
)
678 SetLastError(E_INVALIDARG
);
683 SetLastError(E_INVALIDARG
);
685 if (signer
->HashEncryptionAlgorithm
.pszObjId
)
687 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
691 if (!signer
->hCryptProv
)
693 SetLastError(E_INVALIDARG
);
696 if (!CertOIDToAlgId(signer
->HashAlgorithm
.pszObjId
))
698 SetLastError(CRYPT_E_UNKNOWN_ALGO
);
704 static BOOL
CRYPT_ConstructBlob(CRYPT_DATA_BLOB
*out
, const CRYPT_DATA_BLOB
*in
)
708 out
->cbData
= in
->cbData
;
711 out
->pbData
= CryptMemAlloc(out
->cbData
);
713 memcpy(out
->pbData
, in
->pbData
, out
->cbData
);
722 static BOOL
CRYPT_ConstructBlobArray(DWORD
*outCBlobs
,
723 PCRYPT_DATA_BLOB
*outPBlobs
, DWORD cBlobs
, const CRYPT_DATA_BLOB
*pBlobs
)
730 *outPBlobs
= CryptMemAlloc(cBlobs
* sizeof(CRYPT_DATA_BLOB
));
735 memset(*outPBlobs
, 0, cBlobs
* sizeof(CRYPT_DATA_BLOB
));
736 for (i
= 0; ret
&& i
< cBlobs
; i
++)
737 ret
= CRYPT_ConstructBlob(&(*outPBlobs
)[i
], &pBlobs
[i
]);
745 static void CRYPT_FreeBlobArray(DWORD cBlobs
, PCRYPT_DATA_BLOB blobs
)
749 for (i
= 0; i
< cBlobs
; i
++)
750 CryptMemFree(blobs
[i
].pbData
);
754 static BOOL
CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE
*out
,
755 const CRYPT_ATTRIBUTE
*in
)
759 out
->pszObjId
= CryptMemAlloc(strlen(in
->pszObjId
) + 1);
762 strcpy(out
->pszObjId
, in
->pszObjId
);
763 ret
= CRYPT_ConstructBlobArray(&out
->cValue
, &out
->rgValue
,
764 in
->cValue
, in
->rgValue
);
771 static BOOL
CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES
*out
,
772 const CRYPT_ATTRIBUTES
*in
)
776 out
->cAttr
= in
->cAttr
;
779 out
->rgAttr
= CryptMemAlloc(out
->cAttr
* sizeof(CRYPT_ATTRIBUTE
));
784 memset(out
->rgAttr
, 0, out
->cAttr
* sizeof(CRYPT_ATTRIBUTE
));
785 for (i
= 0; ret
&& i
< out
->cAttr
; i
++)
786 ret
= CRYPT_ConstructAttribute(&out
->rgAttr
[i
], &in
->rgAttr
[i
]);
796 /* Constructs a CMSG_CMS_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
797 static BOOL
CSignerInfo_Construct(CMSG_CMS_SIGNER_INFO
*info
,
798 const CMSG_SIGNER_ENCODE_INFO_WITH_CMS
*in
)
802 if (in
->cbSize
== sizeof(CMSG_SIGNER_ENCODE_INFO
))
804 info
->dwVersion
= CMSG_SIGNER_INFO_V1
;
805 ret
= CRYPT_ConstructBlob(&info
->SignerId
.u
.IssuerSerialNumber
.Issuer
,
806 &in
->pCertInfo
->Issuer
);
808 ret
= CRYPT_ConstructBlob(
809 &info
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
,
810 &in
->pCertInfo
->SerialNumber
);
811 info
->SignerId
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
812 info
->HashEncryptionAlgorithm
.pszObjId
=
813 in
->pCertInfo
->SubjectPublicKeyInfo
.Algorithm
.pszObjId
;
815 ret
= CRYPT_ConstructBlob(&info
->HashEncryptionAlgorithm
.Parameters
,
816 &in
->pCertInfo
->SubjectPublicKeyInfo
.Algorithm
.Parameters
);
820 const CRYPT_ALGORITHM_IDENTIFIER
*pEncrAlg
;
822 /* Implicitly in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS).
823 * See CRYPT_IsValidSigner.
825 if (!in
->SignerId
.dwIdChoice
)
827 info
->dwVersion
= CMSG_SIGNER_INFO_V1
;
828 ret
= CRYPT_ConstructBlob(&info
->SignerId
.u
.IssuerSerialNumber
.Issuer
,
829 &in
->pCertInfo
->Issuer
);
831 ret
= CRYPT_ConstructBlob(
832 &info
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
,
833 &in
->pCertInfo
->SerialNumber
);
834 info
->SignerId
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
836 else if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
838 info
->dwVersion
= CMSG_SIGNER_INFO_V1
;
839 info
->SignerId
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
840 ret
= CRYPT_ConstructBlob(&info
->SignerId
.u
.IssuerSerialNumber
.Issuer
,
841 &in
->SignerId
.u
.IssuerSerialNumber
.Issuer
);
843 ret
= CRYPT_ConstructBlob(
844 &info
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
,
845 &in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
);
849 /* Implicitly dwIdChoice == CERT_ID_KEY_IDENTIFIER */
850 info
->dwVersion
= CMSG_SIGNER_INFO_V3
;
851 info
->SignerId
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
852 ret
= CRYPT_ConstructBlob(&info
->SignerId
.u
.KeyId
,
853 &in
->SignerId
.u
.KeyId
);
855 pEncrAlg
= in
->HashEncryptionAlgorithm
.pszObjId
?
856 &in
->HashEncryptionAlgorithm
:
857 &in
->pCertInfo
->SubjectPublicKeyInfo
.Algorithm
;
858 info
->HashEncryptionAlgorithm
.pszObjId
= pEncrAlg
->pszObjId
;
860 ret
= CRYPT_ConstructBlob(&info
->HashEncryptionAlgorithm
.Parameters
,
861 &pEncrAlg
->Parameters
);
863 /* Assumption: algorithm IDs will point to static strings, not
864 * stack-based ones, so copying the pointer values is safe.
866 info
->HashAlgorithm
.pszObjId
= in
->HashAlgorithm
.pszObjId
;
868 ret
= CRYPT_ConstructBlob(&info
->HashAlgorithm
.Parameters
,
869 &in
->HashAlgorithm
.Parameters
);
871 ret
= CRYPT_ConstructAttributes(&info
->AuthAttrs
,
872 (CRYPT_ATTRIBUTES
*)&in
->cAuthAttr
);
874 ret
= CRYPT_ConstructAttributes(&info
->UnauthAttrs
,
875 (CRYPT_ATTRIBUTES
*)&in
->cUnauthAttr
);
879 static void CSignerInfo_Free(CMSG_CMS_SIGNER_INFO
*info
)
883 if (info
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
885 CryptMemFree(info
->SignerId
.u
.IssuerSerialNumber
.Issuer
.pbData
);
886 CryptMemFree(info
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
.pbData
);
889 CryptMemFree(info
->SignerId
.u
.KeyId
.pbData
);
890 CryptMemFree(info
->HashAlgorithm
.Parameters
.pbData
);
891 CryptMemFree(info
->HashEncryptionAlgorithm
.Parameters
.pbData
);
892 CryptMemFree(info
->EncryptedHash
.pbData
);
893 for (i
= 0; i
< info
->AuthAttrs
.cAttr
; i
++)
895 for (j
= 0; j
< info
->AuthAttrs
.rgAttr
[i
].cValue
; j
++)
896 CryptMemFree(info
->AuthAttrs
.rgAttr
[i
].rgValue
[j
].pbData
);
897 CryptMemFree(info
->AuthAttrs
.rgAttr
[i
].rgValue
);
898 CryptMemFree(info
->AuthAttrs
.rgAttr
[i
].pszObjId
);
900 CryptMemFree(info
->AuthAttrs
.rgAttr
);
901 for (i
= 0; i
< info
->UnauthAttrs
.cAttr
; i
++)
903 for (j
= 0; j
< info
->UnauthAttrs
.rgAttr
[i
].cValue
; j
++)
904 CryptMemFree(info
->UnauthAttrs
.rgAttr
[i
].rgValue
[j
].pbData
);
905 CryptMemFree(info
->UnauthAttrs
.rgAttr
[i
].rgValue
);
906 CryptMemFree(info
->UnauthAttrs
.rgAttr
[i
].pszObjId
);
908 CryptMemFree(info
->UnauthAttrs
.rgAttr
);
911 typedef struct _CSignerHandles
913 HCRYPTHASH contentHash
;
914 HCRYPTHASH authAttrHash
;
917 typedef struct _CSignedMsgData
919 CRYPT_SIGNED_INFO
*info
;
921 CSignerHandles
*signerHandles
;
924 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
925 * Assumes signerIndex is a valid index, and that msg_data's info has already
928 static BOOL
CSignedMsgData_ConstructSignerHandles(CSignedMsgData
*msg_data
,
929 DWORD signerIndex
, HCRYPTPROV
*crypt_prov
, DWORD
*flags
)
934 algID
= CertOIDToAlgId(
935 msg_data
->info
->rgSignerInfo
[signerIndex
].HashAlgorithm
.pszObjId
);
939 *crypt_prov
= I_CryptGetDefaultCryptProv(algID
);
940 if (!*crypt_prov
) return FALSE
;
941 *flags
&= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG
;
944 ret
= CryptCreateHash(*crypt_prov
, algID
, 0, 0,
945 &msg_data
->signerHandles
->contentHash
);
946 if (ret
&& msg_data
->info
->rgSignerInfo
[signerIndex
].AuthAttrs
.cAttr
> 0)
947 ret
= CryptCreateHash(*crypt_prov
, algID
, 0, 0,
948 &msg_data
->signerHandles
->authAttrHash
);
952 /* Allocates a CSignedMsgData's handles. Assumes its info has already been
955 static BOOL
CSignedMsgData_AllocateHandles(CSignedMsgData
*msg_data
)
959 if (msg_data
->info
->cSignerInfo
)
961 msg_data
->signerHandles
=
962 CryptMemAlloc(msg_data
->info
->cSignerInfo
* sizeof(CSignerHandles
));
963 if (msg_data
->signerHandles
)
965 msg_data
->cSignerHandle
= msg_data
->info
->cSignerInfo
;
966 memset(msg_data
->signerHandles
, 0,
967 msg_data
->info
->cSignerInfo
* sizeof(CSignerHandles
));
971 msg_data
->cSignerHandle
= 0;
977 msg_data
->cSignerHandle
= 0;
978 msg_data
->signerHandles
= NULL
;
983 static void CSignedMsgData_CloseHandles(CSignedMsgData
*msg_data
)
987 for (i
= 0; i
< msg_data
->cSignerHandle
; i
++)
989 if (msg_data
->signerHandles
[i
].contentHash
)
990 CryptDestroyHash(msg_data
->signerHandles
[i
].contentHash
);
991 if (msg_data
->signerHandles
[i
].authAttrHash
)
992 CryptDestroyHash(msg_data
->signerHandles
[i
].authAttrHash
);
994 CryptMemFree(msg_data
->signerHandles
);
995 msg_data
->signerHandles
= NULL
;
996 msg_data
->cSignerHandle
= 0;
999 static BOOL
CSignedMsgData_UpdateHash(CSignedMsgData
*msg_data
,
1000 const BYTE
*pbData
, DWORD cbData
)
1005 for (i
= 0; ret
&& i
< msg_data
->cSignerHandle
; i
++)
1006 ret
= CryptHashData(msg_data
->signerHandles
[i
].contentHash
, pbData
,
1011 static BOOL
CRYPT_AppendAttribute(CRYPT_ATTRIBUTES
*out
,
1012 const CRYPT_ATTRIBUTE
*in
)
1016 out
->rgAttr
= CryptMemRealloc(out
->rgAttr
,
1017 (out
->cAttr
+ 1) * sizeof(CRYPT_ATTRIBUTE
));
1020 ret
= CRYPT_ConstructAttribute(&out
->rgAttr
[out
->cAttr
], in
);
1027 static BOOL
CSignedMsgData_AppendMessageDigestAttribute(
1028 CSignedMsgData
*msg_data
, DWORD signerIndex
)
1032 CRYPT_HASH_BLOB hash
= { 0, NULL
}, encodedHash
= { 0, NULL
};
1033 char messageDigest
[] = szOID_RSA_messageDigest
;
1034 CRYPT_ATTRIBUTE messageDigestAttr
= { messageDigest
, 1, &encodedHash
};
1036 size
= sizeof(DWORD
);
1037 ret
= CryptGetHashParam(
1038 msg_data
->signerHandles
[signerIndex
].contentHash
, HP_HASHSIZE
,
1039 (LPBYTE
)&hash
.cbData
, &size
, 0);
1042 hash
.pbData
= CryptMemAlloc(hash
.cbData
);
1043 ret
= CryptGetHashParam(
1044 msg_data
->signerHandles
[signerIndex
].contentHash
, HP_HASHVAL
,
1045 hash
.pbData
, &hash
.cbData
, 0);
1048 ret
= CRYPT_AsnEncodeOctets(0, NULL
, &hash
, CRYPT_ENCODE_ALLOC_FLAG
,
1049 NULL
, (LPBYTE
)&encodedHash
.pbData
, &encodedHash
.cbData
);
1052 ret
= CRYPT_AppendAttribute(
1053 &msg_data
->info
->rgSignerInfo
[signerIndex
].AuthAttrs
,
1054 &messageDigestAttr
);
1055 LocalFree(encodedHash
.pbData
);
1058 CryptMemFree(hash
.pbData
);
1068 static BOOL
CSignedMsgData_UpdateAuthenticatedAttributes(
1069 CSignedMsgData
*msg_data
, SignOrVerify flag
)
1074 TRACE("(%p)\n", msg_data
);
1076 for (i
= 0; ret
&& i
< msg_data
->info
->cSignerInfo
; i
++)
1078 if (msg_data
->info
->rgSignerInfo
[i
].AuthAttrs
.cAttr
)
1082 BYTE oid_rsa_data_encoded
[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
1083 0xf7,0x0d,0x01,0x07,0x01 };
1084 CRYPT_DATA_BLOB content
= { sizeof(oid_rsa_data_encoded
),
1085 oid_rsa_data_encoded
};
1086 char contentType
[] = szOID_RSA_contentType
;
1087 CRYPT_ATTRIBUTE contentTypeAttr
= { contentType
, 1, &content
};
1089 /* FIXME: does this depend on inner OID? */
1090 ret
= CRYPT_AppendAttribute(
1091 &msg_data
->info
->rgSignerInfo
[i
].AuthAttrs
, &contentTypeAttr
);
1093 ret
= CSignedMsgData_AppendMessageDigestAttribute(msg_data
,
1098 LPBYTE encodedAttrs
;
1101 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, PKCS_ATTRIBUTES
,
1102 &msg_data
->info
->rgSignerInfo
[i
].AuthAttrs
,
1103 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &encodedAttrs
, &size
);
1106 ret
= CryptHashData(
1107 msg_data
->signerHandles
[i
].authAttrHash
, encodedAttrs
,
1109 LocalFree(encodedAttrs
);
1114 TRACE("returning %d\n", ret
);
1118 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB
*hash
)
1123 for (i
= 0; i
< hash
->cbData
/ 2; i
++)
1125 tmp
= hash
->pbData
[hash
->cbData
- i
- 1];
1126 hash
->pbData
[hash
->cbData
- i
- 1] = hash
->pbData
[i
];
1127 hash
->pbData
[i
] = tmp
;
1131 static BOOL
CSignedMsgData_Sign(CSignedMsgData
*msg_data
)
1136 TRACE("(%p)\n", msg_data
);
1138 for (i
= 0; ret
&& i
< msg_data
->info
->cSignerInfo
; i
++)
1141 DWORD keySpec
= msg_data
->info
->signerKeySpec
[i
];
1144 keySpec
= AT_SIGNATURE
;
1145 if (msg_data
->info
->rgSignerInfo
[i
].AuthAttrs
.cAttr
)
1146 hash
= msg_data
->signerHandles
[i
].authAttrHash
;
1148 hash
= msg_data
->signerHandles
[i
].contentHash
;
1149 ret
= CryptSignHashW(hash
, keySpec
, NULL
, 0, NULL
,
1150 &msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
.cbData
);
1153 msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
.pbData
=
1155 msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
.cbData
);
1156 if (msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
.pbData
)
1158 ret
= CryptSignHashW(hash
, keySpec
, NULL
, 0,
1159 msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
.pbData
,
1160 &msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
.cbData
);
1163 &msg_data
->info
->rgSignerInfo
[i
].EncryptedHash
);
1172 static BOOL
CSignedMsgData_Update(CSignedMsgData
*msg_data
,
1173 const BYTE
*pbData
, DWORD cbData
, BOOL fFinal
, SignOrVerify flag
)
1175 BOOL ret
= CSignedMsgData_UpdateHash(msg_data
, pbData
, cbData
);
1179 ret
= CSignedMsgData_UpdateAuthenticatedAttributes(msg_data
, flag
);
1180 if (ret
&& flag
== Sign
)
1181 ret
= CSignedMsgData_Sign(msg_data
);
1186 typedef struct _CSignedEncodeMsg
1190 CRYPT_DATA_BLOB data
;
1191 CSignedMsgData msg_data
;
1194 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg
)
1196 CSignedEncodeMsg
*msg
= hCryptMsg
;
1199 CryptMemFree(msg
->innerOID
);
1200 CryptMemFree(msg
->data
.pbData
);
1201 CRYPT_FreeBlobArray(msg
->msg_data
.info
->cCertEncoded
,
1202 msg
->msg_data
.info
->rgCertEncoded
);
1203 CRYPT_FreeBlobArray(msg
->msg_data
.info
->cCrlEncoded
,
1204 msg
->msg_data
.info
->rgCrlEncoded
);
1205 for (i
= 0; i
< msg
->msg_data
.info
->cSignerInfo
; i
++)
1206 CSignerInfo_Free(&msg
->msg_data
.info
->rgSignerInfo
[i
]);
1207 CSignedMsgData_CloseHandles(&msg
->msg_data
);
1208 CryptMemFree(msg
->msg_data
.info
->signerKeySpec
);
1209 CryptMemFree(msg
->msg_data
.info
->rgSignerInfo
);
1210 CryptMemFree(msg
->msg_data
.info
);
1213 static BOOL
CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
1214 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
1216 CSignedEncodeMsg
*msg
= hCryptMsg
;
1219 switch (dwParamType
)
1221 case CMSG_CONTENT_PARAM
:
1223 CRYPT_CONTENT_INFO info
;
1225 ret
= CryptMsgGetParam(hCryptMsg
, CMSG_BARE_CONTENT_PARAM
, 0, NULL
,
1226 &info
.Content
.cbData
);
1229 info
.Content
.pbData
= CryptMemAlloc(info
.Content
.cbData
);
1230 if (info
.Content
.pbData
)
1232 ret
= CryptMsgGetParam(hCryptMsg
, CMSG_BARE_CONTENT_PARAM
, 0,
1233 info
.Content
.pbData
, &info
.Content
.cbData
);
1236 char oid_rsa_signed
[] = szOID_RSA_signedData
;
1238 info
.pszObjId
= oid_rsa_signed
;
1239 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
,
1240 PKCS_CONTENT_INFO
, &info
, 0, NULL
, pvData
, pcbData
);
1242 CryptMemFree(info
.Content
.pbData
);
1249 case CMSG_BARE_CONTENT_PARAM
:
1251 CRYPT_SIGNED_INFO info
;
1252 BOOL freeContent
= FALSE
;
1254 info
= *msg
->msg_data
.info
;
1255 if (!msg
->innerOID
|| !strcmp(msg
->innerOID
, szOID_RSA_data
))
1257 char oid_rsa_data
[] = szOID_RSA_data
;
1259 /* Quirk: OID is only encoded messages if an update has happened */
1260 if (msg
->base
.state
!= MsgStateInit
)
1261 info
.content
.pszObjId
= oid_rsa_data
;
1263 info
.content
.pszObjId
= NULL
;
1264 if (msg
->data
.cbData
)
1266 CRYPT_DATA_BLOB blob
= { msg
->data
.cbData
, msg
->data
.pbData
};
1268 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_OCTET_STRING
,
1269 &blob
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
1270 &info
.content
.Content
.pbData
, &info
.content
.Content
.cbData
);
1275 info
.content
.Content
.cbData
= 0;
1276 info
.content
.Content
.pbData
= NULL
;
1282 info
.content
.pszObjId
= msg
->innerOID
;
1283 info
.content
.Content
.cbData
= msg
->data
.cbData
;
1284 info
.content
.Content
.pbData
= msg
->data
.pbData
;
1289 ret
= CRYPT_AsnEncodeCMSSignedInfo(&info
, pvData
, pcbData
);
1291 LocalFree(info
.content
.Content
.pbData
);
1295 case CMSG_COMPUTED_HASH_PARAM
:
1296 if (dwIndex
>= msg
->msg_data
.cSignerHandle
)
1297 SetLastError(CRYPT_E_INVALID_INDEX
);
1299 ret
= CryptGetHashParam(
1300 msg
->msg_data
.signerHandles
[dwIndex
].contentHash
, HP_HASHVAL
,
1301 pvData
, pcbData
, 0);
1303 case CMSG_ENCODED_SIGNER
:
1304 if (dwIndex
>= msg
->msg_data
.info
->cSignerInfo
)
1305 SetLastError(CRYPT_E_INVALID_INDEX
);
1307 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
1308 CMS_SIGNER_INFO
, &msg
->msg_data
.info
->rgSignerInfo
[dwIndex
], 0,
1309 NULL
, pvData
, pcbData
);
1311 case CMSG_VERSION_PARAM
:
1312 ret
= CRYPT_CopyParam(pvData
, pcbData
, &msg
->msg_data
.info
->version
,
1313 sizeof(msg
->msg_data
.info
->version
));
1316 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
1321 static BOOL
CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
1322 DWORD cbData
, BOOL fFinal
)
1324 CSignedEncodeMsg
*msg
= hCryptMsg
;
1327 if (msg
->base
.state
== MsgStateFinalized
)
1328 SetLastError(CRYPT_E_MSG_ERROR
);
1329 else if (msg
->base
.streamed
|| (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
))
1331 ret
= CSignedMsgData_Update(&msg
->msg_data
, pbData
, cbData
, fFinal
,
1333 if (msg
->base
.streamed
)
1334 FIXME("streamed partial stub\n");
1335 msg
->base
.state
= fFinal
? MsgStateFinalized
: MsgStateUpdated
;
1340 SetLastError(CRYPT_E_MSG_ERROR
);
1345 msg
->data
.pbData
= CryptMemAlloc(cbData
);
1346 if (msg
->data
.pbData
)
1348 memcpy(msg
->data
.pbData
, pbData
, cbData
);
1349 msg
->data
.cbData
= cbData
;
1356 ret
= CSignedMsgData_Update(&msg
->msg_data
, pbData
, cbData
,
1358 msg
->base
.state
= MsgStateFinalized
;
1364 static HCRYPTMSG
CSignedEncodeMsg_Open(DWORD dwFlags
,
1365 const void *pvMsgEncodeInfo
, LPCSTR pszInnerContentObjID
,
1366 PCMSG_STREAM_INFO pStreamInfo
)
1368 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS
*info
= pvMsgEncodeInfo
;
1370 CSignedEncodeMsg
*msg
;
1372 if (info
->cbSize
!= sizeof(CMSG_SIGNED_ENCODE_INFO
) &&
1373 info
->cbSize
!= sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS
))
1375 SetLastError(E_INVALIDARG
);
1378 if (info
->cbSize
== sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS
) &&
1379 info
->cAttrCertEncoded
)
1381 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1384 for (i
= 0; i
< info
->cSigners
; i
++)
1385 if (!CRYPT_IsValidSigner(&info
->rgSigners
[i
]))
1387 msg
= CryptMemAlloc(sizeof(CSignedEncodeMsg
));
1392 CryptMsgBase_Init((CryptMsgBase
*)msg
, dwFlags
, pStreamInfo
,
1393 CSignedEncodeMsg_Close
, CSignedEncodeMsg_GetParam
,
1394 CSignedEncodeMsg_Update
, CRYPT_DefaultMsgControl
);
1395 if (pszInnerContentObjID
)
1397 msg
->innerOID
= CryptMemAlloc(strlen(pszInnerContentObjID
) + 1);
1399 strcpy(msg
->innerOID
, pszInnerContentObjID
);
1404 msg
->innerOID
= NULL
;
1405 msg
->data
.cbData
= 0;
1406 msg
->data
.pbData
= NULL
;
1408 msg
->msg_data
.info
= CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO
));
1410 msg
->msg_data
.info
= NULL
;
1411 if (msg
->msg_data
.info
)
1413 memset(msg
->msg_data
.info
, 0, sizeof(CRYPT_SIGNED_INFO
));
1414 msg
->msg_data
.info
->version
= CMSG_SIGNED_DATA_V1
;
1422 msg
->msg_data
.info
->rgSignerInfo
=
1423 CryptMemAlloc(info
->cSigners
* sizeof(CMSG_CMS_SIGNER_INFO
));
1424 if (msg
->msg_data
.info
->rgSignerInfo
)
1426 msg
->msg_data
.info
->cSignerInfo
= info
->cSigners
;
1427 memset(msg
->msg_data
.info
->rgSignerInfo
, 0,
1428 msg
->msg_data
.info
->cSignerInfo
*
1429 sizeof(CMSG_CMS_SIGNER_INFO
));
1430 ret
= CSignedMsgData_AllocateHandles(&msg
->msg_data
);
1431 msg
->msg_data
.info
->signerKeySpec
= CryptMemAlloc(info
->cSigners
* sizeof(DWORD
));
1432 if (!msg
->msg_data
.info
->signerKeySpec
)
1434 for (i
= 0; ret
&& i
< msg
->msg_data
.info
->cSignerInfo
; i
++)
1436 if (info
->rgSigners
[i
].SignerId
.dwIdChoice
==
1437 CERT_ID_KEY_IDENTIFIER
)
1438 msg
->msg_data
.info
->version
= CMSG_SIGNED_DATA_V3
;
1439 ret
= CSignerInfo_Construct(
1440 &msg
->msg_data
.info
->rgSignerInfo
[i
],
1441 &info
->rgSigners
[i
]);
1444 ret
= CSignedMsgData_ConstructSignerHandles(
1445 &msg
->msg_data
, i
, &info
->rgSigners
[i
].hCryptProv
, &dwFlags
);
1446 if (dwFlags
& CMSG_CRYPT_RELEASE_CONTEXT_FLAG
)
1447 CryptReleaseContext(info
->rgSigners
[i
].hCryptProv
,
1450 msg
->msg_data
.info
->signerKeySpec
[i
] =
1451 info
->rgSigners
[i
].dwKeySpec
;
1459 msg
->msg_data
.info
->cSignerInfo
= 0;
1460 msg
->msg_data
.signerHandles
= NULL
;
1461 msg
->msg_data
.cSignerHandle
= 0;
1465 ret
= CRYPT_ConstructBlobArray(&msg
->msg_data
.info
->cCertEncoded
,
1466 &msg
->msg_data
.info
->rgCertEncoded
, info
->cCertEncoded
,
1467 info
->rgCertEncoded
);
1469 ret
= CRYPT_ConstructBlobArray(&msg
->msg_data
.info
->cCrlEncoded
,
1470 &msg
->msg_data
.info
->rgCrlEncoded
, info
->cCrlEncoded
,
1471 info
->rgCrlEncoded
);
1474 CSignedEncodeMsg_Close(msg
);
1482 typedef struct _CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
1485 HCRYPTPROV_LEGACY hCryptProv
;
1486 CRYPT_ALGORITHM_IDENTIFIER ContentEncryptionAlgorithm
;
1487 void *pvEncryptionAuxInfo
;
1489 PCERT_INFO
*rgpRecipientCert
;
1490 PCMSG_RECIPIENT_ENCODE_INFO rgCmsRecipients
;
1492 PCERT_BLOB rgCertEncoded
;
1494 PCRL_BLOB rgCrlEncoded
;
1495 DWORD cAttrCertEncoded
;
1496 PCERT_BLOB rgAttrCertEncoded
;
1497 DWORD cUnprotectedAttr
;
1498 PCRYPT_ATTRIBUTE rgUnprotectedAttr
;
1499 } CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
;
1501 typedef struct _CEnvelopedEncodeMsg
1504 CRYPT_ALGORITHM_IDENTIFIER algo
;
1507 DWORD cRecipientInfo
;
1508 CMSG_KEY_TRANS_RECIPIENT_INFO
*recipientInfo
;
1509 CRYPT_DATA_BLOB data
;
1510 } CEnvelopedEncodeMsg
;
1512 static BOOL
CRYPT_ConstructAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER
*out
,
1513 const CRYPT_ALGORITHM_IDENTIFIER
*in
)
1515 out
->pszObjId
= CryptMemAlloc(strlen(in
->pszObjId
) + 1);
1518 strcpy(out
->pszObjId
, in
->pszObjId
);
1519 return CRYPT_ConstructBlob(&out
->Parameters
, &in
->Parameters
);
1525 static BOOL
CRYPT_ConstructBitBlob(CRYPT_BIT_BLOB
*out
, const CRYPT_BIT_BLOB
*in
)
1527 out
->cbData
= in
->cbData
;
1528 out
->cUnusedBits
= in
->cUnusedBits
;
1531 out
->pbData
= CryptMemAlloc(out
->cbData
);
1533 memcpy(out
->pbData
, in
->pbData
, out
->cbData
);
1542 static BOOL
CRYPT_GenKey(CMSG_CONTENT_ENCRYPT_INFO
*info
, ALG_ID algID
)
1544 static HCRYPTOIDFUNCSET set
= NULL
;
1545 PFN_CMSG_GEN_CONTENT_ENCRYPT_KEY genKeyFunc
= NULL
;
1546 HCRYPTOIDFUNCADDR hFunc
;
1550 set
= CryptInitOIDFunctionSet(CMSG_OID_GEN_CONTENT_ENCRYPT_KEY_FUNC
, 0);
1551 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
,
1552 info
->ContentEncryptionAlgorithm
.pszObjId
, 0, (void **)&genKeyFunc
, &hFunc
);
1555 ret
= genKeyFunc(info
, 0, NULL
);
1556 CryptFreeOIDFunctionAddress(hFunc
, 0);
1559 ret
= CryptGenKey(info
->hCryptProv
, algID
, CRYPT_EXPORTABLE
,
1560 &info
->hContentEncryptKey
);
1564 static BOOL WINAPI
CRYPT_ExportKeyTrans(
1565 PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo
,
1566 PCMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO pKeyTransEncodeInfo
,
1567 PCMSG_KEY_TRANS_ENCRYPT_INFO pKeyTransEncryptInfo
,
1568 DWORD dwFlags
, void *pvReserved
)
1570 CERT_PUBLIC_KEY_INFO keyInfo
;
1574 ret
= CRYPT_ConstructAlgorithmId(&keyInfo
.Algorithm
,
1575 &pKeyTransEncodeInfo
->KeyEncryptionAlgorithm
);
1577 ret
= CRYPT_ConstructBitBlob(&keyInfo
.PublicKey
,
1578 &pKeyTransEncodeInfo
->RecipientPublicKey
);
1580 ret
= CryptImportPublicKeyInfo(pKeyTransEncodeInfo
->hCryptProv
,
1581 X509_ASN_ENCODING
, &keyInfo
, &expKey
);
1586 ret
= CryptExportKey(pContentEncryptInfo
->hContentEncryptKey
, expKey
,
1587 SIMPLEBLOB
, 0, NULL
, &size
);
1592 keyBlob
= CryptMemAlloc(size
);
1595 ret
= CryptExportKey(pContentEncryptInfo
->hContentEncryptKey
,
1596 expKey
, SIMPLEBLOB
, 0, keyBlob
, &size
);
1599 DWORD head
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
);
1601 pKeyTransEncryptInfo
->EncryptedKey
.pbData
=
1602 CryptMemAlloc(size
- head
);
1603 if (pKeyTransEncryptInfo
->EncryptedKey
.pbData
)
1607 pKeyTransEncryptInfo
->EncryptedKey
.cbData
= size
- head
;
1608 for (i
= size
- 1; i
>= head
; --i
, ++k
)
1609 pKeyTransEncryptInfo
->EncryptedKey
.pbData
[k
] =
1615 CryptMemFree(keyBlob
);
1620 CryptDestroyKey(expKey
);
1623 CryptMemFree(keyInfo
.PublicKey
.pbData
);
1624 CryptMemFree(keyInfo
.Algorithm
.pszObjId
);
1625 CryptMemFree(keyInfo
.Algorithm
.Parameters
.pbData
);
1629 static BOOL
CRYPT_ExportEncryptedKey(CMSG_CONTENT_ENCRYPT_INFO
*info
, DWORD i
,
1630 CRYPT_DATA_BLOB
*key
)
1632 static HCRYPTOIDFUNCSET set
= NULL
;
1633 PFN_CMSG_EXPORT_KEY_TRANS exportKeyFunc
= NULL
;
1634 HCRYPTOIDFUNCADDR hFunc
= NULL
;
1635 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO
*encodeInfo
=
1636 info
->rgCmsRecipients
[i
].u
.pKeyTrans
;
1637 CMSG_KEY_TRANS_ENCRYPT_INFO encryptInfo
;
1640 memset(&encryptInfo
, 0, sizeof(encryptInfo
));
1641 encryptInfo
.cbSize
= sizeof(encryptInfo
);
1642 encryptInfo
.dwRecipientIndex
= i
;
1643 ret
= CRYPT_ConstructAlgorithmId(&encryptInfo
.KeyEncryptionAlgorithm
,
1644 &encodeInfo
->KeyEncryptionAlgorithm
);
1647 set
= CryptInitOIDFunctionSet(CMSG_OID_EXPORT_KEY_TRANS_FUNC
, 0);
1648 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
,
1649 encryptInfo
.KeyEncryptionAlgorithm
.pszObjId
, 0, (void **)&exportKeyFunc
,
1652 exportKeyFunc
= CRYPT_ExportKeyTrans
;
1655 ret
= exportKeyFunc(info
, encodeInfo
, &encryptInfo
, 0, NULL
);
1658 key
->cbData
= encryptInfo
.EncryptedKey
.cbData
;
1659 key
->pbData
= encryptInfo
.EncryptedKey
.pbData
;
1663 CryptFreeOIDFunctionAddress(hFunc
, 0);
1665 CryptMemFree(encryptInfo
.KeyEncryptionAlgorithm
.pszObjId
);
1666 CryptMemFree(encryptInfo
.KeyEncryptionAlgorithm
.Parameters
.pbData
);
1670 static LPVOID WINAPI
mem_alloc(size_t size
)
1672 return HeapAlloc(GetProcessHeap(), 0, size
);
1675 static VOID WINAPI
mem_free(LPVOID pv
)
1677 HeapFree(GetProcessHeap(), 0, pv
);
1681 static BOOL
CContentEncryptInfo_Construct(CMSG_CONTENT_ENCRYPT_INFO
*info
,
1682 const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
*in
, HCRYPTPROV prov
)
1686 info
->cbSize
= sizeof(CMSG_CONTENT_ENCRYPT_INFO
);
1687 info
->hCryptProv
= prov
;
1688 ret
= CRYPT_ConstructAlgorithmId(&info
->ContentEncryptionAlgorithm
,
1689 &in
->ContentEncryptionAlgorithm
);
1690 info
->pvEncryptionAuxInfo
= in
->pvEncryptionAuxInfo
;
1691 info
->cRecipients
= in
->cRecipients
;
1694 info
->rgCmsRecipients
= CryptMemAlloc(in
->cRecipients
*
1695 sizeof(CMSG_RECIPIENT_ENCODE_INFO
));
1696 if (info
->rgCmsRecipients
)
1700 for (i
= 0; ret
&& i
< in
->cRecipients
; ++i
)
1702 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO
*encodeInfo
;
1703 CERT_INFO
*cert
= in
->rgpRecipientCert
[i
];
1705 info
->rgCmsRecipients
[i
].dwRecipientChoice
=
1706 CMSG_KEY_TRANS_RECIPIENT
;
1707 encodeInfo
= CryptMemAlloc(sizeof(*encodeInfo
));
1708 info
->rgCmsRecipients
[i
].u
.pKeyTrans
= encodeInfo
;
1711 encodeInfo
->cbSize
= sizeof(*encodeInfo
);
1712 ret
= CRYPT_ConstructAlgorithmId(
1713 &encodeInfo
->KeyEncryptionAlgorithm
,
1714 &cert
->SubjectPublicKeyInfo
.Algorithm
);
1715 encodeInfo
->pvKeyEncryptionAuxInfo
= NULL
;
1716 encodeInfo
->hCryptProv
= prov
;
1718 ret
= CRYPT_ConstructBitBlob(
1719 &encodeInfo
->RecipientPublicKey
,
1720 &cert
->SubjectPublicKeyInfo
.PublicKey
);
1722 ret
= CRYPT_ConstructBlob(
1723 &encodeInfo
->RecipientId
.u
.IssuerSerialNumber
.Issuer
,
1726 ret
= CRYPT_ConstructBlob(
1727 &encodeInfo
->RecipientId
.u
.IssuerSerialNumber
.SerialNumber
,
1728 &cert
->SerialNumber
);
1737 info
->pfnAlloc
= mem_alloc
;
1738 info
->pfnFree
= mem_free
;
1742 static void CContentEncryptInfo_Free(CMSG_CONTENT_ENCRYPT_INFO
*info
)
1744 CryptMemFree(info
->ContentEncryptionAlgorithm
.pszObjId
);
1745 CryptMemFree(info
->ContentEncryptionAlgorithm
.Parameters
.pbData
);
1746 if (info
->rgCmsRecipients
)
1750 for (i
= 0; i
< info
->cRecipients
; ++i
)
1752 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO
*encodeInfo
=
1753 info
->rgCmsRecipients
[i
].u
.pKeyTrans
;
1755 CryptMemFree(encodeInfo
->KeyEncryptionAlgorithm
.pszObjId
);
1756 CryptMemFree(encodeInfo
->KeyEncryptionAlgorithm
.Parameters
.pbData
);
1757 CryptMemFree(encodeInfo
->RecipientPublicKey
.pbData
);
1759 encodeInfo
->RecipientId
.u
.IssuerSerialNumber
.Issuer
.pbData
);
1761 encodeInfo
->RecipientId
.u
.IssuerSerialNumber
.SerialNumber
.pbData
);
1762 CryptMemFree(encodeInfo
);
1764 CryptMemFree(info
->rgCmsRecipients
);
1768 static BOOL
CRecipientInfo_Construct(CMSG_KEY_TRANS_RECIPIENT_INFO
*info
,
1769 const CERT_INFO
*cert
, CRYPT_DATA_BLOB
*key
)
1773 info
->dwVersion
= CMSG_KEY_TRANS_PKCS_1_5_VERSION
;
1774 info
->RecipientId
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1775 ret
= CRYPT_ConstructBlob(&info
->RecipientId
.u
.IssuerSerialNumber
.Issuer
,
1778 ret
= CRYPT_ConstructBlob(
1779 &info
->RecipientId
.u
.IssuerSerialNumber
.SerialNumber
,
1780 &cert
->SerialNumber
);
1782 ret
= CRYPT_ConstructAlgorithmId(&info
->KeyEncryptionAlgorithm
,
1783 &cert
->SubjectPublicKeyInfo
.Algorithm
);
1784 info
->EncryptedKey
.cbData
= key
->cbData
;
1785 info
->EncryptedKey
.pbData
= key
->pbData
;
1789 static void CRecipientInfo_Free(CMSG_KEY_TRANS_RECIPIENT_INFO
*info
)
1791 CryptMemFree(info
->RecipientId
.u
.IssuerSerialNumber
.Issuer
.pbData
);
1792 CryptMemFree(info
->RecipientId
.u
.IssuerSerialNumber
.SerialNumber
.pbData
);
1793 CryptMemFree(info
->KeyEncryptionAlgorithm
.pszObjId
);
1794 CryptMemFree(info
->KeyEncryptionAlgorithm
.Parameters
.pbData
);
1795 CryptMemFree(info
->EncryptedKey
.pbData
);
1798 static void CEnvelopedEncodeMsg_Close(HCRYPTMSG hCryptMsg
)
1800 CEnvelopedEncodeMsg
*msg
= hCryptMsg
;
1802 CryptMemFree(msg
->algo
.pszObjId
);
1803 CryptMemFree(msg
->algo
.Parameters
.pbData
);
1804 if (msg
->base
.open_flags
& CMSG_CRYPT_RELEASE_CONTEXT_FLAG
)
1805 CryptReleaseContext(msg
->prov
, 0);
1806 CryptDestroyKey(msg
->key
);
1807 if (msg
->recipientInfo
)
1811 for (i
= 0; i
< msg
->cRecipientInfo
; ++i
)
1812 CRecipientInfo_Free(&msg
->recipientInfo
[i
]);
1813 CryptMemFree(msg
->recipientInfo
);
1815 CryptMemFree(msg
->data
.pbData
);
1818 static BOOL
CEnvelopedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
1819 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
1821 CEnvelopedEncodeMsg
*msg
= hCryptMsg
;
1824 switch (dwParamType
)
1826 case CMSG_BARE_CONTENT_PARAM
:
1827 if (msg
->base
.streamed
)
1828 SetLastError(E_INVALIDARG
);
1831 char oid_rsa_data
[] = szOID_RSA_data
;
1832 CRYPT_ENVELOPED_DATA envelopedData
= {
1833 CMSG_ENVELOPED_DATA_PKCS_1_5_VERSION
, msg
->cRecipientInfo
,
1834 msg
->recipientInfo
, { oid_rsa_data
, {
1836 { msg
->algo
.Parameters
.cbData
, msg
->algo
.Parameters
.pbData
}
1838 { msg
->data
.cbData
, msg
->data
.pbData
}
1842 ret
= CRYPT_AsnEncodePKCSEnvelopedData(&envelopedData
, pvData
,
1846 case CMSG_CONTENT_PARAM
:
1848 CRYPT_CONTENT_INFO info
;
1850 ret
= CryptMsgGetParam(hCryptMsg
, CMSG_BARE_CONTENT_PARAM
, 0, NULL
,
1851 &info
.Content
.cbData
);
1854 info
.Content
.pbData
= CryptMemAlloc(info
.Content
.cbData
);
1855 if (info
.Content
.pbData
)
1857 ret
= CryptMsgGetParam(hCryptMsg
, CMSG_BARE_CONTENT_PARAM
, 0,
1858 info
.Content
.pbData
, &info
.Content
.cbData
);
1861 char oid_rsa_enveloped
[] = szOID_RSA_envelopedData
;
1863 info
.pszObjId
= oid_rsa_enveloped
;
1864 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
,
1865 PKCS_CONTENT_INFO
, &info
, 0, NULL
, pvData
, pcbData
);
1867 CryptMemFree(info
.Content
.pbData
);
1875 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
1880 static BOOL
CEnvelopedEncodeMsg_Update(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
1881 DWORD cbData
, BOOL fFinal
)
1883 CEnvelopedEncodeMsg
*msg
= hCryptMsg
;
1886 if (msg
->base
.state
== MsgStateFinalized
)
1887 SetLastError(CRYPT_E_MSG_ERROR
);
1888 else if (msg
->base
.streamed
)
1890 FIXME("streamed stub\n");
1891 msg
->base
.state
= fFinal
? MsgStateFinalized
: MsgStateUpdated
;
1898 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
1899 SetLastError(E_INVALIDARG
);
1901 SetLastError(CRYPT_E_MSG_ERROR
);
1907 DWORD dataLen
= cbData
;
1909 msg
->data
.cbData
= cbData
;
1910 msg
->data
.pbData
= CryptMemAlloc(cbData
);
1911 if (msg
->data
.pbData
)
1913 memcpy(msg
->data
.pbData
, pbData
, cbData
);
1914 ret
= CryptEncrypt(msg
->key
, 0, TRUE
, 0, msg
->data
.pbData
,
1915 &dataLen
, msg
->data
.cbData
);
1916 msg
->data
.cbData
= dataLen
;
1917 if (dataLen
> cbData
)
1919 msg
->data
.pbData
= CryptMemRealloc(msg
->data
.pbData
,
1921 if (msg
->data
.pbData
)
1924 ret
= CryptEncrypt(msg
->key
, 0, TRUE
, 0,
1925 msg
->data
.pbData
, &dataLen
, msg
->data
.cbData
);
1931 CryptMemFree(msg
->data
.pbData
);
1937 msg
->data
.cbData
= 0;
1938 msg
->data
.pbData
= NULL
;
1943 msg
->base
.state
= MsgStateFinalized
;
1949 static HCRYPTMSG
CEnvelopedEncodeMsg_Open(DWORD dwFlags
,
1950 const void *pvMsgEncodeInfo
, LPCSTR pszInnerContentObjID
,
1951 PCMSG_STREAM_INFO pStreamInfo
)
1953 CEnvelopedEncodeMsg
*msg
;
1954 const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
*info
= pvMsgEncodeInfo
;
1958 if (info
->cbSize
!= sizeof(CMSG_ENVELOPED_ENCODE_INFO
) &&
1959 info
->cbSize
!= sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
))
1961 SetLastError(E_INVALIDARG
);
1964 if (info
->cbSize
== sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
))
1965 FIXME("CMS fields unsupported\n");
1966 if (!(algID
= CertOIDToAlgId(info
->ContentEncryptionAlgorithm
.pszObjId
)))
1968 SetLastError(CRYPT_E_UNKNOWN_ALGO
);
1971 if (info
->cRecipients
&& !info
->rgpRecipientCert
)
1973 SetLastError(E_INVALIDARG
);
1976 if (info
->hCryptProv
)
1977 prov
= info
->hCryptProv
;
1980 prov
= I_CryptGetDefaultCryptProv(0);
1981 dwFlags
&= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG
;
1983 msg
= CryptMemAlloc(sizeof(CEnvelopedEncodeMsg
));
1986 CRYPT_DATA_BLOB encryptedKey
= { 0, NULL
};
1987 CMSG_CONTENT_ENCRYPT_INFO encryptInfo
;
1991 CryptMsgBase_Init((CryptMsgBase
*)msg
, dwFlags
, pStreamInfo
,
1992 CEnvelopedEncodeMsg_Close
, CEnvelopedEncodeMsg_GetParam
,
1993 CEnvelopedEncodeMsg_Update
, CRYPT_DefaultMsgControl
);
1994 ret
= CRYPT_ConstructAlgorithmId(&msg
->algo
,
1995 &info
->ContentEncryptionAlgorithm
);
1997 msg
->data
.cbData
= 0;
1998 msg
->data
.pbData
= NULL
;
1999 msg
->cRecipientInfo
= info
->cRecipients
;
2000 msg
->recipientInfo
= CryptMemAlloc(info
->cRecipients
*
2001 sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO
));
2002 if (!msg
->recipientInfo
)
2004 memset(&encryptInfo
, 0, sizeof(encryptInfo
));
2007 ret
= CContentEncryptInfo_Construct(&encryptInfo
, info
, prov
);
2010 ret
= CRYPT_GenKey(&encryptInfo
, algID
);
2012 msg
->key
= encryptInfo
.hContentEncryptKey
;
2015 for (i
= 0; ret
&& i
< msg
->cRecipientInfo
; ++i
)
2017 ret
= CRYPT_ExportEncryptedKey(&encryptInfo
, i
, &encryptedKey
);
2019 ret
= CRecipientInfo_Construct(&msg
->recipientInfo
[i
],
2020 info
->rgpRecipientCert
[i
], &encryptedKey
);
2022 CContentEncryptInfo_Free(&encryptInfo
);
2029 if (!msg
&& (dwFlags
& CMSG_CRYPT_RELEASE_CONTEXT_FLAG
))
2030 CryptReleaseContext(prov
, 0);
2034 HCRYPTMSG WINAPI
CryptMsgOpenToEncode(DWORD dwMsgEncodingType
, DWORD dwFlags
,
2035 DWORD dwMsgType
, const void *pvMsgEncodeInfo
, LPSTR pszInnerContentObjID
,
2036 PCMSG_STREAM_INFO pStreamInfo
)
2038 HCRYPTMSG msg
= NULL
;
2040 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType
, dwFlags
,
2041 dwMsgType
, pvMsgEncodeInfo
, debugstr_a(pszInnerContentObjID
), pStreamInfo
);
2043 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType
) != PKCS_7_ASN_ENCODING
)
2045 SetLastError(E_INVALIDARG
);
2051 msg
= CDataEncodeMsg_Open(dwFlags
, pvMsgEncodeInfo
,
2052 pszInnerContentObjID
, pStreamInfo
);
2055 msg
= CHashEncodeMsg_Open(dwFlags
, pvMsgEncodeInfo
,
2056 pszInnerContentObjID
, pStreamInfo
);
2059 msg
= CSignedEncodeMsg_Open(dwFlags
, pvMsgEncodeInfo
,
2060 pszInnerContentObjID
, pStreamInfo
);
2062 case CMSG_ENVELOPED
:
2063 msg
= CEnvelopedEncodeMsg_Open(dwFlags
, pvMsgEncodeInfo
,
2064 pszInnerContentObjID
, pStreamInfo
);
2066 case CMSG_SIGNED_AND_ENVELOPED
:
2067 case CMSG_ENCRYPTED
:
2068 /* defined but invalid, fall through */
2070 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2075 typedef struct _CEnvelopedDecodeMsg
2077 CRYPT_ENVELOPED_DATA
*data
;
2078 HCRYPTPROV crypt_prov
;
2079 CRYPT_DATA_BLOB content
;
2081 } CEnvelopedDecodeMsg
;
2083 typedef struct _CDecodeMsg
2087 HCRYPTPROV crypt_prov
;
2090 CSignedMsgData signed_data
;
2091 CEnvelopedDecodeMsg enveloped_data
;
2093 CRYPT_DATA_BLOB msg_data
;
2094 CRYPT_DATA_BLOB detached_data
;
2095 CONTEXT_PROPERTY_LIST
*properties
;
2098 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg
)
2100 CDecodeMsg
*msg
= hCryptMsg
;
2102 if (msg
->crypt_prov
&& msg
->base
.open_flags
& CMSG_CRYPT_RELEASE_CONTEXT_FLAG
)
2103 CryptReleaseContext(msg
->crypt_prov
, 0);
2108 CryptDestroyHash(msg
->u
.hash
);
2110 case CMSG_ENVELOPED
:
2111 if (msg
->u
.enveloped_data
.crypt_prov
)
2112 CryptReleaseContext(msg
->u
.enveloped_data
.crypt_prov
, 0);
2113 LocalFree(msg
->u
.enveloped_data
.data
);
2114 CryptMemFree(msg
->u
.enveloped_data
.content
.pbData
);
2117 if (msg
->u
.signed_data
.info
)
2119 LocalFree(msg
->u
.signed_data
.info
);
2120 CSignedMsgData_CloseHandles(&msg
->u
.signed_data
);
2124 CryptMemFree(msg
->msg_data
.pbData
);
2125 CryptMemFree(msg
->detached_data
.pbData
);
2126 ContextPropertyList_Free(msg
->properties
);
2129 static BOOL
CDecodeMsg_CopyData(CRYPT_DATA_BLOB
*blob
, const BYTE
*pbData
,
2137 blob
->pbData
= CryptMemRealloc(blob
->pbData
,
2138 blob
->cbData
+ cbData
);
2140 blob
->pbData
= CryptMemAlloc(cbData
);
2143 memcpy(blob
->pbData
+ blob
->cbData
, pbData
, cbData
);
2144 blob
->cbData
+= cbData
;
2152 static BOOL
CDecodeMsg_DecodeDataContent(CDecodeMsg
*msg
, const CRYPT_DER_BLOB
*blob
)
2155 CRYPT_DATA_BLOB
*data
;
2158 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_OCTET_STRING
,
2159 blob
->pbData
, blob
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &data
, &size
);
2162 ret
= ContextPropertyList_SetProperty(msg
->properties
,
2163 CMSG_CONTENT_PARAM
, data
->pbData
, data
->cbData
);
2169 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg
*msg
, DWORD param
,
2170 const CRYPT_ALGORITHM_IDENTIFIER
*id
)
2172 static const BYTE nullParams
[] = { ASN_NULL
, 0 };
2173 CRYPT_ALGORITHM_IDENTIFIER
*copy
;
2174 DWORD len
= sizeof(CRYPT_ALGORITHM_IDENTIFIER
);
2176 /* Linearize algorithm id */
2177 len
+= strlen(id
->pszObjId
) + 1;
2178 len
+= id
->Parameters
.cbData
;
2179 copy
= CryptMemAlloc(len
);
2183 (LPSTR
)((BYTE
*)copy
+ sizeof(CRYPT_ALGORITHM_IDENTIFIER
));
2184 strcpy(copy
->pszObjId
, id
->pszObjId
);
2185 copy
->Parameters
.pbData
= (BYTE
*)copy
->pszObjId
+ strlen(id
->pszObjId
)
2187 /* Trick: omit NULL parameters */
2188 if (id
->Parameters
.cbData
== sizeof(nullParams
) &&
2189 !memcmp(id
->Parameters
.pbData
, nullParams
, sizeof(nullParams
)))
2191 copy
->Parameters
.cbData
= 0;
2192 len
-= sizeof(nullParams
);
2195 copy
->Parameters
.cbData
= id
->Parameters
.cbData
;
2196 if (copy
->Parameters
.cbData
)
2197 memcpy(copy
->Parameters
.pbData
, id
->Parameters
.pbData
,
2198 id
->Parameters
.cbData
);
2199 ContextPropertyList_SetProperty(msg
->properties
, param
, (BYTE
*)copy
,
2205 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER
*id
)
2207 id
->pszObjId
= (LPSTR
)((BYTE
*)id
+ sizeof(CRYPT_ALGORITHM_IDENTIFIER
));
2208 id
->Parameters
.pbData
= (BYTE
*)id
->pszObjId
+ strlen(id
->pszObjId
) + 1;
2211 static BOOL
CDecodeMsg_DecodeHashedContent(CDecodeMsg
*msg
,
2212 const CRYPT_DER_BLOB
*blob
)
2215 CRYPT_DIGESTED_DATA
*digestedData
;
2218 ret
= CRYPT_AsnDecodePKCSDigestedData(blob
->pbData
, blob
->cbData
,
2219 CRYPT_DECODE_ALLOC_FLAG
, NULL
, (CRYPT_DIGESTED_DATA
*)&digestedData
,
2223 ContextPropertyList_SetProperty(msg
->properties
, CMSG_VERSION_PARAM
,
2224 (const BYTE
*)&digestedData
->version
, sizeof(digestedData
->version
));
2225 CDecodeMsg_SaveAlgorithmID(msg
, CMSG_HASH_ALGORITHM_PARAM
,
2226 &digestedData
->DigestAlgorithm
);
2227 ContextPropertyList_SetProperty(msg
->properties
,
2228 CMSG_INNER_CONTENT_TYPE_PARAM
,
2229 (const BYTE
*)digestedData
->ContentInfo
.pszObjId
,
2230 digestedData
->ContentInfo
.pszObjId
?
2231 strlen(digestedData
->ContentInfo
.pszObjId
) + 1 : 0);
2232 if (!(msg
->base
.open_flags
& CMSG_DETACHED_FLAG
))
2234 if (digestedData
->ContentInfo
.Content
.cbData
)
2235 CDecodeMsg_DecodeDataContent(msg
,
2236 &digestedData
->ContentInfo
.Content
);
2238 ContextPropertyList_SetProperty(msg
->properties
,
2239 CMSG_CONTENT_PARAM
, NULL
, 0);
2241 ContextPropertyList_SetProperty(msg
->properties
, CMSG_HASH_DATA_PARAM
,
2242 digestedData
->hash
.pbData
, digestedData
->hash
.cbData
);
2243 LocalFree(digestedData
);
2248 static BOOL
CDecodeMsg_DecodeEnvelopedContent(CDecodeMsg
*msg
,
2249 const CRYPT_DER_BLOB
*blob
)
2252 CRYPT_ENVELOPED_DATA
*envelopedData
;
2255 ret
= CRYPT_AsnDecodePKCSEnvelopedData(blob
->pbData
, blob
->cbData
,
2256 CRYPT_DECODE_ALLOC_FLAG
, NULL
, (CRYPT_ENVELOPED_DATA
*)&envelopedData
,
2259 msg
->u
.enveloped_data
.data
= envelopedData
;
2263 static BOOL
CDecodeMsg_DecodeSignedContent(CDecodeMsg
*msg
,
2264 const CRYPT_DER_BLOB
*blob
)
2267 CRYPT_SIGNED_INFO
*signedInfo
;
2270 ret
= CRYPT_AsnDecodeCMSSignedInfo(blob
->pbData
, blob
->cbData
,
2271 CRYPT_DECODE_ALLOC_FLAG
, NULL
, (CRYPT_SIGNED_INFO
*)&signedInfo
,
2274 msg
->u
.signed_data
.info
= signedInfo
;
2278 /* Decodes the content in blob as the type given, and updates the value
2279 * (type, parameters, etc.) of msg based on what blob contains.
2280 * It doesn't just use msg's type, to allow a recursive call from an implicitly
2281 * typed message once the outer content info has been decoded.
2283 static BOOL
CDecodeMsg_DecodeContent(CDecodeMsg
*msg
, const CRYPT_DER_BLOB
*blob
,
2291 if ((ret
= CDecodeMsg_DecodeDataContent(msg
, blob
)))
2292 msg
->type
= CMSG_DATA
;
2295 if ((ret
= CDecodeMsg_DecodeHashedContent(msg
, blob
)))
2296 msg
->type
= CMSG_HASHED
;
2298 case CMSG_ENVELOPED
:
2299 if ((ret
= CDecodeMsg_DecodeEnvelopedContent(msg
, blob
)))
2300 msg
->type
= CMSG_ENVELOPED
;
2303 if ((ret
= CDecodeMsg_DecodeSignedContent(msg
, blob
)))
2304 msg
->type
= CMSG_SIGNED
;
2308 CRYPT_CONTENT_INFO
*info
;
2311 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, PKCS_CONTENT_INFO
,
2312 msg
->msg_data
.pbData
, msg
->msg_data
.cbData
, CRYPT_DECODE_ALLOC_FLAG
,
2313 NULL
, &info
, &size
);
2316 if (!strcmp(info
->pszObjId
, szOID_RSA_data
))
2317 ret
= CDecodeMsg_DecodeContent(msg
, &info
->Content
, CMSG_DATA
);
2318 else if (!strcmp(info
->pszObjId
, szOID_RSA_digestedData
))
2319 ret
= CDecodeMsg_DecodeContent(msg
, &info
->Content
,
2321 else if (!strcmp(info
->pszObjId
, szOID_RSA_envelopedData
))
2322 ret
= CDecodeMsg_DecodeContent(msg
, &info
->Content
,
2324 else if (!strcmp(info
->pszObjId
, szOID_RSA_signedData
))
2325 ret
= CDecodeMsg_DecodeContent(msg
, &info
->Content
,
2329 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2339 static BOOL
CDecodeMsg_FinalizeHashedContent(CDecodeMsg
*msg
,
2340 CRYPT_DER_BLOB
*blob
)
2342 CRYPT_ALGORITHM_IDENTIFIER
*hashAlgoID
= NULL
;
2347 CryptMsgGetParam(msg
, CMSG_HASH_ALGORITHM_PARAM
, 0, NULL
, &size
);
2348 hashAlgoID
= CryptMemAlloc(size
);
2349 ret
= CryptMsgGetParam(msg
, CMSG_HASH_ALGORITHM_PARAM
, 0, hashAlgoID
,
2352 algID
= CertOIDToAlgId(hashAlgoID
->pszObjId
);
2354 if (!msg
->crypt_prov
)
2356 msg
->crypt_prov
= I_CryptGetDefaultCryptProv(algID
);
2357 if (msg
->crypt_prov
)
2358 msg
->base
.open_flags
&= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG
;
2361 ret
= CryptCreateHash(msg
->crypt_prov
, algID
, 0, 0, &msg
->u
.hash
);
2364 CRYPT_DATA_BLOB content
;
2366 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
2368 /* Unlike for non-detached messages, the data were never stored as
2369 * the content param, but were saved in msg->detached_data instead.
2371 content
.pbData
= msg
->detached_data
.pbData
;
2372 content
.cbData
= msg
->detached_data
.cbData
;
2375 ret
= ContextPropertyList_FindProperty(msg
->properties
,
2376 CMSG_CONTENT_PARAM
, &content
);
2378 ret
= CryptHashData(msg
->u
.hash
, content
.pbData
, content
.cbData
, 0);
2380 CryptMemFree(hashAlgoID
);
2384 static BOOL
CDecodeMsg_FinalizeEnvelopedContent(CDecodeMsg
*msg
,
2385 CRYPT_DER_BLOB
*blob
)
2387 CRYPT_DATA_BLOB
*content
;
2389 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
2390 content
= &msg
->detached_data
;
2393 &msg
->u
.enveloped_data
.data
->encryptedContentInfo
.encryptedContent
;
2395 return CRYPT_ConstructBlob(&msg
->u
.enveloped_data
.content
, content
);
2398 static BOOL
CDecodeMsg_FinalizeSignedContent(CDecodeMsg
*msg
,
2399 CRYPT_DER_BLOB
*blob
)
2404 ret
= CSignedMsgData_AllocateHandles(&msg
->u
.signed_data
);
2405 for (i
= 0; ret
&& i
< msg
->u
.signed_data
.info
->cSignerInfo
; i
++)
2406 ret
= CSignedMsgData_ConstructSignerHandles(&msg
->u
.signed_data
, i
,
2407 &msg
->crypt_prov
, &msg
->base
.open_flags
);
2410 CRYPT_DATA_BLOB
*content
;
2412 /* Now that we have all the content, update the hash handles with
2413 * it. If the message is a detached message, the content is stored
2414 * in msg->detached_data rather than in the signed message's
2417 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
2418 content
= &msg
->detached_data
;
2420 content
= &msg
->u
.signed_data
.info
->content
.Content
;
2421 if (content
->cbData
)
2423 /* If the message is not detached, have to decode the message's
2424 * content if the type is szOID_RSA_data.
2426 if (!(msg
->base
.open_flags
& CMSG_DETACHED_FLAG
) &&
2427 !strcmp(msg
->u
.signed_data
.info
->content
.pszObjId
,
2430 CRYPT_DATA_BLOB
*rsa_blob
;
2432 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
2433 X509_OCTET_STRING
, content
->pbData
, content
->cbData
,
2434 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &rsa_blob
, &size
);
2437 ret
= CSignedMsgData_Update(&msg
->u
.signed_data
,
2438 rsa_blob
->pbData
, rsa_blob
->cbData
, TRUE
, Verify
);
2439 LocalFree(rsa_blob
);
2443 ret
= CSignedMsgData_Update(&msg
->u
.signed_data
,
2444 content
->pbData
, content
->cbData
, TRUE
, Verify
);
2450 static BOOL
CDecodeMsg_FinalizeContent(CDecodeMsg
*msg
, CRYPT_DER_BLOB
*blob
)
2457 ret
= CDecodeMsg_FinalizeHashedContent(msg
, blob
);
2459 case CMSG_ENVELOPED
:
2460 ret
= CDecodeMsg_FinalizeEnvelopedContent(msg
, blob
);
2463 ret
= CDecodeMsg_FinalizeSignedContent(msg
, blob
);
2471 static BOOL
CDecodeMsg_Update(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
2472 DWORD cbData
, BOOL fFinal
)
2474 CDecodeMsg
*msg
= hCryptMsg
;
2477 TRACE("(%p, %p, %d, %d)\n", hCryptMsg
, pbData
, cbData
, fFinal
);
2479 if (msg
->base
.state
== MsgStateFinalized
)
2480 SetLastError(CRYPT_E_MSG_ERROR
);
2481 else if (msg
->base
.streamed
)
2483 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg
, pbData
,
2485 switch (msg
->base
.state
)
2488 ret
= CDecodeMsg_CopyData(&msg
->msg_data
, pbData
, cbData
);
2491 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
2492 msg
->base
.state
= MsgStateDataFinalized
;
2494 msg
->base
.state
= MsgStateFinalized
;
2497 msg
->base
.state
= MsgStateUpdated
;
2499 case MsgStateUpdated
:
2500 ret
= CDecodeMsg_CopyData(&msg
->msg_data
, pbData
, cbData
);
2503 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
2504 msg
->base
.state
= MsgStateDataFinalized
;
2506 msg
->base
.state
= MsgStateFinalized
;
2509 case MsgStateDataFinalized
:
2510 ret
= CDecodeMsg_CopyData(&msg
->detached_data
, pbData
, cbData
);
2512 msg
->base
.state
= MsgStateFinalized
;
2515 SetLastError(CRYPT_E_MSG_ERROR
);
2522 SetLastError(CRYPT_E_MSG_ERROR
);
2525 switch (msg
->base
.state
)
2528 ret
= CDecodeMsg_CopyData(&msg
->msg_data
, pbData
, cbData
);
2529 if (msg
->base
.open_flags
& CMSG_DETACHED_FLAG
)
2530 msg
->base
.state
= MsgStateDataFinalized
;
2532 msg
->base
.state
= MsgStateFinalized
;
2534 case MsgStateDataFinalized
:
2535 ret
= CDecodeMsg_CopyData(&msg
->detached_data
, pbData
, cbData
);
2536 msg
->base
.state
= MsgStateFinalized
;
2539 SetLastError(CRYPT_E_MSG_ERROR
);
2543 if (ret
&& fFinal
&&
2544 ((msg
->base
.open_flags
& CMSG_DETACHED_FLAG
&& msg
->base
.state
==
2545 MsgStateDataFinalized
) ||
2546 (!(msg
->base
.open_flags
& CMSG_DETACHED_FLAG
) && msg
->base
.state
==
2547 MsgStateFinalized
)))
2548 ret
= CDecodeMsg_DecodeContent(msg
, &msg
->msg_data
, msg
->type
);
2549 if (ret
&& msg
->base
.state
== MsgStateFinalized
)
2550 ret
= CDecodeMsg_FinalizeContent(msg
, &msg
->msg_data
);
2554 static BOOL
CDecodeHashMsg_GetParam(CDecodeMsg
*msg
, DWORD dwParamType
,
2555 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
2559 switch (dwParamType
)
2561 case CMSG_TYPE_PARAM
:
2562 ret
= CRYPT_CopyParam(pvData
, pcbData
, &msg
->type
, sizeof(msg
->type
));
2564 case CMSG_HASH_ALGORITHM_PARAM
:
2566 CRYPT_DATA_BLOB blob
;
2568 ret
= ContextPropertyList_FindProperty(msg
->properties
, dwParamType
,
2572 ret
= CRYPT_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
2574 CRYPT_FixUpAlgorithmID(pvData
);
2577 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2580 case CMSG_COMPUTED_HASH_PARAM
:
2581 ret
= CryptGetHashParam(msg
->u
.hash
, HP_HASHVAL
, pvData
, pcbData
, 0);
2585 CRYPT_DATA_BLOB blob
;
2587 ret
= ContextPropertyList_FindProperty(msg
->properties
, dwParamType
,
2590 ret
= CRYPT_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
2592 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2598 /* nextData is an in/out parameter - on input it's the memory location in
2599 * which a copy of in's data should be made, and on output it's the memory
2600 * location immediately after out's copy of in's data.
2602 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB
*out
,
2603 const CRYPT_DATA_BLOB
*in
, LPBYTE
*nextData
)
2605 out
->cbData
= in
->cbData
;
2608 out
->pbData
= *nextData
;
2609 memcpy(out
->pbData
, in
->pbData
, in
->cbData
);
2610 *nextData
+= in
->cbData
;
2614 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER
*out
,
2615 const CRYPT_ALGORITHM_IDENTIFIER
*in
, LPBYTE
*nextData
)
2619 out
->pszObjId
= (LPSTR
)*nextData
;
2620 strcpy(out
->pszObjId
, in
->pszObjId
);
2621 *nextData
+= strlen(out
->pszObjId
) + 1;
2623 CRYPT_CopyBlob(&out
->Parameters
, &in
->Parameters
, nextData
);
2626 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES
*out
,
2627 const CRYPT_ATTRIBUTES
*in
, LPBYTE
*nextData
)
2629 out
->cAttr
= in
->cAttr
;
2634 *nextData
= POINTER_ALIGN_DWORD_PTR(*nextData
);
2635 out
->rgAttr
= (CRYPT_ATTRIBUTE
*)*nextData
;
2636 *nextData
+= in
->cAttr
* sizeof(CRYPT_ATTRIBUTE
);
2637 for (i
= 0; i
< in
->cAttr
; i
++)
2639 if (in
->rgAttr
[i
].pszObjId
)
2641 out
->rgAttr
[i
].pszObjId
= (LPSTR
)*nextData
;
2642 strcpy(out
->rgAttr
[i
].pszObjId
, in
->rgAttr
[i
].pszObjId
);
2643 *nextData
+= strlen(in
->rgAttr
[i
].pszObjId
) + 1;
2645 if (in
->rgAttr
[i
].cValue
)
2649 out
->rgAttr
[i
].cValue
= in
->rgAttr
[i
].cValue
;
2650 *nextData
= POINTER_ALIGN_DWORD_PTR(*nextData
);
2651 out
->rgAttr
[i
].rgValue
= (PCRYPT_DATA_BLOB
)*nextData
;
2652 *nextData
+= in
->rgAttr
[i
].cValue
* sizeof(CRYPT_DATA_BLOB
);
2653 for (j
= 0; j
< in
->rgAttr
[i
].cValue
; j
++)
2654 CRYPT_CopyBlob(&out
->rgAttr
[i
].rgValue
[j
],
2655 &in
->rgAttr
[i
].rgValue
[j
], nextData
);
2661 static DWORD
CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES
*attr
)
2663 DWORD size
= attr
->cAttr
* sizeof(CRYPT_ATTRIBUTE
), i
, j
;
2665 for (i
= 0; i
< attr
->cAttr
; i
++)
2667 if (attr
->rgAttr
[i
].pszObjId
)
2668 size
+= strlen(attr
->rgAttr
[i
].pszObjId
) + 1;
2670 size
= ALIGN_DWORD_PTR(size
);
2671 size
+= attr
->rgAttr
[i
].cValue
* sizeof(CRYPT_DATA_BLOB
);
2672 for (j
= 0; j
< attr
->rgAttr
[i
].cValue
; j
++)
2673 size
+= attr
->rgAttr
[i
].rgValue
[j
].cbData
;
2675 /* align pointer again to be conservative */
2676 size
= ALIGN_DWORD_PTR(size
);
2680 static DWORD
CRYPT_SizeOfKeyIdAsIssuerAndSerial(const CRYPT_DATA_BLOB
*keyId
)
2682 static char oid_key_rdn
[] = szOID_KEYID_RDN
;
2685 CERT_RDN rdn
= { 1, &attr
};
2686 CERT_NAME_INFO name
= { 1, &rdn
};
2688 attr
.pszObjId
= oid_key_rdn
;
2689 attr
.dwValueType
= CERT_RDN_OCTET_STRING
;
2690 attr
.Value
.cbData
= keyId
->cbData
;
2691 attr
.Value
.pbData
= keyId
->pbData
;
2692 if (CryptEncodeObject(X509_ASN_ENCODING
, X509_NAME
, &name
, NULL
, &size
))
2693 size
++; /* Only include size of special zero serial number on success */
2697 static BOOL
CRYPT_CopyKeyIdAsIssuerAndSerial(CERT_NAME_BLOB
*issuer
,
2698 CRYPT_INTEGER_BLOB
*serialNumber
, const CRYPT_DATA_BLOB
*keyId
, DWORD encodedLen
,
2701 static char oid_key_rdn
[] = szOID_KEYID_RDN
;
2703 CERT_RDN rdn
= { 1, &attr
};
2704 CERT_NAME_INFO name
= { 1, &rdn
};
2707 /* Encode special zero serial number */
2708 serialNumber
->cbData
= 1;
2709 serialNumber
->pbData
= *nextData
;
2713 issuer
->pbData
= *nextData
;
2714 attr
.pszObjId
= oid_key_rdn
;
2715 attr
.dwValueType
= CERT_RDN_OCTET_STRING
;
2716 attr
.Value
.cbData
= keyId
->cbData
;
2717 attr
.Value
.pbData
= keyId
->pbData
;
2718 ret
= CryptEncodeObject(X509_ASN_ENCODING
, X509_NAME
, &name
, *nextData
,
2722 *nextData
+= encodedLen
;
2723 issuer
->cbData
= encodedLen
;
2728 static BOOL
CRYPT_CopySignerInfo(void *pvData
, DWORD
*pcbData
,
2729 const CMSG_CMS_SIGNER_INFO
*in
)
2731 DWORD size
= sizeof(CMSG_SIGNER_INFO
), rdnSize
= 0;
2734 TRACE("(%p, %d, %p)\n", pvData
, pvData
? *pcbData
: 0, in
);
2736 if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
2738 size
+= in
->SignerId
.u
.IssuerSerialNumber
.Issuer
.cbData
;
2739 size
+= in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
.cbData
;
2743 rdnSize
= CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in
->SignerId
.u
.KeyId
);
2746 if (in
->HashAlgorithm
.pszObjId
)
2747 size
+= strlen(in
->HashAlgorithm
.pszObjId
) + 1;
2748 size
+= in
->HashAlgorithm
.Parameters
.cbData
;
2749 if (in
->HashEncryptionAlgorithm
.pszObjId
)
2750 size
+= strlen(in
->HashEncryptionAlgorithm
.pszObjId
) + 1;
2751 size
+= in
->HashEncryptionAlgorithm
.Parameters
.cbData
;
2752 size
+= in
->EncryptedHash
.cbData
;
2754 size
= ALIGN_DWORD_PTR(size
);
2755 size
+= CRYPT_SizeOfAttributes(&in
->AuthAttrs
);
2756 size
+= CRYPT_SizeOfAttributes(&in
->UnauthAttrs
);
2761 else if (*pcbData
< size
)
2763 SetLastError(ERROR_MORE_DATA
);
2768 LPBYTE nextData
= (BYTE
*)pvData
+ sizeof(CMSG_SIGNER_INFO
);
2769 CMSG_SIGNER_INFO
*out
= pvData
;
2772 out
->dwVersion
= in
->dwVersion
;
2773 if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
2775 CRYPT_CopyBlob(&out
->Issuer
,
2776 &in
->SignerId
.u
.IssuerSerialNumber
.Issuer
, &nextData
);
2777 CRYPT_CopyBlob(&out
->SerialNumber
,
2778 &in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
, &nextData
);
2781 ret
= CRYPT_CopyKeyIdAsIssuerAndSerial(&out
->Issuer
, &out
->SerialNumber
,
2782 &in
->SignerId
.u
.KeyId
, rdnSize
, &nextData
);
2785 CRYPT_CopyAlgorithmId(&out
->HashAlgorithm
, &in
->HashAlgorithm
,
2787 CRYPT_CopyAlgorithmId(&out
->HashEncryptionAlgorithm
,
2788 &in
->HashEncryptionAlgorithm
, &nextData
);
2789 CRYPT_CopyBlob(&out
->EncryptedHash
, &in
->EncryptedHash
, &nextData
);
2790 nextData
= POINTER_ALIGN_DWORD_PTR(nextData
);
2791 CRYPT_CopyAttributes(&out
->AuthAttrs
, &in
->AuthAttrs
, &nextData
);
2792 CRYPT_CopyAttributes(&out
->UnauthAttrs
, &in
->UnauthAttrs
, &nextData
);
2796 TRACE("returning %d\n", ret
);
2800 static BOOL
CRYPT_CopyCMSSignerInfo(void *pvData
, DWORD
*pcbData
,
2801 const CMSG_CMS_SIGNER_INFO
*in
)
2803 DWORD size
= sizeof(CMSG_CMS_SIGNER_INFO
);
2806 TRACE("(%p, %d, %p)\n", pvData
, pvData
? *pcbData
: 0, in
);
2808 if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
2810 size
+= in
->SignerId
.u
.IssuerSerialNumber
.Issuer
.cbData
;
2811 size
+= in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
.cbData
;
2814 size
+= in
->SignerId
.u
.KeyId
.cbData
;
2815 if (in
->HashAlgorithm
.pszObjId
)
2816 size
+= strlen(in
->HashAlgorithm
.pszObjId
) + 1;
2817 size
+= in
->HashAlgorithm
.Parameters
.cbData
;
2818 if (in
->HashEncryptionAlgorithm
.pszObjId
)
2819 size
+= strlen(in
->HashEncryptionAlgorithm
.pszObjId
) + 1;
2820 size
+= in
->HashEncryptionAlgorithm
.Parameters
.cbData
;
2821 size
+= in
->EncryptedHash
.cbData
;
2823 size
= ALIGN_DWORD_PTR(size
);
2824 size
+= CRYPT_SizeOfAttributes(&in
->AuthAttrs
);
2825 size
+= CRYPT_SizeOfAttributes(&in
->UnauthAttrs
);
2831 else if (*pcbData
< size
)
2834 SetLastError(ERROR_MORE_DATA
);
2839 LPBYTE nextData
= (BYTE
*)pvData
+ sizeof(CMSG_CMS_SIGNER_INFO
);
2840 CMSG_CMS_SIGNER_INFO
*out
= pvData
;
2842 out
->dwVersion
= in
->dwVersion
;
2843 out
->SignerId
.dwIdChoice
= in
->SignerId
.dwIdChoice
;
2844 if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
2846 CRYPT_CopyBlob(&out
->SignerId
.u
.IssuerSerialNumber
.Issuer
,
2847 &in
->SignerId
.u
.IssuerSerialNumber
.Issuer
, &nextData
);
2848 CRYPT_CopyBlob(&out
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
,
2849 &in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
, &nextData
);
2852 CRYPT_CopyBlob(&out
->SignerId
.u
.KeyId
, &in
->SignerId
.u
.KeyId
, &nextData
);
2853 CRYPT_CopyAlgorithmId(&out
->HashAlgorithm
, &in
->HashAlgorithm
,
2855 CRYPT_CopyAlgorithmId(&out
->HashEncryptionAlgorithm
,
2856 &in
->HashEncryptionAlgorithm
, &nextData
);
2857 CRYPT_CopyBlob(&out
->EncryptedHash
, &in
->EncryptedHash
, &nextData
);
2858 nextData
= POINTER_ALIGN_DWORD_PTR(nextData
);
2859 CRYPT_CopyAttributes(&out
->AuthAttrs
, &in
->AuthAttrs
, &nextData
);
2860 CRYPT_CopyAttributes(&out
->UnauthAttrs
, &in
->UnauthAttrs
, &nextData
);
2863 TRACE("returning %d\n", ret
);
2867 static BOOL
CRYPT_CopySignerCertInfo(void *pvData
, DWORD
*pcbData
,
2868 const CMSG_CMS_SIGNER_INFO
*in
)
2870 DWORD size
= sizeof(CERT_INFO
), rdnSize
= 0;
2873 TRACE("(%p, %d, %p)\n", pvData
, pvData
? *pcbData
: 0, in
);
2875 if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
2877 size
+= in
->SignerId
.u
.IssuerSerialNumber
.Issuer
.cbData
;
2878 size
+= in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
.cbData
;
2882 rdnSize
= CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in
->SignerId
.u
.KeyId
);
2890 else if (*pcbData
< size
)
2893 SetLastError(ERROR_MORE_DATA
);
2898 LPBYTE nextData
= (BYTE
*)pvData
+ sizeof(CERT_INFO
);
2899 CERT_INFO
*out
= pvData
;
2901 memset(out
, 0, sizeof(CERT_INFO
));
2902 if (in
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
2904 CRYPT_CopyBlob(&out
->Issuer
,
2905 &in
->SignerId
.u
.IssuerSerialNumber
.Issuer
, &nextData
);
2906 CRYPT_CopyBlob(&out
->SerialNumber
,
2907 &in
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
, &nextData
);
2911 ret
= CRYPT_CopyKeyIdAsIssuerAndSerial(&out
->Issuer
, &out
->SerialNumber
,
2912 &in
->SignerId
.u
.KeyId
, rdnSize
, &nextData
);
2914 TRACE("returning %d\n", ret
);
2918 static BOOL
CRYPT_CopyRecipientInfo(void *pvData
, DWORD
*pcbData
,
2919 const CERT_ISSUER_SERIAL_NUMBER
*in
)
2921 DWORD size
= sizeof(CERT_INFO
);
2924 TRACE("(%p, %d, %p)\n", pvData
, pvData
? *pcbData
: 0, in
);
2926 size
+= in
->SerialNumber
.cbData
;
2927 size
+= in
->Issuer
.cbData
;
2933 else if (*pcbData
< size
)
2936 SetLastError(ERROR_MORE_DATA
);
2941 LPBYTE nextData
= (BYTE
*)pvData
+ sizeof(CERT_INFO
);
2942 CERT_INFO
*out
= pvData
;
2944 CRYPT_CopyBlob(&out
->SerialNumber
, &in
->SerialNumber
, &nextData
);
2945 CRYPT_CopyBlob(&out
->Issuer
, &in
->Issuer
, &nextData
);
2948 TRACE("returning %d\n", ret
);
2952 static BOOL
CDecodeEnvelopedMsg_GetParam(CDecodeMsg
*msg
, DWORD dwParamType
,
2953 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
2957 switch (dwParamType
)
2959 case CMSG_TYPE_PARAM
:
2960 ret
= CRYPT_CopyParam(pvData
, pcbData
, &msg
->type
, sizeof(msg
->type
));
2962 case CMSG_CONTENT_PARAM
:
2963 if (msg
->u
.enveloped_data
.data
)
2964 ret
= CRYPT_CopyParam(pvData
, pcbData
,
2965 msg
->u
.enveloped_data
.content
.pbData
,
2966 msg
->u
.enveloped_data
.content
.cbData
);
2968 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2970 case CMSG_RECIPIENT_COUNT_PARAM
:
2971 if (msg
->u
.enveloped_data
.data
)
2972 ret
= CRYPT_CopyParam(pvData
, pcbData
,
2973 &msg
->u
.enveloped_data
.data
->cRecipientInfo
, sizeof(DWORD
));
2975 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2977 case CMSG_RECIPIENT_INFO_PARAM
:
2978 if (msg
->u
.enveloped_data
.data
)
2980 if (dwIndex
< msg
->u
.enveloped_data
.data
->cRecipientInfo
)
2982 PCMSG_KEY_TRANS_RECIPIENT_INFO recipientInfo
=
2983 &msg
->u
.enveloped_data
.data
->rgRecipientInfo
[dwIndex
];
2985 ret
= CRYPT_CopyRecipientInfo(pvData
, pcbData
,
2986 &recipientInfo
->RecipientId
.u
.IssuerSerialNumber
);
2989 SetLastError(CRYPT_E_INVALID_INDEX
);
2992 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2995 FIXME("unimplemented for %d\n", dwParamType
);
2996 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3001 static BOOL
CRYPT_CopyUnauthAttr(void *pvData
, DWORD
*pcbData
,
3002 const CMSG_CMS_SIGNER_INFO
*in
)
3007 TRACE("(%p, %d, %p)\n", pvData
, pvData
? *pcbData
: 0, in
);
3009 size
= CRYPT_SizeOfAttributes(&in
->UnauthAttrs
);
3015 else if (*pcbData
< size
)
3018 SetLastError(ERROR_MORE_DATA
);
3023 CRYPT_ATTRIBUTES
*out
= pvData
;
3025 CRYPT_ConstructAttributes(out
,&in
->UnauthAttrs
);
3028 TRACE("returning %d\n", ret
);
3032 static BOOL
CDecodeSignedMsg_GetParam(CDecodeMsg
*msg
, DWORD dwParamType
,
3033 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
3037 switch (dwParamType
)
3039 case CMSG_TYPE_PARAM
:
3040 ret
= CRYPT_CopyParam(pvData
, pcbData
, &msg
->type
, sizeof(msg
->type
));
3042 case CMSG_CONTENT_PARAM
:
3043 if (msg
->u
.signed_data
.info
)
3045 if (!strcmp(msg
->u
.signed_data
.info
->content
.pszObjId
,
3048 CRYPT_DATA_BLOB
*blob
;
3051 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_OCTET_STRING
,
3052 msg
->u
.signed_data
.info
->content
.Content
.pbData
,
3053 msg
->u
.signed_data
.info
->content
.Content
.cbData
,
3054 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &blob
, &size
);
3057 ret
= CRYPT_CopyParam(pvData
, pcbData
, blob
->pbData
,
3063 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3064 msg
->u
.signed_data
.info
->content
.Content
.pbData
,
3065 msg
->u
.signed_data
.info
->content
.Content
.cbData
);
3068 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3070 case CMSG_INNER_CONTENT_TYPE_PARAM
:
3071 if (msg
->u
.signed_data
.info
)
3072 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3073 msg
->u
.signed_data
.info
->content
.pszObjId
,
3074 strlen(msg
->u
.signed_data
.info
->content
.pszObjId
) + 1);
3076 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3078 case CMSG_SIGNER_COUNT_PARAM
:
3079 if (msg
->u
.signed_data
.info
)
3080 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3081 &msg
->u
.signed_data
.info
->cSignerInfo
, sizeof(DWORD
));
3083 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3085 case CMSG_SIGNER_INFO_PARAM
:
3086 if (msg
->u
.signed_data
.info
)
3088 if (dwIndex
>= msg
->u
.signed_data
.info
->cSignerInfo
)
3089 SetLastError(CRYPT_E_INVALID_INDEX
);
3091 ret
= CRYPT_CopySignerInfo(pvData
, pcbData
,
3092 &msg
->u
.signed_data
.info
->rgSignerInfo
[dwIndex
]);
3095 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3097 case CMSG_SIGNER_CERT_INFO_PARAM
:
3098 if (msg
->u
.signed_data
.info
)
3100 if (dwIndex
>= msg
->u
.signed_data
.info
->cSignerInfo
)
3101 SetLastError(CRYPT_E_INVALID_INDEX
);
3103 ret
= CRYPT_CopySignerCertInfo(pvData
, pcbData
,
3104 &msg
->u
.signed_data
.info
->rgSignerInfo
[dwIndex
]);
3107 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3109 case CMSG_CERT_COUNT_PARAM
:
3110 if (msg
->u
.signed_data
.info
)
3111 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3112 &msg
->u
.signed_data
.info
->cCertEncoded
, sizeof(DWORD
));
3114 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3116 case CMSG_CERT_PARAM
:
3117 if (msg
->u
.signed_data
.info
)
3119 if (dwIndex
>= msg
->u
.signed_data
.info
->cCertEncoded
)
3120 SetLastError(CRYPT_E_INVALID_INDEX
);
3122 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3123 msg
->u
.signed_data
.info
->rgCertEncoded
[dwIndex
].pbData
,
3124 msg
->u
.signed_data
.info
->rgCertEncoded
[dwIndex
].cbData
);
3127 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3129 case CMSG_CRL_COUNT_PARAM
:
3130 if (msg
->u
.signed_data
.info
)
3131 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3132 &msg
->u
.signed_data
.info
->cCrlEncoded
, sizeof(DWORD
));
3134 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3136 case CMSG_CRL_PARAM
:
3137 if (msg
->u
.signed_data
.info
)
3139 if (dwIndex
>= msg
->u
.signed_data
.info
->cCrlEncoded
)
3140 SetLastError(CRYPT_E_INVALID_INDEX
);
3142 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3143 msg
->u
.signed_data
.info
->rgCrlEncoded
[dwIndex
].pbData
,
3144 msg
->u
.signed_data
.info
->rgCrlEncoded
[dwIndex
].cbData
);
3147 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3149 case CMSG_COMPUTED_HASH_PARAM
:
3150 if (msg
->u
.signed_data
.info
)
3152 if (dwIndex
>= msg
->u
.signed_data
.cSignerHandle
)
3153 SetLastError(CRYPT_E_INVALID_INDEX
);
3155 ret
= CryptGetHashParam(
3156 msg
->u
.signed_data
.signerHandles
[dwIndex
].contentHash
,
3157 HP_HASHVAL
, pvData
, pcbData
, 0);
3160 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3162 case CMSG_ENCODED_SIGNER
:
3163 if (msg
->u
.signed_data
.info
)
3165 if (dwIndex
>= msg
->u
.signed_data
.info
->cSignerInfo
)
3166 SetLastError(CRYPT_E_INVALID_INDEX
);
3168 ret
= CryptEncodeObjectEx(
3169 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
, CMS_SIGNER_INFO
,
3170 &msg
->u
.signed_data
.info
->rgSignerInfo
[dwIndex
], 0, NULL
,
3174 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3176 case CMSG_ATTR_CERT_COUNT_PARAM
:
3177 if (msg
->u
.signed_data
.info
)
3179 DWORD attrCertCount
= 0;
3181 ret
= CRYPT_CopyParam(pvData
, pcbData
,
3182 &attrCertCount
, sizeof(DWORD
));
3185 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3187 case CMSG_ATTR_CERT_PARAM
:
3188 if (msg
->u
.signed_data
.info
)
3189 SetLastError(CRYPT_E_INVALID_INDEX
);
3191 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3193 case CMSG_CMS_SIGNER_INFO_PARAM
:
3194 if (msg
->u
.signed_data
.info
)
3196 if (dwIndex
>= msg
->u
.signed_data
.info
->cSignerInfo
)
3197 SetLastError(CRYPT_E_INVALID_INDEX
);
3199 ret
= CRYPT_CopyCMSSignerInfo(pvData
, pcbData
,
3200 &msg
->u
.signed_data
.info
->rgSignerInfo
[dwIndex
]);
3203 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3205 case CMSG_SIGNER_UNAUTH_ATTR_PARAM
:
3206 if (msg
->u
.signed_data
.info
)
3208 if (dwIndex
>= msg
->u
.signed_data
.info
->cSignerInfo
)
3209 SetLastError(CRYPT_E_INVALID_INDEX
);
3211 ret
= CRYPT_CopyUnauthAttr(pvData
, pcbData
,
3212 &msg
->u
.signed_data
.info
->rgSignerInfo
[dwIndex
]);
3215 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3218 FIXME("unimplemented for %d\n", dwParamType
);
3219 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3224 static BOOL
CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
3225 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
3227 CDecodeMsg
*msg
= hCryptMsg
;
3233 ret
= CDecodeHashMsg_GetParam(msg
, dwParamType
, dwIndex
, pvData
,
3236 case CMSG_ENVELOPED
:
3237 ret
= CDecodeEnvelopedMsg_GetParam(msg
, dwParamType
, dwIndex
, pvData
,
3241 ret
= CDecodeSignedMsg_GetParam(msg
, dwParamType
, dwIndex
, pvData
,
3245 switch (dwParamType
)
3247 case CMSG_TYPE_PARAM
:
3248 ret
= CRYPT_CopyParam(pvData
, pcbData
, &msg
->type
,
3253 CRYPT_DATA_BLOB blob
;
3255 ret
= ContextPropertyList_FindProperty(msg
->properties
, dwParamType
,
3258 ret
= CRYPT_CopyParam(pvData
, pcbData
, blob
.pbData
,
3261 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3268 static BOOL
CDecodeHashMsg_VerifyHash(CDecodeMsg
*msg
)
3271 CRYPT_DATA_BLOB hashBlob
;
3273 ret
= ContextPropertyList_FindProperty(msg
->properties
,
3274 CMSG_HASH_DATA_PARAM
, &hashBlob
);
3277 DWORD computedHashSize
= 0;
3279 ret
= CDecodeHashMsg_GetParam(msg
, CMSG_COMPUTED_HASH_PARAM
, 0, NULL
,
3281 if (hashBlob
.cbData
== computedHashSize
)
3283 LPBYTE computedHash
= CryptMemAlloc(computedHashSize
);
3287 ret
= CDecodeHashMsg_GetParam(msg
, CMSG_COMPUTED_HASH_PARAM
, 0,
3288 computedHash
, &computedHashSize
);
3291 if (memcmp(hashBlob
.pbData
, computedHash
, hashBlob
.cbData
))
3293 SetLastError(CRYPT_E_HASH_VALUE
);
3297 CryptMemFree(computedHash
);
3301 SetLastError(ERROR_OUTOFMEMORY
);
3307 SetLastError(CRYPT_E_HASH_VALUE
);
3314 static BOOL
CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg
*msg
,
3315 HCRYPTPROV prov
, DWORD signerIndex
, PCERT_PUBLIC_KEY_INFO keyInfo
)
3321 prov
= msg
->crypt_prov
;
3322 ret
= CryptImportPublicKeyInfo(prov
, X509_ASN_ENCODING
, keyInfo
, &key
);
3326 CRYPT_HASH_BLOB reversedHash
;
3328 if (msg
->u
.signed_data
.info
->rgSignerInfo
[signerIndex
].AuthAttrs
.cAttr
)
3329 hash
= msg
->u
.signed_data
.signerHandles
[signerIndex
].authAttrHash
;
3331 hash
= msg
->u
.signed_data
.signerHandles
[signerIndex
].contentHash
;
3332 ret
= CRYPT_ConstructBlob(&reversedHash
,
3333 &msg
->u
.signed_data
.info
->rgSignerInfo
[signerIndex
].EncryptedHash
);
3336 CRYPT_ReverseBytes(&reversedHash
);
3337 ret
= CryptVerifySignatureW(hash
, reversedHash
.pbData
,
3338 reversedHash
.cbData
, key
, NULL
, 0);
3339 CryptMemFree(reversedHash
.pbData
);
3341 CryptDestroyKey(key
);
3346 static BOOL
CDecodeSignedMsg_VerifySignature(CDecodeMsg
*msg
, PCERT_INFO info
)
3351 if (!msg
->u
.signed_data
.signerHandles
)
3353 SetLastError(NTE_BAD_SIGNATURE
);
3356 for (i
= 0; !ret
&& i
< msg
->u
.signed_data
.info
->cSignerInfo
; i
++)
3358 PCMSG_CMS_SIGNER_INFO signerInfo
=
3359 &msg
->u
.signed_data
.info
->rgSignerInfo
[i
];
3361 if (signerInfo
->SignerId
.dwIdChoice
== CERT_ID_ISSUER_SERIAL_NUMBER
)
3363 ret
= CertCompareCertificateName(X509_ASN_ENCODING
,
3364 &signerInfo
->SignerId
.u
.IssuerSerialNumber
.Issuer
,
3368 ret
= CertCompareIntegerBlob(
3369 &signerInfo
->SignerId
.u
.IssuerSerialNumber
.SerialNumber
,
3370 &info
->SerialNumber
);
3377 FIXME("signer %d: unimplemented for key id\n", i
);
3381 ret
= CDecodeSignedMsg_VerifySignatureWithKey(msg
, 0, i
,
3382 &info
->SubjectPublicKeyInfo
);
3384 SetLastError(CRYPT_E_SIGNER_NOT_FOUND
);
3389 static BOOL
CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg
*msg
,
3390 PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para
)
3394 if (para
->cbSize
!= sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA
))
3395 SetLastError(ERROR_INVALID_PARAMETER
);
3396 else if (para
->dwSignerIndex
>= msg
->u
.signed_data
.info
->cSignerInfo
)
3397 SetLastError(CRYPT_E_SIGNER_NOT_FOUND
);
3398 else if (!msg
->u
.signed_data
.signerHandles
)
3399 SetLastError(NTE_BAD_SIGNATURE
);
3402 switch (para
->dwSignerType
)
3404 case CMSG_VERIFY_SIGNER_PUBKEY
:
3405 ret
= CDecodeSignedMsg_VerifySignatureWithKey(msg
,
3406 para
->hCryptProv
, para
->dwSignerIndex
, para
->pvSigner
);
3408 case CMSG_VERIFY_SIGNER_CERT
:
3410 PCCERT_CONTEXT cert
= para
->pvSigner
;
3412 ret
= CDecodeSignedMsg_VerifySignatureWithKey(msg
, para
->hCryptProv
,
3413 para
->dwSignerIndex
, &cert
->pCertInfo
->SubjectPublicKeyInfo
);
3417 FIXME("unimplemented for signer type %d\n", para
->dwSignerType
);
3418 SetLastError(CRYPT_E_SIGNER_NOT_FOUND
);
3424 static BOOL WINAPI
CRYPT_ImportKeyTrans(
3425 PCRYPT_ALGORITHM_IDENTIFIER pContentEncryptionAlgorithm
,
3426 PCMSG_CTRL_KEY_TRANS_DECRYPT_PARA pKeyTransDecryptPara
, DWORD dwFlags
,
3427 void *pvReserved
, HCRYPTKEY
*phContentEncryptKey
)
3432 ret
= CryptGetUserKey(pKeyTransDecryptPara
->hCryptProv
,
3433 pKeyTransDecryptPara
->dwKeySpec
? pKeyTransDecryptPara
->dwKeySpec
:
3434 AT_KEYEXCHANGE
, &key
);
3437 CMSG_KEY_TRANS_RECIPIENT_INFO
*info
=
3438 &pKeyTransDecryptPara
->pKeyTrans
[pKeyTransDecryptPara
->dwRecipientIndex
];
3439 CRYPT_DATA_BLOB
*encryptedKey
= &info
->EncryptedKey
;
3440 DWORD size
= encryptedKey
->cbData
+ sizeof(BLOBHEADER
) + sizeof(ALG_ID
);
3441 BYTE
*keyBlob
= CryptMemAlloc(size
);
3445 DWORD i
, k
= size
- 1;
3446 BLOBHEADER
*blobHeader
= (BLOBHEADER
*)keyBlob
;
3447 ALG_ID
*algID
= (ALG_ID
*)(keyBlob
+ sizeof(BLOBHEADER
));
3449 blobHeader
->bType
= SIMPLEBLOB
;
3450 blobHeader
->bVersion
= CUR_BLOB_VERSION
;
3451 blobHeader
->reserved
= 0;
3452 blobHeader
->aiKeyAlg
= CertOIDToAlgId(
3453 pContentEncryptionAlgorithm
->pszObjId
);
3454 *algID
= CertOIDToAlgId(info
->KeyEncryptionAlgorithm
.pszObjId
);
3455 for (i
= 0; i
< encryptedKey
->cbData
; ++i
, --k
)
3456 keyBlob
[k
] = encryptedKey
->pbData
[i
];
3458 ret
= CryptImportKey(pKeyTransDecryptPara
->hCryptProv
, keyBlob
,
3459 size
, key
, 0, phContentEncryptKey
);
3460 CryptMemFree(keyBlob
);
3464 CryptDestroyKey(key
);
3469 static BOOL
CRYPT_ImportEncryptedKey(PCRYPT_ALGORITHM_IDENTIFIER contEncrAlg
,
3470 PCMSG_CTRL_DECRYPT_PARA para
, PCMSG_KEY_TRANS_RECIPIENT_INFO info
,
3473 static HCRYPTOIDFUNCSET set
= NULL
;
3474 PFN_CMSG_IMPORT_KEY_TRANS importKeyFunc
= NULL
;
3475 HCRYPTOIDFUNCADDR hFunc
= NULL
;
3476 CMSG_CTRL_KEY_TRANS_DECRYPT_PARA decryptPara
;
3479 memset(&decryptPara
, 0, sizeof(decryptPara
));
3480 decryptPara
.cbSize
= sizeof(decryptPara
);
3481 decryptPara
.hCryptProv
= para
->hCryptProv
;
3482 decryptPara
.dwKeySpec
= para
->dwKeySpec
;
3483 decryptPara
.pKeyTrans
= info
;
3484 decryptPara
.dwRecipientIndex
= para
->dwRecipientIndex
;
3487 set
= CryptInitOIDFunctionSet(CMSG_OID_IMPORT_KEY_TRANS_FUNC
, 0);
3488 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, contEncrAlg
->pszObjId
, 0,
3489 (void **)&importKeyFunc
, &hFunc
);
3491 importKeyFunc
= CRYPT_ImportKeyTrans
;
3492 ret
= importKeyFunc(contEncrAlg
, &decryptPara
, 0, NULL
, key
);
3494 CryptFreeOIDFunctionAddress(hFunc
, 0);
3498 static BOOL
CDecodeEnvelopedMsg_CrtlDecrypt(CDecodeMsg
*msg
,
3499 PCMSG_CTRL_DECRYPT_PARA para
)
3502 CEnvelopedDecodeMsg
*enveloped_data
= &msg
->u
.enveloped_data
;
3503 CRYPT_ENVELOPED_DATA
*data
= enveloped_data
->data
;
3505 if (para
->cbSize
!= sizeof(CMSG_CTRL_DECRYPT_PARA
))
3506 SetLastError(E_INVALIDARG
);
3508 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3509 else if (para
->dwRecipientIndex
>= data
->cRecipientInfo
)
3510 SetLastError(CRYPT_E_INVALID_INDEX
);
3511 else if (enveloped_data
->decrypted
)
3512 SetLastError(CRYPT_E_ALREADY_DECRYPTED
);
3513 else if (!para
->hCryptProv
)
3514 SetLastError(ERROR_INVALID_PARAMETER
);
3515 else if (enveloped_data
->content
.cbData
)
3519 ret
= CRYPT_ImportEncryptedKey(
3520 &data
->encryptedContentInfo
.contentEncryptionAlgorithm
, para
,
3521 data
->rgRecipientInfo
, &key
);
3524 ret
= CryptDecrypt(key
, 0, TRUE
, 0, enveloped_data
->content
.pbData
,
3525 &enveloped_data
->content
.cbData
);
3526 CryptDestroyKey(key
);
3532 enveloped_data
->decrypted
= TRUE
;
3536 static BOOL
CDecodeMsg_Control(HCRYPTMSG hCryptMsg
, DWORD dwFlags
,
3537 DWORD dwCtrlType
, const void *pvCtrlPara
)
3539 CDecodeMsg
*msg
= hCryptMsg
;
3544 case CMSG_CTRL_VERIFY_SIGNATURE
:
3548 ret
= CDecodeSignedMsg_VerifySignature(msg
, (PCERT_INFO
)pvCtrlPara
);
3551 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3554 case CMSG_CTRL_DECRYPT
:
3557 case CMSG_ENVELOPED
:
3558 ret
= CDecodeEnvelopedMsg_CrtlDecrypt(msg
,
3559 (PCMSG_CTRL_DECRYPT_PARA
)pvCtrlPara
);
3560 if (ret
&& (dwFlags
& CMSG_CRYPT_RELEASE_CONTEXT_FLAG
))
3561 msg
->u
.enveloped_data
.crypt_prov
=
3562 ((PCMSG_CTRL_DECRYPT_PARA
)pvCtrlPara
)->hCryptProv
;
3565 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3568 case CMSG_CTRL_VERIFY_HASH
:
3572 ret
= CDecodeHashMsg_VerifyHash(msg
);
3575 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3578 case CMSG_CTRL_VERIFY_SIGNATURE_EX
:
3582 ret
= CDecodeSignedMsg_VerifySignatureEx(msg
,
3583 (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA
)pvCtrlPara
);
3586 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
3590 SetLastError(CRYPT_E_CONTROL_TYPE
);
3595 HCRYPTMSG WINAPI
CryptMsgOpenToDecode(DWORD dwMsgEncodingType
, DWORD dwFlags
,
3596 DWORD dwMsgType
, HCRYPTPROV_LEGACY hCryptProv
, PCERT_INFO pRecipientInfo
,
3597 PCMSG_STREAM_INFO pStreamInfo
)
3601 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType
,
3602 dwFlags
, dwMsgType
, hCryptProv
, pRecipientInfo
, pStreamInfo
);
3604 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType
) != PKCS_7_ASN_ENCODING
)
3606 SetLastError(E_INVALIDARG
);
3609 msg
= CryptMemAlloc(sizeof(CDecodeMsg
));
3612 CryptMsgBase_Init((CryptMsgBase
*)msg
, dwFlags
, pStreamInfo
,
3613 CDecodeMsg_Close
, CDecodeMsg_GetParam
, CDecodeMsg_Update
,
3614 CDecodeMsg_Control
);
3615 msg
->type
= dwMsgType
;
3616 msg
->crypt_prov
= hCryptProv
;
3617 memset(&msg
->u
, 0, sizeof(msg
->u
));
3618 msg
->msg_data
.cbData
= 0;
3619 msg
->msg_data
.pbData
= NULL
;
3620 msg
->detached_data
.cbData
= 0;
3621 msg
->detached_data
.pbData
= NULL
;
3622 msg
->properties
= ContextPropertyList_Create();
3627 HCRYPTMSG WINAPI
CryptMsgDuplicate(HCRYPTMSG hCryptMsg
)
3629 TRACE("(%p)\n", hCryptMsg
);
3633 CryptMsgBase
*msg
= hCryptMsg
;
3635 InterlockedIncrement(&msg
->ref
);
3640 BOOL WINAPI
CryptMsgClose(HCRYPTMSG hCryptMsg
)
3642 TRACE("(%p)\n", hCryptMsg
);
3646 CryptMsgBase
*msg
= hCryptMsg
;
3648 if (InterlockedDecrement(&msg
->ref
) == 0)
3650 TRACE("freeing %p\n", msg
);
3659 BOOL WINAPI
CryptMsgUpdate(HCRYPTMSG hCryptMsg
, const BYTE
*pbData
,
3660 DWORD cbData
, BOOL fFinal
)
3662 CryptMsgBase
*msg
= hCryptMsg
;
3664 TRACE("(%p, %p, %d, %d)\n", hCryptMsg
, pbData
, cbData
, fFinal
);
3666 return msg
->update(hCryptMsg
, pbData
, cbData
, fFinal
);
3669 BOOL WINAPI
CryptMsgGetParam(HCRYPTMSG hCryptMsg
, DWORD dwParamType
,
3670 DWORD dwIndex
, void *pvData
, DWORD
*pcbData
)
3672 CryptMsgBase
*msg
= hCryptMsg
;
3674 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg
, dwParamType
, dwIndex
,
3676 return msg
->get_param(hCryptMsg
, dwParamType
, dwIndex
, pvData
, pcbData
);
3679 BOOL WINAPI
CryptMsgControl(HCRYPTMSG hCryptMsg
, DWORD dwFlags
,
3680 DWORD dwCtrlType
, const void *pvCtrlPara
)
3682 CryptMsgBase
*msg
= hCryptMsg
;
3684 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg
, dwFlags
, dwCtrlType
,
3686 return msg
->control(hCryptMsg
, dwFlags
, dwCtrlType
, pvCtrlPara
);
3689 static CERT_INFO
*CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg
,
3690 DWORD dwSignerIndex
)
3692 CERT_INFO
*certInfo
= NULL
;
3695 if (CryptMsgGetParam(msg
, CMSG_SIGNER_CERT_INFO_PARAM
, dwSignerIndex
, NULL
,
3698 certInfo
= CryptMemAlloc(size
);
3701 if (!CryptMsgGetParam(msg
, CMSG_SIGNER_CERT_INFO_PARAM
,
3702 dwSignerIndex
, certInfo
, &size
))
3704 CryptMemFree(certInfo
);
3712 BOOL WINAPI
CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg
, DWORD cSignerStore
,
3713 HCERTSTORE
*rghSignerStore
, DWORD dwFlags
, PCCERT_CONTEXT
*ppSigner
,
3714 DWORD
*pdwSignerIndex
)
3717 DWORD i
, signerIndex
= 0;
3718 PCCERT_CONTEXT signerCert
= NULL
;
3721 TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg
, cSignerStore
,
3722 rghSignerStore
, dwFlags
, ppSigner
, pdwSignerIndex
);
3724 /* Clear output parameters */
3727 if (pdwSignerIndex
&& !(dwFlags
& CMSG_USE_SIGNER_INDEX_FLAG
))
3728 *pdwSignerIndex
= 0;
3730 /* Create store to search for signer certificates */
3731 store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
3732 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
3733 if (!(dwFlags
& CMSG_TRUSTED_SIGNER_FLAG
))
3735 HCERTSTORE msgStore
= CertOpenStore(CERT_STORE_PROV_MSG
, 0, 0, 0,
3738 CertAddStoreToCollection(store
, msgStore
, 0, 0);
3739 CertCloseStore(msgStore
, 0);
3741 for (i
= 0; i
< cSignerStore
; i
++)
3742 CertAddStoreToCollection(store
, rghSignerStore
[i
], 0, 0);
3744 /* Find signer cert */
3745 if (dwFlags
& CMSG_USE_SIGNER_INDEX_FLAG
)
3747 CERT_INFO
*signer
= CRYPT_GetSignerCertInfoFromMsg(hCryptMsg
,
3752 signerIndex
= *pdwSignerIndex
;
3753 signerCert
= CertFindCertificateInStore(store
, X509_ASN_ENCODING
,
3754 0, CERT_FIND_SUBJECT_CERT
, signer
, NULL
);
3755 CryptMemFree(signer
);
3760 DWORD count
, size
= sizeof(count
);
3762 if (CryptMsgGetParam(hCryptMsg
, CMSG_SIGNER_COUNT_PARAM
, 0, &count
,
3765 for (i
= 0; !signerCert
&& i
< count
; i
++)
3767 CERT_INFO
*signer
= CRYPT_GetSignerCertInfoFromMsg(hCryptMsg
,
3772 signerCert
= CertFindCertificateInStore(store
,
3773 X509_ASN_ENCODING
, 0, CERT_FIND_SUBJECT_CERT
, signer
,
3777 CryptMemFree(signer
);
3782 SetLastError(CRYPT_E_NO_TRUSTED_SIGNER
);
3786 if (!(dwFlags
& CMSG_SIGNER_ONLY_FLAG
))
3787 ret
= CryptMsgControl(hCryptMsg
, 0, CMSG_CTRL_VERIFY_SIGNATURE
,
3788 signerCert
->pCertInfo
);
3794 *ppSigner
= CertDuplicateCertificateContext(signerCert
);
3796 *pdwSignerIndex
= signerIndex
;
3798 CertFreeCertificateContext(signerCert
);
3801 CertCloseStore(store
, 0);
3805 BOOL WINAPI
CryptMsgVerifyCountersignatureEncoded(HCRYPTPROV_LEGACY hCryptProv
,
3806 DWORD dwEncodingType
, BYTE
*pbSignerInfo
, DWORD cbSignerInfo
,
3807 PBYTE pbSignerInfoCountersignature
, DWORD cbSignerInfoCountersignature
,
3808 CERT_INFO
*pciCountersigner
)
3810 FIXME("(%08lx, %08x, %p, %d, %p, %d, %p): stub\n", hCryptProv
,
3811 dwEncodingType
, pbSignerInfo
, cbSignerInfo
, pbSignerInfoCountersignature
,
3812 cbSignerInfoCountersignature
, pciCountersigner
);
3816 BOOL WINAPI
CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv
,
3817 DWORD dwEncodingType
, PBYTE pbSignerInfo
, DWORD cbSignerInfo
,
3818 PBYTE pbSignerInfoCountersignature
, DWORD cbSignerInfoCountersignature
,
3819 DWORD dwSignerType
, void *pvSigner
, DWORD dwFlags
, void *pvReserved
)
3821 FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv
,
3822 dwEncodingType
, pbSignerInfo
, cbSignerInfo
, pbSignerInfoCountersignature
,
3823 cbSignerInfoCountersignature
, dwSignerType
, pvSigner
, dwFlags
, pvReserved
);
3827 BOOL WINAPI
CryptMsgEncodeAndSignCTL(DWORD dwMsgEncodingType
,
3828 PCTL_INFO pCtlInfo
, PCMSG_SIGNED_ENCODE_INFO pSignInfo
, DWORD dwFlags
,
3829 BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
3835 TRACE("(%08x, %p, %p, %08x, %p, %p)\n", dwMsgEncodingType
, pCtlInfo
,
3836 pSignInfo
, dwFlags
, pbEncoded
, pcbEncoded
);
3840 FIXME("unimplemented for flags %08x\n", dwFlags
);
3843 if ((ret
= CryptEncodeObjectEx(dwMsgEncodingType
, PKCS_CTL
, pCtlInfo
,
3844 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &pbCtlContent
, &cbCtlContent
)))
3846 ret
= CryptMsgSignCTL(dwMsgEncodingType
, pbCtlContent
, cbCtlContent
,
3847 pSignInfo
, dwFlags
, pbEncoded
, pcbEncoded
);
3848 LocalFree(pbCtlContent
);
3853 BOOL WINAPI
CryptMsgSignCTL(DWORD dwMsgEncodingType
, BYTE
*pbCtlContent
,
3854 DWORD cbCtlContent
, PCMSG_SIGNED_ENCODE_INFO pSignInfo
, DWORD dwFlags
,
3855 BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
3857 static char oid_ctl
[] = szOID_CTL
;
3861 TRACE("(%08x, %p, %d, %p, %08x, %p, %p)\n", dwMsgEncodingType
,
3862 pbCtlContent
, cbCtlContent
, pSignInfo
, dwFlags
, pbEncoded
, pcbEncoded
);
3866 FIXME("unimplemented for flags %08x\n", dwFlags
);
3869 msg
= CryptMsgOpenToEncode(dwMsgEncodingType
, 0, CMSG_SIGNED
, pSignInfo
,
3873 ret
= CryptMsgUpdate(msg
, pbCtlContent
, cbCtlContent
, TRUE
);
3875 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, pbEncoded
,