Coverage Report

Created: 2026-08-02 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xmlsec/src/openssl/kt_rsa.c
Line
Count
Source
1
/**
2
 * XML Security Library (http://www.aleksey.com/xmlsec).
3
 *
4
 * This is free software; see the Copyright file in the source distribution for precise wording.
5
 *
6
 * Copyright (C) 2002-2026 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
7
 */
8
/**
9
 * @addtogroup xmlsec_openssl_crypto
10
 * @brief RSA Key Transport transforms implementation for OpenSSL.
11
 */
12
#include "globals.h"
13
14
#include <stdlib.h>
15
#include <string.h>
16
17
#include <openssl/rsa.h>
18
#include <openssl/evp.h>
19
#include <openssl/sha.h>
20
#include <openssl/objects.h>
21
22
#include <xmlsec/xmlsec.h>
23
#include <xmlsec/buffer.h>
24
#include <xmlsec/errors.h>
25
#include <xmlsec/keys.h>
26
#include <xmlsec/private.h>
27
#include <xmlsec/strings.h>
28
#include <xmlsec/transforms.h>
29
30
#include <xmlsec/openssl/crypto.h>
31
#include <xmlsec/openssl/evp.h>
32
33
#ifdef XMLSEC_OPENSSL_API_300
34
#include <openssl/core_names.h>
35
#include <openssl/param_build.h>
36
#endif /* XMLSEC_OPENSSL_API_300 */
37
38
#include "../cast_helpers.h"
39
#include "../transform_helpers.h"
40
#include "openssl_compat.h"
41
#include "private.h"
42
43
#ifndef XMLSEC_NO_RSA
44
45
#ifndef XMLSEC_NO_RSA_PKCS15
46
47
/******************************************************************************
48
 *
49
 * Internal OpenSSL RSA PKCS1 CTX
50
 *
51
  *****************************************************************************/
52
typedef struct _xmlSecOpenSSLRsaPkcs1Ctx        xmlSecOpenSSLRsaPkcs1Ctx,
53
                                                *xmlSecOpenSSLRsaPkcs1CtxPtr;
54
struct _xmlSecOpenSSLRsaPkcs1Ctx {
55
#ifndef XMLSEC_OPENSSL_API_300
56
    EVP_PKEY*           pKey;
57
#else /* XMLSEC_OPENSSL_API_300 */
58
    EVP_PKEY_CTX*       pKeyCtx;
59
#endif /* XMLSEC_OPENSSL_API_300 */
60
    xmlSecSize          keySize;
61
};
62
63
/******************************************************************************
64
 *
65
 * RSA PKCS1 key transport transform
66
 *
67
  *****************************************************************************/
68
0
XMLSEC_TRANSFORM_DECLARE(OpenSSLRsaPkcs1, xmlSecOpenSSLRsaPkcs1Ctx)
69
0
#define xmlSecOpenSSLRsaPkcs1Size XMLSEC_TRANSFORM_SIZE(OpenSSLRsaPkcs1)
70
0
71
0
static int      xmlSecOpenSSLRsaPkcs1Initialize                 (xmlSecTransformPtr transform);
72
0
static void     xmlSecOpenSSLRsaPkcs1Finalize                   (xmlSecTransformPtr transform);
73
0
static int      xmlSecOpenSSLRsaPkcs1SetKeyReq                  (xmlSecTransformPtr transform,
74
0
                                                                 xmlSecKeyReqPtr keyReq);
75
0
static int      xmlSecOpenSSLRsaPkcs1SetKey                     (xmlSecTransformPtr transform,
76
0
                                                                 xmlSecKeyPtr key);
77
0
static int      xmlSecOpenSSLRsaPkcs1Execute                    (xmlSecTransformPtr transform,
78
0
                                                                 int last,
79
0
                                                                 xmlSecTransformCtxPtr transformCtx);
80
0
static int      xmlSecOpenSSLRsaPkcs1Process                    (xmlSecTransformPtr transform);
81
0
82
0
static xmlSecTransformKlass xmlSecOpenSSLRsaPkcs1Klass = {
83
0
    /* klass/object sizes */
84
0
    sizeof(xmlSecTransformKlass),               /* xmlSecSize klassSize */
85
0
    xmlSecOpenSSLRsaPkcs1Size,                  /* xmlSecSize objSize */
86
0
87
0
    xmlSecNameRsaPkcs1,                         /* const xmlChar* name; */
88
0
    xmlSecHrefRsaPkcs1,                         /* const xmlChar* href; */
89
0
    xmlSecTransformUsageEncryptionMethod,       /* xmlSecAlgorithmUsage usage; */
90
0
91
0
    xmlSecOpenSSLRsaPkcs1Initialize,            /* xmlSecTransformInitializeMethod initialize; */
92
0
    xmlSecOpenSSLRsaPkcs1Finalize,              /* xmlSecTransformFinalizeMethod finalize; */
93
0
    NULL,                                       /* xmlSecTransformNodeReadMethod readNode; */
94
0
    NULL,                                       /* xmlSecTransformNodeWriteMethod writeNode; */
95
0
    xmlSecOpenSSLRsaPkcs1SetKeyReq,             /* xmlSecTransformSetKeyMethod setKeyReq; */
96
0
    xmlSecOpenSSLRsaPkcs1SetKey,                /* xmlSecTransformSetKeyMethod setKey; */
97
0
    NULL,                                       /* xmlSecTransformValidateMethod validate; */
98
0
    xmlSecTransformDefaultGetDataType,          /* xmlSecTransformGetDataTypeMethod getDataType; */
99
0
    xmlSecTransformDefaultPushBin,              /* xmlSecTransformPushBinMethod pushBin; */
100
0
    xmlSecTransformDefaultPopBin,               /* xmlSecTransformPopBinMethod popBin; */
101
0
    NULL,                                       /* xmlSecTransformPushXmlMethod pushXml; */
102
0
    NULL,                                       /* xmlSecTransformPopXmlMethod popXml; */
103
0
    xmlSecOpenSSLRsaPkcs1Execute,               /* xmlSecTransformExecuteMethod execute; */
104
0
105
0
    NULL,                                       /* void* reserved0; */
106
0
    NULL,                                       /* void* reserved1; */
107
0
};
108
0
109
0
/**
110
0
 * @brief The RSA-PKCS1 key transport transform klass.
111
0
 * @return RSA-PKCS1 key transport transform klass.
112
0
 */
113
0
xmlSecTransformId
114
0
xmlSecOpenSSLTransformRsaPkcs1GetKlass(void) {
115
0
    return(&xmlSecOpenSSLRsaPkcs1Klass);
116
0
}
117
118
#if !defined(XMLSEC_OPENSSL_API_300)
119
120
static int
121
xmlSecOpenSSLRsaPkcs1SetKeyImpl(xmlSecOpenSSLRsaPkcs1CtxPtr ctx, EVP_PKEY* pKey,
122
                                int encrypt XMLSEC_ATTRIBUTE_UNUSED) {
123
    RSA *rsa = NULL;
124
    xmlSecOpenSSLSizeT keyLen;
125
126
    xmlSecAssert2(ctx != NULL, -1);
127
    xmlSecAssert2(ctx->pKey == NULL, -1);
128
    xmlSecAssert2(pKey != NULL, -1);
129
    UNREFERENCED_PARAMETER(encrypt);
130
131
    rsa = EVP_PKEY_get0_RSA(pKey);
132
    xmlSecAssert2(rsa != NULL, -1);
133
134
    keyLen = RSA_size(rsa);
135
    if(keyLen <= 0) {
136
        xmlSecOpenSSLError("RSA_size", NULL);
137
        return (-1);
138
    }
139
    XMLSEC_OPENSSL_SAFE_CAST_SIZE_T_TO_SIZE(keyLen, ctx->keySize, return(-1), NULL);
140
141
    ctx->pKey = xmlSecOpenSSLEvpKeyDup(pKey);
142
    if(ctx->pKey == NULL) {
143
        xmlSecInternalError("xmlSecOpenSSLEvpKeyDup", NULL);
144
        return(-1);
145
    }
146
147
    /* success */
148
    return(0);
149
}
150
151
static int
152
xmlSecOpenSSLRsaPkcs1ProcessImpl(xmlSecOpenSSLRsaPkcs1CtxPtr ctx, const xmlSecByte* inBuf, xmlSecSize inSize,
153
                                 xmlSecByte* outBuf, xmlSecSize* outSize, int encrypt) {
154
    RSA* rsa;
155
    xmlSecOpenSSLSizeT inLen;
156
    int ret;
157
158
    xmlSecAssert2(ctx != NULL, -1);
159
    xmlSecAssert2(ctx->pKey != NULL, -1);
160
    xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(ctx->pKey) == 0, -1);
161
    xmlSecAssert2(inBuf != NULL, -1);
