VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkcs7-asn1-decoder.cpp@ 62477

Last change on this file since 62477 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/* $Id: pkcs7-asn1-decoder.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - PKCS \#7, Decoder for ASN.1.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/crypto/pkcs7.h>
33
34#include <iprt/err.h>
35#include <iprt/string.h>
36#include <iprt/crypto/spc.h>
37#include <iprt/crypto/tsp.h>
38
39#include "pkcs7-internal.h"
40
41
42/*
43 * PKCS #7 ContentInfo
44 */
45typedef enum RTCRPKCS7CONTENTINFOCHOICE
46{
47 RTCRPKCS7CONTENTINFOCHOICE_INVALID = 0,
48 RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN,
49 RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA,
50 RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT,
51 RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO,
52 RTCRPKCS7CONTENTINFOCHOICE_END,
53 RTCRPKCS7CONTENTINFOCHOICE_32BIT_HACK = 0x7fffffff
54} RTCRPKCS7CONTENTINFOCHOICE;
55
56static int rtCrPkcs7ContentInfo_DecodeExtra(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTCRPKCS7CONTENTINFO pThis,
57 const char *pszErrorTag)
58{
59 pThis->u.pCore = NULL;
60
61 /*
62 * Figure the type.
63 */
64 RTCRPKCS7CONTENTINFOCHOICE enmChoice;
65 size_t cbContent = 0;
66 if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRPKCS7SIGNEDDATA_OID) == 0)
67 {
68 enmChoice = RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA;
69 cbContent = sizeof(*pThis->u.pSignedData);
70 }
71 else if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRSPCINDIRECTDATACONTENT_OID) == 0)
72 {
73 enmChoice = RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT;
74 cbContent = sizeof(*pThis->u.pIndirectDataContent);
75 }
76 else if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRTSPTSTINFO_OID) == 0)
77 {
78 enmChoice = RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO;
79 cbContent = sizeof(*pThis->u.pTstInfo);
80 }
81 else
82 {
83 enmChoice = RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN;
84 cbContent = 0;
85 }
86
87 int rc = VINF_SUCCESS;
88 if (enmChoice != RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN)
89 {
90 /*
91 * Detect CMS octet string and open the content cursor.
92 * Current we don't have work with any contet which is octet string,
93 * they're all sequences, which make detection so much simpler.
94 */
95 PRTASN1OCTETSTRING pOctetString = &pThis->Content;
96 RTASN1CURSOR ContentCursor;
97 rc = RTAsn1CursorInitSubFromCore(pCursor, &pThis->Content.Asn1Core, &ContentCursor, "Content");
98 if ( RT_SUCCESS(rc)
99 && RTAsn1CursorIsNextEx(&ContentCursor, ASN1_TAG_OCTET_STRING, ASN1_TAGFLAG_PRIMITIVE | ASN1_TAGCLASS_UNIVERSAL))
100 {
101 rc = RTAsn1MemAllocZ(&pThis->Content.EncapsulatedAllocation, (void **)&pThis->Content.pEncapsulated,
102 sizeof(*pOctetString));
103 if (RT_SUCCESS(rc))
104 {
105 pThis->pCmsContent = pOctetString = (PRTASN1OCTETSTRING)pThis->Content.pEncapsulated;
106 rc = RTAsn1OctetString_DecodeAsn1(&ContentCursor, 0, pOctetString, "CmsContent");
107 if (RT_SUCCESS(rc))
108 rc = RTAsn1CursorCheckEnd(&ContentCursor);
109 if (RT_SUCCESS(rc))
110 rc = RTAsn1CursorInitSubFromCore(pCursor, &pOctetString->Asn1Core, &ContentCursor, "CmsContent");
111 }
112 }
113 if (RT_SUCCESS(rc))
114 {
115 /*
116 * Allocate memory for the decoded content.
117 */
118 rc = RTAsn1MemAllocZ(&pOctetString->EncapsulatedAllocation, (void **)&pOctetString->pEncapsulated, cbContent);
119 if (RT_SUCCESS(rc))
120 {
121 pThis->u.pCore = pOctetString->pEncapsulated;
122
123 /*
124 * Decode it.
125 */
126 switch (enmChoice)
127 {
128 case RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA:
129 rc = RTCrPkcs7SignedData_DecodeAsn1(&ContentCursor, 0, pThis->u.pSignedData, "SignedData");
130 break;
131 case RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT:
132 rc = RTCrSpcIndirectDataContent_DecodeAsn1(&ContentCursor, 0, pThis->u.pIndirectDataContent,
133 "IndirectDataContent");
134 break;
135 case RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO:
136 rc = RTCrTspTstInfo_DecodeAsn1(&ContentCursor, 0, pThis->u.pTstInfo, "TstInfo");
137 break;
138 default:
139 AssertFailed();
140 rc = VERR_IPE_NOT_REACHED_DEFAULT_CASE;
141 break;
142 }
143 if (RT_SUCCESS(rc))
144 rc = RTAsn1CursorCheckEnd(&ContentCursor);
145 if (RT_SUCCESS(rc))
146 return VINF_SUCCESS;
147
148 RTAsn1MemFree(&pOctetString->EncapsulatedAllocation, pOctetString->pEncapsulated);
149 pOctetString->pEncapsulated = NULL;
150 pThis->u.pCore = NULL;
151 }
152 }
153 }
154 return rc;
155}
156
157
158/*
159 * Generate the code.
160 */
161#include <iprt/asn1-generator-asn1-decoder.h>
162
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette