예제 #1
1
/***********************
    Decode
************************/
int mite_string_2_base64(char *input,int input_len,char **base64)
{
    EVP_ENCODE_CTX ectx;

    EVP_EncodeInit(&ectx);

    int out_len =0;
    if(input_len%3==0)
        out_len = input_len/3 *4;
    else
        out_len = (input_len/3 +1)*4;

    //数据够长的话会有多个换行
    char *out = (char *)malloc(out_len + out_len/80 +1);
    int total = 0;
    int encode_len = 0;
    EVP_EncodeUpdate(&ectx,out,&encode_len,input,input_len);
    total += encode_len;
    EVP_EncodeFinal(&ectx,out+total,&encode_len);
    total += encode_len;

    printf("%s",out);
    *base64 = out;
    return total;
}
예제 #2
1
std::string base64_encodestring(const char* text, int len) 

{
    EVP_ENCODE_CTX ectx;
    int size = len*2;
    size = size > 64 ? size : 64;
    unsigned char* out = (unsigned char*)malloc( size );
    int outlen = 0;
    int tlen = 0;

    EVP_EncodeInit(&ectx);
    EVP_EncodeUpdate(&ectx,
            out,
            &outlen,
            (const unsigned char*)text,
            len
            );
    tlen += outlen;
    EVP_EncodeFinal( &ectx, out+tlen, &outlen );
    tlen += outlen;

    std::string str((char*)out, tlen );
    free( out );
    return str;
}
예제 #3
0
파일: pem_lib.c 프로젝트: RobinWuDev/Qt
int PEM_write_bio(BIO *bp, const char *name, const char *header,
		  const unsigned char *data, long len)
	{
	int nlen,n,i,j,outl;
	unsigned char *buf = NULL;
	EVP_ENCODE_CTX ctx;
	int reason=ERR_R_BUF_LIB;
	
	EVP_EncodeInit(&ctx);
	nlen=strlen(name);

	if (	(BIO_write(bp,"-----BEGIN ",11) != 11) ||
		(BIO_write(bp,name,nlen) != nlen) ||
		(BIO_write(bp,"-----\n",6) != 6))
		goto err;
		
	i=strlen(header);
	if (i > 0)
		{
		if (	(BIO_write(bp,header,i) != i) ||
			(BIO_write(bp,"\n",1) != 1))
			goto err;
		}

	buf = OPENSSL_malloc(PEM_BUFSIZE*8);
	if (buf == NULL)
		{
		reason=ERR_R_MALLOC_FAILURE;
		goto err;
		}

	i=j=0;
	while (len > 0)
		{
		n=(int)((len>(PEM_BUFSIZE*5))?(PEM_BUFSIZE*5):len);
		EVP_EncodeUpdate(&ctx,buf,&outl,&(data[j]),n);
		if ((outl) && (BIO_write(bp,(char *)buf,outl) != outl))
			goto err;
		i+=outl;
		len-=n;
		j+=n;
		}
	EVP_EncodeFinal(&ctx,buf,&outl);
	if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err;
	OPENSSL_cleanse(buf, PEM_BUFSIZE*8);
	OPENSSL_free(buf);
	buf = NULL;
	if (	(BIO_write(bp,"-----END ",9) != 9) ||
		(BIO_write(bp,name,nlen) != nlen) ||
		(BIO_write(bp,"-----\n",6) != 6))
		goto err;
	return(i+outl);
err:
	if (buf) {
		OPENSSL_cleanse(buf, PEM_BUFSIZE*8);
		OPENSSL_free(buf);
	}
	OPENSSL_PUT_ERROR(PEM, PEM_write_bio, reason);
	return(0);
	}
