Exemple #1
0
static int cfb64_test(unsigned char *cfb_cipher)
	{
	des_key_schedule ks;
	int err=0,i,n;

	DES_set_key_checked(&cfb_key,&ks);
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	n=0;
	des_cfb64_encrypt(plain,cfb_buf1,12,ks,&cfb_tmp,&n,DES_ENCRYPT);
	des_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),sizeof(plain)-12,ks,
			  &cfb_tmp,&n,DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt encrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf1[i])));
		}
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	n=0;
	des_cfb64_encrypt(cfb_buf1,cfb_buf2,17,ks,&cfb_tmp,&n,DES_DECRYPT);
	des_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
			  sizeof(plain)-17,ks,&cfb_tmp,&n,DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(plain,cfb_buf2,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt decrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf2[i])));
		}
	return(err);
	}
Exemple #2
0
static int cfb_test(int bits, unsigned char *cfb_cipher)
	{
	des_key_schedule ks;
	int i,err=0;

	DES_set_key_checked(&cfb_key,&ks);
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	des_cfb_encrypt(plain,cfb_buf1,bits,sizeof(plain),ks,&cfb_tmp,
			DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt encrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf1[i])));
		}
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	des_cfb_encrypt(cfb_buf1,cfb_buf2,bits,sizeof(plain),ks,&cfb_tmp,
			DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(plain,cfb_buf2,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt decrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf1[i])));
		}
	return(err);
	}
Exemple #3
0
static int cca_get_random_bytes(unsigned char* buf, int num)
	{
	long ret_code;
	long reason_code;
	long exit_data_length;
	unsigned char exit_data[4];
	unsigned char form[] = "RANDOM  ";
	unsigned char rand_buf[8];

	while(num >= (int)sizeof(rand_buf))
		{
		randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
			exit_data, form, rand_buf);
		if (ret_code)
			return 0;
		num -= sizeof(rand_buf);
		TINYCLR_SSL_MEMCPY(buf, rand_buf, sizeof(rand_buf));
		buf += sizeof(rand_buf);
		}

	if (num)
		{
		randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
			form, rand_buf);
		if (ret_code)
			return 0;
		TINYCLR_SSL_MEMCPY(buf, rand_buf, num);
		}

	return 1;
	}
Exemple #4
0
/*
 * Compute hash value from current state of ctx
 * state of hash ctx becomes invalid and cannot be used for further
 * hashing.
 */ 
int finish_hash(gost_hash_ctx *ctx,byte *hashval)
	{
	byte buf[32];
	byte H[32];
	byte S[32];
	ghosthash_len fin_len=ctx->len;
	byte *bptr;
	TINYCLR_SSL_MEMCPY(H,ctx->H,32);
	TINYCLR_SSL_MEMCPY(S,ctx->S,32);
	if (ctx->left)
		{
		TINYCLR_SSL_MEMSET(buf,0,32);
		TINYCLR_SSL_MEMCPY(buf,ctx->remainder,ctx->left);
		hash_step(ctx->cipher_ctx,H,buf);
		add_blocks(32,S,buf);
		fin_len+=ctx->left;
		}
	TINYCLR_SSL_MEMSET(buf,0,32);
	bptr=buf;
	fin_len<<=3; /* Hash length in BITS!!*/
	while(fin_len>0)
		{
		*(bptr++)=(byte)(fin_len&0xFF);
		fin_len>>=8;
		};
	hash_step(ctx->cipher_ctx,H,buf);
	hash_step(ctx->cipher_ctx,H,S);
	TINYCLR_SSL_MEMCPY(hashval,H,32);
	return 1;
	}
