Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/crypto/asn1/x_algor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1998-2020 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 <stddef.h>
11
#include <openssl/x509.h>
12
#include <openssl/asn1.h>
13
#include <openssl/asn1t.h>
14
#include "crypto/evp.h"
15
16
ASN1_SEQUENCE(X509_ALGOR) = {
17
        ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
18
        ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
19
} ASN1_SEQUENCE_END(X509_ALGOR)
20
21
ASN1_ITEM_TEMPLATE(X509_ALGORS) =
22
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
23
ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
24
25
IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
26
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
27
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
28
29
int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
30
36
{
31
36
    if (alg == NULL)
32
0
        return 0;
33
34
36
    if (ptype != V_ASN1_UNDEF) {
35
0
        if (alg->parameter == NULL)
36
0
            alg->parameter = ASN1_TYPE_new();
37
0
        if (alg->parameter == NULL)
38
0
            return 0;
39
0
    }
40
41
36
    ASN1_OBJECT_free(alg->algorithm);
42
36
    alg->algorithm = aobj;
43
44
36
    if (ptype == 0)
45
0
        return 1;
46
36
    if (ptype == V_ASN1_UNDEF) {
47
36
        ASN1_TYPE_free(alg->parameter);
48
36
        alg->parameter = NULL;
49
36
    } else
50
0
        ASN1_TYPE_set(alg->parameter, ptype, pval);
51
36
    return 1;
52
36
}
53
54
void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
55
                     const void **ppval, const X509_ALGOR *algor)
56
1.00M
{
57
1.00M
    if (paobj)
58
426k
        *paobj = algor->algorithm;
59
1.00M
    if (pptype) {
60
636k
        if (algor->parameter == NULL) {
61
52.9k
            *pptype = V_ASN1_UNDEF;
62
52.9k
            return;
63
52.9k
        } else
64
583k
            *pptype = algor->parameter->type;
65
583k
        if (ppval)
66
581k
            *ppval = algor->parameter->value.ptr;
67
583k
    }
68
1.00M
}
69
70
/* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
71
72
void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
73
0
{
74
0
    int param_type;
75
76
0
    if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
77
0
        param_type = V_ASN1_UNDEF;
78
0
    else
79
0
        param_type = V_ASN1_NULL;
80
81
0
    X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
82
83
0
}
84
85
int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)
86
708
{
87
708
    int rv;
88
708
    rv = OBJ_cmp(a->algorithm, b->algorithm);
89
708
    if (rv)
90
289
        return rv;
91
419
    if (!a->parameter && !b->parameter)
92
1
        return 0;
93
418
    return ASN1_TYPE_cmp(a->parameter, b->parameter);
94
419
}
95
96
int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src)
97
0
{
98
0
    if (src == NULL || dest == NULL)
99
0
  return 0;
100
101
0
    if (dest->algorithm)
102
0
         ASN1_OBJECT_free(dest->algorithm);
103
0
    dest->algorithm = NULL;
104
105
0
    if (dest->parameter)
106
0
        ASN1_TYPE_free(dest->parameter);
107
0
    dest->parameter = NULL;
108
109
0
    if (src->algorithm)
110
0
        if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL)
111
0
      return 0;
112
113
0
    if (src->parameter) {
114
0
        dest->parameter = ASN1_TYPE_new();
115
0
        if (dest->parameter == NULL)
116
0
            return 0;
117
118
        /* Assuming this is also correct for a BOOL.
119
         * set does copy as a side effect.
120
         */
121
0
        if (ASN1_TYPE_set1(dest->parameter, 
122
0
                src->parameter->type, src->parameter->value.ptr) == 0)
123
0
            return 0;
124
0
    }
125
0
    return 1;
126
0
}