1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/x509.h>
11
12 #include <openssl/asn1.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/digest.h>
15 #include <openssl/obj.h>
16
17 #include "../asn1/internal.h"
18
19
ASN1_SEQUENCE(X509_ALGOR)20 ASN1_SEQUENCE(X509_ALGOR) = {
21 ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
22 ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY),
23 } ASN1_SEQUENCE_END(X509_ALGOR)
24
25 IMPLEMENT_ASN1_FUNCTIONS_const(X509_ALGOR)
26 IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_ALGOR)
27
28 int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval) {
29 if (!alg) {
30 return 0;
31 }
32 if (ptype != V_ASN1_UNDEF) {
33 if (alg->parameter == NULL) {
34 alg->parameter = ASN1_TYPE_new();
35 }
36 if (alg->parameter == NULL) {
37 return 0;
38 }
39 }
40 if (alg) {
41 ASN1_OBJECT_free(alg->algorithm);
42 alg->algorithm = aobj;
43 }
44 if (ptype == 0) {
45 return 1;
46 }
47 if (ptype == V_ASN1_UNDEF) {
48 if (alg->parameter) {
49 ASN1_TYPE_free(alg->parameter);
50 alg->parameter = NULL;
51 }
52 } else {
53 ASN1_TYPE_set(alg->parameter, ptype, pval);
54 }
55 return 1;
56 }
57
X509_ALGOR_get0(const ASN1_OBJECT ** out_obj,int * out_param_type,const void ** out_param_value,const X509_ALGOR * alg)58 void X509_ALGOR_get0(const ASN1_OBJECT **out_obj, int *out_param_type,
59 const void **out_param_value, const X509_ALGOR *alg) {
60 if (out_obj != NULL) {
61 *out_obj = alg->algorithm;
62 }
63 if (out_param_type != NULL) {
64 int type = V_ASN1_UNDEF;
65 const void *value = NULL;
66 if (alg->parameter != NULL) {
67 type = alg->parameter->type;
68 value = asn1_type_value_as_pointer(alg->parameter);
69 }
70 *out_param_type = type;
71 if (out_param_value != NULL) {
72 *out_param_value = value;
73 }
74 }
75 }
76
77 // Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD
78
X509_ALGOR_set_md(X509_ALGOR * alg,const EVP_MD * md)79 int X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md) {
80 int param_type;
81
82 if (EVP_MD_flags(md) & EVP_MD_FLAG_DIGALGID_ABSENT) {
83 param_type = V_ASN1_UNDEF;
84 } else {
85 param_type = V_ASN1_NULL;
86 }
87
88 return X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
89 }
90
91 // X509_ALGOR_cmp returns 0 if |a| and |b| are equal and non-zero otherwise.
X509_ALGOR_cmp(const X509_ALGOR * a,const X509_ALGOR * b)92 int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b) {
93 int rv;
94 rv = OBJ_cmp(a->algorithm, b->algorithm);
95 if (rv) {
96 return rv;
97 }
98 if (!a->parameter && !b->parameter) {
99 return 0;
100 }
101 return ASN1_TYPE_cmp(a->parameter, b->parameter);
102 }
103