If input is bad, we still need to clear the buffer.
[openssl.git] / crypto / evp / encode.c
index 6ff9c1783cf60720ee60a29298746ec5e0427356..2d738f4b0108fb8c0f98301d2080426dce7f8424 100644 (file)
@@ -129,13 +129,14 @@ void EVP_EncodeInit(EVP_ENCODE_CTX *ctx)
        }
 
 void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
        }
 
 void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
-            unsigned char *in, int inl)
+            const unsigned char *in, int inl)
        {
        int i,j;
        unsigned int total=0;
 
        *outl=0;
        if (inl == 0) return;
        {
        int i,j;
        unsigned int total=0;
 
        *outl=0;
        if (inl == 0) return;
+       OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data));
        if ((ctx->num+inl) < ctx->length)
                {
                memcpy(&(ctx->enc_data[ctx->num]),in,inl);
        if ((ctx->num+inl) < ctx->length)
                {
                memcpy(&(ctx->enc_data[ctx->num]),in,inl);
@@ -232,7 +233,7 @@ void EVP_DecodeInit(EVP_ENCODE_CTX *ctx)
  *  1 for full line
  */
 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
  *  1 for full line
  */
 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
-            unsigned char *in, int inl)
+            const unsigned char *in, int inl)
        {
        int seof= -1,eof=0,rv= -1,ret=0,i,v,tmp,n,ln,tmp2,exp_nl;
        unsigned char *d;
        {
        int seof= -1,eof=0,rv= -1,ret=0,i,v,tmp,n,ln,tmp2,exp_nl;
        unsigned char *d;
@@ -258,6 +259,7 @@ int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
                /* only save the good data :-) */
                if (!B64_NOT_BASE64(v))
                        {
                /* only save the good data :-) */
                if (!B64_NOT_BASE64(v))
                        {
+                       OPENSSL_assert(n < (int)sizeof(ctx->enc_data));
                        d[n++]=tmp;
                        ln++;
                        }
                        d[n++]=tmp;
                        ln++;
                        }
@@ -277,6 +279,13 @@ int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
                        eof++;
                        }
 
                        eof++;
                        }
 
+               if (v == B64_CR)
+                       {
+                       ln = 0;
+                       if (exp_nl)
+                               continue;
+                       }
+
                /* eoln */
                if (v == B64_EOLN)
                        {
                /* eoln */
                if (v == B64_EOLN)
                        {
@@ -314,8 +323,8 @@ int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
                        if (n > 0)
                                {
                                v=EVP_DecodeBlock(out,d,n);
                        if (n > 0)
                                {
                                v=EVP_DecodeBlock(out,d,n);
-                               if (v < 0) { rv=0; goto end; }
                                n=0;
                                n=0;
+                               if (v < 0) { rv=0; goto end; }
                                ret+=(v-eof);
                                }
                        else
                                ret+=(v-eof);
                                }
                        else