int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len)
	{
	size_t i,j;

	i=c->num;
	if (i != 0)
		{
		if (i+len < MDC2_BLOCK)
			{
			/* partial block */
			TINYCLR_SSL_MEMCPY(&(c->data[i]),in,len);
			c->num+=(int)len;
			return 1;
			}
		else
			{
			/* filled one */
			j=MDC2_BLOCK-i;
			TINYCLR_SSL_MEMCPY(&(c->data[i]),in,j);
			len-=j;
			in+=j;
			c->num=0;
			mdc2_body(c,&(c->data[0]),MDC2_BLOCK);
			}
		}
	i=len&~((size_t)MDC2_BLOCK-1);
	if (i > 0) mdc2_body(c,in,i);
	j=len-i;
	if (j > 0)
		{
		TINYCLR_SSL_MEMCPY(&(c->data[0]),&(in[i]),j);
		c->num=(int)j;
		}
	return 1;
	}
/* Add the server's renegotiation binding */
int ssl_add_serverhello_renegotiate_ext(SSL *s, unsigned char *p, int *len,
					int maxlen)
    {
    if(p)
        {
        if((s->s3->previous_client_finished_len +
            s->s3->previous_server_finished_len + 1) > maxlen)
            {
            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_RENEGOTIATE_EXT,SSL_R_RENEGOTIATE_EXT_TOO_LONG);
            return 0;
            }
        
        /* Length byte */
        *p = s->s3->previous_client_finished_len + s->s3->previous_server_finished_len;
        p++;

        TINYCLR_SSL_MEMCPY(p, s->s3->previous_client_finished,
	       s->s3->previous_client_finished_len);
        p += s->s3->previous_client_finished_len;

        TINYCLR_SSL_MEMCPY(p, s->s3->previous_server_finished,
	       s->s3->previous_server_finished_len);
#ifdef OPENSSL_RI_DEBUG
    TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR, "%s RI extension sent by server\n",
    		s->s3->previous_client_finished_len ? "Non-empty" : "Empty");
#endif
        }
    
    *len=s->s3->previous_client_finished_len
	+ s->s3->previous_server_finished_len + 1;
    
    return 1;
    }
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
	{
	unsigned char *tmp_buf;
	if ((in == NULL) || (in->digest == NULL))
		{
		EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,EVP_R_INPUT_NOT_INITIALIZED);
		return 0;
		}
#ifndef OPENSSL_NO_ENGINE
	/* Make sure it's safe to copy a digest context using an ENGINE */
	if (in->engine && !ENGINE_init(in->engine))
		{
		EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB);
		return 0;
		}
#endif

	if (out->digest == in->digest)
		{
		tmp_buf = (unsigned char*)out->md_data;
	    	EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE);
		}
	else tmp_buf = NULL;
	EVP_MD_CTX_cleanup(out);
	TINYCLR_SSL_MEMCPY(out,in,sizeof *out);

	if (in->md_data && out->digest->ctx_size)
		{
		if (tmp_buf)
			out->md_data = tmp_buf;
		else
			{
			out->md_data=OPENSSL_malloc(out->digest->ctx_size);
			if (!out->md_data)
				{
				EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_MALLOC_FAILURE);
				return 0;
				}
			}
		TINYCLR_SSL_MEMCPY(out->md_data,in->md_data,out->digest->ctx_size);
		}

	out->update = in->update;

	if (in->pctx)
		{
		out->pctx = EVP_PKEY_CTX_dup(in->pctx);
		if (!out->pctx)
			{
			EVP_MD_CTX_cleanup(out);
			return 0;
			}
		}

	if (out->digest->copy)
		return out->digest->copy(out,in);
	
	return 1;
	}
