]> git.saurik.com Git - apple/security.git/blob - CertTool/cdsaUtils/pem.cpp
Security-54.1.tar.gz
[apple/security.git] / CertTool / cdsaUtils / pem.cpp
1 /*
2 File: pem.h
3
4 Description: PEM encode/decode routines
5
6 Author: dmitch
7
8 Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved.
9
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.
16
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.
35
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.
42
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.
52 */
53
54 #include "pem.h"
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <strings.h>
58 #include "cuEnc64.h"
59
60 int pemEncode(
61 const unsigned char *inData,
62 unsigned inDataLen,
63 unsigned char **outData,
64 unsigned *outDataLen,
65 const char *headerString)
66 {
67 unsigned char *enc;
68 unsigned encLen;
69
70 /* First base64 encode */
71 enc = enc64WithLines(inData, inDataLen, 64, &encLen);
72 if(enc == NULL) {
73 /* malloc error is actually the only known failure */
74 printf("***pemEncode: Error encoding file. Aborting.\n");
75 return -1;
76 }
77
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);
84
85 if((*outData)[*outDataLen - 1] == '\0') {
86 (*outDataLen)--;
87 }
88 free(enc);
89 return 0;
90 }
91
92 int pemDecode(
93 const unsigned char *inData,
94 unsigned inDataLen,
95 unsigned char **outData,
96 unsigned *outDataLen)
97 {
98 char *cp;
99 int freeCp = 0;
100 char *curr1, *curr2;
101 char *startPem = NULL;
102 char *endPem = NULL;
103 unsigned char *out;
104 unsigned outLen;
105 int ourRtn = 0;
106
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';
112 inDataLen++;
113 freeCp = 1;
114 }
115 else {
116 /* already is */
117 cp = (char *)inData;
118 }
119
120 /* cp is start of NULL-terminated buffer, size inDataLen */
121 /* skip over everything until "-----" */
122 curr1 = strstr(cp, "-----");
123 if(curr1 == NULL) {
124 printf("***pemDecode: no terminator found\n");
125 ourRtn = -1;
126 goto abort;
127 }
128
129 /* find end of separator line, handling both flavors of terminator */
130 cp = curr1;
131 curr1 = strchr(cp, '\n');
132 curr2 = strchr(cp, '\r');
133 if((curr1 == NULL) & (curr2 == NULL)) {
134 printf("***pemDecode: Bad PEM format (1)\n");
135 ourRtn = -1;
136 goto abort;
137 }
138 if(curr1 == NULL) {
139 startPem = curr2;
140 }
141 else {
142 startPem = curr1;
143 }
144
145 /* startPem points to end of separator line */
146 /* locate ending terminator and lop it off */
147 curr1 = strstr(startPem, "-----");
148 if(curr1 == NULL) {
149 printf("***pemDecode: Bad PEM format (2)\n");
150 ourRtn = -1;
151 goto abort;
152 }
153 endPem = curr1;
154 /* endPem points to last PEM data plus one */
155
156 out = dec64((unsigned char *)startPem, endPem-startPem, &outLen);
157 if(out == NULL) {
158 printf("Bad PEM format (3)\n");
159 ourRtn = -1;
160 goto abort;
161 }
162 *outData = out;
163 *outDataLen = outLen;
164 abort:
165 if(freeCp) {
166 free(cp);
167 }
168 return ourRtn;
169 }
170