162
    xmlSecAssert2(inSize > 0, -1);
163
    xmlSecAssert2(outBuf != NULL, -1);
164
    xmlSecAssert2(outSize != NULL, -1);
165
166
    rsa = EVP_PKEY_get0_RSA(ctx->pKey);
167
    xmlSecAssert2(rsa != NULL, -1);
168
169
    XMLSEC_OPENSSL_SAFE_CAST_SIZE_TO_SIZE_T(inSize, inLen, return(-1), NULL);
170
    if(encrypt != 0) {
171
        ret = RSA_public_encrypt(inLen, inBuf, outBuf, rsa, RSA_PKCS1_PADDING);
172
        if(ret <= 0) {
173
            xmlSecOpenSSLError2("RSA_public_encrypt", NULL,
174
                "size=" XMLSEC_SIZE_FMT, inSize);
175
            return(-1);
176
        }
177
178
    } else {
179
        ret = RSA_private_decrypt(inLen, inBuf, outBuf, rsa, RSA_PKCS1_PADDING);
180
        if(ret <= 0) {
181
            xmlSecOpenSSLError2("RSA_private_decrypt", NULL,
182
                "size=" XMLSEC_SIZE_FMT, inSize);
183
            return(-1);
184
        }
185
   }
186
   XMLSEC_SAFE_CAST_INT_TO_SIZE(ret, (*outSize), return(-1), NULL);
187
188
   /* success */
189
   return(0);
190
}
191
192
#else /* !defined(XMLSEC_OPENSSL_API_300) */
193
194
static int
195
xmlSecOpenSSLRsaPkcs1SetKeyImpl(xmlSecOpenSSLRsaPkcs1CtxPtr ctx, EVP_PKEY* pKey,
196
0
                                int encrypt) {
197
0
    int keyLen;
198
0
    int ret;
199
200
0
    xmlSecAssert2(ctx != NULL, -1);
201
0
    xmlSecAssert2(ctx->pKeyCtx == NULL, -1);
202
0
    xmlSecAssert2(pKey != NULL, -1);
203
204
0
    keyLen = EVP_PKEY_get_size(pKey);
205
0
    if(keyLen <= 0) {
206
0
        xmlSecOpenSSLError("EVP_PKEY_get_size", NULL);
207
0
        return (-1);
208
0
    }
209
0
    XMLSEC_SAFE_CAST_INT_TO_SIZE(keyLen, ctx->keySize, return(-1), NULL);
210
211
0
    ctx->pKeyCtx = EVP_PKEY_CTX_new_from_pkey(xmlSecOpenSSLGetLibCtx(), pKey, NULL);
212
0
    if (ctx->pKeyCtx == NULL) {
213
0
        xmlSecOpenSSLError("EVP_PKEY_CTX_new_from_pkey", NULL);
214
0
        return (-1);
215
0
    }
216
217
0
    if (encrypt != 0) {
218
0
        ret = EVP_PKEY_encrypt_init(ctx->pKeyCtx);
219
0
        if (ret <= 0) {
220
0
            xmlSecOpenSSLError("EVP_PKEY_encrypt_init", NULL);
221
0
            return (-1);
222
0
        }
223
0
    } else {
224
0
        ret = EVP_PKEY_decrypt_init(ctx->pKeyCtx);
225
0
        if (ret <= 0) {
226
0
            xmlSecOpenSSLError("EVP_PKEY_decrypt_init", NULL);
227
0
            return (-1);
228
0
        }
229
0
    }
230
231
0
    ret = EVP_PKEY_CTX_set_rsa_padding(ctx->pKeyCtx, RSA_PKCS1_PADDING);
232
0
    if (ret <= 0) {
233
0
        xmlSecOpenSSLError("EVP_PKEY_CTX_set_rsa_padding", NULL);
234
0
        return (-1);
235
0
    }
236
237
    /* success */
238
0
    return(0);
239
0
}
240
241
static int
242
xmlSecOpenSSLRsaPkcs1ProcessImpl(xmlSecOpenSSLRsaPkcs1CtxPtr ctx, const xmlSecByte* inBuf, xmlSecSize inSize,
243
0
                                 xmlSecByte* outBuf, xmlSecSize* outSize, int encrypt) {
244
0
    size_t outLen = 0;
245
0
    int ret;
246
247
0
    xmlSecAssert2(ctx != NULL, -1);
248
0
    xmlSecAssert2(ctx->pKeyCtx != NULL, -1);
249
0
    xmlSecAssert2(inBuf != NULL, -1);
250
0
    xmlSecAssert2(inSize > 0, -1);
251
0
    xmlSecAssert2(outBuf != NULL, -1);
252
0
    xmlSecAssert2(outSize != NULL, -1);
253
254
0
    outLen = (*outSize);
255
0
    if(encrypt != 0) {
256
0
        ret = EVP_PKEY_encrypt(ctx->pKeyCtx, outBuf, &outLen, inBuf, inSize);
257
0
        if(ret <= 0) {
258
0
            xmlSecOpenSSLError2("EVP_PKEY_encrypt", NULL,
259
0
                "size=" XMLSEC_SIZE_FMT, inSize);
260
0
            return(-1);
261
0
        }
262
0
    } else {
263
0
        ret = EVP_PKEY_decrypt(ctx->pKeyCtx, outBuf, &outLen, inBuf, inSize);
264
0
        if (ret <= 0) {
265
0
            xmlSecOpenSSLError2("EVP_PKEY_decrypt", NULL,
266
0
                "size=" XMLSEC_SIZE_FMT, inSize);
267
0
            return(-1);
268
0
        }
269
0
    }
270
0
    XMLSEC_SAFE_CAST_SIZE_T_TO_SIZE(outLen, (*outSize), return(-1), NULL);
271
272
    /* success */
273
0
    return(0);
274
0
}
275
#endif /* !defined(XMLSEC_OPENSSL_API_300) */
276
277
static int
278
0
xmlSecOpenSSLRsaPkcs1Initialize(xmlSecTransformPtr transform) {
279
0
    xmlSecOpenSSLRsaPkcs1CtxPtr ctx;
280
281
0
    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaPkcs1Id), -1);
282
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaPkcs1Size), -1);
283
284
0
    ctx = xmlSecOpenSSLRsaPkcs1GetCtx(transform);
285
0
    xmlSecAssert2(ctx != NULL, -1);
286
287
0
    memset(ctx, 0, sizeof(xmlSecOpenSSLRsaPkcs1Ctx));
288
0
    return(0);
289
0
}
290
291
static void
292
0
xmlSecOpenSSLRsaPkcs1Finalize(xmlSecTransformPtr transform) {
293
0
    xmlSecOpenSSLRsaPkcs1CtxPtr ctx;
294
295
0
    xmlSecAssert(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaPkcs1Id));
296
0
    xmlSecAssert(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaPkcs1Size));
297
298
0
    ctx = xmlSecOpenSSLRsaPkcs1GetCtx(transform);
299
0
    xmlSecAssert(ctx != NULL);
300
301
302
#ifndef XMLSEC_OPENSSL_API_300
303
    if(ctx->pKey != NULL) {
304
        EVP_PKEY_free(ctx->pKey);
305
    }
306
#else /* XMLSEC_OPENSSL_API_300 */
307
0
    if(ctx->pKeyCtx != NULL) {
308
0
        EVP_PKEY_CTX_free(ctx->pKeyCtx);
309
0
    }
310
0
#endif /* XMLSEC_OPENSSL_API_300 */
311
312
0
    memset(ctx, 0, sizeof(xmlSecOpenSSLRsaPkcs1Ctx));
313
0
}
314
315
static int
316
0
xmlSecOpenSSLRsaPkcs1SetKeyReq(xmlSecTransformPtr transform,  xmlSecKeyReqPtr keyReq) {
317
0
    xmlSecOpenSSLRsaPkcs1CtxPtr ctx;
318
319
0
    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaPkcs1Id), -1);
320
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
321
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaPkcs1Size), -1);
322
0
    xmlSecAssert2(keyReq != NULL, -1);
323
324
0
    ctx = xmlSecOpenSSLRsaPkcs1GetCtx(transform);
325
0
    xmlSecAssert2(ctx != NULL, -1);
326
327
0
    keyReq->keyId        = xmlSecOpenSSLKeyDataRsaId;
328
0
    if(transform->operation == xmlSecTransformOperationEncrypt) {
329
0
        keyReq->keyType  = xmlSecKeyDataTypePublic;
330
0
        keyReq->keyUsage = xmlSecKeyUsageEncrypt;
331
0
    } else {
332
0
        keyReq->keyType  = xmlSecKeyDataTypePrivate;
333
0
        keyReq->keyUsage = xmlSecKeyUsageDecrypt;
334
0
    }
335
0
    return(0);
