a_digest.c revision 2139:6243c3338933
11573Srgrimes/* crypto/asn1/a_digest.c */
21573Srgrimes/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
31573Srgrimes * All rights reserved.
41573Srgrimes *
51573Srgrimes * This package is an SSL implementation written
61573Srgrimes * by Eric Young (eay@cryptsoft.com).
71573Srgrimes * The implementation was written so as to conform with Netscapes SSL.
81573Srgrimes *
91573Srgrimes * This library is free for commercial and non-commercial use as long as
101573Srgrimes * the following conditions are aheared to.  The following conditions
111573Srgrimes * apply to all code found in this distribution, be it the RC4, RSA,
121573Srgrimes * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
131573Srgrimes * included with this distribution is covered by the same copyright terms
141573Srgrimes * except that the holder is Tim Hudson (tjh@cryptsoft.com).
151573Srgrimes *
161573Srgrimes * Copyright remains Eric Young's, and as such any Copyright notices in
171573Srgrimes * the code are not to be removed.
181573Srgrimes * If this package is used in a product, Eric Young should be given attribution
191573Srgrimes * as the author of the parts of the library used.
201573Srgrimes * This can be in the form of a textual message at program startup or
211573Srgrimes * in documentation (online or textual) provided with the package.
221573Srgrimes *
231573Srgrimes * Redistribution and use in source and binary forms, with or without
241573Srgrimes * modification, are permitted provided that the following conditions
251573Srgrimes * are met:
261573Srgrimes * 1. Redistributions of source code must retain the copyright
271573Srgrimes *    notice, this list of conditions and the following disclaimer.
281573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
291573Srgrimes *    notice, this list of conditions and the following disclaimer in the
301573Srgrimes *    documentation and/or other materials provided with the distribution.
311573Srgrimes * 3. All advertising materials mentioning features or use of this software
321573Srgrimes *    must display the following acknowledgement:
331573Srgrimes *    "This product includes cryptographic software written by
341573Srgrimes *     Eric Young (eay@cryptsoft.com)"
351573Srgrimes *    The word 'cryptographic' can be left out if the rouines from the library
361573Srgrimes *    being used are not cryptographic related :-).
371573Srgrimes * 4. If you include any Windows specific code (or a derivative thereof) from
381573Srgrimes *    the apps directory (application code) you must include an acknowledgement:
391573Srgrimes *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
401573Srgrimes *
411573Srgrimes * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
421573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
431573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
441573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
451573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4611659Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
471573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
481573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
491573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
501573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
511573Srgrimes * SUCH DAMAGE.
521573Srgrimes *
531573Srgrimes * The licence and distribution terms for any publically available version or
541573Srgrimes * derivative of this code cannot be changed.  i.e. this code cannot simply be
551573Srgrimes * copied and put under another distribution licence
561573Srgrimes * [including the GNU Public Licence.]
571573Srgrimes */
581573Srgrimes
591573Srgrimes#include <stdio.h>
601573Srgrimes#include <time.h>
611573Srgrimes
621573Srgrimes#include "cryptlib.h"
631573Srgrimes
641573Srgrimes#ifndef NO_SYS_TYPES_H
651573Srgrimes# include <sys/types.h>
661573Srgrimes#endif
671573Srgrimes
681573Srgrimes#include <openssl/err.h>
691573Srgrimes#include <openssl/evp.h>
701573Srgrimes#include <openssl/buffer.h>
711573Srgrimes#include <openssl/x509.h>
721573Srgrimes
731573Srgrimes#ifndef NO_ASN1_OLD
741573Srgrimes
751573Srgrimesint ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
761573Srgrimes		unsigned char *md, unsigned int *len)
771573Srgrimes	{
781573Srgrimes	int i;
791573Srgrimes	unsigned char *str,*p;
801573Srgrimes
811573Srgrimes	i=i2d(data,NULL);
821573Srgrimes	if ((str=(unsigned char *)OPENSSL_malloc(i)) == NULL)
831573Srgrimes		{
841573Srgrimes		ASN1err(ASN1_F_ASN1_DIGEST,ERR_R_MALLOC_FAILURE);
851573Srgrimes		return(0);
861573Srgrimes		}
871573Srgrimes	p=str;
881573Srgrimes	i2d(data,&p);
891573Srgrimes
901573Srgrimes	EVP_Digest(str, i, md, len, type, NULL);
911573Srgrimes	OPENSSL_free(str);
921573Srgrimes	return(1);
931573Srgrimes	}
941573Srgrimes
951573Srgrimes#endif
961573Srgrimes
971573Srgrimes
981573Srgrimesint ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *asn,
991573Srgrimes		unsigned char *md, unsigned int *len)
1001573Srgrimes	{
1011573Srgrimes	int i;
1021573Srgrimes	unsigned char *str = NULL;
1031573Srgrimes
1041573Srgrimes	i=ASN1_item_i2d(asn,&str, it);
1051573Srgrimes	if (!str) return(0);
1061573Srgrimes
1071573Srgrimes	EVP_Digest(str, i, md, len, type, NULL);
1081573Srgrimes	OPENSSL_free(str);
1091573Srgrimes	return(1);
1101573Srgrimes	}
1111573Srgrimes
1121573Srgrimes