Exemple #8
0
/* was A_A */
static void circle_xor8 (const byte *w, byte *k) 
	{
	byte buf[8];
	int i;
	TINYCLR_SSL_MEMCPY(buf,w,8);
	TINYCLR_SSL_MEMCPY(k,w+8,24);
	for(i=0;i<8;i++) 
		k[i+24]=buf[i]^k[i];
	}
ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
	{
	ASN1_OBJECT *r;
	int i;
	char *ln=NULL,*sn=NULL;
	unsigned char *data=NULL;

	if (o == NULL) return(NULL);
	if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))
		return((ASN1_OBJECT *)o); /* XXX: ugh! Why? What kind of
					     duplication is this??? */

	r=ASN1_OBJECT_new();
	if (r == NULL)
		{
		OBJerr(OBJ_F_OBJ_DUP,ERR_R_ASN1_LIB);
		return(NULL);
		}
	data=(unsigned char*)OPENSSL_malloc(o->length);
	if (data == NULL)
		goto err;
	if (o->data != NULL)
		TINYCLR_SSL_MEMCPY(data,o->data,o->length);
	/* once data attached to object it remains const */
	r->data = data;
	r->length=o->length;
	r->nid=o->nid;
	r->ln=r->sn=NULL;
	if (o->ln != NULL)
		{
		i=TINYCLR_SSL_STRLEN(o->ln)+1;
		ln=(char*)OPENSSL_malloc(i);
		if (ln == NULL) goto err;
		TINYCLR_SSL_MEMCPY(ln,o->ln,i);
		r->ln=ln;
		}

	if (o->sn != NULL)
		{
		i=TINYCLR_SSL_STRLEN(o->sn)+1;
		sn=(char*)OPENSSL_malloc(i);
		if (sn == NULL) goto err;
		TINYCLR_SSL_MEMCPY(sn,o->sn,i);
		r->sn=sn;
		}
	r->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC|
		ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA);
	return(r);
err:
	OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);
	if (ln != NULL)		OPENSSL_free(ln);
	if (sn != NULL)		OPENSSL_free(sn);
	if (data != NULL)	OPENSSL_free(data);
	if (r != NULL)		OPENSSL_free(r);
	return(NULL);
	}
Exemple #10
0
static int getModulusAndExponent(const unsigned char*token, long *exponentLength,
		unsigned char *exponent, long *modulusLength, long *modulusFieldLength,
		unsigned char *modulus)
	{
	unsigned long len;

	if (*token++ != (char)0x1E) /* internal PKA token? */
		return 0;

	if (*token++) /* token version must be zero */
		return 0;

	len = *token++;
	len = len << 8;
	len |= (unsigned char)*token++;

	token += 4; /* skip reserved bytes */

	if (*token++ == (char)0x04)
		{
		if (*token++) /* token version must be zero */
			return 0;

		len = *token++;
		len = len << 8;
		len |= (unsigned char)*token++;

		token+=2; /* skip reserved section */

		len = *token++;
		len = len << 8;
		len |= (unsigned char)*token++;

		*exponentLength = len;

		len = *token++;
		len = len << 8;
		len |= (unsigned char)*token++;

		*modulusLength = len;

		len = *token++;
		len = len << 8;
		len |= (unsigned char)*token++;

		*modulusFieldLength = len;

		TINYCLR_SSL_MEMCPY(exponent, token, *exponentLength);
		token+= *exponentLength;

		TINYCLR_SSL_MEMCPY(modulus, token, *modulusFieldLength);
		return 1;
		}
	return 0;
	}
static int do_digest(int ses,unsigned char *md,const void *data,int len)
    {
    struct crypt_op cryp;
    static unsigned char md5zero[16]=
	{
	0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
	0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
	};

    /* some cards can't do zero length */
    if(!len)
	{
	TINYCLR_SSL_MEMCPY(md,md5zero,16);
	return 1;
	}

    TINYCLR_SSL_MEMSET(&cryp,'\0',sizeof cryp);
    cryp.ses=ses;
    cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
    cryp.len=len;
    cryp.src=(caddr_t)data;
    cryp.dst=(caddr_t)data; // FIXME!!!
    cryp.mac=(caddr_t)md;

    if(TINYCLR_SSL_IOCTL(fd, CIOCCRYPT, &cryp) == -1)
	{
	if(errno == EINVAL) /* buffer is misaligned */
	    {
	    char *dcopy;

	    dcopy=OPENSSL_malloc(len);
	    TINYCLR_SSL_MEMCPY(dcopy,data,len);
	    cryp.src=dcopy;
	    cryp.dst=cryp.src; // FIXME!!!

	    if(TINYCLR_SSL_IOCTL(fd, CIOCCRYPT, &cryp) == -1)
		{
		err("CIOCCRYPT(MAC2) failed");
		TINYCLR_SSL_ABORT();
		return 0;
		}
	    OPENSSL_free(dcopy);
	    }
	else
	    {
	    err("CIOCCRYPT(MAC) failed");
	    TINYCLR_SSL_ABORT();
	    return 0;
	    }
	}
    //    TINYCLR_SSL_PRINTF("done\n");

    return 1;
    }