336
0
}
337
338
static int
339
0
xmlSecOpenSSLRsaPkcs1SetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) {
340
0
    xmlSecOpenSSLRsaPkcs1CtxPtr ctx;
341
0
    EVP_PKEY* pKey;
342
0
    int encrypt;
343
0
    int ret;
344
345
0
    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaPkcs1Id), -1);
346
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
347
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaPkcs1Size), -1);
348
0
    xmlSecAssert2(key != NULL, -1);
349
0
    xmlSecAssert2(xmlSecKeyDataCheckId(xmlSecKeyGetValue(key), xmlSecOpenSSLKeyDataRsaId), -1);
350
351
0
    ctx = xmlSecOpenSSLRsaPkcs1GetCtx(transform);
352
0
    xmlSecAssert2(ctx != NULL, -1);
353
0
    xmlSecAssert2(ctx->keySize == 0, -1);
354
355
356
0
    pKey = xmlSecOpenSSLKeyDataRsaGetEvp(xmlSecKeyGetValue(key));
357
0
    if(pKey == NULL) {
358
0
        xmlSecInternalError("xmlSecOpenSSLKeyDataRsaGetEvp",
359
0
                            xmlSecTransformGetName(transform));
360
0
        return(-1);
361
0
    }
362
0
    xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, -1);
363
364
0
    if (transform->operation == xmlSecTransformOperationEncrypt) {
365
0
        encrypt = 1;
366
0
    } else if (transform->operation == xmlSecTransformOperationDecrypt) {
367
0
        encrypt = 0;
368
0
    } else {
369
0
        xmlSecOtherError2(XMLSEC_ERRORS_R_INVALID_OPERATION,
370
0
            xmlSecTransformGetName(transform),
371
0
            "Unexpected transform operation: " XMLSEC_ENUM_FMT,
372
0
            XMLSEC_ENUM_CAST(transform->operation));
373
0
        return(-1);
374
0
    }
375
376
0
    ret = xmlSecOpenSSLRsaPkcs1SetKeyImpl(ctx, pKey, encrypt);
377
0
    if(ret < 0) {
378
0
        xmlSecInternalError("xmlSecOpenSSLRsaPkcs1SetKeyImpl",
379
0
            xmlSecTransformGetName(transform));
380
0
        return (-1);
381
0
    }
382
383
    /* success */
384
0
    return(0);
385
0
}
386
387
static int
388
xmlSecOpenSSLRsaPkcs1Execute(xmlSecTransformPtr transform, int last,
389
0
                             xmlSecTransformCtxPtr transformCtx XMLSEC_ATTRIBUTE_UNUSED) {
390
0
    xmlSecOpenSSLRsaPkcs1CtxPtr ctx;
391
0
    int ret;
392
393
0
    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaPkcs1Id), -1);
394
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
395
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaPkcs1Size), -1);
396
0
    UNREFERENCED_PARAMETER(transformCtx);
397
398
0
    ctx = xmlSecOpenSSLRsaPkcs1GetCtx(transform);
399
0
    xmlSecAssert2(ctx != NULL, -1);
400
0
    xmlSecAssert2(ctx->keySize > 0, -1);
401
402
0
    if(transform->status == xmlSecTransformStatusNone) {
403
0
        transform->status = xmlSecTransformStatusWorking;
404
0
    }
405
406
0
    if((transform->status == xmlSecTransformStatusWorking) && (last == 0)) {
407
        /* just do nothing */
408
0
    } else  if((transform->status == xmlSecTransformStatusWorking) && (last != 0)) {
409
0
        ret = xmlSecOpenSSLRsaPkcs1Process(transform);
410
0
        if(ret < 0) {
411
0
            xmlSecInternalError("xmlSecOpenSSLRsaPkcs1Process",
412
0
                                xmlSecTransformGetName(transform));
413
0
            return(-1);
414
0
        }
415
0
        transform->status = xmlSecTransformStatusFinished;
416
0
    } else if(transform->status == xmlSecTransformStatusFinished) {
417
        /* the only way we can get here is if there is no input */
418
0
        xmlSecAssert2(xmlSecBufferGetSize(&(transform->inBuf)) == 0, -1);
419
0
    } else {
420
0
        xmlSecInvalidTransfromStatusError(transform);
421
0
        return(-1);
422
0
    }
423
0
    return(0);
424
0
}
425
426
static int
427
0
xmlSecOpenSSLRsaPkcs1Process(xmlSecTransformPtr transform) {
428
0
    xmlSecOpenSSLRsaPkcs1CtxPtr ctx;
429
0
    xmlSecBufferPtr in, out;
430
0
    xmlSecSize inSize, outSize;
431
0
    int encrypt;
432
0
    int ret;
433
434
0
    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaPkcs1Id), -1);
435
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
436
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaPkcs1Size), -1);
437
438
0
    ctx = xmlSecOpenSSLRsaPkcs1GetCtx(transform);
439
0
    xmlSecAssert2(ctx != NULL, -1);
440
0
    xmlSecAssert2(ctx->keySize > 0, -1);
441
442
0
    in = &(transform->inBuf);
443
0
    out = &(transform->outBuf);
444
445
0
    inSize = xmlSecBufferGetSize(in);
446
0
    outSize = xmlSecBufferGetSize(out);
447
0
    xmlSecAssert2(outSize == 0, -1);
448
449
0
    if (transform->operation == xmlSecTransformOperationEncrypt) {
450
0
        encrypt = 1;
451
0
    } else if (transform->operation == xmlSecTransformOperationDecrypt) {
452
0
        encrypt = 0;
453
0
    } else {
454
0
        xmlSecOtherError2(XMLSEC_ERRORS_R_INVALID_OPERATION,
455
0
            xmlSecTransformGetName(transform),
456
0
            "Unexpected transform operation: " XMLSEC_ENUM_FMT,
457
0
            XMLSEC_ENUM_CAST(transform->operation));
458
0
        return(-1);
459
0
    }
460
461
    /* the encoded size is equal to the keys size so we could not
462
     * process more than that */
463
0
    if((encrypt != 0) && (inSize >= ctx->keySize)) {
464
0
        xmlSecInvalidSizeLessThanError("Input data", inSize, ctx->keySize,
465
0
            xmlSecTransformGetName(transform));
466
0
        return(-1);
467
0
    } else if((encrypt == 0) && (inSize != ctx->keySize)) {
468
0
        xmlSecInvalidSizeError("Input data", inSize, ctx->keySize,
469
0
            xmlSecTransformGetName(transform));
470
0
        return(-1);
471
0
    }
472
473
0
    outSize = ctx->keySize;
474
0
    ret = xmlSecBufferSetMaxSize(out, outSize);
475
0
    if(ret < 0) {
476
0
        xmlSecInternalError2("xmlSecBufferSetMaxSize",
477
0
            xmlSecTransformGetName(transform),
478
0
            "size=" XMLSEC_SIZE_FMT, outSize);
479
0
        return(-1);
480
0
    }
481
482
0
    ret = xmlSecOpenSSLRsaPkcs1ProcessImpl(ctx, xmlSecBufferGetData(in), inSize,
483
0
        xmlSecBufferGetData(out), &outSize, encrypt);
484
0
    if(ret < 0) {
485
0
        xmlSecInternalError("xmlSecOpenSSLRsaPkcs1ProcessImpl",
486
0
            xmlSecTransformGetName(transform));
487
0
        return(-1);
488
0
    }
489
490
0
    ret = xmlSecBufferSetSize(out, outSize);
491
0
    if(ret < 0) {
492
0
        xmlSecInternalError2("xmlSecBufferSetSize",
493
0
            xmlSecTransformGetName(transform),
494
0
            "size=" XMLSEC_SIZE_FMT, outSize);
495
0
        return(-1);
496
0
    }
497
498
0
    ret = xmlSecBufferRemoveHead(in, inSize);
499
0
    if(ret < 0) {
500
0
        xmlSecInternalError2("xmlSecBufferRemoveHead",
501
0
            xmlSecTransformGetName(transform),
502
0
            "size=" XMLSEC_SIZE_FMT, inSize);
503
0
        return(-1);
504
0
    }
505
506
0
    return(0);
507
0
}
508
#endif /* XMLSEC_NO_RSA_PKCS15 */
509
510
#ifndef XMLSEC_NO_RSA_OAEP
511
/******************************************************************************
512
 *
513
 * Internal OpenSSL RSA OAEP CTX
514
 *
515
  *****************************************************************************/
516
typedef struct _xmlSecOpenSSLRsaOaepCtx         xmlSecOpenSSLRsaOaepCtx,
517
                                                *xmlSecOpenSSLRsaOaepCtxPtr;