예제 #4
0
int main(void)
{
	int i, len, total = 0, total2 = 0, ret;
	EVP_ENCODE_CTX ctx1, ctx2;
	unsigned char f[60];
	unsigned char t[80 + 1];
	for(i = 0; i < 60; i++)
	{
	  memset(&f[i], i, 1);	
	}
	EVP_EncodeInit(&ctx1);
	EVP_EncodeUpdate(&ctx1, t, &len, f, 60);
	total += len;
	printf("total = %d\n", total);
//	printf("%s\n", t);
	EVP_EncodeFinal(&ctx1, t + total, &len);
	total += len;
	printf("%total = %d\n", total);
	printf("%s\n", t);
	EVP_DecodeInit(&ctx2);
	ret = EVP_DecodeUpdate(&ctx2, f, &len, t, total);
	total2 += len;
	ret = EVP_DecodeFinal(&ctx2, f, &len);
	total2 += len;
	for(i = 0; i < total2; i++)
	{
		printf("%02x\t", f[i]);
	}
printf("\n");
	return 0;
}
예제 #5
0
void cm_base_64_encode(const unsigned char *in, int32_t inlen,unsigned char *out, int32_t *outlen)
{
  
  EVP_ENCODE_CTX ctx;
 
  EVP_EncodeInit(&ctx);
  EVP_EncodeUpdate(&ctx,out,outlen,(char*)in, inlen);
  EVP_EncodeFinal(&ctx,out,outlen);
  return;
}
unsigned int OpenSSLCryptoBase64::encodeFinish(unsigned char * outData,
							 	      unsigned int outLength) {

	int outLen;
	outLen = outLength;

	EVP_EncodeFinal(&m_ectx, outData, &outLen); 

	return outLen;

}
예제 #7
0
/*
 * Convert a raw byte string into a null-terminated base64 ASCII string.
 * Returns 1 on success or 0 on error.
 */
