1 | /* $Id: x509-asn1-decoder.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - X.509, Decoder for ASN.1.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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/x509.h>
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 | #include "x509-internal.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * One X.509 Extension.
|
---|
42 | */
|
---|
43 | RTDECL(int) RTCrX509Extension_ExtnValue_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags,
|
---|
44 | PRTCRX509EXTENSION pThis, const char *pszErrorTag)
|
---|
45 | {
|
---|
46 | pThis->enmValue = RTCRX509EXTENSIONVALUE_UNKNOWN;
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Decode the encapsulated extension bytes if know the format.
|
---|
50 | */
|
---|
51 | RTASN1CURSOR ValueCursor;
|
---|
52 | int rc = RTAsn1CursorInitSubFromCore(pCursor, &pThis->ExtnValue.Asn1Core, &ValueCursor, "ExtnValue");
|
---|
53 | if (RT_FAILURE(rc))
|
---|
54 | return rc;
|
---|
55 | pCursor = &ValueCursor;
|
---|
56 |
|
---|
57 | if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_AUTHORITY_KEY_IDENTIFIER_OID) == 0)
|
---|
58 | {
|
---|
59 | /* 4.2.1.1 Authority Key Identifier */
|
---|
60 | PRTCRX509AUTHORITYKEYIDENTIFIER pThat;
|
---|
61 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
62 | if (RT_SUCCESS(rc))
|
---|
63 | {
|
---|
64 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
65 | pThis->enmValue = RTCRX509EXTENSIONVALUE_AUTHORITY_KEY_IDENTIFIER;
|
---|
66 | rc = RTCrX509AuthorityKeyIdentifier_DecodeAsn1(&ValueCursor, 0, pThat, "AuthorityKeyIdentifier");
|
---|
67 | }
|
---|
68 | }
|
---|
69 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_OLD_AUTHORITY_KEY_IDENTIFIER_OID) == 0)
|
---|
70 | {
|
---|
71 | /* Old and obsolete version of the above, still found in microsoft certificates. */
|
---|
72 | PRTCRX509OLDAUTHORITYKEYIDENTIFIER pThat;
|
---|
73 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
74 | if (RT_SUCCESS(rc))
|
---|
75 | {
|
---|
76 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
77 | pThis->enmValue = RTCRX509EXTENSIONVALUE_OLD_AUTHORITY_KEY_IDENTIFIER;
|
---|
78 | rc = RTCrX509OldAuthorityKeyIdentifier_DecodeAsn1(&ValueCursor, 0, pThat, "OldAuthorityKeyIdentifier");
|
---|
79 | }
|
---|
80 | }
|
---|
81 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_SUBJECT_KEY_IDENTIFIER_OID) == 0)
|
---|
82 | {
|
---|
83 | /* 4.2.1.2 Subject Key Identifier */
|
---|
84 | PRTASN1OCTETSTRING pThat;
|
---|
85 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | pThis->ExtnValue.pEncapsulated = &pThat->Asn1Core;
|
---|
89 | pThis->enmValue = RTCRX509EXTENSIONVALUE_OCTET_STRING;
|
---|
90 | rc = RTAsn1CursorGetOctetString(&ValueCursor, 0, pThat, "SubjectKeyIdentifier");
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_KEY_USAGE_OID) == 0)
|
---|
94 | {
|
---|
95 | /* 4.2.1.3 Key Usage */
|
---|
96 | PRTASN1BITSTRING pThat;
|
---|
97 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | pThis->ExtnValue.pEncapsulated = &pThat->Asn1Core;
|
---|
101 | pThis->enmValue = RTCRX509EXTENSIONVALUE_BIT_STRING;
|
---|
102 | rc = RTAsn1CursorGetBitStringEx(&ValueCursor, 0, 9, pThat, "KeyUsage");
|
---|
103 | }
|
---|
104 | }
|
---|
105 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_CERTIFICATE_POLICIES_OID) == 0)
|
---|
106 | {
|
---|
107 | /* 4.2.1.4 Certificate Policies */
|
---|
108 | PRTCRX509CERTIFICATEPOLICIES pThat;
|
---|
109 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | {
|
---|
112 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
113 | pThis->enmValue = RTCRX509EXTENSIONVALUE_CERTIFICATE_POLICIES;
|
---|
114 | rc = RTCrX509CertificatePolicies_DecodeAsn1(&ValueCursor, 0, pThat, "CertPolicies");
|
---|
115 | }
|
---|
116 | }
|
---|
117 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_POLICY_MAPPINGS_OID) == 0)
|
---|
118 | {
|
---|
119 | /* 4.2.1.5 Policy Mappings */
|
---|
120 | PRTCRX509POLICYMAPPINGS pThat;
|
---|
121 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
122 | if (RT_SUCCESS(rc))
|
---|
123 | {
|
---|
124 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
125 | pThis->enmValue = RTCRX509EXTENSIONVALUE_POLICY_MAPPINGS;
|
---|
126 | rc = RTCrX509PolicyMappings_DecodeAsn1(&ValueCursor, 0, pThat, "PolicyMapppings");
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else if ( RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_SUBJECT_ALT_NAME_OID) == 0
|
---|
130 | || RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_ISSUER_ALT_NAME_OID) == 0)
|
---|
131 | {
|
---|
132 | /* 4.2.1.6 Subject Alternative Name / 4.2.1.7 Issuer Alternative Name */
|
---|
133 | PRTCRX509GENERALNAMES pThat;
|
---|
134 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
138 | pThis->enmValue = RTCRX509EXTENSIONVALUE_GENERAL_NAMES;
|
---|
139 | rc = RTCrX509GeneralNames_DecodeAsn1(&ValueCursor, 0, pThat, "AltName");
|
---|
140 | }
|
---|
141 | }
|
---|
142 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_BASIC_CONSTRAINTS_OID) == 0)
|
---|
143 | {
|
---|
144 | /* 4.2.1.9 Basic Constraints */
|
---|
145 | PRTCRX509BASICCONSTRAINTS pThat;
|
---|
146 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
147 | if (RT_SUCCESS(rc))
|
---|
148 | {
|
---|
149 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
150 | pThis->enmValue = RTCRX509EXTENSIONVALUE_BASIC_CONSTRAINTS;
|
---|
151 | rc = RTCrX509BasicConstraints_DecodeAsn1(&ValueCursor, 0, pThat, "BasicConstraints");
|
---|
152 | }
|
---|
153 | }
|
---|
154 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_NAME_CONSTRAINTS_OID) == 0)
|
---|
155 | {
|
---|
156 | /* 4.2.1.10 Name Constraints */
|
---|
157 | PRTCRX509NAMECONSTRAINTS pThat;
|
---|
158 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
159 | if (RT_SUCCESS(rc))
|
---|
160 | {
|
---|
161 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
162 | pThis->enmValue = RTCRX509EXTENSIONVALUE_NAME_CONSTRAINTS;
|
---|
163 | rc = RTCrX509NameConstraints_DecodeAsn1(&ValueCursor, 0, pThat, "NameConstraints");
|
---|
164 | }
|
---|
165 | }
|
---|
166 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_POLICY_CONSTRAINTS_OID) == 0)
|
---|
167 | {
|
---|
168 | /* 4.2.1.11 Policy Constraints */
|
---|
169 | PRTCRX509POLICYCONSTRAINTS pThat;
|
---|
170 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
171 | if (RT_SUCCESS(rc))
|
---|
172 | {
|
---|
173 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
174 | pThis->enmValue = RTCRX509EXTENSIONVALUE_POLICY_CONSTRAINTS;
|
---|
175 | rc = RTCrX509PolicyConstraints_DecodeAsn1(&ValueCursor, 0, pThat, "PolicyConstraints");
|
---|
176 | }
|
---|
177 | }
|
---|
178 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_EXT_KEY_USAGE_OID) == 0)
|
---|
179 | {
|
---|
180 | /* 4.2.1.12 Extended Key Usage */
|
---|
181 | PRTASN1SEQOFOBJIDS pThat;
|
---|
182 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | {
|
---|
185 | pThis->ExtnValue.pEncapsulated = &pThat->SeqCore.Asn1Core;
|
---|
186 | pThis->enmValue = RTCRX509EXTENSIONVALUE_SEQ_OF_OBJ_IDS;
|
---|
187 | rc = RTAsn1SeqOfObjIds_DecodeAsn1(&ValueCursor, 0, pThat, "ExKeyUsage");
|
---|
188 | }
|
---|
189 | }
|
---|
190 | else if (RTAsn1ObjId_CompareWithString(&pThis->ExtnId, RTCRX509_ID_CE_EXT_KEY_USAGE_OID) == 0)
|
---|
191 | {
|
---|
192 | /* 4.2.1.14 Inhibit anyPolicy */
|
---|
193 | PRTASN1INTEGER pThat;
|
---|
194 | rc = RTAsn1MemAllocZ(&pThis->ExtnValue.EncapsulatedAllocation, (void **)&pThat, sizeof(*pThat));
|
---|
195 | if (RT_SUCCESS(rc))
|
---|
196 | {
|
---|
197 | pThis->ExtnValue.pEncapsulated = &pThat->Asn1Core;
|
---|
198 | pThis->enmValue = RTCRX509EXTENSIONVALUE_INTEGER;
|
---|
199 | rc = RTAsn1CursorGetInteger(&ValueCursor, 0, pThat, "InhibitAnyPolicy");
|
---|
200 | }
|
---|
201 | }
|
---|
202 | else
|
---|
203 | return VINF_SUCCESS;
|
---|
204 |
|
---|
205 | if (RT_SUCCESS(rc))
|
---|
206 | rc = RTAsn1CursorCheckEnd(&ValueCursor);
|
---|
207 |
|
---|
208 | if (RT_SUCCESS(rc))
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Generate the code.
|
---|
216 | */
|
---|
217 | #include <iprt/asn1-generator-asn1-decoder.h>
|
---|
218 |
|
---|