518
struct _xmlSecOpenSSLRsaOaepCtx {
519
#ifndef XMLSEC_OPENSSL_API_300
520
    EVP_PKEY*           pKey;
521
    const EVP_MD*       md;
522
    const EVP_MD*       mgf1md;
523
#else /* XMLSEC_OPENSSL_API_300 */
524
    EVP_PKEY_CTX*       pKeyCtx;
525
    const char*         mdName;
526
    const char*         mgf1mdName;
527
    int                 paramsInitialized;
528
#endif /* XMLSEC_OPENSSL_API_300 */
529
    xmlSecSize          keySize;
530
    xmlSecBuffer        oaepParams;
531
};
532
533
/******************************************************************************
534
 *
535
 * RSA OAEP key transport transform (both XMLEnc 1.0 and XMLEnc 1.1)
536
 *
537
  *****************************************************************************/
538
0
XMLSEC_TRANSFORM_DECLARE(OpenSSLRsaOaep, xmlSecOpenSSLRsaOaepCtx)
539
0
#define xmlSecOpenSSLRsaOaepSize XMLSEC_TRANSFORM_SIZE(OpenSSLRsaOaep)
540
0
541
0
static int      xmlSecOpenSSLRsaOaepInitialize                  (xmlSecTransformPtr transform);
542
0
static void     xmlSecOpenSSLRsaOaepFinalize                    (xmlSecTransformPtr transform);
543
0
static int      xmlSecOpenSSLRsaOaepNodeRead                    (xmlSecTransformPtr transform,
544
0
                                                                 xmlNodePtr node,
545
0
                                                                 xmlSecTransformCtxPtr transformCtx);
546
0
static int      xmlSecOpenSSLRsaOaepSetKeyReq                   (xmlSecTransformPtr transform,
547
0
                                                                 xmlSecKeyReqPtr keyReq);
548
0
static int      xmlSecOpenSSLRsaOaepSetKey                      (xmlSecTransformPtr transform,
549
0
                                                                 xmlSecKeyPtr key);
550
0
static int      xmlSecOpenSSLRsaOaepExecute                     (xmlSecTransformPtr transform,
551
0
                                                                 int last,
552
0
                                                                 xmlSecTransformCtxPtr transformCtx);
553
0
static int      xmlSecOpenSSLRsaOaepProcess                     (xmlSecTransformPtr transform);
554
0
555
0
static int      xmlSecOpenSSLRsaOaepCheckId                     (xmlSecTransformPtr transform);
556
0
557
0
static xmlSecTransformKlass xmlSecOpenSSLRsaOaepKlass = {
558
0
    /* klass/object sizes */
559
0
    sizeof(xmlSecTransformKlass),               /* xmlSecSize klassSize */
560
0
    xmlSecOpenSSLRsaOaepSize,                   /* xmlSecSize objSize */
561
0
562
0
    xmlSecNameRsaOaep,                          /* const xmlChar* name; */
563
0
    xmlSecHrefRsaOaep,                          /* const xmlChar* href; */
564
0
    xmlSecTransformUsageEncryptionMethod,       /* xmlSecAlgorithmUsage usage; */
565
0
566
0
    xmlSecOpenSSLRsaOaepInitialize,             /* xmlSecTransformInitializeMethod initialize; */
567
0
    xmlSecOpenSSLRsaOaepFinalize,               /* xmlSecTransformFinalizeMethod finalize; */
568
0
    xmlSecOpenSSLRsaOaepNodeRead,               /* xmlSecTransformNodeReadMethod readNode; */
569
0
    NULL,                                       /* xmlSecTransformNodeWriteMethod writeNode; */
570
0
    xmlSecOpenSSLRsaOaepSetKeyReq,              /* xmlSecTransformSetKeyMethod setKeyReq; */
571
0
    xmlSecOpenSSLRsaOaepSetKey,                 /* xmlSecTransformSetKeyMethod setKey; */
572
0
    NULL,                                       /* xmlSecTransformValidateMethod validate; */
573
0
    xmlSecTransformDefaultGetDataType,          /* xmlSecTransformGetDataTypeMethod getDataType; */
574
0
    xmlSecTransformDefaultPushBin,              /* xmlSecTransformPushBinMethod pushBin; */
575
0
    xmlSecTransformDefaultPopBin,               /* xmlSecTransformPopBinMethod popBin; */
576
0
    NULL,                                       /* xmlSecTransformPushXmlMethod pushXml; */
577
0
    NULL,                                       /* xmlSecTransformPopXmlMethod popXml; */
578
0
    xmlSecOpenSSLRsaOaepExecute,                /* xmlSecTransformExecuteMethod execute; */
579
0
580
0
    NULL,                                       /* void* reserved0; */
581
0
    NULL,                                       /* void* reserved1; */
582
0
};
583
0
584
0
/**
585
0
 * @brief RSA-OAEP key transport klass (XMLEnc 1.0).
586
0
 * @details The RSA-OAEP key transport transform klass (XMLEnc 1.0).
587
0
 * @return RSA-OAEP key transport transform klass.
588
0
 */
589
0
xmlSecTransformId
590
0
xmlSecOpenSSLTransformRsaOaepGetKlass(void) {
591
0
    return(&xmlSecOpenSSLRsaOaepKlass);
592
0
}
593
594
static xmlSecTransformKlass xmlSecOpenSSLRsaOaepEnc11Klass = {
595
    /* klass/object sizes */
596
    sizeof(xmlSecTransformKlass),               /* xmlSecSize klassSize */
597
    xmlSecOpenSSLRsaOaepSize,                   /* xmlSecSize objSize */
598
599
    xmlSecNameRsaOaepEnc11,                     /* const xmlChar* name; */
600
    xmlSecHrefRsaOaepEnc11,                     /* const xmlChar* href; */
601
    xmlSecTransformUsageEncryptionMethod,       /* xmlSecAlgorithmUsage usage; */
602
603
    xmlSecOpenSSLRsaOaepInitialize,             /* xmlSecTransformInitializeMethod initialize; */
604
    xmlSecOpenSSLRsaOaepFinalize,               /* xmlSecTransformFinalizeMethod finalize; */
605
    xmlSecOpenSSLRsaOaepNodeRead,               /* xmlSecTransformNodeReadMethod readNode; */
606
    NULL,                                       /* xmlSecTransformNodeWriteMethod writeNode; */
607
    xmlSecOpenSSLRsaOaepSetKeyReq,              /* xmlSecTransformSetKeyMethod setKeyReq; */
608
    xmlSecOpenSSLRsaOaepSetKey,                 /* xmlSecTransformSetKeyMethod setKey; */
609
    NULL,                                       /* xmlSecTransformValidateMethod validate; */
610
    xmlSecTransformDefaultGetDataType,          /* xmlSecTransformGetDataTypeMethod getDataType; */
611
    xmlSecTransformDefaultPushBin,              /* xmlSecTransformPushBinMethod pushBin; */
612
    xmlSecTransformDefaultPopBin,               /* xmlSecTransformPopBinMethod popBin; */
613
    NULL,                                       /* xmlSecTransformPushXmlMethod pushXml; */
614
    NULL,                                       /* xmlSecTransformPopXmlMethod popXml; */
615
    xmlSecOpenSSLRsaOaepExecute,                /* xmlSecTransformExecuteMethod execute; */
616
617
    NULL,                                       /* void* reserved0; */
618
    NULL,                                       /* void* reserved1; */
619
};
620
621
/**
622
 * @brief RSA-OAEP key transport klass (XMLEnc 1.1).
623
 * @details The RSA-OAEP key transport transform klass (XMLEnc 1.1).
624
 * @return RSA-OAEP key transport transform klass.
625
 */
626
xmlSecTransformId
627
0
xmlSecOpenSSLTransformRsaOaepEnc11GetKlass(void) {
628
0
    return(&xmlSecOpenSSLRsaOaepEnc11Klass);
629
0
}
630
631
static int
632
0
xmlSecOpenSSLRsaOaepCheckId(xmlSecTransformPtr transform) {
633
0
    if(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaOaepId)) {
634
0
        return(1);
635
0
    } else if(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformRsaOaepEnc11Id)) {
636
0
        return(1);
637
0
    }
638
639
    /* not found */
640
0
    return(0);
