VirtualBox

Changeset 84163 in vbox


Ignore:
Timestamp:
May 6, 2020 3:31:33 PM (5 years ago)
Author:
vboxsync
Message:

IPRT: PEM writer functions. bugref:9699

Location:
trunk
Files:
1 added
6 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/crypto/pem.h

    r82968 r84163  
    3131
    3232#include <iprt/types.h>
     33#include <iprt/asn1.h>   /* PRTASN1CORE */
     34#include <iprt/string.h> /* PFNRTSTROUTPUT */
    3335
    3436
     
    205207                                                      PCRTCRPEMMARKER paMarkers, size_t cMarkers);
    206208
     209
     210/**
     211 * PEM formatter for a binary data blob.
     212 *
     213 * @returns Number of output bytes (sum of @a pfnOutput return values).
     214 * @param   pfnOutput       The output callback function.
     215 * @param   pvUser          The user argument to the output callback.
     216 * @param   pvContent       The binary blob to output.
     217 * @param   cbContent       Size of the binary blob.
     218 * @param   pszMarker       The PEM marker, .e.g "PRIVATE KEY", "CERTIFICATE" or
     219 *                          similar.
     220 * @sa      RTCrPemWriteAsn1, RTCrPemWriteAsn1ToVfsFile,
     221 *          RTCrPemWriteAsn1ToVfsFile
     222 */
     223RTDECL(size_t) RTCrPemWriteBlob(PFNRTSTROUTPUT pfnOutput, void *pvUser,
     224                                const void *pvContent, size_t cbContent, const char *pszMarker);
     225
     226/**
     227 * PEM formatter for a generic ASN.1 structure.
     228 *
     229 * This will call both RTAsn1EncodePrepare() and RTAsn1EncodeWrite() on
     230 * @a pRoot.  Uses DER encoding.
     231 *
     232 * @returns Number of outputted chars (sum of @a pfnOutput return values),
     233 *          negative values are error status codes from the ASN.1 encoding.
     234 * @param   pfnOutput       The output callback function.
     235 * @param   pvUser          The user argument to the output callback.
     236 * @param   fFlags          Reserved, MBZ.
     237 * @param   pRoot           The root of the ASN.1 to encode and format as PEM.
     238 * @param   pszMarker       The PEM marker, .e.g "PRIVATE KEY", "CERTIFICATE" or
     239 *                          similar.
     240 * @sa      RTCrPemWriteAsn1ToVfsFile, RTCrPemWriteAsn1ToVfsFile,
     241 *          RTCrPemWriteBlob
     242 */
     243RTDECL(ssize_t) RTCrPemWriteAsn1(PFNRTSTROUTPUT pfnOutput, void *pvUser, PRTASN1CORE pRoot,
     244                                 uint32_t fFlags, const char *pszMarker, PRTERRINFO pErrInfo);
     245
     246/**
     247 * PEM formatter for a generic ASN.1 structure and output it to @a hVfsIos.
     248 *
     249 * This will call both RTAsn1EncodePrepare() and RTAsn1EncodeWrite() on
     250 * @a pRoot.  Uses DER encoding.
     251 *
     252 * @returns Number of chars written, negative values are error status codes from
     253 *          the ASN.1 encoding or from RTVfsIoStrmWrite().
     254 * @param   hVfsIos         Handle to the I/O stream to write it to.
     255 * @param   fFlags          Reserved, MBZ.
     256 * @param   pRoot           The root of the ASN.1 to encode and format as PEM.
     257 * @param   pszMarker       The PEM marker, .e.g "PRIVATE KEY", "CERTIFICATE" or
     258 *                          similar.
     259 * @sa      RTCrPemWriteAsn1ToVfsFile, RTCrPemWriteAsn1, RTCrPemWriteBlob
     260 */
     261RTDECL(ssize_t) RTCrPemWriteAsn1ToVfsIoStrm(RTVFSIOSTREAM hVfsIos, PRTASN1CORE pRoot,
     262                                            uint32_t fFlags, const char *pszMarker, PRTERRINFO pErrInfo);
     263
     264/**
     265 * PEM formatter for a generic ASN.1 structure and output it to @a hVfsFile.
     266 *
     267 * This will call both RTAsn1EncodePrepare() and RTAsn1EncodeWrite() on
     268 * @a pRoot.  Uses DER encoding.
     269 *
     270 * @returns Number of chars written, negative values are error status codes from
     271 *          the ASN.1 encoding or from RTVfsIoStrmWrite().
     272 * @param   hVfsFile        Handle to the file to write it to.
     273 * @param   fFlags          Reserved, MBZ.
     274 * @param   pRoot           The root of the ASN.1 to encode and format as PEM.
     275 * @param   pszMarker       The PEM marker, .e.g "PRIVATE KEY", "CERTIFICATE" or
     276 *                          similar.
     277 * @sa      RTCrPemWriteAsn1ToVfsIoStrm, RTCrPemWriteAsn1, RTCrPemWriteBlob
     278 */
     279RTDECL(ssize_t) RTCrPemWriteAsn1ToVfsFile(RTVFSFILE hVfsFile, PRTASN1CORE pRoot,
     280                                          uint32_t fFlags, const char *pszMarker, PRTERRINFO pErrInfo);
     281
    207282/** @} */
    208283
  • trunk/include/iprt/crypto/x509.h

    r82968 r84163  
    10341034
    10351035
     1036/** Wrapper around RTCrPemWriteAsn1ToVfsIoStrm().  */
     1037DECLINLINE(int) RTCrX509Certificate_WriteToVfsIoStrm(RTVFSIOSTREAM hVfsIos, PRTCRX509CERTIFICATE pCertificate, PRTERRINFO pErrInfo)
     1038{
     1039    return RTCrPemWriteAsn1ToVfsIoStrm(hVfsIos, &pCertificate->SeqCore.Asn1Core, 0 /*fFlags*/,
     1040                                       g_aRTCrX509CertificateMarkers[0].paWords[0].pszWord, pErrInfo);
     1041}
     1042
     1043/** Wrapper around RTCrPemWriteAsn1ToVfsFile().  */
     1044DECLINLINE(int) RTCrX509Certificate_WriteToVfsFile(RTVFSFILE hVfsFile, PRTCRX509CERTIFICATE pCertificate, PRTERRINFO pErrInfo)
     1045{
     1046    return RTCrPemWriteAsn1ToVfsFile(hVfsFile, &pCertificate->SeqCore.Asn1Core, 0 /*fFlags*/,
     1047                                     g_aRTCrX509CertificateMarkers[0].paWords[0].pszWord, pErrInfo);
     1048}
    10361049
    10371050/** @name X.509 Certificate Extensions
  • trunk/include/iprt/mangling.h

    r84146 r84163  
    27642764# define RTVfsIoStrmSgWrite                             RT_MANGLER(RTVfsIoStrmSgWrite)
    27652765# define RTVfsIoStrmSkip                                RT_MANGLER(RTVfsIoStrmSkip)
     2766# define RTVfsIoStrmStrOutputCallback                   RT_MANGLER(RTVfsIoStrmStrOutputCallback)
    27662767# define RTVfsIoStrmTell                                RT_MANGLER(RTVfsIoStrmTell)
    27672768# define RTVfsIoStrmToFile                              RT_MANGLER(RTVfsIoStrmToFile)
     
    33413342# define RTCrPemParseContent                            RT_MANGLER(RTCrPemParseContent)
    33423343# define RTCrPemReadFile                                RT_MANGLER(RTCrPemReadFile)
     3344# define RTCrPemWriteBlob                               RT_MANGLER(RTCrPemWriteBlob)
     3345# define RTCrPemWriteAsn1                               RT_MANGLER(RTCrPemWriteAsn1)
     3346# define RTCrPemWriteAsn1ToVfsIoStrm                    RT_MANGLER(RTCrPemWriteAsn1ToVfsIoStrm)
     3347# define RTCrPemWriteAsn1ToVfsFile                      RT_MANGLER(RTCrPemWriteAsn1ToVfsFile)
    33433348# define RTCrPkcs5Pbkdf2Hmac                            RT_MANGLER(RTCrPkcs5Pbkdf2Hmac)
    33443349# define RTCrPkcs7Attribute_DecodeAsn1                  RT_MANGLER(RTCrPkcs7Attribute_DecodeAsn1)
  • trunk/include/iprt/vfs.h

    r84146 r84163  
    12291229RTDECL(ssize_t)     RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va);
    12301230
     1231/**
     1232 * VFS I/O stream output buffer structure to use with
     1233 * RTVfsIoStrmStrOutputCallback().
     1234 */
     1235typedef struct VFSIOSTRMOUTBUF
     1236{
     1237    /** The I/O stream handle. */
     1238    RTVFSIOSTREAM   hVfsIos;
     1239    /** Size of this structure (for sanity). */
     1240    size_t          cbSelf;
     1241    /** Status code of the operation. */
     1242    int             rc;
     1243    /** Current offset into szBuf (number of output bytes pending). */
     1244    size_t          offBuf;
     1245    /** Modest output buffer. */
     1246    char            szBuf[256];
     1247} VFSIOSTRMOUTBUF;
     1248/** Pointer to an VFS I/O stream output buffer for use with
     1249 *  RTVfsIoStrmStrOutputCallback() */
     1250typedef VFSIOSTRMOUTBUF *PVFSIOSTRMOUTBUF;
     1251
     1252/** Initializer for a VFS I/O stream output buffer. */
     1253#define VFSIOSTRMOUTBUF_INIT(a_pOutBuf, a_hVfsIos) \
     1254    do { \
     1255        (a_pOutBuf)->hVfsIos  = a_hVfsIos; \
     1256        (a_pOutBuf)->cbSelf   = sizeof(*(a_pOutBuf)); \
     1257        (a_pOutBuf)->rc       = VINF_SUCCESS; \
     1258        (a_pOutBuf)->offBuf   = 0; \
     1259        (a_pOutBuf)->szBuf[0] = '\0'; \
     1260    } while (0)
     1261
     1262/**
     1263 * @callback_method_impl{FNRTSTROUTPUT,
     1264 * For use with VFSIOSTRMOUTBUF.
     1265 *
     1266 * Users must use VFSIOSTRMOUTBUF_INIT to initialize a VFSIOSTRMOUTBUF and pass
     1267 * that as the outputter argument to the function this callback is handed to.}
     1268 */
     1269RTDECL(size_t) RTVfsIoStrmStrOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
     1270
    12311271/** @} */
    12321272
  • trunk/src/VBox/Runtime/Makefile.kmk

    r84146 r84163  
    393393        common/crypto/rsa-init.cpp \
    394394        common/crypto/rsa-sanity.cpp \
    395         common/crypto/pemfile.cpp \
     395        common/crypto/pemfile-read.cpp \
     396        common/crypto/pemfile-write.cpp \
    396397        common/crypto/pkcs7-asn1-decoder.cpp \
    397398        common/crypto/pkcs7-core.cpp \
     
    16821683        common/checksum/sha512str.cpp \
    16831684        common/crypto/digest-core.cpp \
    1684         common/crypto/pemfile.cpp \
     1685        common/crypto/pemfile-read.cpp \
     1686        common/crypto/pemfile-write.cpp \
    16851687        common/crypto/pkcs7-asn1-decoder.cpp \
    16861688        common/crypto/pkcs7-core.cpp \
  • trunk/src/VBox/Runtime/common/crypto/pemfile-read.cpp

    r84162 r84163  
    11/* $Id$ */
    22/** @file
    3  * IPRT - Crypto - PEM file reader / writer.
     3 * IPRT - Crypto - PEM file reader.
    44 *
    55 * See RFC-1341 for the original ideas for the format, but keep in mind
     
    651651    return NULL;
    652652}
     653
  • trunk/src/VBox/Runtime/common/vfs/vfsprintf.cpp

    r84152 r84163  
    3535
    3636
    37 /*********************************************************************************************************************************
    38 *   Structures and Typedefs                                                                                                      *
    39 *********************************************************************************************************************************/
    40 typedef struct PRINTFBUF
    41 {
    42     RTVFSIOSTREAM   hVfsIos;
    43     int             rc;
    44     size_t          offBuf;
    45     char            szBuf[256];
    46 } PRINTFBUF;
    47 
    48 
    4937/** Writes the buffer to the VFS file. */
    50 static void FlushPrintfBuffer(PRINTFBUF *pBuf)
     38static void FlushPrintfBuffer(PVFSIOSTRMOUTBUF pBuf)
    5139{
    5240    if (pBuf->offBuf)
     
    6149
    6250
    63 /** @callback_method_impl{FNRTSTROUTPUT} */
    64 static DECLCALLBACK(size_t) MyPrintfOutputter(void *pvArg, const char *pachChars, size_t cbChars)
     51/**
     52 * @callback_method_impl{FNRTSTROUTPUT,
     53 *      For use with VFSIOSTRMOUTBUF.}
     54 */
     55RTDECL(size_t) RTVfsIoStrmStrOutputCallback(void *pvArg, const char *pachChars, size_t cbChars)
    6556{
    66     PRINTFBUF *pBuf = (PRINTFBUF *)pvArg;
     57    PVFSIOSTRMOUTBUF pBuf = (PVFSIOSTRMOUTBUF)pvArg;
     58    AssertReturn(pBuf->cbSelf == sizeof(*pBuf), 0);
     59
    6760    if (cbChars != 0)
    6861    {
    69         size_t offSrc = 0;
    70         while  (offSrc < cbChars)
     62        if (cbChars <= sizeof(pBuf->szBuf) * 3 / 2)
    7163        {
    72             size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
    73             if (cbLeft > 0)
     64            /*
     65             * Small piece of output: Buffer it.
     66             */
     67            size_t offSrc = 0;
     68            while  (offSrc < cbChars)
    7469            {
    75                 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
    76                 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
    77                 pBuf->offBuf += cbToCopy;
    78                 pBuf->szBuf[pBuf->offBuf] = '\0';
    79                 if (cbLeft > cbToCopy)
    80                     break;
    81                 offSrc += cbToCopy;
     70                size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
     71                if (cbLeft > 0)
     72                {
     73                    size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
     74                    memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
     75                    pBuf->offBuf += cbToCopy;
     76                    pBuf->szBuf[pBuf->offBuf] = '\0';
     77                    if (cbLeft > cbToCopy)
     78                        break;
     79                    offSrc += cbToCopy;
     80                }
     81                FlushPrintfBuffer(pBuf);
    8282            }
     83        }
     84        else
     85        {
     86            /*
     87             * Large chunk of output: Output it directly.
     88            */
    8389            FlushPrintfBuffer(pBuf);
     90
     91            int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pachChars, cbChars, true /*fBlocking*/, NULL);
     92            if (RT_FAILURE(rc))
     93                pBuf->rc = rc;
    8494        }
    8595    }
     
    90100
    91101
    92 
    93102RTDECL(ssize_t) RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va)
    94103{
    95     PRINTFBUF Buf;
    96     Buf.hVfsIos  = hVfsIos;
    97     Buf.rc       = VINF_SUCCESS;
    98     Buf.offBuf   = 0;
    99     Buf.szBuf[0] = '\0';
     104    VFSIOSTRMOUTBUF Buf;
     105    VFSIOSTRMOUTBUF_INIT(&Buf, hVfsIos);
    100106
    101     size_t cchRet = RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, va);
     107    size_t cchRet = RTStrFormatV(RTVfsIoStrmStrOutputCallback, &Buf, NULL, NULL, pszFormat, va);
    102108    if (RT_SUCCESS(Buf.rc))
    103109        return cchRet;
Note: See TracChangeset for help on using the changeset viewer.

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