1 | /** @file
|
---|
2 | * IPRT - Crypto - PEM-file Reader & Writer.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2016 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_pem_h
|
---|
27 | #define ___iprt_crypto_pem_h
|
---|
28 |
|
---|
29 | #include <iprt/types.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | RT_C_DECLS_BEGIN
|
---|
33 |
|
---|
34 | /** @defgroup grp_rt_spc RTCrPem - PEM-file Reader & Writer
|
---|
35 | * @ingroup grp_rt_crypto
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * One PEM marker word (use RT_STR_TUPLE to initialize).
|
---|
42 | */
|
---|
43 | typedef struct RTCRPEMMARKERWORD
|
---|
44 | {
|
---|
45 | /** The word string. */
|
---|
46 | const char *pszWord;
|
---|
47 | /** The length. */
|
---|
48 | uint32_t cchWord;
|
---|
49 | } RTCRPEMMARKERWORD;
|
---|
50 | /** Pointer to a const marker word. */
|
---|
51 | typedef RTCRPEMMARKERWORD const *PCRTCRPEMMARKERWORD;
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * A PEM marker.
|
---|
56 | *
|
---|
57 | * This is an array of words with lengths, optimized for avoid unnecessary
|
---|
58 | * strlen() while searching the file content. It is ASSUMED that all PEM
|
---|
59 | * section markers starts with either 'BEGIN' or 'END', followed by the words
|
---|
60 | * in the this structure.
|
---|
61 | */
|
---|
62 | typedef struct RTCRPEMMARKER
|
---|
63 | {
|
---|
64 | /** Pointer to an array of marker words. */
|
---|
65 | PCRTCRPEMMARKERWORD paWords;
|
---|
66 | /** Number of works in the array papszWords points to. */
|
---|
67 | uint32_t cWords;
|
---|
68 | } RTCRPEMMARKER;
|
---|
69 | /** Pointer to a const PEM marker. */
|
---|
70 | typedef RTCRPEMMARKER const *PCRTCRPEMMARKER;
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * A PEM section.
|
---|
75 | *
|
---|
76 | * The API works on linked lists of these.
|
---|
77 | */
|
---|
78 | typedef struct RTCRPEMSECTION
|
---|
79 | {
|
---|
80 | /** Pointer to the next file section. */
|
---|
81 | struct RTCRPEMSECTION const *pNext;
|
---|
82 | /** The marker for this section. NULL if binary file. */
|
---|
83 | PCRTCRPEMMARKER pMarker;
|
---|
84 | /** Pointer to the binary data. */
|
---|
85 | uint8_t *pbData;
|
---|
86 | /** The size of the binary data. */
|
---|
87 | size_t cbData;
|
---|
88 | /** Additional text preceeding the binary data. NULL if none. */
|
---|
89 | char *pszPreamble;
|
---|
90 | /** The length of the preamble. */
|
---|
91 | size_t cchPreamble;
|
---|
92 | } RTCRPEMSECTION;
|
---|
93 | /** Pointer to a PEM section. */
|
---|
94 | typedef RTCRPEMSECTION *PRTCRPEMSECTION;
|
---|
95 | /** Pointer to a const PEM section. */
|
---|
96 | typedef RTCRPEMSECTION const *PCRTCRPEMSECTION;
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Frees sections returned by RTCrPemReadFile and RTCrPemParseContent.
|
---|
101 | * @returns IPRT status code.
|
---|
102 | * @param pSectionHead The first section.
|
---|
103 | */
|
---|
104 | RTDECL(int) RTCrPemFreeSections(PCRTCRPEMSECTION pSectionHead);
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Parses the given data and returns a list of binary sections.
|
---|
108 | *
|
---|
109 | * If the file isn't an ASCII file or if no markers were found, the entire file
|
---|
110 | * content is returned as one single section (with pMarker = NULL).
|
---|
111 | *
|
---|
112 | * @returns IPRT status code.
|
---|
113 | * @retval VINF_EOF if the file is empty. The @a ppSectionHead value will be
|
---|
114 | * NULL.
|
---|
115 | * @retval VWRN_NOT_FOUND no section was found and RTCRPEMREADFILE_F_ONLY_PEM
|
---|
116 | * is specified. The @a ppSectionHead value will be NULL.
|
---|
117 | *
|
---|
118 | * @param pvContent The content bytes to parse.
|
---|
119 | * @param cbContent The number of content bytes.
|
---|
120 | * @param fFlags RTCRPEMREADFILE_F_XXX.
|
---|
121 | * @param paMarkers Array of one or more section markers to look for.
|
---|
122 | * @param cMarkers Number of markers in the array.
|
---|
123 | * @param ppSectionHead Where to return the head of the section list. Call
|
---|
124 | * RTCrPemFreeSections to free.
|
---|
125 | * @param pErrInfo Where to return extend error info. Optional.
|
---|
126 | */
|
---|
127 | RTDECL(int) RTCrPemParseContent(void const *pvContent, size_t cbContent, uint32_t fFlags,
|
---|
128 | PCRTCRPEMMARKER paMarkers, size_t cMarkers, PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Reads the content of the given file and returns a list of binary sections
|
---|
132 | * found in the file.
|
---|
133 | *
|
---|
134 | * If the file isn't an ASCII file or if no markers were found, the entire file
|
---|
135 | * content is returned as one single section (with pMarker = NULL).
|
---|
136 | *
|
---|
137 | * @returns IPRT status code.
|
---|
138 | * @retval VINF_EOF if the file is empty. The @a ppSectionHead value will be
|
---|
139 | * NULL.
|
---|
140 | * @retval VWRN_NOT_FOUND no section was found and RTCRPEMREADFILE_F_ONLY_PEM
|
---|
141 | * is specified. The @a ppSectionHead value will be NULL.
|
---|
142 | *
|
---|
143 | * @param pszFilename The path to the file to read.
|
---|
144 | * @param fFlags RTCRPEMREADFILE_F_XXX.
|
---|
145 | * @param paMarkers Array of one or more section markers to look for.
|
---|
146 | * @param cMarkers Number of markers in the array.
|
---|
147 | * @param ppSectionHead Where to return the head of the section list. Call
|
---|
148 | * RTCrPemFreeSections to free.
|
---|
149 | * @param pErrInfo Where to return extend error info. Optional.
|
---|
150 | */
|
---|
151 | RTDECL(int) RTCrPemReadFile(const char *pszFilename, uint32_t fFlags, PCRTCRPEMMARKER paMarkers, size_t cMarkers,
|
---|
152 | PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo);
|
---|
153 | /** @name RTCRPEMREADFILE_F_XXX - Flags for RTCrPemReadFile and
|
---|
154 | * RTCrPemParseContent.
|
---|
155 | * @{ */
|
---|
156 | /** Continue on encoding error. */
|
---|
157 | #define RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR RT_BIT(0)
|
---|
158 | /** Only PEM sections, no binary fallback. */
|
---|
159 | #define RTCRPEMREADFILE_F_ONLY_PEM RT_BIT(1)
|
---|
160 | /** Valid flags. */
|
---|
161 | #define RTCRPEMREADFILE_F_VALID_MASK UINT32_C(0x00000003)
|
---|
162 | /** @} */
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Finds the beginning of first PEM section using the specified markers.
|
---|
166 | *
|
---|
167 | * This will not look any further than the first section. Nor will it check for
|
---|
168 | * binaries.
|
---|
169 | *
|
---|
170 | * @returns Pointer to the "-----BEGIN XXXX" sequence on success.
|
---|
171 | * NULL if not found.
|
---|
172 | * @param pvContent The content bytes to parse.
|
---|
173 | * @param cbContent The number of content bytes.
|
---|
174 | * @param paMarkers Array of one or more section markers to look for.
|
---|
175 | * @param cMarkers Number of markers in the array.
|
---|
176 | */
|
---|
177 | RTDECL(const char *) RTCrPemFindFirstSectionInContent(void const *pvContent, size_t cbContent,
|
---|
178 | PCRTCRPEMMARKER paMarkers, size_t cMarkers);
|
---|
179 |
|
---|
180 | /** @} */
|
---|
181 |
|
---|
182 | RT_C_DECLS_END
|
---|
183 |
|
---|
184 | #endif
|
---|
185 |
|
---|