VirtualBox

source: vbox/trunk/include/iprt/crypto/pem.h@ 77807

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

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/** @file
2 * IPRT - Crypto - PEM-file Reader & Writer.
3 */
4
5/*
6 * Copyright (C) 2006-2019 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_INCLUDED_crypto_pem_h
27#define IPRT_INCLUDED_crypto_pem_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33
34
35RT_C_DECLS_BEGIN
36
37/** @defgroup grp_rt_spc RTCrPem - PEM-file Reader & Writer
38 * @ingroup grp_rt_crypto
39 * @{
40 */
41
42
43/**
44 * One PEM marker word (use RT_STR_TUPLE to initialize).
45 */
46typedef struct RTCRPEMMARKERWORD
47{
48 /** The word string. */
49 const char *pszWord;
50 /** The length. */
51 uint32_t cchWord;
52} RTCRPEMMARKERWORD;
53/** Pointer to a const marker word. */
54typedef RTCRPEMMARKERWORD const *PCRTCRPEMMARKERWORD;
55
56
57/**
58 * A PEM marker.
59 *
60 * This is an array of words with lengths, optimized for avoid unnecessary
61 * strlen() while searching the file content. It is ASSUMED that all PEM
62 * section markers starts with either 'BEGIN' or 'END', followed by the words
63 * in the this structure.
64 */
65typedef struct RTCRPEMMARKER
66{
67 /** Pointer to an array of marker words. */
68 PCRTCRPEMMARKERWORD paWords;
69 /** Number of works in the array papszWords points to. */
70 uint32_t cWords;
71} RTCRPEMMARKER;
72/** Pointer to a const PEM marker. */
73typedef RTCRPEMMARKER const *PCRTCRPEMMARKER;
74
75
76/**
77 * A PEM field.
78 */
79typedef struct RTCRPEMFIELD
80{
81 /** Pointer to the next field. */
82 struct RTCRPEMFIELD const *pNext;
83 /** The field value. */
84 char const *pszValue;
85 /** The field value length. */
86 size_t cchValue;
87 /** The field name length. */
88 size_t cchName;
89 /** The field name. */
90 char szName[RT_FLEXIBLE_ARRAY];
91} RTCRPEMFIELD;
92/** Pointer to a PEM field. */
93typedef RTCRPEMFIELD *PRTCRPEMFIELD;
94/** Pointer to a const PEM field. */
95typedef RTCRPEMFIELD const *PCRTCRPEMFIELD;
96
97
98/**
99 * A PEM section.
100 *
101 * The API works on linked lists of these.
102 */
103typedef struct RTCRPEMSECTION
104{
105 /** Pointer to the next file section. */
106 struct RTCRPEMSECTION const *pNext;
107 /** The marker for this section. NULL if binary file. */
108 PCRTCRPEMMARKER pMarker;
109 /** Pointer to the binary data. */
110 uint8_t *pbData;
111 /** The size of the binary data. */
112 size_t cbData;
113 /** List of fields, NULL if none. */
114 PCRTCRPEMFIELD pFieldHead;
115 /** Set if RTCRPEMREADFILE_F_SENSITIVE was specified. */
116 bool fSensitive;
117} RTCRPEMSECTION;
118/** Pointer to a PEM section. */
119typedef RTCRPEMSECTION *PRTCRPEMSECTION;
120/** Pointer to a const PEM section. */
121typedef RTCRPEMSECTION const *PCRTCRPEMSECTION;
122
123
124/**
125 * Frees sections returned by RTCrPemReadFile and RTCrPemParseContent.
126 * @returns IPRT status code.
127 * @param pSectionHead The first section.
128 */
129RTDECL(int) RTCrPemFreeSections(PCRTCRPEMSECTION pSectionHead);
130
131/**
132 * Parses the given data and returns a list of binary sections.
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 pvContent The content bytes to parse.
144 * @param cbContent The number of content bytes.
145 * @param fFlags RTCRPEMREADFILE_F_XXX.
146 * @param paMarkers Array of one or more section markers to look for.
147 * @param cMarkers Number of markers in the array.
148 * @param ppSectionHead Where to return the head of the section list. Call
149 * RTCrPemFreeSections to free.
150 * @param pErrInfo Where to return extend error info. Optional.
151 */
152RTDECL(int) RTCrPemParseContent(void const *pvContent, size_t cbContent, uint32_t fFlags,
153 PCRTCRPEMMARKER paMarkers, size_t cMarkers, PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo);
154
155/**
156 * Reads the content of the given file and returns a list of binary sections
157 * found in the file.
158 *
159 * If the file isn't an ASCII file or if no markers were found, the entire file
160 * content is returned as one single section (with pMarker = NULL).
161 *
162 * @returns IPRT status code.
163 * @retval VINF_EOF if the file is empty. The @a ppSectionHead value will be
164 * NULL.
165 * @retval VWRN_NOT_FOUND no section was found and RTCRPEMREADFILE_F_ONLY_PEM
166 * is specified. The @a ppSectionHead value will be NULL.
167 *
168 * @param pszFilename The path to the file to read.
169 * @param fFlags RTCRPEMREADFILE_F_XXX.
170 * @param paMarkers Array of one or more section markers to look for.
171 * @param cMarkers Number of markers in the array.
172 * @param ppSectionHead Where to return the head of the section list. Call
173 * RTCrPemFreeSections to free.
174 * @param pErrInfo Where to return extend error info. Optional.
175 */
176RTDECL(int) RTCrPemReadFile(const char *pszFilename, uint32_t fFlags, PCRTCRPEMMARKER paMarkers, size_t cMarkers,
177 PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo);
178/** @name RTCRPEMREADFILE_F_XXX - Flags for RTCrPemReadFile and
179 * RTCrPemParseContent.
180 * @{ */
181/** Continue on encoding error. */
182#define RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR RT_BIT(0)
183/** Only PEM sections, no binary fallback. */
184#define RTCRPEMREADFILE_F_ONLY_PEM RT_BIT(1)
185/** Sensitive data, use the safer allocator. */
186#define RTCRPEMREADFILE_F_SENSITIVE RT_BIT(2)
187/** Valid flags. */
188#define RTCRPEMREADFILE_F_VALID_MASK UINT32_C(0x00000007)
189/** @} */
190
191/**
192 * Finds the beginning of first PEM section using the specified markers.
193 *
194 * This will not look any further than the first section. Nor will it check for
195 * binaries.
196 *
197 * @returns Pointer to the "-----BEGIN XXXX" sequence on success.
198 * NULL if not found.
199 * @param pvContent The content bytes to parse.
200 * @param cbContent The number of content bytes.
201 * @param paMarkers Array of one or more section markers to look for.
202 * @param cMarkers Number of markers in the array.
203 */
204RTDECL(const char *) RTCrPemFindFirstSectionInContent(void const *pvContent, size_t cbContent,
205 PCRTCRPEMMARKER paMarkers, size_t cMarkers);
206
207/** @} */
208
209RT_C_DECLS_END
210
211#endif /* !IPRT_INCLUDED_crypto_pem_h */
212
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