641
0
}
642
643
#ifndef XMLSEC_OPENSSL_API_300
644
645
static int
646
xmlSecOpenSSLRsaOaepSetKeyImpl(xmlSecOpenSSLRsaOaepCtxPtr ctx, EVP_PKEY* pKey,
647
                            int encrypt XMLSEC_ATTRIBUTE_UNUSED) {
648
    RSA *rsa = NULL;
649
    int keyLen;
650
651
    xmlSecAssert2(ctx != NULL, -1);
652
    xmlSecAssert2(ctx->pKey == NULL, -1);
653
    xmlSecAssert2(pKey != NULL, -1);
654
    UNREFERENCED_PARAMETER(encrypt);
655
656
    rsa = EVP_PKEY_get0_RSA(pKey);
657
    xmlSecAssert2(rsa != NULL, -1);
658
659
    keyLen = RSA_size(rsa);
660
    if(keyLen <= 0) {
661
        xmlSecOpenSSLError("RSA_size", NULL);
662
        return (-1);
663
    }
664
    XMLSEC_SAFE_CAST_INT_TO_SIZE(keyLen, ctx->keySize, return(-1), NULL);
665
666
    ctx->pKey = xmlSecOpenSSLEvpKeyDup(pKey);
667
    if(ctx->pKey == NULL) {
668
        xmlSecInternalError("xmlSecOpenSSLEvpKeyDup", NULL);
669
        return(-1);
670
    }
671
672
    /* success */
673
    return(0);
674
}
675
676
static int
677
xmlSecOpenSSLRsaOaepProcessImpl(xmlSecOpenSSLRsaOaepCtxPtr ctx, const xmlSecByte* inBuf, xmlSecSize inSize,
678
                            xmlSecByte* outBuf, xmlSecSize* outSize, int encrypt) {
679
    xmlSecByte* oaepLabel;
680
    xmlSecSize oaepLabelSize;
681
    RSA* rsa;
682
    int inLen, keyLen, oaepLabelLen;
683
    int ret;
684
685
    xmlSecAssert2(ctx != NULL, -1);
686
    xmlSecAssert2(ctx->pKey != NULL, -1);
687
    xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(ctx->pKey) == 0, -1);
688
    xmlSecAssert2(inBuf != NULL, -1);
689
    xmlSecAssert2(inSize > 0, -1);
690
    xmlSecAssert2(outBuf != NULL, -1);
691
    xmlSecAssert2(outSize != NULL, -1);
692
693
    rsa = EVP_PKEY_get0_RSA(ctx->pKey);
694
    xmlSecAssert2(rsa != NULL, -1);
695
696
    oaepLabel = xmlSecBufferGetData(&(ctx->oaepParams));
697
    oaepLabelSize = xmlSecBufferGetSize(&(ctx->oaepParams));
698
699
    XMLSEC_SAFE_CAST_SIZE_TO_INT(inSize, inLen, return(-1), NULL);
700
    XMLSEC_SAFE_CAST_SIZE_TO_INT(ctx->keySize, keyLen, return(-1), NULL);
701
    XMLSEC_SAFE_CAST_SIZE_TO_INT(oaepLabelSize, oaepLabelLen, return(-1), NULL);
702
703
    if(encrypt != 0) {
704
        /* encrypt */
705
        xmlSecBuffer tmp;
706
707
        /* allocate space for temp buffer */
708
        ret = xmlSecBufferInitialize(&tmp, ctx->keySize);
709
        if(ret < 0) {
710
            xmlSecInternalError2("xmlSecBufferInitialize", NULL,
711
                "size=" XMLSEC_SIZE_FMT, ctx->keySize);
712
            return(-1);
713
        }
714
715
        /* add padding */
716
        ret = RSA_padding_add_PKCS1_OAEP_mgf1(
717
            xmlSecBufferGetData(&tmp), keyLen,
718
            inBuf, inLen,
719
            oaepLabel, oaepLabelLen,
720
            ctx->md, ctx->mgf1md);
721
        if(ret != 1) {
722
            xmlSecOpenSSLError("RSA_padding_add_PKCS1_OAEP_mgf1", NULL);
723
            xmlSecBufferFinalize(&tmp);
724
            return(-1);
725
        }
726
727
        /* encode with OAEPParams */
728
        ret = RSA_public_encrypt(keyLen, xmlSecBufferGetData(&tmp),
729
            outBuf, rsa, RSA_NO_PADDING);
730
        if(ret <= 0) {
731
            xmlSecOpenSSLError("RSA_public_encrypt(RSA_NO_PADDING)", NULL);
732
            xmlSecBufferFinalize(&tmp);
733
            return(-1);
734
        }
735
        xmlSecBufferFinalize(&tmp);
736
737
        /* success */
738
        XMLSEC_SAFE_CAST_INT_TO_SIZE(ret, (*outSize), return(-1), NULL);
739
    } else {
740
        /* decrypt */
741
        BIGNUM * bn;
742
        int outLen;
743
744
        ret = RSA_private_decrypt(inLen, inBuf, outBuf, rsa, RSA_NO_PADDING);
745
        if(ret <= 0) {
746
            xmlSecOpenSSLError("RSA_private_decrypt(RSA_NO_PADDING)", NULL);
747
            return(-1);
748
        }
749
        outLen = ret;
750
751
        /*
752
         * the private decrypt w/o padding adds '0's at the beginning.
753
         * it's not clear for me can I simply skip all '0's from the
754
         * beggining so I have to do decode it back to BIGNUM and dump
755
         * buffer again
756
         */
757
        bn = BN_new();
758
        if(bn == NULL) {
759
            xmlSecOpenSSLError("BN_new()", NULL);
760
            return(-1);
761
        }
762
763
        if(BN_bin2bn(outBuf, outLen, bn) == NULL) {
764
            xmlSecOpenSSLError2("BN_bin2bn", NULL,
765
                "size=%d", outLen);
766
            BN_clear_free(bn);
767
            return(-1);
768
        }
769
770
        ret = BN_bn2bin(bn, outBuf);
771
        if(ret <= 0) {
772
            xmlSecOpenSSLError("BN_bn2bin", NULL);
773
            BN_clear_free(bn);
774
            return(-1);
775
        }
776
        outLen = ret;
777
        BN_clear_free(bn);
778
779
        ret = RSA_padding_check_PKCS1_OAEP_mgf1(
780
            outBuf, outLen, outBuf, outLen, keyLen,
781
            oaepLabel, oaepLabelLen,
782
            ctx->md, ctx->mgf1md);
783
        if(ret < 0) {
784
            xmlSecOpenSSLError("RSA_padding_check_PKCS1_OAEP_mgf1",  NULL);
785
            return(-1);
786
        }
787
788
        /* success */
789
        XMLSEC_SAFE_CAST_INT_TO_SIZE(ret, (*outSize), return(-1), NULL);
790
    }
791
792
    /* success */
793
    return(0);
794
}
795
796
#else /* XMLSEC_OPENSSL_API_300 */
797
798
static int
799
xmlSecOpenSSLRsaOaepSetKeyImpl(xmlSecOpenSSLRsaOaepCtxPtr ctx, EVP_PKEY* pKey,
800
0
                            int encrypt) {
801
0
    int keyLen;
802
0
    int ret;
803
804
0
    xmlSecAssert2(ctx != NULL, -1);
805
0
    xmlSecAssert2(ctx->pKeyCtx == NULL, -1);
806
0
    xmlSecAssert2(pKey != NULL, -1);
807
808
0
    keyLen = EVP_PKEY_get_size(pKey);
809
0
    if(keyLen <= 0) {
810
0
        xmlSecOpenSSLError("EVP_PKEY_get_size", NULL);
811
0
        return (-1);
812
0
    }
813
0
    XMLSEC_SAFE_CAST_INT_TO_SIZE(keyLen, ctx->keySize, return(-1), NULL);
814
815
0
    ctx->pKeyCtx = EVP_PKEY_CTX_new_from_pkey(xmlSecOpenSSLGetLibCtx(), pKey, NULL);
816
0
    if (ctx->pKeyCtx == NULL) {
817
0
        xmlSecOpenSSLError("EVP_PKEY_CTX_new_from_pkey", NULL);
818
0
        return (-1);
819
0
    }
820
821
0
    if (encrypt != 0) {
822
0
        ret = EVP_PKEY_encrypt_init(ctx->pKeyCtx);
823
0
        if (ret <= 0) {
824
0
            xmlSecOpenSSLError("EVP_PKEY_encrypt_init", NULL);
825
0
            return (-1);
826
0
        }
827
0
    } else {
828
0
        ret = EVP_PKEY_decrypt_init(ctx->pKeyCtx);
829
0
        if (ret <= 0) {
830
0
            xmlSecOpenSSLError("EVP_PKEY_decrypt_init", NULL);
831
0
            return (-1);
832
0
        }
833
0
    }
834
835
0
    ret = EVP_PKEY_CTX_set_rsa_padding(ctx->pKeyCtx, RSA_PKCS1_OAEP_PADDING);
836
0
    if (ret <= 0) {
837
0
         xmlSecOpenSSLError("EVP_PKEY_CTX_set_rsa_padding", NULL);
838
0
        return(-1);
839
0
    }
840
841
    /* success */
842
0
    return(0);
843
0
}
844
845
// We can put all the params into one OSSL_PARAM array and setup everything at-once.
846
// However, in OpenSSL <= 3.0.7 there is a bug that mixes OAEP digest and
847
// OAEP MGf1 digest (https://pullanswer.com/questions/mgf1-digest-not-set-correctly-when-configuring-rsa-evp_pkey_ctx-with-ossl_params)
848
// so we do one param at a time.
849
static int
850
0
xmlSecOpenSSSLRsaOaepSetParamsIfNeeded(xmlSecOpenSSLRsaOaepCtxPtr ctx) {
851
0
    xmlSecByte* label;
852
0
    xmlSecSize labelSize;
853
0
    int ret;
854
0
    int res = -1;
855
856
0
    xmlSecAssert2(ctx != NULL, -1);
857
0
    xmlSecAssert2(ctx->pKeyCtx != NULL, -1);
858
859
    /* check if we already initialized oaep params */
860
0
    if(ctx->paramsInitialized != 0) {
861
0
        return(0);
862
0
    }
863
864
    /* OAEP label */
865
0
    label = xmlSecBufferGetData(&(ctx->oaepParams));
866
0
    labelSize = xmlSecBufferGetSize(&(ctx->oaepParams));
867
0
    if((label != NULL) && (labelSize > 0)) {
868
0
        OSSL_PARAM params[2];
869
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, label, labelSize);
870
0
        params[1] = OSSL_PARAM_construct_end();
871
872
0
        ret = EVP_PKEY_CTX_set_params(ctx->pKeyCtx, params);
873
0
        if(ret <= 0) {
874
0
            xmlSecOpenSSLError("EVP_PKEY_CTX_set_params", NULL);
875
0
            goto done;
876
0
        }
877
0
    }