int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen)
	{
	unsigned char *p,*d;
	int i;
	unsigned long l;

	if (s->state == a)
		{
		d=(unsigned char *)s->init_buf->data;
		p= &(d[4]);

		i=s->method->ssl3_enc->final_finish_mac(s,
			sender,slen,s->s3->tmp.finish_md);
		s->s3->tmp.finish_md_len = i;
		TINYCLR_SSL_MEMCPY(p, s->s3->tmp.finish_md, i);
		p+=i;
		l=i;

                /* Copy the finished so we can use it for
                   renegotiation checks */
                if(s->type == SSL_ST_CONNECT)
                        {
                         TINYCLR_SSL_ASSERT(i <= EVP_MAX_MD_SIZE);
                         TINYCLR_SSL_MEMCPY(s->s3->previous_client_finished, 
                             s->s3->tmp.finish_md, i);
                         s->s3->previous_client_finished_len=i;
                        }
                else
                        {
                        TINYCLR_SSL_ASSERT(i <= EVP_MAX_MD_SIZE);
                        TINYCLR_SSL_MEMCPY(s->s3->previous_server_finished, 
                            s->s3->tmp.finish_md, i);
                        s->s3->previous_server_finished_len=i;
                        }

#ifdef OPENSSL_SYS_WIN16
		/* MSVC 1.5 does not clear the top bytes of the word unless
		 * I do this.
		 */
		l&=0xffff;
#endif

		*(d++)=SSL3_MT_FINISHED;
		l2n3(l,d);
		s->init_num=(int)l+4;
		s->init_off=0;

		s->state=b;
		}

	/* SSL3_ST_SEND_xxxxxx_HELLO_B */
	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
	}
int handleAppData(size_t size, int len, char* data, unsigned char* sslData,
		SSL_Conext* sslContext) {
	int rc;
	int cpylen = min(size, len);
	TINYCLR_SSL_MEMCPY(data, sslData, cpylen);
	if (cpylen < len) {
		sslContext->SslBuffer.ReadBuffer = &sslData[cpylen];
		sslContext->SslBuffer.Count = len - cpylen;
		sslContext->SslBuffer.BaseBuffer = sslData;
		sslContext->SslBuffer.Length = len;
		MATRIXSSL_PDEBUG_ALL("Bytes rec: %i, returned %i\n", len, cpylen);
	} else {
		rc = matrixSslProcessedData(sslContext->SslContext, &sslData,
				(uint32*) &len);
		PRINT_RETURN_VALUE(rc);
		if (rc == MATRIXSSL_APP_DATA) {
			sslContext->SslBuffer.Init(sslData, len);
			MATRIXSSL_PDEBUG_ALL("oldData count: %i,\n", len);
		} else if (rc != MATRIXSSL_REQUEST_RECV && rc != MATRIXSSL_SUCCESS) {
			PRINT_UNEXPECTED_RETURN_VALUE(rc);
			return SOCK_SOCKET_ERROR;
		}
	}
	return cpylen;
}
static int dlfcn_pathbyaddr(void *addr,char *path,int sz)
	{
#ifdef HAVE_DLINFO
	Dl_info dli;
	int len;

	if (addr == NULL)
		{
		union	{ int(*f)(void*,char*,int); void *p; } t =
			{ dlfcn_pathbyaddr };
		addr = t.p;
		}

	if (dladdr(addr,&dli))
		{
		len = (int)TINYCLR_SSL_STRLEN(dli.dli_fname);
		if (sz <= 0) return len+1;
		if (len >= sz) len=sz-1;
		TINYCLR_SSL_MEMCPY(path,dli.dli_fname,len);
		path[len++]=0;
		return len;
		}

	ERR_add_error_data(4, "dlfcn_pathbyaddr(): ", dlerror());
#endif
	return -1;
	}
