summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/f_int.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/f_int.c')
-rw-r--r--src/lib/libcrypto/asn1/f_int.c173
1 files changed, 96 insertions, 77 deletions
diff --git a/src/lib/libcrypto/asn1/f_int.c b/src/lib/libcrypto/asn1/f_int.c
index 6cea1f8485..f355dbacbe 100644
--- a/src/lib/libcrypto/asn1/f_int.c
+++ b/src/lib/libcrypto/asn1/f_int.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -61,132 +61,151 @@
61#include <openssl/buffer.h> 61#include <openssl/buffer.h>
62#include <openssl/asn1.h> 62#include <openssl/asn1.h>
63 63
64int i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a) 64int
65i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a)
65{ 66{
66 int i,n=0; 67 int i, n = 0;
67 static const char *h="0123456789ABCDEF"; 68 static const char *h = "0123456789ABCDEF";
68 char buf[2]; 69 char buf[2];
69 70
70 if (a == NULL) return(0); 71 if (a == NULL)
72 return (0);
71 73
72 if (a->type & V_ASN1_NEG) { 74 if (a->type & V_ASN1_NEG) {
73 if (BIO_write(bp, "-", 1) != 1) goto err; 75 if (BIO_write(bp, "-", 1) != 1)
76 goto err;
74 n = 1; 77 n = 1;
75 } 78 }
76 79
77 if (a->length == 0) { 80 if (a->length == 0) {
78 if (BIO_write(bp,"00",2) != 2) goto err; 81 if (BIO_write(bp, "00", 2) != 2)
82 goto err;
79 n += 2; 83 n += 2;
80 } else { 84 } else {
81 for (i=0; i<a->length; i++) { 85 for (i = 0; i < a->length; i++) {
82 if ((i != 0) && (i%35 == 0)) { 86 if ((i != 0) && (i % 35 == 0)) {
83 if (BIO_write(bp,"\\\n",2) != 2) goto err; 87 if (BIO_write(bp, "\\\n", 2) != 2)
84 n+=2; 88 goto err;
89 n += 2;
85 } 90 }
86 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f]; 91 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
87 buf[1]=h[((unsigned char)a->data[i] )&0x0f]; 92 buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
88 if (BIO_write(bp,buf,2) != 2) goto err; 93 if (BIO_write(bp, buf, 2) != 2)
89 n+=2; 94 goto err;
95 n += 2;
90 } 96 }
91 } 97 }
92 return(n); 98 return (n);
99
93err: 100err:
94 return(-1); 101 return (-1);
95} 102}
96 103
97int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size) 104int
105a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
98{ 106{
99 int ret=0; 107 int ret = 0;
100 int i,j,k,m,n,again,bufsize; 108 int i, j,k, m,n, again, bufsize;
101 unsigned char *s=NULL,*sp; 109 unsigned char *s = NULL, *sp;
102 unsigned char *bufp; 110 unsigned char *bufp;
103 int num=0,slen=0,first=1; 111 int num = 0, slen = 0, first = 1;
104 112
105 bs->type=V_ASN1_INTEGER; 113 bs->type = V_ASN1_INTEGER;
106 114
107 bufsize=BIO_gets(bp,buf,size); 115 bufsize = BIO_gets(bp, buf, size);
108 for (;;) { 116 for (;;) {
109 if (bufsize < 1) goto err_sl; 117 if (bufsize < 1)
110 i=bufsize; 118 goto err_sl;
111 if (buf[i-1] == '\n') buf[--i]='\0'; 119 i = bufsize;
112 if (i == 0) goto err_sl; 120 if (buf[i - 1] == '\n')
113 if (buf[i-1] == '\r') buf[--i]='\0'; 121 buf[--i] = '\0';
114 if (i == 0) goto err_sl; 122 if (i == 0)
115 again=(buf[i-1] == '\\'); 123 goto err_sl;
124 if (buf[i - 1] == '\r')
125 buf[--i] = '\0';
126 if (i == 0)
127 goto err_sl;
128 again = (buf[i - 1] == '\\');
116 129
117 for (j=0; j<i; j++) { 130 for (j = 0; j < i; j++) {
118 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) || 131 if (!(((buf[j] >= '0') && (buf[j] <= '9')) ||
119 ((buf[j] >= 'a') && (buf[j] <= 'f')) || 132 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
120 ((buf[j] >= 'A') && (buf[j] <= 'F')))) { 133 ((buf[j] >= 'A') && (buf[j] <= 'F')))) {
121 i=j; 134 i = j;
122 break; 135 break;
123 } 136 }
124 } 137 }
125 buf[i]='\0'; 138 buf[i] = '\0';
126 /* We have now cleared all the crap off the end of the 139 /* We have now cleared all the crap off the end of the
127 * line */ 140 * line */
128 if (i < 2) goto err_sl; 141 if (i < 2)
142 goto err_sl;
129 143
130 bufp=(unsigned char *)buf; 144 bufp = (unsigned char *)buf;
131 if (first) { 145 if (first) {
132 first=0; 146 first = 0;
133 if ((bufp[0] == '0') && (buf[1] == '0')) { 147 if ((bufp[0] == '0') && (buf[1] == '0')) {
134 bufp+=2; 148 bufp += 2;
135 i-=2; 149 i -= 2;
136 } 150 }
137 } 151 }
138 k=0; 152 k = 0;
139 i-=again; 153 i -= again;
140 if (i%2 != 0) { 154 if (i % 2 != 0) {
141 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_ODD_NUMBER_OF_CHARS); 155 ASN1err(ASN1_F_A2I_ASN1_INTEGER,
156 ASN1_R_ODD_NUMBER_OF_CHARS);
142 goto err; 157 goto err;
143 } 158 }
144 i/=2; 159 i /= 2;
145 if (num+i > slen) { 160 if (num + i > slen) {
146 if (s == NULL) 161 if (s == NULL)
147 sp=(unsigned char *)malloc( 162 sp = (unsigned char *)malloc(
148 (unsigned int)num+i*2); 163 (unsigned int)num + i * 2);
149 else 164 else
150 sp=OPENSSL_realloc_clean(s,slen,num+i*2); 165 sp = OPENSSL_realloc_clean(s, slen,
166 num + i * 2);
151 if (sp == NULL) { 167 if (sp == NULL) {
152 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ERR_R_MALLOC_FAILURE); 168 ASN1err(ASN1_F_A2I_ASN1_INTEGER,
153 if (s != NULL) free(s); 169 ERR_R_MALLOC_FAILURE);
170 if (s != NULL)
171 free(s);
154 goto err; 172 goto err;
155 } 173 }
156 s=sp; 174 s = sp;
157 slen=num+i*2; 175 slen = num + i * 2;
158 } 176 }
159 for (j=0; j<i; j++,k+=2) { 177 for (j = 0; j < i; j++, k += 2) {
160 for (n=0; n<2; n++) { 178 for (n = 0; n < 2; n++) {
161 m=bufp[k+n]; 179 m = bufp[k + n];
162 if ((m >= '0') && (m <= '9')) 180 if ((m >= '0') && (m <= '9'))
163 m-='0'; 181 m -= '0';
164 else if ((m >= 'a') && (m <= 'f')) 182 else if ((m >= 'a') && (m <= 'f'))
165 m=m-'a'+10; 183 m = m - 'a' + 10;
166 else if ((m >= 'A') && (m <= 'F')) 184 else if ((m >= 'A') && (m <= 'F'))
167 m=m-'A'+10; 185 m = m - 'A' + 10;
168 else { 186 else {
169 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_NON_HEX_CHARACTERS); 187 ASN1err(ASN1_F_A2I_ASN1_INTEGER,
188 ASN1_R_NON_HEX_CHARACTERS);
170 goto err; 189 goto err;
171 } 190 }
172 s[num+j]<<=4; 191 s[num + j] <<= 4;
173 s[num+j]|=m; 192 s[num + j] |= m;
174 } 193 }
175 } 194 }
176 num+=i; 195 num += i;
177 if (again) 196 if (again)
178 bufsize=BIO_gets(bp,buf,size); 197 bufsize = BIO_gets(bp, buf, size);
179 else 198 else
180 break; 199 break;
181 } 200 }
182 bs->length=num; 201 bs->length = num;
183 bs->data=s; 202 bs->data = s;
184 ret=1; 203 ret = 1;
204
185err: 205err:
186 if (0) { 206 if (0) {
187err_sl: 207err_sl:
188 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_SHORT_LINE); 208 ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE);
189 } 209 }
190 return(ret); 210 return (ret);
191} 211}
192