878
879
    /* Digest */
880
0
    if(ctx->mdName != NULL) {
881
0
        OSSL_PARAM params[2];
882
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, (char*)ctx->mdName, 0);
883
0
        params[1] = OSSL_PARAM_construct_end();
884
885
0
        ret = EVP_PKEY_CTX_set_params(ctx->pKeyCtx, params);
886
0
        if(ret <= 0) {
887
0
            xmlSecOpenSSLError("EVP_PKEY_CTX_set_params", NULL);
888
0
            goto done;
889
0
        }
890
0
    }
891
892
    /* MGF1 digest */
893
0
    if(ctx->mgf1mdName != NULL) {
894
0
        OSSL_PARAM params[2];
895
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, (char*)ctx->mgf1mdName, 0);
896
0
        params[1] = OSSL_PARAM_construct_end();
897
898
0
        ret = EVP_PKEY_CTX_set_params(ctx->pKeyCtx, params);
899
0
        if(ret <= 0) {
900
0
            xmlSecOpenSSLError("EVP_PKEY_CTX_set_params", NULL);
901
0
            goto done;
902
0
        }
903
0
    }
904
905
    /* success */
906
0
    ctx->paramsInitialized = 1;
907
0
    res = 0;
908
909
0
done:
910
0
    return(res);
911
0
}
912
913
static int
914
xmlSecOpenSSLRsaOaepProcessImpl(xmlSecOpenSSLRsaOaepCtxPtr ctx, const xmlSecByte* inBuf, xmlSecSize inSize,
915
0
                            xmlSecByte* outBuf, xmlSecSize* outSize, int encrypt) {
916
0
    size_t outSizeT;
917
0
    int ret;
918
919
0
    xmlSecAssert2(ctx != NULL, -1);
920
0
    xmlSecAssert2(ctx->pKeyCtx != NULL, -1);
921
0
    xmlSecAssert2(inBuf != NULL, -1);
922
0
    xmlSecAssert2(inSize > 0, -1);
923
0
    xmlSecAssert2(outBuf != NULL, -1);
924
0
    xmlSecAssert2(outSize != NULL, -1);
925
926
0
    ret = xmlSecOpenSSSLRsaOaepSetParamsIfNeeded(ctx);
927
0
    if(ret != 0) {
928
0
        xmlSecInternalError("xmlSecOpenSSSLRsaOaepSetParamsIfNeeded", NULL);
929
0
        return(-1);
930
0
    }
931
932
0
    outSizeT = (*outSize);
933
0
    if(encrypt != 0) {
934
0
        ret = EVP_PKEY_encrypt(ctx->pKeyCtx, outBuf, &outSizeT, inBuf, inSize);
935
0
        if (ret <= 0) {
936
0
            xmlSecOpenSSLError("EVP_PKEY_encrypt", NULL);
937
0
            return(-1);
938
0
        }
939
0
    } else {
940
0
        ret = EVP_PKEY_decrypt(ctx->pKeyCtx, outBuf, &outSizeT, inBuf, inSize);
941
0
        if (ret <= 0) {
942
0
            xmlSecOpenSSLError("EVP_PKEY_decrypt", NULL);
943
0
            return(-1);
944
0
        }
945
0
    }
946
    /* success */
947
0
    XMLSEC_SAFE_CAST_SIZE_T_TO_SIZE(outSizeT, (*outSize), return(-1), NULL);
948
0
    return(0);
949
950
0
}
951
#endif /* XMLSEC_OPENSSL_API_300 */
952
953
static int
954
0
xmlSecOpenSSLRsaOaepInitialize(xmlSecTransformPtr transform) {
955
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
956
0
    int ret;
957
958
0
    xmlSecAssert2(xmlSecOpenSSLRsaOaepCheckId(transform), -1);
959
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize), -1);
960
961
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
962
0
    xmlSecAssert2(ctx != NULL, -1);
963
964
0
    memset(ctx, 0, sizeof(xmlSecOpenSSLRsaOaepCtx));
965
966
0
    ret = xmlSecBufferInitialize(&(ctx->oaepParams), 0);
967
0
    if(ret < 0) {
968
0
        xmlSecInternalError("xmlSecBufferInitialize",
969
0
                            xmlSecTransformGetName(transform));
970
0
        return(-1);
971
0
    }
972
0
    return(0);
973
0
}
974
975
static void
976
0
xmlSecOpenSSLRsaOaepFinalize(xmlSecTransformPtr transform) {
977
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
978
979
0
    xmlSecAssert(xmlSecOpenSSLRsaOaepCheckId(transform));
980
0
    xmlSecAssert(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize));
981
982
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
983
0
    xmlSecAssert(ctx != NULL);
984
985
#ifndef XMLSEC_OPENSSL_API_300
986
    if(ctx->pKey != NULL) {
987
        EVP_PKEY_free(ctx->pKey);
988
    }
989
#else /* XMLSEC_OPENSSL_API_300 */
990
0
    if(ctx->pKeyCtx != NULL) {
991
0
        EVP_PKEY_CTX_free(ctx->pKeyCtx);
992
0
    }
993
0
#endif /* XMLSEC_OPENSSL_API_300 */
994
995
0
    xmlSecBufferFinalize(&(ctx->oaepParams));
996
0
    memset(ctx, 0, sizeof(xmlSecOpenSSLRsaOaepCtx));
997
0
}
998
999
/* small helper macros to reduce clutter in the code */
1000
#ifndef XMLSEC_OPENSSL_API_300
1001
#define XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, digestVal, digestNameVal) \
1002
    (ctx)->md = (digestVal)
1003
#define XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, digestVal, digestNameVal) \
1004
    (ctx)->mgf1md = (digestVal)
1005
#else /* XMLSEC_OPENSSL_API_300 */
1006
#define XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, digestVal, digestNameVal) \
1007
0
    (ctx)->mdName = (digestNameVal)
1008
#define XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, digestVal, digestNameVal) \
1009
0
    (ctx)->mgf1mdName = (digestNameVal)
1010
#endif /* XMLSEC_OPENSSL_API_300 */
1011
1012
static int
1013
xmlSecOpenSSLRsaOaepNodeRead(xmlSecTransformPtr transform, xmlNodePtr node,
1014
0
                             xmlSecTransformCtxPtr transformCtx XMLSEC_ATTRIBUTE_UNUSED) {
1015
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
1016
0
    xmlSecTransformRsaOaepParams oaepParams;
1017
0
    int ret;
1018
1019
0
    xmlSecAssert2(xmlSecOpenSSLRsaOaepCheckId(transform), -1);
1020
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize), -1);
1021
0
    xmlSecAssert2(node != NULL, -1);
1022
0
    UNREFERENCED_PARAMETER(transformCtx);
1023
1024
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
1025
0
    xmlSecAssert2(ctx != NULL, -1);
1026
0
    xmlSecAssert2(xmlSecBufferGetSize(&(ctx->oaepParams)) == 0, -1);
1027
1028
0
    ret = xmlSecTransformRsaOaepParamsInitialize(&oaepParams);
1029
0
    if (ret < 0) {
1030
0
        xmlSecInternalError("xmlSecTransformRsaOaepParamsInitialize",
1031
0
            xmlSecTransformGetName(transform));
1032
0
        return(-1);
1033
0
    }
1034
1035
0
    ret = xmlSecTransformRsaOaepParamsRead(&oaepParams, node);
