VirtualBox

source: vbox/trunk/include/iprt/crypto/applecodesign.h@ 76507

Last change on this file since 76507 was 76507, checked in by vboxsync, 6 years ago

/include: scm --fix-header-guards. bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/** @file
2 * IPRT - Apple Code Signing Structures and APIs.
3 */
4
5/*
6 * Copyright (C) 2018 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_crypto_applecodesign_h
27#define ___iprt_crypto_applecodesign_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33#include <iprt/assertcompile.h>
34#include <iprt/crypto/pkcs7.h>
35
36/** @defgroup grp_rt_craplcs RTCrAppleCs - Apple Code Signing
37 * @ingroup grp_rt_crypto
38 * @{
39 */
40
41/** Apple developer ID for application signing. */
42#define RTCR_APPLE_CS_DEVID_APPLICATION_OID "1.2.840.113635.100.6.1.13"
43/** Apple developer ID for installer signing. */
44#define RTCR_APPLE_CS_DEVID_INSTALLER_OID "1.2.840.113635.100.6.1.14"
45/** Apple developer ID for kernel extension signing. */
46#define RTCR_APPLE_CS_DEVID_KEXT_OID "1.2.840.113635.100.6.1.18"
47/** Apple certificate policy OID. */
48#define RTCR_APPLE_CS_CERTIFICATE_POLICY_OID "1.2.840.113635.100.5.1"
49
50
51/** @name RTCRAPLCS_MAGIC_XXX - Apple code signing magic values for identifying blobs
52 * @note No byte order conversion required.
53 * @{ */
54#define RTCRAPLCS_MAGIC_BLOBWRAPPER RT_N2H_U32_C(UINT32_C(0xfade0b01))
55#define RTCRAPLCS_MAGIC_EMBEDDED_SIGNATURE_OLD RT_N2H_U32_C(UINT32_C(0xfade0b02))
56#define RTCRAPLCS_MAGIC_REQUIREMENT RT_N2H_U32_C(UINT32_C(0xfade0c00))
57#define RTCRAPLCS_MAGIC_REQUIREMENTS RT_N2H_U32_C(UINT32_C(0xfade0c01))
58#define RTCRAPLCS_MAGIC_CODEDIRECTORY RT_N2H_U32_C(UINT32_C(0xfade0c02))
59#define RTCRAPLCS_MAGIC_EMBEDDED_SIGNATURE RT_N2H_U32_C(UINT32_C(0xfade0cc0))
60#define RTCRAPLCS_MAGIC_DETACHED_SIGNATURE RT_N2H_U32_C(UINT32_C(0xfade0cc1))
61/** @} */
62
63/** @name Apple code signing versions.
64 * @note Requires byte order conversion of the field value. That way
65 * greater-than and less-than comparisons works correctly.
66 * @{ */
67#define RTCRAPLCS_VER_2_0 UINT32_C(0x00020000)
68#define RTCRAPLCS_VER_SUPPORTS_SCATTER UINT32_C(0x00020100)
69#define RTCRAPLCS_VER_SUPPORTS_TEAMID UINT32_C(0x00020200)
70#define RTCRAPLCS_VER_SUPPORTS_CODE_LIMIT_64 UINT32_C(0x00020300)
71#define RTCRAPLCS_VER_SUPPORTS_EXEC_SEG UINT32_C(0x00020400)
72/** @} */
73
74/** @name RTCRAPLCS_SLOT_XXX - Apple code signing slots.
75 * @note No byte order conversion required.
76 * @{ */
77#define RTCRAPLCS_SLOT_CODEDIRECTORY RT_N2H_U32_C(UINT32_C(0x00000000))
78#define RTCRAPLCS_SLOT_INFO RT_N2H_U32_C(UINT32_C(0x00000001))
79#define RTCRAPLCS_SLOT_REQUIREMENTS RT_N2H_U32_C(UINT32_C(0x00000002))
80#define RTCRAPLCS_SLOT_RESOURCEDIR RT_N2H_U32_C(UINT32_C(0x00000003))
81#define RTCRAPLCS_SLOT_APPLICATION RT_N2H_U32_C(UINT32_C(0x00000004))
82#define RTCRAPLCS_SLOT_ENTITLEMENTS RT_N2H_U32_C(UINT32_C(0x00000005))
83#define RTCRAPLCS_SLOT_ALTERNATE_CODEDIRECTORIES RT_N2H_U32_C(UINT32_C(0x00001000))
84#define RTCRAPLCS_SLOT_ALTERNATE_CODEDIRECTORIES_END RT_N2H_U32_C(UINT32_C(0x00001005))
85#define RTCRAPLCS_SLOT_ALTERNATE_CODEDIRECTORIES_COUNT UINT32_C(0x00000005)
86#define RTCRAPLCS_SLOT_ALTERNATE_CODEDIRECTORY_INC RT_N2H_U32_C(UINT32_C(0x00000001))
87/** The signature.
88 * This is simply a RTCRAPLCSHDR/RTCRAPLCS_MAGIC_BLOBWRAPPER followed by a DER
89 * encoded \#PKCS7 ContentInfo structure containing signedData. The inner
90 * signedData structure signs external data, so its ContentInfo member is set
91 * to 1.2.840.113549.1.7.1 and has no data. */
92#define RTCRAPLCS_SLOT_SIGNATURE RT_N2H_U32_C(UINT32_C(0x00010000))
93/** @} */
94
95/** @name RTCRAPLCS_HASHTYPE_XXX - Apple code signing hash types
96 * @note Byte sized field, so no byte order concerns.
97 * @{ */
98#define RTCRAPLCS_HASHTYPE_SHA1 UINT8_C(1)
99#define RTCRAPLCS_HASHTYPE_SHA256 UINT8_C(2)
100#define RTCRAPLCS_HASHTYPE_SHA256_TRUNCATED UINT8_C(3) /**< Truncated to 20 bytes (SHA1 size). */
101#define RTCRAPLCS_HASHTYPE_SHA384 UINT8_C(4)
102/** @} */
103
104
105/**
106 * Apple code signing blob header.
107 */
108typedef struct RTCRAPLCSHDR
109{
110 /** The magic value (RTCRAPLCS_MAGIC_XXX).
111 * (Big endian, but constant are big endian already.) */
112 uint32_t uMagic;
113 /** The total length of the blob. Big endian. */
114 uint32_t cb;
115} RTCRAPLCSHDR;
116AssertCompileSize(RTCRAPLCSHDR, 8);
117/** Pointer to a CS blob header. */
118typedef RTCRAPLCSHDR *PRTCRAPLCSHDR;
119/** Pointer to a const CS blob header. */
120typedef RTCRAPLCSHDR const *PCRTCRAPLCSHDR;
121
122/**
123 * Apple code signing super blob slot.
124 */
125typedef struct RTCRAPLCSBLOBSLOT
126{
127 /** Slot type, RTCRAPLCS_SLOT_XXX.
128 * (Big endian, but so are the constants too). */
129 uint32_t uType;
130 /** Data offset. Big endian. */
131 uint32_t offData;
132} RTCRAPLCSBLOBSLOT;
133AssertCompileSize(RTCRAPLCSBLOBSLOT, 8);
134/** Pointer to a super blob slot. */
135typedef RTCRAPLCSBLOBSLOT *PRTCRAPLCSBLOBSLOT;
136/** Pointer to a const super blob slot. */
137typedef RTCRAPLCSBLOBSLOT const *PCRTCRAPLCSBLOBSLOT;
138
139/**
140 * Apple code signing super blob.
141 */
142typedef struct RTCRAPLCSSUPERBLOB
143{
144 /** Header (uMagic = RTCRAPLCS_MAGIC_EMBEDDED_SIGNATURE?
145 * or RTCRAPLCS_MAGIC_EMBEDDED_SIGNATURE_OLD? ). */
146 RTCRAPLCSHDR Hdr;
147 /** Number of slots. Big endian. */
148 uint32_t cSlots;
149 /** Slots. */
150 RTCRAPLCSBLOBSLOT aSlots[RT_FLEXIBLE_ARRAY];
151} RTCRAPLCSSUPERBLOB;
152AssertCompileMemberOffset(RTCRAPLCSSUPERBLOB, aSlots, 12);
153/** Pointer to a CS super blob. */
154typedef RTCRAPLCSSUPERBLOB *PRTCRAPLCSSUPERBLOB;
155/** Pointer to a const CS super blob. */
156typedef RTCRAPLCSSUPERBLOB const *PCRTCRAPLCSSUPERBLOB;
157
158/**
159 * Code directory (RTCRAPLCS_MAGIC_CODEDIRECTORY).
160 */
161typedef struct RTCRAPLCSCODEDIRECTORY
162{
163 /** 0x00: Header (uMagic = RTCRAPLCS_MAGIC_CODEDIRECTORY). */
164 RTCRAPLCSHDR Hdr;
165 /** 0x08: The version number (RTCRAPLCS_VER_XXX).
166 * @note Big endian, host order constants. */
167 uint32_t uVersion;
168 /** 0x0c: Flags & mode, RTCRAPLCS_???. (Big endian. ) */
169 uint32_t fFlags;
170 /** 0x10: Offset of the hash slots. Big endian.
171 * Special slots found below this offset, code slots at and after. */
172 uint32_t offHashSlots;
173 /** 0x14: Offset of the identifier string. Big endian. */
174 uint32_t offIdentifier;
175 /** 0x18: Number of special hash slots. Hubertus Bigend style. */
176 uint32_t cSpecialSlots;
177 /** 0x1c: Number of code hash slots. Big endian. */
178 uint32_t cCodeSlots;
179 /** 0x20: Number of bytes of code that's covered, 32-bit wide. Big endian. */
180 uint32_t cbCodeLimit32;
181 /** 0x24: The hash size. */
182 uint8_t cbHash;
183 /** 0x25: The hash type (RTCRAPLCS_HASHTYPE_XXX). */
184 uint8_t bHashType;
185 /** 0x26: Platform identifier or zero. */
186 uint8_t idPlatform;
187 /** 0x27: The page shift value. zero if infinite page size. */
188 uint8_t cPageShift;
189 /** 0x28: Spare field, MBZ. */
190 uint32_t uUnused1;
191 /** 0x2c: Offset of scatter vector (optional). Big endian.
192 * @since RTCRAPLCS_VER_SUPPORTS_SCATTER */
193 uint32_t offScatter;
194 /** 0x30: Offset of team identifier (optional). Big endian.
195 * @since RTCRAPLCS_VER_SUPPORTS_TEAMID */
196 uint32_t offTeamId;
197 /** 0x34: Unused field, MBZ.
198 * @since RTCRAPLCS_VER_SUPPORTS_CODE_LIMIT_64 */
199 uint32_t uUnused2;
200 /** 0x38: Number of bytes of code that's covered, 64-bit wide. Big endian.
201 * @since RTCRAPLCS_VER_SUPPORTS_CODE_LIMIT_64 */
202 uint64_t cbCodeLimit64;
203 /** 0x40: File offset of the first segment. Big endian.
204 * @since RTCRAPLCS_VER_SUPPORTS_EXEC_SEG */
205 uint64_t offExecSeg;
206 /** 0x48: The size of the first segment. Big endian.
207 * @since RTCRAPLCS_VER_SUPPORTS_EXEC_SEG */
208 uint64_t cbExecSeg;
209 /** 0x50: Flags for the first segment. Big endian.
210 * @since RTCRAPLCS_VER_SUPPORTS_EXEC_SEG */
211 uint64_t fExecSeg;
212} RTCRAPLCSCODEDIRECTORY;
213AssertCompileSize(RTCRAPLCSCODEDIRECTORY, 0x58);
214/** Pointer to a CS code directory. */
215typedef RTCRAPLCSCODEDIRECTORY *PRTCRAPLCSCODEDIRECTORY;
216/** Pointer to a const CS code directory. */
217typedef RTCRAPLCSCODEDIRECTORY const *PCRTCRAPLCSCODEDIRECTORY;
218
219
220/**
221 * IPRT structure for working with an Apple code signing blob.
222 */
223typedef struct RTCRAPLCS
224{
225 uint8_t const *pbBlob;
226 size_t cbBlob;
227 size_t auReserved[4];
228} RTCRAPLCS;
229/** Pointer to an IPRT CS blob descriptor. */
230typedef RTCRAPLCS *PRTCRAPLCS;
231
232/**
233 * Initialize a RTCRAPLCS descriptor and validate the blob data.
234 *
235 * @returns IPRT status code.
236 * @param pDesc The descirptor to initialize.
237 * @param pvBlob The blob bytes.
238 * @param cbBlob The number of bytes in the blob.
239 * @param fFlags Future validation flags, MBZ.
240 * @param pErrInfo Where to return additional error details. Optional.
241 */
242RTDECL(int) RTCrAppleCsInit(PRTCRAPLCS pDesc, void const *pvBlob, size_t cbBlob, uint32_t fFlags, PRTERRINFO pErrInfo);
243
244/**
245 * Callback used by RTCrAppleCsVerifyImage to digest a section of the image.
246 *
247 * @return IPRT status code.
248 * @param hDigest The digest to feed the bytes to.
249 * @param off The RVA of the bytes to digest.
250 * @param cb Number of bytes to digest.
251 * @param pvUser User argument.
252 */
253typedef DECLCALLBACK(int) FNRTCRAPPLECSDIGESTAREA(RTCRDIGEST hDigest, size_t off, size_t cb, void *pvUser);
254/** Pointer to a image digest callback. */
255typedef FNRTCRAPPLECSDIGESTAREA *PFNRTCRAPPLECSDIGESTAREA;
256
257/**
258 * Verifies an image against the given signature blob.
259 *
260 * @return IPRT status code.
261 * @param pDesc The apple code signing blob to verify against.
262 * @param fFlags Future verification flags, MBZ.
263 * @param pfnCallback Image digest callback.
264 * @param pvUser User argument for the callback.
265 * @param pErrInfo Where to return additional error details. Optional.
266 */
267RTDECL(int) RTCrAppleCsVerifyImage(PRTCRAPLCS pDesc, uint32_t fFlags, PFNRTCRAPPLECSDIGESTAREA pfnCallback,
268 void *pvUser, PRTERRINFO pErrInfo);
269
270RTDECL(int) RTCrAppleCsQuerySigneddData(PRTCRAPLCS pDesc, PRTCRPKCS7SIGNEDDATA pSignedData, PRTERRINFO pErrInfo);
271
272/** @} */
273
274#endif
275
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