58c01e8a4779988b48c5135069e9be69d85f5b0e
1 /* Copyright (C) 2007 The Written Word, Inc. All rights reserved.
2 * Author: Simon Josefsson
4 * Redistribution and use in source and binary forms,
5 * with or without modification, are permitted provided
6 * that the following conditions are met:
8 * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * Neither the name of the copyright holder nor the names
18 * of any other contributors may be used to endorse or
19 * promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #include "libssh2_priv.h"
41 readline(char *line
, int line_size
, FILE * fp
)
43 if (!fgets(line
, line_size
, fp
)) {
46 if (*line
&& line
[strlen(line
) - 1] == '\n') {
47 line
[strlen(line
) - 1] = '\0';
49 if (*line
&& line
[strlen(line
) - 1] == '\r') {
50 line
[strlen(line
) - 1] = '\0';
58 _libssh2_pem_parse(LIBSSH2_SESSION
* session
,
59 const char *headerbegin
,
60 const char *headerend
,
61 FILE * fp
, char **data
, unsigned int *datalen
)
65 unsigned int b64datalen
= 0;
69 if (readline(line
, LINE_SIZE
, fp
)) {
73 while (strcmp(line
, headerbegin
) != 0);
82 linelen
= strlen(line
);
83 tmp
= LIBSSH2_REALLOC(session
, b64data
, b64datalen
+ linelen
);
88 memcpy(tmp
+ b64datalen
, line
, linelen
);
90 b64datalen
+= linelen
;
93 if (readline(line
, LINE_SIZE
, fp
)) {
97 } while (strcmp(line
, headerend
) != 0);
99 if (libssh2_base64_decode(session
, data
, datalen
, b64data
, b64datalen
)) {
107 LIBSSH2_FREE(session
, b64data
);
113 read_asn1_length(const unsigned char *data
,
114 unsigned int datalen
, unsigned int *len
)
125 lenlen
= *len
& 0x7F;
127 if (1 + lenlen
> datalen
) {
138 nextpos
= 1 + lenlen
;
139 if (lenlen
> 2 || 1 + lenlen
+ *len
> datalen
) {
147 _libssh2_pem_decode_sequence(unsigned char **data
, unsigned int *datalen
)
156 if ((*data
)[0] != '\x30') {
163 lenlen
= read_asn1_length(*data
, *datalen
, &len
);
164 if (lenlen
< 0 || lenlen
+ len
!= *datalen
) {
175 _libssh2_pem_decode_integer(unsigned char **data
, unsigned int *datalen
,
176 unsigned char **i
, unsigned int *ilen
)
185 if ((*data
)[0] != '\x02') {
192 lenlen
= read_asn1_length(*data
, *datalen
, &len
);
193 if (lenlen
< 0 || lenlen
+ len
> *datalen
) {