1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4e1051a39Sopenharmony_ci *
5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
6e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
9e1051a39Sopenharmony_ci */
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci/* We need to use some engine deprecated APIs */
12e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci#include "eng_local.h"
15e1051a39Sopenharmony_ci#include <openssl/conf.h>
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ciint ENGINE_set_default(ENGINE *e, unsigned int flags)
18e1051a39Sopenharmony_ci{
19e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_CIPHERS) && !ENGINE_set_default_ciphers(e))
20e1051a39Sopenharmony_ci        return 0;
21e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_DIGESTS) && !ENGINE_set_default_digests(e))
22e1051a39Sopenharmony_ci        return 0;
23e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_RSA) && !ENGINE_set_default_RSA(e))
24e1051a39Sopenharmony_ci        return 0;
25e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
26e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_DSA) && !ENGINE_set_default_DSA(e))
27e1051a39Sopenharmony_ci        return 0;
28e1051a39Sopenharmony_ci#endif
29e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH
30e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_DH) && !ENGINE_set_default_DH(e))
31e1051a39Sopenharmony_ci        return 0;
32e1051a39Sopenharmony_ci#endif
33e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
34e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_EC) && !ENGINE_set_default_EC(e))
35e1051a39Sopenharmony_ci        return 0;
36e1051a39Sopenharmony_ci#endif
37e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_RAND) && !ENGINE_set_default_RAND(e))
38e1051a39Sopenharmony_ci        return 0;
39e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_PKEY_METHS)
40e1051a39Sopenharmony_ci        && !ENGINE_set_default_pkey_meths(e))
41e1051a39Sopenharmony_ci        return 0;
42e1051a39Sopenharmony_ci    if ((flags & ENGINE_METHOD_PKEY_ASN1_METHS)
43e1051a39Sopenharmony_ci        && !ENGINE_set_default_pkey_asn1_meths(e))
44e1051a39Sopenharmony_ci        return 0;
45e1051a39Sopenharmony_ci    return 1;
46e1051a39Sopenharmony_ci}
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci/* Set default algorithms using a string */
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_cistatic int int_def_cb(const char *alg, int len, void *arg)
51e1051a39Sopenharmony_ci{
52e1051a39Sopenharmony_ci    unsigned int *pflags = arg;
53e1051a39Sopenharmony_ci    if (alg == NULL)
54e1051a39Sopenharmony_ci        return 0;
55e1051a39Sopenharmony_ci    if (strncmp(alg, "ALL", len) == 0)
56e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_ALL;
57e1051a39Sopenharmony_ci    else if (strncmp(alg, "RSA", len) == 0)
58e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_RSA;
59e1051a39Sopenharmony_ci    else if (strncmp(alg, "DSA", len) == 0)
60e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_DSA;
61e1051a39Sopenharmony_ci    else if (strncmp(alg, "DH", len) == 0)
62e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_DH;
63e1051a39Sopenharmony_ci    else if (strncmp(alg, "EC", len) == 0)
64e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_EC;
65e1051a39Sopenharmony_ci    else if (strncmp(alg, "RAND", len) == 0)
66e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_RAND;
67e1051a39Sopenharmony_ci    else if (strncmp(alg, "CIPHERS", len) == 0)
68e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_CIPHERS;
69e1051a39Sopenharmony_ci    else if (strncmp(alg, "DIGESTS", len) == 0)
70e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_DIGESTS;
71e1051a39Sopenharmony_ci    else if (strncmp(alg, "PKEY", len) == 0)
72e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_PKEY_METHS | ENGINE_METHOD_PKEY_ASN1_METHS;
73e1051a39Sopenharmony_ci    else if (strncmp(alg, "PKEY_CRYPTO", len) == 0)
74e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_PKEY_METHS;
75e1051a39Sopenharmony_ci    else if (strncmp(alg, "PKEY_ASN1", len) == 0)
76e1051a39Sopenharmony_ci        *pflags |= ENGINE_METHOD_PKEY_ASN1_METHS;
77e1051a39Sopenharmony_ci    else
78e1051a39Sopenharmony_ci        return 0;
79e1051a39Sopenharmony_ci    return 1;
80e1051a39Sopenharmony_ci}
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ciint ENGINE_set_default_string(ENGINE *e, const char *def_list)
83e1051a39Sopenharmony_ci{
84e1051a39Sopenharmony_ci    unsigned int flags = 0;
85e1051a39Sopenharmony_ci    if (!CONF_parse_list(def_list, ',', 1, int_def_cb, &flags)) {
86e1051a39Sopenharmony_ci        ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_INVALID_STRING,
87e1051a39Sopenharmony_ci                       "str=%s", def_list);
88e1051a39Sopenharmony_ci        return 0;
89e1051a39Sopenharmony_ci    }
90e1051a39Sopenharmony_ci    return ENGINE_set_default(e, flags);
91e1051a39Sopenharmony_ci}
92e1051a39Sopenharmony_ci
93e1051a39Sopenharmony_ciint ENGINE_register_complete(ENGINE *e)
94e1051a39Sopenharmony_ci{
95e1051a39Sopenharmony_ci    ENGINE_register_ciphers(e);
96e1051a39Sopenharmony_ci    ENGINE_register_digests(e);
97e1051a39Sopenharmony_ci    ENGINE_register_RSA(e);
98e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
99e1051a39Sopenharmony_ci    ENGINE_register_DSA(e);
100e1051a39Sopenharmony_ci#endif
101e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH
102e1051a39Sopenharmony_ci    ENGINE_register_DH(e);
103e1051a39Sopenharmony_ci#endif
104e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
105e1051a39Sopenharmony_ci    ENGINE_register_EC(e);
106e1051a39Sopenharmony_ci#endif
107e1051a39Sopenharmony_ci    ENGINE_register_RAND(e);
108e1051a39Sopenharmony_ci    ENGINE_register_pkey_meths(e);
109e1051a39Sopenharmony_ci    ENGINE_register_pkey_asn1_meths(e);
110e1051a39Sopenharmony_ci    return 1;
111e1051a39Sopenharmony_ci}
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ciint ENGINE_register_all_complete(void)
114e1051a39Sopenharmony_ci{
115e1051a39Sopenharmony_ci    ENGINE *e;
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_ci    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
118e1051a39Sopenharmony_ci        if (!(e->flags & ENGINE_FLAGS_NO_REGISTER_ALL))
119e1051a39Sopenharmony_ci            ENGINE_register_complete(e);
120e1051a39Sopenharmony_ci    return 1;
121e1051a39Sopenharmony_ci}
122