1036
0
    if (ret < 0) {
1037
0
        xmlSecInternalError("xmlSecTransformRsaOaepParamsRead",
1038
0
            xmlSecTransformGetName(transform));
1039
0
        xmlSecTransformRsaOaepParamsFinalize(&oaepParams);
1040
0
        return(-1);
1041
0
    }
1042
1043
    /* digest algorithm */
1044
0
    if(oaepParams.digestAlgorithm == NULL) {
1045
0
#ifndef XMLSEC_NO_SHA1
1046
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha1(), OSSL_DIGEST_NAME_SHA1);
1047
#else  /* XMLSEC_NO_SHA1 */
1048
        xmlSecOtherError(XMLSEC_ERRORS_R_DISABLED, NULL, "No OAEP digest algorithm is specified and the default SHA1 digest is disabled");
1049
        xmlSecTransformRsaOaepParamsFinalize(&oaepParams);
1050
        return(-1);
1051
#endif /* XMLSEC_NO_SHA1 */
1052
0
    } else
1053
0
#ifndef XMLSEC_NO_MD5
1054
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefMd5) == 0) {
1055
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_md5(), OSSL_DIGEST_NAME_MD5);
1056
0
    } else
1057
0
#endif /* XMLSEC_NO_MD5 */
1058
1059
0
#ifndef XMLSEC_NO_RIPEMD160
1060
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefRipemd160) == 0) {
1061
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_ripemd160(), OSSL_DIGEST_NAME_RIPEMD160);
1062
0
    } else
1063
0
#endif /* XMLSEC_NO_RIPEMD160 */
1064
1065
0
#ifndef XMLSEC_NO_SHA1
1066
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha1) == 0) {
1067
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha1(), OSSL_DIGEST_NAME_SHA1);
1068
0
    } else
1069
0
#endif /* XMLSEC_NO_SHA1 */
1070
1071
0
#ifndef XMLSEC_NO_SHA224
1072
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha224) == 0) {
1073
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha224(), OSSL_DIGEST_NAME_SHA2_224);
1074
0
    } else
1075
0
#endif /* XMLSEC_NO_SHA224 */
1076
1077
0
#ifndef XMLSEC_NO_SHA256
1078
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha256) == 0) {
1079
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha256(), OSSL_DIGEST_NAME_SHA2_256);
1080
0
    } else
1081
0
#endif /* XMLSEC_NO_SHA256 */
1082
1083
0
#ifndef XMLSEC_NO_SHA384
1084
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha384) == 0) {
1085
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha384(), OSSL_DIGEST_NAME_SHA2_384);
1086
0
    } else
1087
0
#endif /* XMLSEC_NO_SHA384 */
1088
1089
0
#ifndef XMLSEC_NO_SHA512
1090
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha512) == 0) {
1091
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha512(), OSSL_DIGEST_NAME_SHA2_512);
1092
0
    } else
1093
0
#endif /* XMLSEC_NO_SHA512 */
1094
1095
0
#ifndef XMLSEC_NO_SHA3
1096
0
    if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha3_224) == 0) {
1097
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha3_224(), OSSL_DIGEST_NAME_SHA3_224);
1098
0
    } else if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha3_256) == 0) {
1099
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha3_256(), OSSL_DIGEST_NAME_SHA3_256);
1100
0
    } else if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha3_384) == 0) {
1101
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha3_384(), OSSL_DIGEST_NAME_SHA3_384);
1102
0
    } else if(xmlStrcmp(oaepParams.digestAlgorithm, xmlSecHrefSha3_512) == 0) {
1103
0
        XMLSEC_OPENSSL_OAEP_DIGEST_SETUP(ctx, EVP_sha3_512(), OSSL_DIGEST_NAME_SHA3_512);
1104
0
    } else
1105
0
#endif /* XMLSEC_NO_SHA3 */
1106
1107
0
    {
1108
0
       xmlSecInvalidTransfromError2(transform,
1109
0
            "digest algorithm=\"%s\" is not supported for rsa/oaep",
1110
0
            xmlSecErrorsSafeString(oaepParams.digestAlgorithm));
1111
0
        xmlSecTransformRsaOaepParamsFinalize(&oaepParams);
1112
0
        return(-1);
1113
0
    }
1114
1115
    /* mgf1 algorithm */
1116
0
    if(oaepParams.mgf1DigestAlgorithm == NULL) {
1117
0
#ifndef XMLSEC_NO_SHA1
1118
0
        XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, EVP_sha1(), OSSL_DIGEST_NAME_SHA1);
1119
#else  /* XMLSEC_NO_SHA1 */
1120
        xmlSecOtherError(XMLSEC_ERRORS_R_DISABLED, NULL, "No OAEP mgf1 digest algorithm is specified and the default SHA1 digest is disabled");
1121
        xmlSecTransformRsaOaepParamsFinalize(&oaepParams);
1122
        return(-1);
1123
#endif /* XMLSEC_NO_SHA1 */
1124
0
    } else
1125
0
#ifndef XMLSEC_NO_SHA1
1126
0
    if(xmlStrcmp(oaepParams.mgf1DigestAlgorithm, xmlSecHrefMgf1Sha1) == 0) {
1127
0
        XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, EVP_sha1(), OSSL_DIGEST_NAME_SHA1);
1128
0
    } else
1129
0
#endif /* XMLSEC_NO_SHA1 */
1130
1131
0
#ifndef XMLSEC_NO_SHA224
1132
0
    if(xmlStrcmp(oaepParams.mgf1DigestAlgorithm, xmlSecHrefMgf1Sha224) == 0) {
1133
0
        XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, EVP_sha224(), OSSL_DIGEST_NAME_SHA2_224);
1134
0
    } else
1135
0
#endif /* XMLSEC_NO_SHA224 */
1136
1137
0
#ifndef XMLSEC_NO_SHA256
1138
0
    if(xmlStrcmp(oaepParams.mgf1DigestAlgorithm, xmlSecHrefMgf1Sha256) == 0) {
1139
0
        XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, EVP_sha256(), OSSL_DIGEST_NAME_SHA2_256);
1140
0
    } else
1141
0
#endif /* XMLSEC_NO_SHA256 */
1142
1143
0
#ifndef XMLSEC_NO_SHA384
1144
0
    if(xmlStrcmp(oaepParams.mgf1DigestAlgorithm, xmlSecHrefMgf1Sha384) == 0) {
1145
0
        XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, EVP_sha384(), OSSL_DIGEST_NAME_SHA2_384);
1146
0
    } else
1147
0
#endif /* XMLSEC_NO_SHA384 */
1148
1149
0
#ifndef XMLSEC_NO_SHA512
1150
0
    if(xmlStrcmp(oaepParams.mgf1DigestAlgorithm, xmlSecHrefMgf1Sha512) == 0) {
1151
0
        XMLSEC_OPENSSL_OAEP_MGF1_DIGEST_SETUP(ctx, EVP_sha512(), OSSL_DIGEST_NAME_SHA2_512);
1152
0
    } else
1153
0
#endif /* XMLSEC_NO_SHA512 */
1154
0
    {
1155
0
       xmlSecInvalidTransfromError2(transform,
1156
0
            "mgf1 digest algorithm=\"%s\" is not supported for rsa/oaep",
1157
0
            xmlSecErrorsSafeString(oaepParams.mgf1DigestAlgorithm));
1158
0
        xmlSecTransformRsaOaepParamsFinalize(&oaepParams);
1159
0
        return(-1);
1160
0
    }
1161
1162
    /* put oaep params buffer into ctx */
1163
0
    xmlSecBufferSwap(&(oaepParams.oaepParams), &(ctx->oaepParams));
1164
1165
    /* cleanup */
1166
0
    xmlSecTransformRsaOaepParamsFinalize(&oaepParams);
1167
0
    return(0);
1168
0
}
1169
1170
static int
1171
0
xmlSecOpenSSLRsaOaepSetKeyReq(xmlSecTransformPtr transform,  xmlSecKeyReqPtr keyReq) {
1172
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
1173
1174
0
    xmlSecAssert2(xmlSecOpenSSLRsaOaepCheckId(transform), -1);
1175
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
1176
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize), -1);
1177
0
    xmlSecAssert2(keyReq != NULL, -1);
1178
1179
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
1180
0
    xmlSecAssert2(ctx != NULL, -1);
1181
1182
0
    keyReq->keyId        = xmlSecOpenSSLKeyDataRsaId;
1183
0
    if(transform->operation == xmlSecTransformOperationEncrypt) {
1184
0
        keyReq->keyType  = xmlSecKeyDataTypePublic;
1185
0
        keyReq->keyUsage = xmlSecKeyUsageEncrypt;
1186
0
    } else {
1187
0
        keyReq->keyType  = xmlSecKeyDataTypePrivate;
1188
0
        keyReq->keyUsage = xmlSecKeyUsageDecrypt;
1189
0
    }
