summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSCore/CryptoAlg.c
blob: 38533fc8864b3b7be593a373359c7e44b2548b7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* -*- Mode: C; tab-width: 4 -*-
 *
 * Copyright (c) 2011 Apple Computer, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// ***************************************************************************
// CryptoAlg.c:
// Interface to DNSSEC cryptographic algorithms. The crypto support itself is
// provided by the platform and the functions in this file just provide an
// interface to access them in a more generic way.
// ***************************************************************************

#include "mDNSEmbeddedAPI.h"
#include "CryptoAlg.h"

AlgFuncs *DigestAlgFuncs[DIGEST_TYPE_MAX];
AlgFuncs *CryptoAlgFuncs[CRYPTO_ALG_MAX];
AlgFuncs *EncAlgFuncs[ENC_ALG_MAX];

mDNSexport mStatus DigestAlgInit(mDNSu8 digestType, AlgFuncs *func)
{
    if (digestType >= DIGEST_TYPE_MAX)
    {
        LogMsg("DigestAlgInit: digestType %d exceeds bounds", digestType);
        return mStatus_BadParamErr;
    }
    // As digestTypes may not be consecutive, check for specific digest types
    // that we support
    if (digestType != SHA1_DIGEST_TYPE &&
        digestType != SHA256_DIGEST_TYPE)
    {
        LogMsg("DigestAlgInit: digestType %d not supported", digestType);
        return mStatus_BadParamErr;
    }
    DigestAlgFuncs[digestType] = func;
    return mStatus_NoError;
}

mDNSexport mStatus CryptoAlgInit(mDNSu8 alg, AlgFuncs *func)
{
    if (alg >= CRYPTO_ALG_MAX)
    {
        LogMsg("CryptoAlgInit: alg %d exceeds bounds", alg);
        return mStatus_BadParamErr;
    }
    // As algs may not be consecutive, check for specific algorithms
    // that we support
    if (alg != CRYPTO_RSA_SHA1 && alg != CRYPTO_RSA_SHA256 && alg != CRYPTO_RSA_SHA512 &&
        alg != CRYPTO_DSA_NSEC3_SHA1 && alg != CRYPTO_RSA_NSEC3_SHA1)
    {
        LogMsg("CryptoAlgInit: alg %d not supported", alg);
        return mStatus_BadParamErr;
    }

    CryptoAlgFuncs[alg] = func;
    return mStatus_NoError;
}

mDNSexport mStatus EncAlgInit(mDNSu8 alg, AlgFuncs *func)
{
    if (alg >= ENC_ALG_MAX)
    {
        LogMsg("EncAlgInit: alg %d exceeds bounds", alg);
        return mStatus_BadParamErr;
    }

    // As algs may not be consecutive, check for specific algorithms
    // that we support
    if (alg != ENC_BASE32 && alg != ENC_BASE64)
    {
        LogMsg("EncAlgInit: alg %d not supported", alg);
        return mStatus_BadParamErr;
    }

    EncAlgFuncs[alg] = func;
    return mStatus_NoError;
}

mDNSexport AlgContext *AlgCreate(AlgType type, mDNSu8 alg)
{
    AlgFuncs *func = mDNSNULL;
    AlgContext *ctx;

    if (type == CRYPTO_ALG)
    {
        if (alg >= CRYPTO_ALG_MAX) return mDNSNULL;
        func = CryptoAlgFuncs[alg];
    }
    else if (type == DIGEST_ALG)
    {
        if (alg >= DIGEST_TYPE_MAX) return mDNSNULL;
        func = DigestAlgFuncs[alg];
    }
    else if (type == ENC_ALG)
    {
        if (alg >= ENC_ALG_MAX) return mDNSNULL;
        func = EncAlgFuncs[alg];
    }

    if (!func)
    {
        // If there is no support from the platform, this case can happen.
        LogInfo("AlgCreate: func is NULL");
        return mDNSNULL;
    }

    if (func->Create)
    {
        mStatus err;
        ctx = mDNSPlatformMemAllocate(sizeof(AlgContext));
        if (!ctx) return mDNSNULL;
        // Create expects ctx->alg to be initialized
        ctx->alg = alg;
        err = func->Create(ctx);
        if (err == mStatus_NoError)
        {
            ctx->type = type;
            return ctx;
        }
        mDNSPlatformMemFree(ctx);
    }
    return mDNSNULL;
}