static int t_tob64(char *dst, const unsigned char *src, int size)
{
    EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
    int outl = 0, outl2 = 0;
    unsigned char pad[2] = {0, 0};
    size_t leadz = 0;

    if (ctx == NULL)
        return 0;

    EVP_EncodeInit(ctx);
    evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_NO_NEWLINES
                                  | EVP_ENCODE_CTX_USE_SRP_ALPHABET);

    /*
     * We pad at the front with zero bytes until the length is a multiple of 3
     * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
     * padding
     */
    leadz = 3 - (size % 3);
    if (leadz != 3
            && !EVP_EncodeUpdate(ctx, (unsigned char *)dst, &outl, pad,
                                 leadz)) {
        EVP_ENCODE_CTX_free(ctx);
        return 0;
    }

    if (!EVP_EncodeUpdate(ctx, (unsigned char *)dst + outl, &outl2, src,
                          size)) {
        EVP_ENCODE_CTX_free(ctx);
        return 0;
    }
    outl += outl2;
    EVP_EncodeFinal(ctx, (unsigned char *)dst + outl, &outl2);
    outl += outl2;

    /* Strip the encoded padding at the front */
    if (leadz != 3) {
        memmove(dst, dst + leadz, outl - leadz);
        dst[outl - leadz] = '\0';
    }

    EVP_ENCODE_CTX_free(ctx);
    return 1;
}
예제 #8
0
파일: pem_seal.c 프로젝트: benwh4/libressl
int
PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig, int *sigl,
    unsigned char *out, int *outl, EVP_PKEY *priv)
{
	unsigned char *s = NULL;
	int ret = 0, j;
	unsigned int i;

	if (priv->type != EVP_PKEY_RSA) {
		PEMerr(PEM_F_PEM_SEALFINAL, PEM_R_PUBLIC_KEY_NO_RSA);
		goto err;
	}
	i = RSA_size(priv->pkey.rsa);
	if (i < 100)
		i = 100;
	s = reallocarray(NULL, i, 2);
	if (s == NULL) {
		PEMerr(PEM_F_PEM_SEALFINAL, ERR_R_MALLOC_FAILURE);
		goto err;
	}

	if (!EVP_EncryptFinal_ex(&ctx->cipher, s, (int *)&i))
		goto err;
	EVP_EncodeUpdate(&ctx->encode, out, &j, s, i);
	*outl = j;
	out += j;
	EVP_EncodeFinal(&ctx->encode, out, &j);
	*outl += j;

	if (!EVP_SignFinal(&ctx->md, s, &i, priv))
		goto err;
	*sigl = EVP_EncodeBlock(sig, s, i);

	ret = 1;

err:
	EVP_MD_CTX_cleanup(&ctx->md);
	EVP_CIPHER_CTX_cleanup(&ctx->cipher);
	if (s != NULL)
		free(s);
	return (ret);
}
예제 #9
0
void openssl_evp_encode()
{
	FILE *fin, *fout;
	int inLen, outLen;
	EVP_ENCODE_CTX ctx;
	unsigned char ins[MAX1_LEN], outs[MAX2_LEN];

	fin = fopen("/tmp/test.dat", "rb");
	fout = fopen("/tmp/test.txt", "w");
	memset(ins, 0, sizeof(ins));
	memset(outs, 0, sizeof(outs));
	printf("\nEVP_Encode(/tmp/test.dat) = ");
	EVP_EncodeInit(&ctx);
	while ((inLen = fread(ins, 1, MAX1_LEN, fin)) > 0) {
		EVP_EncodeUpdate(&ctx, outs, &outLen, ins, inLen);
		fwrite(outs, 1, outLen, fout);
		printf("%s", outs);
	}
	EVP_EncodeFinal(&ctx, outs, &outLen);
	fwrite(outs, 1, outLen, fout);
	printf("%s", outs);
	fclose(fin);
	fclose(fout);

	fin = fopen("/tmp/test.txt", "r");
	fout = fopen("/tmp/test-1.dat", "wb");
	memset(ins, 0, sizeof(ins));
	memset(outs, 0, sizeof(outs));
	printf("\nEVP_Decode(/tmp/test.txt) = ");
	EVP_DecodeInit(&ctx);
	while ((inLen = fread(ins, 1, MAX1_LEN, fin)) > 0) {
		EVP_DecodeUpdate(&ctx, outs, &outLen, ins, inLen);
		fwrite(outs, 1, outLen, fout);
		printf("%s", outs);
	}
	EVP_DecodeFinal(&ctx, outs, &outLen);
	fwrite(outs, 1, outLen, fout);
	printf("%s\n", outs);
	fclose(fin);
	fclose(fout);
}
예제 #10
0
int _ldapfull_base64_encode( const char *src, int srclen, char **ret, int *rlen ) {
    int tlen = 0;
    char *text;
    EVP_ENCODE_CTX EVP_ctx;

    text = (char *)malloc((srclen*4/3) + 1 );
    if (text == NULL) {
        return 0;
    }

    EVP_EncodeInit(&EVP_ctx);
    EVP_EncodeUpdate(&EVP_ctx, text, &tlen, (char *)src, srclen);
    EVP_EncodeFinal(&EVP_ctx, text, &tlen); 

    *ret = text; 
    if (rlen != NULL) {
        *rlen = tlen;
    }

    return 1;
}
예제 #11
0
파일: format.c 프로젝트: argp/ibe-win32
static void mime_put(byte_string_t bs, FILE *outfp)
{
    char out[1024];
    int outl;
    int i, l;

    EVP_ENCODE_CTX ctx;

    EVP_EncodeInit(&ctx);

    for (i=0; i<bs->len; i+=l) {
	if (i+crypt_buf_size > bs->len) {
	    l = bs->len - i;
	} else {
	    l = crypt_buf_size;
	}
	EVP_EncodeUpdate(&ctx,out,&outl,&bs->data[i],l);
	fwrite(out, 1, outl, outfp);
    }
    EVP_EncodeFinal(&ctx,out,&outl);
    fwrite(out, 1, outl, outfp);
}
예제 #12
0
/** Base64 encode <b>srclen</b> bytes of data from <b>src</b>.  Write
 * the result into <b>dest</b>, if it will fit within <b>destlen</b>
 * bytes.  Return the number of bytes written on success; -1 if
 * destlen is too short, or other failure.
 */
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen)
{
    /* FFFF we might want to rewrite this along the lines of base64_decode, if
     * it ever shows up in the profile. */
    EVP_ENCODE_CTX ctx;
    int len, ret;
//    assert(srclen < INT_MAX);

    /* 48 bytes of input -> 64 bytes of output plus newline.
       Plus one more byte, in case I'm wrong.
     */
    if (destlen < ((srclen/48)+1)*66)
        return -1;
    if (destlen > SIZE_T_CEILING)
        return -1;

    EVP_EncodeInit(&ctx);
    EVP_EncodeUpdate(&ctx, (unsigned char*)dest, &len,
            (unsigned char*)src, (int)srclen);
    EVP_EncodeFinal(&ctx, (unsigned char*)(dest+len), &ret);
    ret += len;
    return ret;
}
예제 #13
0
/*!
 * @brief Perform Basic authentication for now. Support digest in the 
 *  future.
 */