1190
1191
0
    return(0);
1192
0
}
1193
1194
static int
1195
0
xmlSecOpenSSLRsaOaepSetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) {
1196
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
1197
0
    EVP_PKEY* pKey;
1198
0
    int encrypt;
1199
0
    int ret;
1200
1201
0
    xmlSecAssert2(xmlSecOpenSSLRsaOaepCheckId(transform), -1);
1202
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
1203
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize), -1);
1204
0
    xmlSecAssert2(key != NULL, -1);
1205
0
    xmlSecAssert2(xmlSecKeyDataCheckId(xmlSecKeyGetValue(key), xmlSecOpenSSLKeyDataRsaId), -1);
1206
1207
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
1208
0
    xmlSecAssert2(ctx != NULL, -1);
1209
0
    xmlSecAssert2(ctx->keySize == 0, -1);
1210
1211
0
    pKey = xmlSecOpenSSLKeyDataRsaGetEvp(xmlSecKeyGetValue(key));
1212
0
    if(pKey == NULL) {
1213
0
        xmlSecInternalError("xmlSecOpenSSLKeyDataRsaGetEvp",
1214
0
            xmlSecTransformGetName(transform));
1215
0
        return(-1);
1216
0
    }
1217
0
    xmlSecAssert2(xmlSecOpenSSLKeyValueRsaCheckKeyType(pKey) == 0, -1);
1218
1219
0
    if (transform->operation == xmlSecTransformOperationEncrypt) {
1220
0
        encrypt = 1;
1221
0
    } else if (transform->operation == xmlSecTransformOperationDecrypt) {
1222
0
        encrypt = 0;
1223
0
    } else {
1224
0
        xmlSecOtherError2(XMLSEC_ERRORS_R_INVALID_OPERATION,
1225
0
            xmlSecTransformGetName(transform),
1226
0
            "Unexpected transform operation: " XMLSEC_ENUM_FMT,
1227
0
            XMLSEC_ENUM_CAST(transform->operation));
1228
0
        return(-1);
1229
0
    }
1230
1231
0
    ret = xmlSecOpenSSLRsaOaepSetKeyImpl(ctx, pKey, encrypt);
1232
0
    if(ret < 0) {
1233
0
        xmlSecInternalError("xmlSecOpenSSLKeyDataRsaGetEvp",
1234
0
            xmlSecTransformGetName(transform));
1235
0
        return(-1);
1236
0
    }
1237
1238
    /* success */
1239
0
    return(0);
1240
0
}
1241
1242
static int
1243
xmlSecOpenSSLRsaOaepExecute(xmlSecTransformPtr transform, int last,
1244
0
                            xmlSecTransformCtxPtr transformCtx XMLSEC_ATTRIBUTE_UNUSED) {
1245
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
1246
0
    int ret;
1247
1248
0
    xmlSecAssert2(xmlSecOpenSSLRsaOaepCheckId(transform), -1);
1249
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
1250
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize), -1);
1251
0
    UNREFERENCED_PARAMETER(transformCtx);
1252
1253
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
1254
0
    xmlSecAssert2(ctx != NULL, -1);
1255
0
    xmlSecAssert2(ctx->keySize > 0, -1);
1256
1257
0
    if(transform->status == xmlSecTransformStatusNone) {
1258
0
        transform->status = xmlSecTransformStatusWorking;
1259
0
    }
1260
1261
0
    if((transform->status == xmlSecTransformStatusWorking) && (last == 0)) {
1262
        /* just do nothing */
1263
0
    } else  if((transform->status == xmlSecTransformStatusWorking) && (last != 0)) {
1264
0
        ret = xmlSecOpenSSLRsaOaepProcess(transform);
1265
0
        if(ret < 0) {
1266
0
            xmlSecInternalError("xmlSecOpenSSLRsaOaepProcess",
1267
0
                                xmlSecTransformGetName(transform));
1268
0
            return(-1);
1269
0
        }
1270
0
        transform->status = xmlSecTransformStatusFinished;
1271
0
    } else if(transform->status == xmlSecTransformStatusFinished) {
1272
        /* the only way we can get here is if there is no input */
1273
0
        xmlSecAssert2(xmlSecBufferGetSize(&(transform->inBuf)) == 0, -1);
1274
0
    } else {
1275
0
        xmlSecInvalidTransfromStatusError(transform);
1276
0
        return(-1);
1277
0
    }
1278
0
    return(0);
1279
0
}
1280
1281
static int
1282
0
xmlSecOpenSSLRsaOaepProcess(xmlSecTransformPtr transform) {
1283
0
    xmlSecOpenSSLRsaOaepCtxPtr ctx;
1284
0
    xmlSecBufferPtr in, out;
1285
0
    xmlSecSize inSize, outSize;
1286
0
    int encrypt;
1287
0
    int ret;
1288
1289
0
    xmlSecAssert2(xmlSecOpenSSLRsaOaepCheckId(transform), -1);
1290
0
    xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
1291
0
    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLRsaOaepSize), -1);
1292
1293
0
    ctx = xmlSecOpenSSLRsaOaepGetCtx(transform);
1294
0
    xmlSecAssert2(ctx != NULL, -1);
1295
0
    xmlSecAssert2(ctx->keySize > 0, -1);
1296
1297
0
    in = &(transform->inBuf);
1298
0
    out = &(transform->outBuf);
1299
1300
0
    inSize = xmlSecBufferGetSize(in);
1301
0
    outSize = xmlSecBufferGetSize(out);
1302
0
    xmlSecAssert2(outSize == 0, -1);
1303
1304
    /* the encoded size is equal to the keys size so we could not
1305
     * process more than that */
1306
0
    if (transform->operation == xmlSecTransformOperationEncrypt) {
1307
0
        encrypt = 1;
1308
0
    } else if (transform->operation == xmlSecTransformOperationDecrypt) {
1309
0
        encrypt = 0;
1310
0
    } else {
1311
0
        xmlSecOtherError2(XMLSEC_ERRORS_R_INVALID_OPERATION,
1312
0
            xmlSecTransformGetName(transform),
1313
0
            "Unexpected transform operation: " XMLSEC_ENUM_FMT,
1314
0
            XMLSEC_ENUM_CAST(transform->operation));
1315
0
        return(-1);
1316
0
    }
1317
1318
0
    if((encrypt != 0) && (inSize >= ctx->keySize)) {
1319
0
        xmlSecInvalidSizeLessThanError("Input data", inSize, ctx->keySize,
1320
0
            xmlSecTransformGetName(transform));
1321
0
        return(-1);
1322
0
    } else if((encrypt == 0) && (inSize != ctx->keySize)) {
1323
0
        xmlSecInvalidSizeError("Input data", inSize, ctx->keySize,
1324
0
            xmlSecTransformGetName(transform));
1325
0
        return(-1);
1326
0
    }
1327
1328
0
    outSize = ctx->keySize;
1329
0
    ret = xmlSecBufferSetMaxSize(out, outSize);
1330
0
    if(ret < 0) {
1331
0
        xmlSecInternalError2("xmlSecBufferSetMaxSize",
1332
0
            xmlSecTransformGetName(transform),
1333
0
            "size=" XMLSEC_SIZE_FMT, outSize);
1334
0
        return(-1);
1335
0
    }
1336
1337
0
    ret = xmlSecOpenSSLRsaOaepProcessImpl(ctx, xmlSecBufferGetData(in), inSize,
1338
0
        xmlSecBufferGetData(out), &outSize, encrypt);
1339
0
    if(ret < 0) {
1340
0
        xmlSecInternalError("xmlSecOpenSSLRsaOaepProcessImpl",
1341
0
                            xmlSecTransformGetName(transform));
1342
0
        return(-1);
1343
0
    }
1344
1345
0
    ret = xmlSecBufferSetSize(out, outSize);
1346
0
    if(ret < 0) {
1347
0
        xmlSecInternalError2("xmlSecBufferSetSize",
1348
0
            xmlSecTransformGetName(transform),
1349
0
            "size=" XMLSEC_SIZE_FMT, outSize);
1350
0
        return(-1);
1351
0
    }
1352
1353
0
    ret = xmlSecBufferRemoveHead(in, inSize);
1354
0
    if(ret < 0) {
1355
0
        xmlSecInternalError2("xmlSecBufferRemoveHead",
1356
0
            xmlSecTransformGetName(transform),
1357
0
            "size=" XMLSEC_SIZE_FMT, inSize);
1358
0
        return(-1);
1359
0
    }
1360
1361
0
    return(0);
1362
0
}
1363
#endif /* XMLSEC_NO_RSA_OAEP */
1364
1365
1366
#endif /* XMLSEC_NO_RSA */