mDNSexport mStatus AlgDestroy(AlgContext *ctx)
{
    AlgFuncs *func = mDNSNULL;

    if (ctx->type == CRYPTO_ALG)
        func = CryptoAlgFuncs[ctx->alg];
    else if (ctx->type == DIGEST_ALG)
        func = DigestAlgFuncs[ctx->alg];
    else if (ctx->type == ENC_ALG)
        func = EncAlgFuncs[ctx->alg];

    if (!func)
    {
        LogMsg("AlgDestroy: ERROR!! func is NULL");
        mDNSPlatformMemFree(ctx);
        return mStatus_BadParamErr;
    }

    if (func->Destroy)
        func->Destroy(ctx);

    mDNSPlatformMemFree(ctx);
    return mStatus_NoError;
}

mDNSexport mDNSu32 AlgLength(AlgContext *ctx)
{
    AlgFuncs *func = mDNSNULL;

    if (ctx->type == CRYPTO_ALG)
        func = CryptoAlgFuncs[ctx->alg];
    else if (ctx->type == DIGEST_ALG)
        func = DigestAlgFuncs[ctx->alg];
    else if (ctx->type == ENC_ALG)
        func = EncAlgFuncs[ctx->alg];

    // This should never happen as AlgCreate would have failed
    if (!func)
    {
        LogMsg("AlgLength: ERROR!! func is NULL");
        return 0;
    }

    if (func->Length)
        return (func->Length(ctx));
    else
        return 0;
}

mDNSexport mStatus AlgAdd(AlgContext *ctx, const void *data, mDNSu32 len)
{
    AlgFuncs *func = mDNSNULL;

    if (ctx->type == CRYPTO_ALG)
        func = CryptoAlgFuncs[ctx->alg];
    else if (ctx->type == DIGEST_ALG)
        func = DigestAlgFuncs[ctx->alg];
    else if (ctx->type == ENC_ALG)
        func = EncAlgFuncs[ctx->alg];

    // This should never happen as AlgCreate would have failed
    if (!func)
    {
        LogMsg("AlgAdd: ERROR!! func is NULL");
        return mStatus_BadParamErr;
    }

    if (func->Add)
        return (func->Add(ctx, data, len));
    else
        return mStatus_BadParamErr;
}

mDNSexport mStatus AlgVerify(AlgContext *ctx, mDNSu8 *key, mDNSu32 keylen, mDNSu8 *signature, mDNSu32 siglen)
{
    AlgFuncs *func = mDNSNULL;

    if (ctx->type == CRYPTO_ALG)
        func = CryptoAlgFuncs[ctx->alg];
    else if (ctx->type == DIGEST_ALG)
        func = DigestAlgFuncs[ctx->alg];
    else if (ctx->type == ENC_ALG)
        func = EncAlgFuncs[ctx->alg];

    // This should never happen as AlgCreate would have failed
    if (!func)
    {
        LogMsg("AlgVerify: ERROR!! func is NULL");
        return mStatus_BadParamErr;
    }

    if (func->Verify)
        return (func->Verify(ctx, key, keylen, signature, siglen));
    else
        return mStatus_BadParamErr;
}

mDNSexport mDNSu8* AlgEncode(AlgContext *ctx)
{
    AlgFuncs *func = mDNSNULL;

    if (ctx->type == CRYPTO_ALG)
        func = CryptoAlgFuncs[ctx->alg];
    else if (ctx->type == DIGEST_ALG)
        func = DigestAlgFuncs[ctx->alg];
    else if (ctx->type == ENC_ALG)
        func = EncAlgFuncs[ctx->alg];

    // This should never happen as AlgCreate would have failed
    if (!func)
    {
        LogMsg("AlgEncode: ERROR!! func is NULL");
        return mDNSNULL;
    }

    if (func->Encode)
        return (func->Encode(ctx));
    else
        return mDNSNULL;
}

mDNSexport mStatus AlgFinal(AlgContext *ctx, void *data, mDNSu32 len)
{
    AlgFuncs *func = mDNSNULL;

    if (ctx->type == CRYPTO_ALG)
        func = CryptoAlgFuncs[ctx->alg];
    else if (ctx->type == DIGEST_ALG)
        func = DigestAlgFuncs[ctx->alg];
    else if (ctx->type == ENC_ALG)
        func = EncAlgFuncs[ctx->alg];

    // This should never happen as AlgCreate would have failed
    if (!func)
    {
        LogMsg("AlgEncode: ERROR!! func is NULL");
        return mDNSNULL;
    }

    if (func->Final)
        return (func->Final(ctx, data, len));
    else
        return mStatus_BadParamErr;
}