Exemple #15
0
static void sig_out(BIO* b)
{
    BIO_OK_CTX *ctx;
    EVP_MD_CTX *md;

    ctx=(BIO_OK_CTX *)b->ptr;
    md=&ctx->md;

    if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;

    EVP_DigestInit_ex(md, md->digest, NULL);
    /* FIXME: there's absolutely no guarantee this makes any sense at all,
     * particularly now EVP_MD_CTX has been restructured.
     */
    RAND_pseudo_bytes((unsigned char*)md->md_data, md->digest->md_size);
    TINYCLR_SSL_MEMCPY(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size);
    longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
    ctx->buf_len+= md->digest->md_size;

    EVP_DigestUpdate(md, WELLKNOWN, TINYCLR_SSL_STRLEN(WELLKNOWN));
    EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
    ctx->buf_len+= md->digest->md_size;
    ctx->blockout= 1;
    ctx->sigio= 0;
}
main()
	{
	LHASH *conf;
	char buf[256];
	int i;

	conf=lh_new(lh_strhash,TINYCLR_SSL_STRCMP);
	for (;;)
		{
		char *p;

		buf[0]='\0';
		TINYCLR_SSL_FGETS(buf,256,OPENSSL_TYPE__FILE_STDIN);
		if (buf[0] == '\0') break;
		i=TINYCLR_SSL_STRLEN(buf);
		p=OPENSSL_malloc(i+1);
		TINYCLR_SSL_MEMCPY(p,buf,i+1);
		lh_insert(conf,p);
		}

	lh_node_stats(conf,OPENSSL_TYPE__FILE_STDOUT);
	lh_stats(conf,OPENSSL_TYPE__FILE_STDOUT);
	lh_node_usage_stats(conf,OPENSSL_TYPE__FILE_STDOUT);
	TINYCLR_SSL_EXIT(0);
	}
static int null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
	     const unsigned char *in, size_t inl)
	{
	if (in != out)
		TINYCLR_SSL_MEMCPY((char *)out,(const char *)in,inl);
	return 1;
	}
static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
	{
	struct gost_mac_pmeth_data *data =
(struct gost_mac_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);

	switch (type)
		{
		case EVP_PKEY_CTRL_MD:
		{
		if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC)
			{
			GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_DIGEST_TYPE);
			return 0;
			}
		data->md = (EVP_MD *)p2;
		return 1;
		}
		break;

		case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
		case EVP_PKEY_CTRL_PKCS7_DECRYPT:
		case EVP_PKEY_CTRL_PKCS7_SIGN:
			return 1;
		case EVP_PKEY_CTRL_SET_MAC_KEY:
			if (p1 != 32) 
				{
				GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
					GOST_R_INVALID_MAC_KEY_LENGTH);
				return 0;
				}

			TINYCLR_SSL_MEMCPY(data->key,p2,32);
			data->key_set = 1;
			return 1;
		case EVP_PKEY_CTRL_DIGESTINIT:
			{ 
			EVP_MD_CTX *mctx = (EVP_MD_CTX*)p2;
			void *key;
			if (!data->key_set)
				{ 
				EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
				if (!pkey) 
					{
					GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
					return 0;
					}
				key = EVP_PKEY_get0(pkey);
				if (!key) 
					{
					GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
					return 0;
					}
				} else {
				key = &(data->key);
				}
			return mctx->digest->md_ctrl(mctx,EVP_MD_CTRL_SET_KEY,32,key);
			}  
		}	
	return -2;
	}
