]>
git.saurik.com Git - apple/security.git/blob - CertTool/cdsaUtils/pem.cpp
4 Description: PEM encode/decode routines
8 Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved.
10 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
11 Computer, Inc. ("Apple") in consideration of your agreement to
12 the following terms, and your use, installation, modification
13 or redistribution of this Apple software constitutes acceptance
14 of these terms. If you do not agree with these terms, please
15 do not use, install, modify or redistribute this Apple software.
17 In consideration of your agreement to abide by the following
18 terms, and subject to these terms, Apple grants you a personal,
19 non-exclusive license, under Apple's copyrights in this
20 original Apple software (the "Apple Software"), to use,
21 reproduce, modify and redistribute the Apple Software, with
22 or without modifications, in source and/or binary forms;
23 provided that if you redistribute the Apple Software in
24 its entirety and without modifications, you must retain
25 this notice and the following text and disclaimers in all
26 such redistributions of the Apple Software. Neither the
27 name, trademarks, service marks or logos of Apple Computer,
28 Inc. may be used to endorse or promote products derived from the
29 Apple Software without specific prior written permission from
30 Apple. Except as expressly stated in this notice, no other
31 rights or licenses, express or implied, are granted by Apple
32 herein, including but not limited to any patent rights that
33 may be infringed by your derivative works or by other works
34 in which the Apple Software may be incorporated.
36 The Apple Software is provided by Apple on an "AS IS" basis.
37 APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
38 WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
39 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
40 REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE
41 OR IN COMBINATION WITH YOUR PRODUCTS.
43 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
44 INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
48 AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
49 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING
50 NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE
51 HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 const unsigned char *inData
,
63 unsigned char **outData
,
65 const char *headerString
)
70 /* First base64 encode */
71 enc
= enc64WithLines(inData
, inDataLen
, 64, &encLen
);
73 /* malloc error is actually the only known failure */
74 printf("***pemEncode: Error encoding file. Aborting.\n");
78 /* estimate outsize - just be sloppy, way conservative */
79 unsigned outSize
= encLen
+ (2 * strlen(headerString
)) + 200;
80 *outData
= (unsigned char *)malloc(outSize
);
81 sprintf((char *)*outData
, "-----BEGIN %s-----\n%s-----END %s-----\n",
82 headerString
, (char *)enc
, headerString
);
83 *outDataLen
= strlen((char *)*outData
);
85 if((*outData
)[*outDataLen
- 1] == '\0') {
93 const unsigned char *inData
,
95 unsigned char **outData
,
101 char *startPem
= NULL
;
107 /* make the whole thing a NULL-terminated string */
108 if(inData
[inDataLen
- 1] != '\0') {
109 cp
= (char *)malloc(inDataLen
+ 1);
110 memmove(cp
, inData
, inDataLen
);
111 cp
[inDataLen
] = '\0';
120 /* cp is start of NULL-terminated buffer, size inDataLen */
121 /* skip over everything until "-----" */
122 curr1
= strstr(cp
, "-----");
124 printf("***pemDecode: no terminator found\n");
129 /* find end of separator line, handling both flavors of terminator */
131 curr1
= strchr(cp
, '\n');
132 curr2
= strchr(cp
, '\r');
133 if((curr1
== NULL
) & (curr2
== NULL
)) {
134 printf("***pemDecode: Bad PEM format (1)\n");
145 /* startPem points to end of separator line */
146 /* locate ending terminator and lop it off */
147 curr1
= strstr(startPem
, "-----");
149 printf("***pemDecode: Bad PEM format (2)\n");
154 /* endPem points to last PEM data plus one */
156 out
= dec64((unsigned char *)startPem
, endPem
-startPem
, &outLen
);
158 printf("Bad PEM format (3)\n");
163 *outDataLen
= outLen
;