/src/openssl/crypto/asn1/x_algor.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1998-2022 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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 <openssl/err.h>  | 
15  |  | #include "crypto/asn1.h"  | 
16  |  | #include "crypto/evp.h"  | 
17  |  |  | 
18  |  | ASN1_SEQUENCE(X509_ALGOR) = { | 
19  |  |         ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),  | 
20  |  |         ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)  | 
21  |  | } ASN1_SEQUENCE_END(X509_ALGOR)  | 
22  |  |  | 
23  |  | ASN1_ITEM_TEMPLATE(X509_ALGORS) =  | 
24  |  |         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)  | 
25  |  | ASN1_ITEM_TEMPLATE_END(X509_ALGORS)  | 
26  |  |  | 
27  |  | IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)  | 
28  |  | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)  | 
29  |  | IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)  | 
30  |  |  | 
31  |  | int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)  | 
32  | 0  | { | 
33  | 0  |     if (alg == NULL)  | 
34  | 0  |         return 0;  | 
35  |  |  | 
36  | 0  |     if (ptype != V_ASN1_UNDEF && alg->parameter == NULL  | 
37  | 0  |             && (alg->parameter = ASN1_TYPE_new()) == NULL)  | 
38  | 0  |         return 0;  | 
39  |  |  | 
40  | 0  |     ASN1_OBJECT_free(alg->algorithm);  | 
41  | 0  |     alg->algorithm = aobj;  | 
42  |  | 
  | 
43  | 0  |     if (ptype == V_ASN1_EOC)  | 
44  | 0  |         return 1;  | 
45  | 0  |     if (ptype == V_ASN1_UNDEF) { | 
46  | 0  |         ASN1_TYPE_free(alg->parameter);  | 
47  | 0  |         alg->parameter = NULL;  | 
48  | 0  |     } else  | 
49  | 0  |         ASN1_TYPE_set(alg->parameter, ptype, pval);  | 
50  | 0  |     return 1;  | 
51  | 0  | }  | 
52  |  |  | 
53  |  | X509_ALGOR *ossl_X509_ALGOR_from_nid(int nid, int ptype, void *pval)  | 
54  | 0  | { | 
55  | 0  |     ASN1_OBJECT *algo = OBJ_nid2obj(nid);  | 
56  | 0  |     X509_ALGOR *alg = NULL;  | 
57  |  | 
  | 
58  | 0  |     if (algo == NULL)  | 
59  | 0  |         return NULL;  | 
60  | 0  |     if ((alg = X509_ALGOR_new()) == NULL)  | 
61  | 0  |         goto err;  | 
62  | 0  |     if (X509_ALGOR_set0(alg, algo, ptype, pval))  | 
63  | 0  |         return alg;  | 
64  | 0  |     alg->algorithm = NULL; /* precaution to prevent double free */  | 
65  |  | 
  | 
66  | 0  |  err:  | 
67  | 0  |     X509_ALGOR_free(alg);  | 
68  |  |     /* ASN1_OBJECT_free(algo) is not needed due to OBJ_nid2obj() */  | 
69  | 0  |     return NULL;  | 
70  | 0  | }  | 
71  |  |  | 
72  |  | void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,  | 
73  |  |                      const void **ppval, const X509_ALGOR *algor)  | 
74  | 0  | { | 
75  | 0  |     if (paobj)  | 
76  | 0  |         *paobj = algor->algorithm;  | 
77  | 0  |     if (pptype) { | 
78  | 0  |         if (algor->parameter == NULL) { | 
79  | 0  |             *pptype = V_ASN1_UNDEF;  | 
80  | 0  |             return;  | 
81  | 0  |         } else  | 
82  | 0  |             *pptype = algor->parameter->type;  | 
83  | 0  |         if (ppval)  | 
84  | 0  |             *ppval = algor->parameter->value.ptr;  | 
85  | 0  |     }  | 
86  | 0  | }  | 
87  |  |  | 
88  |  | /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */  | 
89  |  | void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)  | 
90  | 0  | { | 
91  | 0  |     int type = md->flags & EVP_MD_FLAG_DIGALGID_ABSENT ? V_ASN1_UNDEF  | 
92  | 0  |                                                        : V_ASN1_NULL;  | 
93  |  | 
  | 
94  | 0  |     (void)X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_get_type(md)), type, NULL);  | 
95  | 0  | }  | 
96  |  |  | 
97  |  | int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)  | 
98  | 0  | { | 
99  | 0  |     int rv;  | 
100  | 0  |     rv = OBJ_cmp(a->algorithm, b->algorithm);  | 
101  | 0  |     if (rv)  | 
102  | 0  |         return rv;  | 
103  | 0  |     if (!a->parameter && !b->parameter)  | 
104  | 0  |         return 0;  | 
105  | 0  |     return ASN1_TYPE_cmp(a->parameter, b->parameter);  | 
106  | 0  | }  | 
107  |  |  | 
108  |  | int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src)  | 
109  | 0  | { | 
110  | 0  |     if (src == NULL || dest == NULL)  | 
111  | 0  |         return 0;  | 
112  |  |  | 
113  | 0  |     if (dest->algorithm)  | 
114  | 0  |          ASN1_OBJECT_free(dest->algorithm);  | 
115  | 0  |     dest->algorithm = NULL;  | 
116  |  | 
  | 
117  | 0  |     if (dest->parameter)  | 
118  | 0  |         ASN1_TYPE_free(dest->parameter);  | 
119  | 0  |     dest->parameter = NULL;  | 
120  |  | 
  | 
121  | 0  |     if (src->algorithm)  | 
122  | 0  |         if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL)  | 
123  | 0  |             return 0;  | 
124  |  |  | 
125  | 0  |     if (src->parameter != NULL) { | 
126  | 0  |         dest->parameter = ASN1_TYPE_new();  | 
127  | 0  |         if (dest->parameter == NULL)  | 
128  | 0  |             return 0;  | 
129  |  |  | 
130  |  |         /* Assuming this is also correct for a BOOL.  | 
131  |  |          * set does copy as a side effect.  | 
132  |  |          */  | 
133  | 0  |         if (ASN1_TYPE_set1(dest->parameter, src->parameter->type,  | 
134  | 0  |                            src->parameter->value.ptr) == 0)  | 
135  | 0  |             return 0;  | 
136  | 0  |     }  | 
137  |  |  | 
138  | 0  |     return 1;  | 
139  | 0  | }  | 
140  |  |  | 
141  |  | /* allocate and set algorithm ID from EVP_MD, default SHA1 */  | 
142  |  | int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md)  | 
143  | 0  | { | 
144  | 0  |     X509_ALGOR *alg;  | 
145  |  |  | 
146  |  |     /* Default is SHA1 so no need to create it - still success */  | 
147  | 0  |     if (md == NULL || EVP_MD_is_a(md, "SHA1"))  | 
148  | 0  |         return 1;  | 
149  | 0  |     if ((alg = X509_ALGOR_new()) == NULL)  | 
150  | 0  |         return 0;  | 
151  | 0  |     X509_ALGOR_set_md(alg, md);  | 
152  | 0  |     *palg = alg;  | 
153  | 0  |     return 1;  | 
154  | 0  | }  | 
155  |  |  | 
156  |  | /* convert algorithm ID to EVP_MD, default SHA1 */  | 
157  |  | const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg)  | 
158  | 0  | { | 
159  | 0  |     const EVP_MD *md;  | 
160  |  | 
  | 
161  | 0  |     if (alg == NULL)  | 
162  | 0  |         return EVP_sha1();  | 
163  | 0  |     md = EVP_get_digestbyobj(alg->algorithm);  | 
164  | 0  |     if (md == NULL)  | 
165  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_DIGEST);  | 
166  | 0  |     return md;  | 
167  | 0  | }  | 
168  |  |  | 
169  |  | X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg)  | 
170  | 0  | { | 
171  | 0  |     if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)  | 
172  | 0  |         return NULL;  | 
173  | 0  |     return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),  | 
174  | 0  |                                      alg->parameter);  | 
175  | 0  | }  | 
176  |  |  | 
177  |  | /* Allocate and set MGF1 algorithm ID from EVP_MD */  | 
178  |  | int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)  | 
179  | 0  | { | 
180  | 0  |     X509_ALGOR *algtmp = NULL;  | 
181  | 0  |     ASN1_STRING *stmp = NULL;  | 
182  |  | 
  | 
183  | 0  |     *palg = NULL;  | 
184  | 0  |     if (mgf1md == NULL || EVP_MD_is_a(mgf1md, "SHA1"))  | 
185  | 0  |         return 1;  | 
186  |  |     /* need to embed algorithm ID inside another */  | 
187  | 0  |     if (!ossl_x509_algor_new_from_md(&algtmp, mgf1md))  | 
188  | 0  |         goto err;  | 
189  | 0  |     if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)  | 
190  | 0  |          goto err;  | 
191  | 0  |     *palg = ossl_X509_ALGOR_from_nid(NID_mgf1, V_ASN1_SEQUENCE, stmp);  | 
192  | 0  |     if (*palg == NULL)  | 
193  | 0  |         goto err;  | 
194  | 0  |     stmp = NULL;  | 
195  | 0  |  err:  | 
196  | 0  |     ASN1_STRING_free(stmp);  | 
197  | 0  |     X509_ALGOR_free(algtmp);  | 
198  | 0  |     return *palg != NULL;  | 
199  | 0  | }  |