int get_signed_string(PKCS7_SIGNER_INFO *si, char *buf, int len)
	{
	ASN1_TYPE *so;
	ASN1_OCTET_STRING *os;
	int i;

	if (signed_string_nid == -1)
		signed_string_nid=
			OBJ_create("1.2.3.4.5","OID_example","Our example OID");
	/* To retrieve */
	so=PKCS7_get_signed_attribute(si,signed_string_nid);
	if (so != NULL)
		{
		if (so->type == V_ASN1_OCTET_STRING)
			{
			os=so->value.octet_string;
			i=os->length;
			if ((i+1) > len)
				i=len-1;
			TINYCLR_SSL_MEMCPY(buf,os->data,i);
			return(i);
			}
		}
	return(0);
	}
Exemple #20
0
static void md2_block(MD2_CTX *c, const unsigned char *d)
{
    register MD2_INT t,*sp1,*sp2;
    register int i,j;
    MD2_INT state[48];

    sp1=c->state;
    sp2=c->cksm;
    j=sp2[MD2_BLOCK-1];
    for (i=0; i<16; i++)
    {
        state[i]=sp1[i];
        state[i+16]=t=d[i];
        state[i+32]=(t^sp1[i]);
        j=sp2[i]^=S[t^j];
    }
    t=0;
    for (i=0; i<18; i++)
    {
        for (j=0; j<48; j+=8)
        {
            t= state[j+ 0]^=S[t];
            t= state[j+ 1]^=S[t];
            t= state[j+ 2]^=S[t];
            t= state[j+ 3]^=S[t];
            t= state[j+ 4]^=S[t];
            t= state[j+ 5]^=S[t];
            t= state[j+ 6]^=S[t];
            t= state[j+ 7]^=S[t];
        }
        t=(t+i)&0xff;
    }
    TINYCLR_SSL_MEMCPY(sp1,state,16*sizeof(MD2_INT));
    OPENSSL_cleanse(state,48*sizeof(MD2_INT));
}
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
	     long len)
	{
	ASN1_OBJECT *ret=NULL;
	const unsigned char *p;
	unsigned char *data;
	int i;
	/* Sanity check OID encoding: can't have leading 0x80 in
	 * subidentifiers, see: X.690 8.19.2
	 */
	for (i = 0, p = *pp + 1; i < len - 1; i++, p++)
		{
		if (*p == 0x80 && (!i || !(p[-1] & 0x80)))
			{
			ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENCODING);
			return NULL;
			}
		}

	/* only the ASN1_OBJECTs from the 'table' will have values
	 * for ->sn or ->ln */
	if ((a == NULL) || ((*a) == NULL) ||
		!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
		{
		if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
		}
	else	ret=(*a);

	p= *pp;
	/* detach data from object */
	data = (unsigned char *)ret->data;
	ret->data = NULL;
	/* once detached we can change it */
	if ((data == NULL) || (ret->length < len))
		{
		ret->length=0;
		if (data != NULL) OPENSSL_free(data);
		data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1);
		if (data == NULL)
			{ i=ERR_R_MALLOC_FAILURE; goto err; }
		ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
		}
	TINYCLR_SSL_MEMCPY(data,p,(int)len);
	/* reattach data to object, after which it remains const */
	ret->data  =data;
	ret->length=(int)len;
	ret->sn=NULL;
	ret->ln=NULL;
	/* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
	p+=len;

	if (a != NULL) (*a)=ret;
	*pp=p;
	return(ret);
err:
	ASN1err(ASN1_F_C2I_ASN1_OBJECT,i);
	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
		ASN1_OBJECT_free(ret);
	return(NULL);
	}
static int gmp2bn(mpz_t g, BIGNUM *bn)
	{
	if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
			(BN_BITS2 == GMP_NUMB_BITS))
		{
		/* The common case */
		int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
		BN_zero(bn);
		if(bn_expand2 (bn, s) == NULL)
			return 0;
		bn->top = s;
		TINYCLR_SSL_MEMCPY(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
		bn_correct_top(bn);
		bn->neg = g->_mp_size >= 0 ? 0 : 1;
		return 1;
		}
	else
		{
		int toret;
		char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
		if(!tmpchar) return 0;
		mpz_get_str(tmpchar, 16, g);
		toret = BN_hex2bn(&bn, tmpchar);
		OPENSSL_free(tmpchar);
		return toret;
		}
	}