static const char *sstp_proxy_basicauth(const char *user, 
        const char *pass, char *buf, int size)
{
    EVP_ENCODE_CTX ctx;
    int tot = 0;
    int len = 0;
    unsigned char out[255];

    EVP_EncodeInit  (&ctx);
    EVP_EncodeUpdate(&ctx, out + tot, &len, 
		(unsigned char*) user, strlen(user));
    tot += len;
    EVP_EncodeUpdate(&ctx, out + tot, &len, 
		(unsigned char*) ":", 1);
    tot += len;
    EVP_EncodeUpdate(&ctx, out + tot, &len, 
		(unsigned char*) pass, strlen(pass));
    tot += len;
    EVP_EncodeFinal (&ctx, out + tot, &len);
    tot += len;

    snprintf(buf, size, "Basic %s", out);
    return (buf);
}
예제 #14
0
파일: bio_b64.c 프로젝트: AndreV84/openssl
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
{
    BIO_B64_CTX *ctx;
    long ret = 1;
    int i;

    ctx = (BIO_B64_CTX *)b->ptr;

    switch (cmd) {
    case BIO_CTRL_RESET:
        ctx->cont = 1;
        ctx->start = 1;
        ctx->encode = B64_NONE;
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;
    case BIO_CTRL_EOF:         /* More to read */
        if (ctx->cont <= 0)
            ret = 1;
        else
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;
    case BIO_CTRL_WPENDING:    /* More to write in buffer */
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
        ret = ctx->buf_len - ctx->buf_off;
        if ((ret == 0) && (ctx->encode != B64_NONE)
            && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
            ret = 1;
        else if (ret <= 0)
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;
    case BIO_CTRL_PENDING:     /* More to read in buffer */
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
        ret = ctx->buf_len - ctx->buf_off;
        if (ret <= 0)
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;
    case BIO_CTRL_FLUSH:
        /* do a final write */
 again:
        while (ctx->buf_len != ctx->buf_off) {
            i = b64_write(b, NULL, 0);
            if (i < 0)
                return i;
        }
        if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
            if (ctx->tmp_len != 0) {
                ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
                                               (unsigned char *)ctx->tmp,
                                               ctx->tmp_len);
                ctx->buf_off = 0;
                ctx->tmp_len = 0;
                goto again;
            }
        } else if (ctx->encode != B64_NONE
                   && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
            ctx->buf_off = 0;
            EVP_EncodeFinal(ctx->base64,
                            (unsigned char *)ctx->buf, &(ctx->buf_len));
            /* push out the bytes */
            goto again;
        }
        /* Finally flush the underlying BIO */
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;

    case BIO_C_DO_STATE_MACHINE:
        BIO_clear_retry_flags(b);
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        BIO_copy_next_retry(b);
        break;

    case BIO_CTRL_DUP:
        break;
    case BIO_CTRL_INFO:
    case BIO_CTRL_GET:
    case BIO_CTRL_SET:
    default:
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;
    }
    return (ret);
}
예제 #15
0
/* {{{ php_crypto_base64_encode_finish */
static inline void php_crypto_base64_encode_finish(
		EVP_ENCODE_CTX *ctx, char *out, int *outl)
{
	EVP_EncodeFinal(ctx, (unsigned char *) out, outl);
}
예제 #16
0
파일: ca.c 프로젝트: danharkins/est
static void
sign_req (int fd, void *unused)
{
    char thefile[80], cmd_buf[300], p7[3000];
    int i, num;
    unsigned char *data, *asn1;
    int32_t msglen;
    BIO *bio = NULL;
    FILE *fp;
    struct stat blah;
    X509_REQ *req = NULL;
    EVP_ENCODE_CTX ctx;
    
    if (recv(fd, (char *)&msglen, sizeof(int32_t), MSG_WAITALL) < sizeof(int32_t)) {
        return;
    }
    msglen = ntohl(msglen);
    if (msglen > 3000) {
        return;
    }
    if ((data = (unsigned char *)malloc(msglen)) == NULL) {
        return;
    }
    if ((asn1 = (unsigned char *)malloc(msglen)) == NULL) {
        free(data);
        return;
    }
    if (recv(fd, (char *)data, msglen, MSG_WAITALL) < msglen) {
        free(data);
        return;
    }

    EVP_DecodeInit(&ctx);
    EVP_DecodeUpdate(&ctx, asn1, &i, data, msglen);
    num = i;
    EVP_DecodeFinal(&ctx, &(asn1[i]), &i);
    num += i;
    free(data);

    if ((bio = BIO_new_mem_buf(asn1, num)) == NULL) {
        free(asn1);
        goto no_cert;
    }
    if ((req = d2i_X509_REQ_bio(bio, NULL)) == NULL) {
        free(asn1);
        goto no_cert;
    }
    free(asn1);
    BIO_free(bio); bio = NULL;
    
    unique++;
    memset(thefile, 0, sizeof(thefile));
    snprintf(thefile, sizeof(thefile), "%dreq.pem", unique);
    if ((fp = fopen(thefile, "w+")) == NULL) {
        goto no_cert;
    }
    if ((bio = BIO_new(BIO_s_file())) == NULL) {
        fprintf(stderr, "unable to create bio for CSR\n");
        goto no_cert;
    }
    BIO_set_fp(bio, fp, BIO_NOCLOSE);
    PEM_write_bio_X509_REQ(bio, req);
    (void)BIO_flush(bio);
    BIO_free(bio); bio = NULL;
    fclose(fp);

    snprintf(cmd_buf, sizeof(cmd_buf),
             "openssl ca "
             "-policy policy_anything -batch -notext "
             "-config ./conf/openssl.cnf "
             "-out %dcert.pem -in %dreq.pem", unique, unique);
    system(cmd_buf);
    unlink(thefile);

    snprintf(thefile, sizeof(thefile), "%dcert.pem", unique);
    if ((stat(thefile, &blah) < 0) || (blah.st_size < 1)) {
        goto no_cert;
    }

    snprintf(cmd_buf, sizeof(cmd_buf),
             "openssl crl2pkcs7 "
             "-certfile %dcert.pem -outform DER -out %dder.p7 -nocrl", unique, unique);
    system(cmd_buf);
    unlink(thefile); 

    snprintf(thefile, sizeof(thefile), "%dder.p7", unique);
    if (stat(thefile, &blah) < 0) {
        goto no_cert;
    }
    i = blah.st_size;
    printf("DER-encoded P7 is %d bytes\n", i);
    if ((data = (unsigned char *)malloc(blah.st_size*2)) == NULL) {
        goto no_cert;
    }
    
    if ((fp = fopen(thefile, "r")) == NULL) {
        free(data);
        goto no_cert;
    }
    if (fread(p7, 1, sizeof(p7), fp) < blah.st_size) {
        free(data);
        goto no_cert;
    }
    fclose(fp);
    unlink(thefile);
    
    i = 0;
    EVP_EncodeInit(&ctx);
    EVP_EncodeUpdate(&ctx, data, &i, (unsigned char *)p7, blah.st_size);
    num = i;
    EVP_EncodeFinal(&ctx, (unsigned char *)&(data[i]), &i);
    num += i;
    printf("PEM-encoded P7 is %d bytes\n", num);
    msglen = num;
    msglen = htonl(msglen);
    send(fd, (char *)&msglen, sizeof(int32_t), 0);
    send(fd, (char *)data, num, 0);
    free(data);

no_cert:
    BIO_free(bio);
    srv_rem_input(srvctx, fd);
    close(fd);
    
    return;
}
예제 #17
0
// Write information about an untrusted certificate
BOOL ProduceUntrustedCertificateXML(BIO *target, const DEFCERT_UNTRUSTED_cert_st *entry, EVP_PKEY *key, int sig_alg)
{
	unsigned int i, read_len=0;
	int outl;
	if(target == NULL)
		return FALSE;
	if(entry->dercert_data == NULL)
		return TRUE;

	EVP_ENCODE_CTX base_64;
	unsigned char *buffer = new unsigned char[BUFFER_SIZE+1];
	if(buffer == NULL)
		return FALSE;

	BOOL ret = FALSE;
	X509 *cert=NULL;
	char *name=NULL;

	do{
		if(!BIO_puts(target, "-->\n<!-- Copyright (C) 2008-2009 Opera Software ASA. All Rights Reserved. -->\n\n<untrusted-certificate>\n"))
			break;

		const unsigned char *data = entry->dercert_data;
		cert = d2i_X509(NULL, &data, entry->dercert_len);
		if(cert == NULL)
			break;

		// Cert name in one line
		char *name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
		if(!name)
			break;
		if(!WriteXML(target, "subject", name, (char *) buffer, BUFFER_SIZE))
			break;

		OPENSSL_free(name);

		// friendly name
		if(entry->nickname)
			if(!WriteXML(target, "shortname", entry->nickname, (char *) buffer, BUFFER_SIZE))
				break;

		// reason it is untrusted
		if(entry->reason)
			if(!WriteXML(target, "untrusted-reason", entry->reason, (char *) buffer, BUFFER_SIZE))
				break;

		if(!BIO_puts(target, "<certificate-data>\n"))
			break;
		EVP_EncodeInit(&base_64);
		read_len=0;
		// Write Base64 encoded certificate
		for(i = 0; i < entry->dercert_len; i+= read_len)
		{
			read_len = (BUFFER_SIZE/4)*3;
			if(i + read_len > entry->dercert_len)
				read_len = entry->dercert_len -i;

			outl =0;
			EVP_EncodeUpdate(&base_64, buffer, &outl, entry->dercert_data+i, read_len);
			if(outl && !BIO_write(target, buffer, outl))
				break;
		}

		outl =0;
		EVP_EncodeFinal(&base_64, buffer, &outl);
		if(outl && !BIO_write(target, buffer, outl))
			break;

		if(!BIO_puts(target, "\n</certificate-data>\n"))
			break;

		if(!BIO_puts(target, "</untrusted-certificate>\n"))
			break;
		ret = TRUE;
	}
	while(0);

	X509_free(cert);
	OPENSSL_free(name);
	delete [] buffer;

	return ret;
}
예제 #18
0
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) {
  BIO_B64_CTX *ctx;
  long ret = 1;
  int i;

  ctx = (BIO_B64_CTX *)b->ptr;

  switch (cmd) {
    case BIO_CTRL_RESET:
      ctx->cont = 1;
      ctx->start = 1;
      ctx->encode = B64_NONE;
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      break;

    case BIO_CTRL_EOF:  // More to read
      if (ctx->cont <= 0) {
        ret = 1;
      } else {
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      }
      break;

    case BIO_CTRL_WPENDING:  // More to write in buffer
      assert(ctx->buf_len >= ctx->buf_off);
      ret = ctx->buf_len - ctx->buf_off;
      if ((ret == 0) && (ctx->encode != B64_NONE) && (ctx->base64.data_used != 0)) {
        ret = 1;
      } else if (ret <= 0) {
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      }
      break;

    case BIO_CTRL_PENDING:  // More to read in buffer
      assert(ctx->buf_len >= ctx->buf_off);
      ret = ctx->buf_len - ctx->buf_off;
      if (ret <= 0) {
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      }
      break;

    case BIO_CTRL_FLUSH:
    // do a final write
    again:
      while (ctx->buf_len != ctx->buf_off) {
        i = b64_write(b, NULL, 0);
        if (i < 0) {
          return i;
        }
      }
      if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
        if (ctx->tmp_len != 0) {
          ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf,
                                         (uint8_t *)ctx->tmp, ctx->tmp_len);
          ctx->buf_off = 0;
          ctx->tmp_len = 0;
          goto again;
        }
      } else if (ctx->encode != B64_NONE && ctx->base64.data_used != 0) {
        ctx->buf_off = 0;
        EVP_EncodeFinal(&(ctx->base64), (uint8_t *)ctx->buf, &(ctx->buf_len));
        // push out the bytes
        goto again;
      }
      // Finally flush the underlying BIO
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      break;

    case BIO_C_DO_STATE_MACHINE:
      BIO_clear_retry_flags(b);
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      BIO_copy_next_retry(b);
      break;

    case BIO_CTRL_INFO:
    case BIO_CTRL_GET:
    case BIO_CTRL_SET:
    default:
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
      break;
  }
  return ret;
}
예제 #19
0
파일: pem_lib.c 프로젝트: Ana06/openssl
int PEM_write_bio(BIO *bp, const char *name, const char *header,
                  const unsigned char *data, long len)
{
    int nlen, n, i, j, outl;
    unsigned char *buf = NULL;
    EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
    int reason = ERR_R_BUF_LIB;
    int retval = 0;

    if (ctx == NULL) {
        reason = ERR_R_MALLOC_FAILURE;
        goto err;
    }

    EVP_EncodeInit(ctx);
    nlen = strlen(name);

    if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
        (BIO_write(bp, name, nlen) != nlen) ||
        (BIO_write(bp, "-----\n", 6) != 6))
        goto err;

    i = strlen(header);
    if (i > 0) {
        if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1))
            goto err;
    }

    buf = OPENSSL_malloc(PEM_BUFSIZE * 8);
    if (buf == NULL) {
        reason = ERR_R_MALLOC_FAILURE;
        goto err;
    }

    i = j = 0;
    while (len > 0) {
        n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
        if (!EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n))
            goto err;
        if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
            goto err;
        i += outl;
        len -= n;
        j += n;
    }
    EVP_EncodeFinal(ctx, buf, &outl);
    if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
        goto err;
    if ((BIO_write(bp, "-----END ", 9) != 9) ||
        (BIO_write(bp, name, nlen) != nlen) ||
        (BIO_write(bp, "-----\n", 6) != 6))
        goto err;
    retval = i + outl;

 err:
    if (retval == 0)
        PEMerr(PEM_F_PEM_WRITE_BIO, reason);
    EVP_ENCODE_CTX_free(ctx);
    OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
    return retval;
}
예제 #20
0
파일: format.c 프로젝트: argp/ibe-win32
void FMT_encrypt_stream_array(char **id, int idcount,
	FILE *infp, FILE *outfp, params_t params)
{
    byte_string_t U, K;
    byte_string_t *V;
    unsigned char in[crypt_buf_size];
    unsigned char *out;
    int inl, outl;
    crypto_ctx_t ctx;
    EVP_ENCODE_CTX mime;
    unsigned char data[1024];
    int i;
    int count;

    out = (unsigned char *) alloca(crypt_buf_size + crypto_block_size());

    V = (byte_string_t *) alloca(sizeof(byte_string_t) * idcount);

    fprintf(outfp, "\n-----BEGIN IBE-----\n");

    crypto_generate_key(K);

    IBE_hide_key_array(U, V, id, idcount, K, params);
    fprintf(outfp, "\nU:\n");
    mime_put(U, outfp);
    fprintf(outfp, "\n");

    for (i=0; i<idcount; i++) {
	fprintf(outfp, "\nID:\n");
	fprintf(outfp, "%s\n", id[i]);

	fprintf(outfp, "\nV:\n");
	mime_put(V[i], outfp);
	fprintf(outfp, "\n");

	byte_string_clear(V[i]);
    }

    fprintf(outfp, "\nW:\n");
    crypto_ctx_init(ctx);
    crypto_encrypt_init(ctx, K);
    EVP_EncodeInit(&mime);
    for (;;) {
	inl = fread(in, 1, crypt_buf_size, infp);
	if (inl < 0) {
	    fprintf(stderr, "read error\n");
	    exit(1);
	}
	crypto_encrypt_update(ctx, out, &outl, in, inl);
	EVP_EncodeUpdate(&mime, data, &count, out, outl);
	fwrite(data, 1, count, outfp);
	if (feof(infp)) break;
    }
    crypto_encrypt_final(ctx, out, &outl);
    crypto_ctx_clear(ctx);
    EVP_EncodeUpdate(&mime, data, &count, out, outl);
    fwrite(data, 1, count, outfp);
    EVP_EncodeFinal(&mime, data, &count);
    fwrite(data, 1, count, outfp);

    fprintf(outfp, "\n-----END IBE-----\n");

    byte_string_clear(K);
    byte_string_clear(U);
}