]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/x_algor.c
Address some code-analysis issues.
[thirdparty/openssl.git] / crypto / asn1 / x_algor.c
CommitLineData
0f113f3e 1/*
2039c421 2 * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
9d6b1ce6 3 *
2039c421
RS
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
d02b48c6
RE
8 */
9
9d6b1ce6 10#include <stddef.h>
f0e8ae72 11#include <openssl/x509.h>
9d6b1ce6
DSH
12#include <openssl/asn1.h>
13#include <openssl/asn1t.h>
ab0a14bb 14#include "internal/evp_int.h"
d02b48c6 15
9d6b1ce6 16ASN1_SEQUENCE(X509_ALGOR) = {
0f113f3e
MC
17 ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
18 ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
d339187b 19} ASN1_SEQUENCE_END(X509_ALGOR)
d02b48c6 20
0f113f3e
MC
21ASN1_ITEM_TEMPLATE(X509_ALGORS) =
22 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
8931b30d
DSH
23ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
24
9d6b1ce6 25IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
8931b30d 26IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
1241126a 27IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
448be743
DSH
28
29int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
0f113f3e 30{
a0fda2cf 31 if (alg == NULL)
0f113f3e 32 return 0;
a0fda2cf 33
0f113f3e
MC
34 if (ptype != V_ASN1_UNDEF) {
35 if (alg->parameter == NULL)
36 alg->parameter = ASN1_TYPE_new();
37 if (alg->parameter == NULL)
38 return 0;
39 }
a0fda2cf
F
40
41 ASN1_OBJECT_free(alg->algorithm);
42 alg->algorithm = aobj;
43
0f113f3e
MC
44 if (ptype == 0)
45 return 1;
46 if (ptype == V_ASN1_UNDEF) {
2ace7450
RS
47 ASN1_TYPE_free(alg->parameter);
48 alg->parameter = NULL;
0f113f3e
MC
49 } else
50 ASN1_TYPE_set(alg->parameter, ptype, pval);
51 return 1;
52}
448be743 53
ac4e2577
DSH
54void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
55 const void **ppval, const X509_ALGOR *algor)
0f113f3e
MC
56{
57 if (paobj)
58 *paobj = algor->algorithm;
59 if (pptype) {
60 if (algor->parameter == NULL) {
61 *pptype = V_ASN1_UNDEF;
62 return;
63 } else
64 *pptype = algor->parameter->type;
65 if (ppval)
66 *ppval = algor->parameter->value.ptr;
67 }
68}
448be743 69
ce25c720
DSH
70/* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
71
72void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
0f113f3e
MC
73{
74 int param_type;
ce25c720 75
0f113f3e
MC
76 if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
77 param_type = V_ASN1_UNDEF;
78 else
79 param_type = V_ASN1_NULL;
ce25c720 80
0f113f3e 81 X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
ce25c720 82
0f113f3e 83}
4c52816d
DSH
84
85int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)
0f113f3e
MC
86{
87 int rv;
88 rv = OBJ_cmp(a->algorithm, b->algorithm);
89 if (rv)
90 return rv;
91 if (!a->parameter && !b->parameter)
92 return 0;
93 return ASN1_TYPE_cmp(a->parameter, b->parameter);
94}