static ASN1_INTEGER *create_nonce(int bits)
	{
	unsigned char buf[20];
	ASN1_INTEGER *nonce = NULL;
	int len = (bits - 1) / 8 + 1;
	int i;

	/* Generating random byte sequence. */
	if (len > (int)sizeof(buf)) goto err;
	if (RAND_bytes(buf, len) <= 0) goto err;

	/* Find the first non-zero byte and creating ASN1_INTEGER object. */
	for (i = 0; i < len && !buf[i]; ++i);
	if (!(nonce = ASN1_INTEGER_new())) goto err;
	OPENSSL_free(nonce->data);
	/* Allocate at least one byte. */
	nonce->length = len - i;
	if (!(nonce->data = (unsigned char*)OPENSSL_malloc(nonce->length + 1))) goto err;
	TINYCLR_SSL_MEMCPY(nonce->data, buf + i, nonce->length);

	return nonce;
 err:
	BIO_printf(bio_err, "could not create nonce\n");
	ASN1_INTEGER_free(nonce);
	return NULL;
	}
static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
		    int utype, char *free_cont, const ASN1_ITEM *it)
{
	int neg, i;
	long ltmp;
	unsigned long utmp = 0;
	char *cp = (char *)pval;
	if(len > (int)sizeof(long)) {
		ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
		return 0;
	}
	/* Is it negative? */
	if(len && (cont[0] & 0x80)) neg = 1;
	else neg = 0;
	utmp = 0;
	for(i = 0; i < len; i++) {
		utmp <<= 8;
		if(neg) utmp |= cont[i] ^ 0xff;
		else utmp |= cont[i];
	}
	ltmp = (long)utmp;
	if(neg) {
		ltmp++;
		ltmp = -ltmp;
	}
	if(ltmp == it->size) {
		ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
		return 0;
	}
	TINYCLR_SSL_MEMCPY(cp, &ltmp, sizeof(long));
	return 1;
}
/* Most often limb sizes will be the same. If not, we use hex conversion
 * which is neat, but extremely inefficient. */
static int bn2gmp(const BIGNUM *bn, mpz_t g)
	{
	bn_check_top(bn);
	if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
			(BN_BITS2 == GMP_NUMB_BITS)) 
		{
		/* The common case */
		if(!_mpz_realloc (g, bn->top))
			return 0;
		TINYCLR_SSL_MEMCPY(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
		g->_mp_size = bn->top;
		if(bn->neg)
			g->_mp_size = -g->_mp_size;
		return 1;
		}
	else
		{
		int toret;
		char *tmpchar = BN_bn2hex(bn);
		if(!tmpchar) return 0;
		toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
		OPENSSL_free(tmpchar);
		return toret;
		}
	}
static int dl_pathbyaddr(void *addr,char *path,int sz)
	{
	struct shl_descriptor inf;
	int i,len;

	if (addr == NULL)
		{
		union	{ int(*f)(void*,char*,int); void *p; } t =
			{ dl_pathbyaddr };
		addr = t.p;
		}

	for (i=-1;shl_get_r(i,&inf)==0;i++)
		{
		if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) ||
		    ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend))
			{
			len = (int)TINYCLR_SSL_STRLEN(inf.filename);
			if (sz <= 0) return len+1;
			if (len >= sz) len=sz-1;
			TINYCLR_SSL_MEMCPY(path,inf.filename,len);
			path[len++] = 0;
			return len;
			}
		}

	return -1;
	}
