1d7d5f0223bd30dfd618762349c6209dd1d5ea3e6Daniel Dunbar/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor * project 2000. */
3b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/* ====================================================================
4b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
54c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *
64c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * Redistribution and use in source and binary forms, with or without
74c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * modification, are permitted provided that the following conditions
83cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl * are met:
93cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl *
104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * 1. Redistributions of source code must retain the above copyright
114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor *    notice, this list of conditions and the following disclaimer.
12636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl *
13636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl * 2. Redistributions in binary form must reproduce the above copyright
14636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl *    notice, this list of conditions and the following disclaimer in
159afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl *    the documentation and/or other materials provided with the
16636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl *    distribution.
177f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl *
187f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl * 3. All advertising materials mentioning features or use of this
197f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl *    software must display the following acknowledgment:
204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *    "This product includes software developed by the OpenSSL Project
213cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
223cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl *
233cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24fc27d268cb34cbb8d186c6ad7cc043d41581ce71Anders Carlsson *    endorse or promote products derived from this software without
25b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl *    prior written permission. For written permission, please contact
264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *    licensing@OpenSSL.org.
274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *
284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * 5. Products derived from this software may not be called "OpenSSL"
294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *    nor may "OpenSSL" appear in their names without prior written
304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *    permission of the OpenSSL Project.
314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *
324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * 6. Redistributions of any form whatsoever must retain the following
334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *    acknowledgment:
34cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl *    "This product includes software developed by the OpenSSL Project
354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl *
374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
415d64e5b6bc03462e8b7d0cd804565829afb1b508Douglas Gregor * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42fc27d268cb34cbb8d186c6ad7cc043d41581ce71Anders Carlsson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43fc27d268cb34cbb8d186c6ad7cc043d41581ce71Anders Carlsson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44ac18b2e3af8d0c5304f74e362a1d4207363e4c94Anders Carlsson * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45ac18b2e3af8d0c5304f74e362a1d4207363e4c94Anders Carlsson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor * OF THE POSSIBILITY OF SUCH DAMAGE.
49e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor * ====================================================================
50e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor *
51e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor * This product includes cryptographic software written by Eric Young
52c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor * (eay@cryptsoft.com).  This product includes software written by Tim
534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl * Hudson (tjh@cryptsoft.com). */
544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl#include <openssl/x509.h>
564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl#include <openssl/asn1.h>
58cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl#include <openssl/asn1t.h>
594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl#include <openssl/digest.h>
604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl#include <openssl/obj.h>
618ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl
624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian RedlASN1_SEQUENCE(X509_ALGOR) = {
644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl	ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
65c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor	ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} ASN1_SEQUENCE_END(X509_ALGOR)
67c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor
68cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlASN1_ITEM_TEMPLATE(X509_ALGORS) =
69cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl	ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
70cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlASN1_ITEM_TEMPLATE_END(X509_ALGORS)
71fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl
72b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian RedlIMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
737f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian RedlIMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
74636a7c42d42800f69caadcdea433312fd642a4b3Sebastian RedlIMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
75636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl
769afe1308ed19dffc281dca5cfbe521826754980fSebastian RedlIMPLEMENT_ASN1_SET_OF(X509_ALGOR)
779afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl
7800e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redlint X509_ALGOR_set0(X509_ALGOR *alg, const ASN1_OBJECT *aobj, int ptype, void *pval)
7900e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl	{
804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl	if (!alg)
814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		return 0;
824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl	if (ptype != V_ASN1_UNDEF)
834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		{
844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		if (alg->parameter == NULL)
854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl			alg->parameter = ASN1_TYPE_new();
864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		if (alg->parameter == NULL)
874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl			return 0;
88fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl		}
894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl	if (alg)
904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		{
914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		if (alg->algorithm)
924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl			ASN1_OBJECT_free(alg->algorithm);
934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		alg->algorithm = (ASN1_OBJECT*) aobj;
944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		}
9528eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner	if (ptype == 0)
964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		return 1;
974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl	if (ptype == V_ASN1_UNDEF)
98fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl		{
994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl		if (alg->parameter)
1009cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor			{
1019cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor			ASN1_TYPE_free(alg->parameter);
1029cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor			alg->parameter = NULL;
1039cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor			}
1049cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor		}
1059cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor	else
1069cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor		ASN1_TYPE_set(alg->parameter, ptype, pval);
1079cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor	return 1;
1089cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor	}
109f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian
110f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanianvoid X509_ALGOR_get0(ASN1_OBJECT **paobj, int *pptype, void **ppval,
1119cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor						X509_ALGOR *algor)
1129cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor	{
1139cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor	if (paobj)
1149cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor		*paobj = algor->algorithm;
1159cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor	if (pptype)
116f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian		{
117f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian		if (algor->parameter == NULL)
1189091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor			{
1199091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor			*pptype = V_ASN1_UNDEF;
1209091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor			return;
1219091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor			}
1229091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor		else
1239091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor			*pptype = algor->parameter->type;
1249091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor		if (ppval)
1259091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor			*ppval = algor->parameter->value.ptr;
1269091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor		}
1279091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor	}
1289091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1299091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor/* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
1309091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1319091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregorvoid X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
1329091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor	{
1339091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor	int param_type;
1349091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1351070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor	if (EVP_MD_flags(md) & EVP_MD_FLAG_DIGALGID_ABSENT)
1365d64e5b6bc03462e8b7d0cd804565829afb1b508Douglas Gregor		param_type = V_ASN1_UNDEF;
1371070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor	else
1381070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor		param_type = V_ASN1_NULL;
1391070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor
140b03bfa55d03ca38922ffedac19225d0832e8d911Fariborz Jahanian	X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
141b03bfa55d03ca38922ffedac19225d0832e8d911Fariborz Jahanian
142b03bfa55d03ca38922ffedac19225d0832e8d911Fariborz Jahanian	}
1431f2fcee89677d597f2a10b54d6cefac0e79db504Fariborz Jahanian