summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_bitstr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/a_bitstr.c')
-rw-r--r--src/lib/libcrypto/asn1/a_bitstr.c74
1 files changed, 51 insertions, 23 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c
index c77456b315..7013a407ad 100644
--- a/src/lib/libcrypto/asn1/a_bitstr.c
+++ b/src/lib/libcrypto/asn1/a_bitstr.c
@@ -70,13 +70,27 @@ int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
70{ return M_ASN1_BIT_STRING_set(x, d, len); } 70{ return M_ASN1_BIT_STRING_set(x, d, len); }
71 71
72int i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp) 72int i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
73{
74 int len, ret;
75 len = i2c_ASN1_BIT_STRING(a, NULL);
76 ret=ASN1_object_size(0,len,V_ASN1_BIT_STRING);
77 if(pp) {
78 ASN1_put_object(pp,0,len,V_ASN1_BIT_STRING,V_ASN1_UNIVERSAL);
79 i2c_ASN1_BIT_STRING(a, pp);
80 }
81 return ret;
82}
83
84int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
73 { 85 {
74 int ret,j,r,bits,len; 86 int ret,j,bits,len;
75 unsigned char *p,*d; 87 unsigned char *p,*d;
76 88
77 if (a == NULL) return(0); 89 if (a == NULL) return(0);
78 90
79 len=a->length; 91 len=a->length;
92 ret=1+len;
93 if (pp == NULL) return(ret);
80 94
81 if (len > 0) 95 if (len > 0)
82 { 96 {
@@ -104,36 +118,27 @@ int i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
104 } 118 }
105 else 119 else
106 bits=0; 120 bits=0;
107 ret=1+len;
108 r=ASN1_object_size(0,ret,V_ASN1_BIT_STRING);
109 if (pp == NULL) return(r);
110 p= *pp; 121 p= *pp;
111 122
112 ASN1_put_object(&p,0,ret,V_ASN1_BIT_STRING,V_ASN1_UNIVERSAL);
113 *(p++)=(unsigned char)bits; 123 *(p++)=(unsigned char)bits;
114 d=a->data; 124 d=a->data;
115 memcpy(p,d,len); 125 memcpy(p,d,len);
116 p+=len; 126 p+=len;
117 if (len > 0) p[-1]&=(0xff<<bits); 127 if (len > 0) p[-1]&=(0xff<<bits);
118 *pp=p; 128 *pp=p;
119 return(r); 129 return(ret);
120 } 130 }
121 131
132
133/* Convert DER encoded ASN1 BIT_STRING to ASN1_BIT_STRING structure */
122ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, unsigned char **pp, 134ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, unsigned char **pp,
123 long length) 135 long length)
124 { 136{
125 ASN1_BIT_STRING *ret=NULL; 137 unsigned char *p;
126 unsigned char *p,*s;
127 long len; 138 long len;
128 int inf,tag,xclass;
129 int i; 139 int i;
130 140 int inf,tag,xclass;
131 if ((a == NULL) || ((*a) == NULL)) 141 ASN1_BIT_STRING *ret;
132 {
133 if ((ret=M_ASN1_BIT_STRING_new()) == NULL) return(NULL);
134 }
135 else
136 ret=(*a);
137 142
138 p= *pp; 143 p= *pp;
139 inf=ASN1_get_object(&p,&len,&tag,&xclass,length); 144 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
@@ -149,7 +154,30 @@ ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, unsigned char **pp,
149 goto err; 154 goto err;
150 } 155 }
151 if (len < 1) { i=ASN1_R_STRING_TOO_SHORT; goto err; } 156 if (len < 1) { i=ASN1_R_STRING_TOO_SHORT; goto err; }
157 ret = c2i_ASN1_BIT_STRING(a, &p, len);
158 if(ret) *pp = p;
159 return ret;
160err:
161 ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,i);
162 return(NULL);
163
164}
165
166ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, unsigned char **pp,
167 long len)
168 {
169 ASN1_BIT_STRING *ret=NULL;
170 unsigned char *p,*s;
171 int i;
172
173 if ((a == NULL) || ((*a) == NULL))
174 {
175 if ((ret=M_ASN1_BIT_STRING_new()) == NULL) return(NULL);
176 }
177 else
178 ret=(*a);
152 179
180 p= *pp;
153 i= *(p++); 181 i= *(p++);
154 /* We do this to preserve the settings. If we modify 182 /* We do this to preserve the settings. If we modify
155 * the settings, via the _set_bit function, we will recalculate 183 * the settings, via the _set_bit function, we will recalculate
@@ -159,7 +187,7 @@ ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, unsigned char **pp,
159 187
160 if (len-- > 1) /* using one because of the bits left byte */ 188 if (len-- > 1) /* using one because of the bits left byte */
161 { 189 {
162 s=(unsigned char *)Malloc((int)len); 190 s=(unsigned char *)OPENSSL_malloc((int)len);
163 if (s == NULL) 191 if (s == NULL)
164 { 192 {
165 i=ERR_R_MALLOC_FAILURE; 193 i=ERR_R_MALLOC_FAILURE;
@@ -173,7 +201,7 @@ ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, unsigned char **pp,
173 s=NULL; 201 s=NULL;
174 202
175 ret->length=(int)len; 203 ret->length=(int)len;
176 if (ret->data != NULL) Free(ret->data); 204 if (ret->data != NULL) OPENSSL_free(ret->data);
177 ret->data=s; 205 ret->data=s;
178 ret->type=V_ASN1_BIT_STRING; 206 ret->type=V_ASN1_BIT_STRING;
179 if (a != NULL) (*a)=ret; 207 if (a != NULL) (*a)=ret;
@@ -204,14 +232,14 @@ int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
204 { 232 {
205 if (!value) return(1); /* Don't need to set */ 233 if (!value) return(1); /* Don't need to set */
206 if (a->data == NULL) 234 if (a->data == NULL)
207 c=(unsigned char *)Malloc(w+1); 235 c=(unsigned char *)OPENSSL_malloc(w+1);
208 else 236 else
209 c=(unsigned char *)Realloc(a->data,w+1); 237 c=(unsigned char *)OPENSSL_realloc(a->data,w+1);
210 if (c == NULL) return(0); 238 if (c == NULL) return(0);
239 if (w+1-a->length > 0) memset(c+a->length, 0, w+1-a->length);
211 a->data=c; 240 a->data=c;
212 a->length=w+1; 241 a->length=w+1;
213 c[w]=0; 242 }
214 }
215 a->data[w]=((a->data[w])&iv)|v; 243 a->data[w]=((a->data[w])&iv)|v;
216 while ((a->length > 0) && (a->data[a->length-1] == 0)) 244 while ((a->length > 0) && (a->data[a->length-1] == 0))
217 a->length--; 245 a->length--;