Exemple #27
0
static int ok_write(BIO *b, const char *in, int inl)
{
    int ret=0,n,i;
    BIO_OK_CTX *ctx;

    if (inl <= 0) return inl;

    ctx=(BIO_OK_CTX *)b->ptr;
    ret=inl;

    if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);

    if(ctx->sigio) sig_out(b);

    do {
        BIO_clear_retry_flags(b);
        n=ctx->buf_len-ctx->buf_off;
        while (ctx->blockout && n > 0)
        {
            i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
            if (i <= 0)
            {
                BIO_copy_next_retry(b);
                if(!BIO_should_retry(b))
                    ctx->cont= 0;
                return(i);
            }
            ctx->buf_off+=i;
            n-=i;
        }

        /* at this point all pending data has been written */
        ctx->blockout= 0;
        if (ctx->buf_len == ctx->buf_off)
        {
            ctx->buf_len=OK_BLOCK_BLOCK;
            ctx->buf_off=0;
        }

        if ((in == NULL) || (inl <= 0)) return(0);

        n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
           (int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl;

        TINYCLR_SSL_MEMCPY((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
        ctx->buf_len+= n;
        inl-=n;
        in+=n;

        if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
        {
            block_out(b);
        }
    } while(inl > 0);

    BIO_clear_retry_flags(b);
    BIO_copy_next_retry(b);
    return(ret);
}
int tinyclr_fputc ( int character, OPENSSL_TYPE__FILE * stream )
{

	TINYCLR_SSL_MEMCPY((void*)&stream->buffer[stream->read], (void*)&character, 1);
	stream->read++;
	
	return character;
}
/* For this case, I will malloc the return strings */
int get_signed_seq2string(PKCS7_SIGNER_INFO *si, char **str1, char **str2)
	{
	ASN1_TYPE *so;

	if (signed_seq2string_nid == -1)
		signed_seq2string_nid=
			OBJ_create("1.9.9999","OID_example","Our example OID");
	/* To retrieve */
	so=PKCS7_get_signed_attribute(si,signed_seq2string_nid);
	if (so && (so->type == V_ASN1_SEQUENCE))
		{
		ASN1_const_CTX c;
		ASN1_STRING *s;
		long length;
		ASN1_OCTET_STRING *os1,*os2;

		s=so->value.sequence;
		c.p=ASN1_STRING_data(s);
		c.max=c.p+ASN1_STRING_length(s);
		if (!asn1_GetSequence(&c,&length)) goto err;
		/* Length is the length of the seqence */

		c.q=c.p;
		if ((os1=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL) 
			goto err;
		c.slen-=(c.p-c.q);

		c.q=c.p;
		if ((os2=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL) 
			goto err;
		c.slen-=(c.p-c.q);

		if (!asn1_const_Finish(&c)) goto err;
		*str1=OPENSSL_malloc(os1->length+1);
		*str2=OPENSSL_malloc(os2->length+1);
		TINYCLR_SSL_MEMCPY(*str1,os1->data,os1->length);
		TINYCLR_SSL_MEMCPY(*str2,os2->data,os2->length);
		(*str1)[os1->length]='\0';
		(*str2)[os2->length]='\0';
		ASN1_OCTET_STRING_free(os1);
		ASN1_OCTET_STRING_free(os2);
		return(1);
		}
err:
	return(0);
	}
int MDC2_Final(unsigned char *md, MDC2_CTX *c)
	{
	unsigned int i;
	int j;

	i=c->num;
	j=c->pad_type;
	if ((i > 0) || (j == 2))
		{
		if (j == 2)
			c->data[i++]=0x80;
		TINYCLR_SSL_MEMSET(&(c->data[i]),0,MDC2_BLOCK-i);
		mdc2_body(c,c->data,MDC2_BLOCK);
		}
	TINYCLR_SSL_MEMCPY(md,(char *)c->h,MDC2_BLOCK);
	TINYCLR_SSL_MEMCPY(&(md[MDC2_BLOCK]),(char *)c->hh,MDC2_BLOCK);
	return 1;
	}