Adapt diverse EVP_CIPHER functions to use get_params and set_params interfaces
[openssl.git] / crypto / evp / evp_lib.c
1 /*
2  * Copyright 1995-2018 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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "internal/evp_int.h"
17 #include "internal/provider.h"
18 #include "evp_locl.h"
19
20 #if !defined(FIPS_MODE)
21 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
22 {
23     int ret;
24     const EVP_CIPHER *cipher = c->cipher;
25
26     if (cipher->prov != NULL) {
27         /*
28          * The cipher has come from a provider and won't have the default flags.
29          * Find the implicit form so we can check the flags.
30          * TODO(3.0): This won't work for 3rd party ciphers we know nothing about
31          * We'll need to think of something else for those.
32          */
33         cipher = EVP_get_cipherbynid(cipher->nid);
34         if (cipher == NULL) {
35             EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER);
36             return -1;
37         }
38     }
39
40     if (cipher->set_asn1_parameters != NULL)
41         ret = cipher->set_asn1_parameters(c, type);
42     else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
43         switch (EVP_CIPHER_mode(cipher)) {
44         case EVP_CIPH_WRAP_MODE:
45             if (EVP_CIPHER_nid(cipher) == NID_id_smime_alg_CMS3DESwrap)
46                 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
47             ret = 1;
48             break;
49
50         case EVP_CIPH_GCM_MODE:
51         case EVP_CIPH_CCM_MODE:
52         case EVP_CIPH_XTS_MODE:
53         case EVP_CIPH_OCB_MODE:
54             ret = -2;
55             break;
56
57         default:
58             ret = EVP_CIPHER_set_asn1_iv(c, type);
59         }
60     } else
61         ret = -1;
62     if (ret <= 0)
63         EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ?
64                ASN1_R_UNSUPPORTED_CIPHER :
65                EVP_R_CIPHER_PARAMETER_ERROR);
66     if (ret < -1)
67         ret = -1;
68     return ret;
69 }
70
71 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
72 {
73     int ret;
74     const EVP_CIPHER *cipher = c->cipher;
75
76     if (cipher->prov != NULL) {
77         /*
78          * The cipher has come from a provider and won't have the default flags.
79          * Find the implicit form so we can check the flags.
80          */
81         cipher = EVP_get_cipherbynid(cipher->nid);
82         if (cipher == NULL)
83             return -1;
84     }
85
86     if (cipher->get_asn1_parameters != NULL)
87         ret = cipher->get_asn1_parameters(c, type);
88     else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
89         switch (EVP_CIPHER_mode(cipher)) {
90
91         case EVP_CIPH_WRAP_MODE:
92             ret = 1;
93             break;
94
95         case EVP_CIPH_GCM_MODE:
96         case EVP_CIPH_CCM_MODE:
97         case EVP_CIPH_XTS_MODE:
98         case EVP_CIPH_OCB_MODE:
99             ret = -2;
100             break;
101
102         default:
103             ret = EVP_CIPHER_get_asn1_iv(c, type);
104             break;
105         }
106     } else
107         ret = -1;
108     if (ret <= 0)
109         EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ?
110                EVP_R_UNSUPPORTED_CIPHER :
111                EVP_R_CIPHER_PARAMETER_ERROR);
112     if (ret < -1)
113         ret = -1;
114     return ret;
115 }
116
117 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
118 {
119     int i = 0;
120     unsigned int l;
121
122     if (type != NULL) {
123         unsigned char iv[EVP_MAX_IV_LENGTH];
124
125         l = EVP_CIPHER_CTX_iv_length(ctx);
126         if (!ossl_assert(l <= sizeof(iv)))
127             return -1;
128         i = ASN1_TYPE_get_octetstring(type, iv, l);
129         if (i != (int)l)
130             return -1;
131
132         if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
133             return -1;
134     }
135     return i;
136 }
137
138 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
139 {
140     int i = 0;
141     unsigned int j;
142
143     if (type != NULL) {
144         j = EVP_CIPHER_CTX_iv_length(c);
145         OPENSSL_assert(j <= sizeof(c->iv));
146         i = ASN1_TYPE_set_octetstring(type, c->oiv, j);
147     }
148     return i;
149 }
150 #endif /* !defined(FIPS_MODE) */
151
152 /* Convert the various cipher NIDs and dummies to a proper OID NID */
153 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
154 {
155     int nid;
156     nid = EVP_CIPHER_nid(ctx);
157
158     switch (nid) {
159
160     case NID_rc2_cbc:
161     case NID_rc2_64_cbc:
162     case NID_rc2_40_cbc:
163
164         return NID_rc2_cbc;
165
166     case NID_rc4:
167     case NID_rc4_40:
168
169         return NID_rc4;
170
171     case NID_aes_128_cfb128:
172     case NID_aes_128_cfb8:
173     case NID_aes_128_cfb1:
174
175         return NID_aes_128_cfb128;
176
177     case NID_aes_192_cfb128:
178     case NID_aes_192_cfb8:
179     case NID_aes_192_cfb1:
180
181         return NID_aes_192_cfb128;
182
183     case NID_aes_256_cfb128:
184     case NID_aes_256_cfb8:
185     case NID_aes_256_cfb1:
186
187         return NID_aes_256_cfb128;
188
189     case NID_des_cfb64:
190     case NID_des_cfb8:
191     case NID_des_cfb1:
192
193         return NID_des_cfb64;
194
195     case NID_des_ede3_cfb64:
196     case NID_des_ede3_cfb8:
197     case NID_des_ede3_cfb1:
198
199         return NID_des_cfb64;
200
201     default:
202 #ifdef FIPS_MODE
203         return NID_undef;
204 #else
205         {
206             /* Check it has an OID and it is valid */
207             ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
208
209             if (OBJ_get0_data(otmp) == NULL)
210                 nid = NID_undef;
211             ASN1_OBJECT_free(otmp);
212             return nid;
213         }
214 #endif
215     }
216 }
217
218 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
219 {
220     int v = cipher->block_size;
221     int ok = evp_do_param(cipher, &v, sizeof(v),
222                           OSSL_CIPHER_PARAM_BLOCK_SIZE, OSSL_PARAM_INTEGER,
223                           evp_do_ciph_getparams, NULL);
224
225     return ok != 0 ? v : -1;
226 }
227
228 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
229 {
230     return EVP_CIPHER_block_size(ctx->cipher);
231 }
232
233 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
234 {
235     return e->ctx_size;
236 }
237
238 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
239                const unsigned char *in, unsigned int inl)
240 {
241     if (ctx->cipher->prov != NULL) {
242         size_t outl = 0;         /* ignored */
243         int blocksize = EVP_CIPHER_CTX_block_size(ctx);
244
245         if (ctx->cipher->ccipher != NULL)
246             return
247                 ctx->cipher->ccipher(ctx->provctx, out, &outl,
248                                      inl + (blocksize == 1 ? 0 : blocksize),
249                                      in, (size_t)inl);
250         return 0;
251     }
252
253     return ctx->cipher->do_cipher(ctx, out, in, inl);
254 }
255
256 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
257 {
258     return ctx->cipher;
259 }
260
261 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
262 {
263     return ctx->encrypt;
264 }
265
266 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
267 {
268     unsigned long v = cipher->flags;
269     int ok = evp_do_param(cipher, &v, sizeof(v),
270                           OSSL_CIPHER_PARAM_FLAGS, OSSL_PARAM_UNSIGNED_INTEGER,
271                           evp_do_ciph_getparams, NULL);
272
273     return ok != 0 ? v : 0;
274 }
275
276 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
277 {
278     return ctx->app_data;
279 }
280
281 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
282 {
283     ctx->app_data = data;
284 }
285
286 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
287 {
288     return ctx->cipher_data;
289 }
290
291 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
292 {
293     void *old_cipher_data;
294
295     old_cipher_data = ctx->cipher_data;
296     ctx->cipher_data = cipher_data;
297
298     return old_cipher_data;
299 }
300
301 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
302 {
303     int v = cipher->iv_len;
304     int ok = evp_do_param(cipher, &v, sizeof(v),
305                           OSSL_CIPHER_PARAM_IVLEN, OSSL_PARAM_UNSIGNED_INTEGER,
306                           evp_do_ciph_getparams, NULL);
307
308     return ok != 0 ? v: -1;
309 }
310
311 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
312 {
313     return EVP_CIPHER_iv_length(ctx->cipher);
314 }
315
316 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
317 {
318     return ctx->oiv;
319 }
320
321 /*
322  * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
323  */
324 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
325 {
326     const unsigned char *v = ctx->iv;
327     int ok = evp_do_param(ctx->cipher, &v, sizeof(ctx->iv),
328                           OSSL_CIPHER_PARAM_IV, OSSL_PARAM_OCTET_PTR,
329                           evp_do_ciph_ctx_getparams, ctx->provctx);
330
331     return ok != 0 ? v: NULL;
332 }
333
334 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
335 {
336     unsigned char *v = ctx->iv;
337     int ok = evp_do_param(ctx->cipher, &v, sizeof(ctx->iv),
338                           OSSL_CIPHER_PARAM_IV, OSSL_PARAM_OCTET_PTR,
339                           evp_do_ciph_ctx_getparams, ctx->provctx);
340
341     return ok != 0 ? v: NULL;
342 }
343
344 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
345 {
346     return ctx->buf;
347 }
348
349 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
350 {
351     int v = ctx->num;
352     int ok = evp_do_param(ctx->cipher, &v, sizeof(v),
353                           OSSL_CIPHER_PARAM_NUM, OSSL_PARAM_INTEGER,
354                           evp_do_ciph_ctx_getparams, ctx->provctx);
355
356     return ok != 0 ? v: -1;
357 }
358
359 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
360 {
361     int ok = evp_do_param(ctx->cipher, &num, sizeof(num),
362                           OSSL_CIPHER_PARAM_NUM, OSSL_PARAM_INTEGER,
363                           evp_do_ciph_ctx_setparams, ctx->provctx);
364
365     ctx->num = num;
366     return ok != 0;
367 }
368
369 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
370 {
371     int v = cipher->key_len;
372     int ok = evp_do_param(cipher, &v, sizeof(v),
373                           OSSL_CIPHER_PARAM_KEYLEN, OSSL_PARAM_INTEGER,
374                           evp_do_ciph_getparams, NULL);
375
376     return ok != 0 ? v: -1;
377 }
378
379 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
380 {
381     int v = ctx->key_len;
382     int ok = evp_do_param(ctx->cipher, &v, sizeof(v),
383                           OSSL_CIPHER_PARAM_KEYLEN, OSSL_PARAM_INTEGER,
384                           evp_do_ciph_ctx_getparams, ctx->provctx);
385
386     return ok != 0 ? v: -1;
387 }
388
389 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
390 {
391     return cipher->nid;
392 }
393
394 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
395 {
396     return ctx->cipher->nid;
397 }
398
399 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
400 {
401     int v = EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
402     int ok = evp_do_param(cipher, &v, sizeof(v),
403                           OSSL_CIPHER_PARAM_MODE, OSSL_PARAM_INTEGER,
404                           evp_do_ciph_getparams, NULL);
405
406     return ok != 0 ? v: 0;
407 }
408
409
410 int EVP_MD_block_size(const EVP_MD *md)
411 {
412     if (md == NULL) {
413         EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
414         return -1;
415     }
416
417     if (md->prov != NULL && md->dblock_size != NULL)
418         return (int)md->dblock_size();
419
420     return md->block_size;
421 }
422
423 int EVP_MD_type(const EVP_MD *md)
424 {
425     return md->type;
426 }
427
428 int EVP_MD_pkey_type(const EVP_MD *md)
429 {
430     return md->pkey_type;
431 }
432
433 int EVP_MD_size(const EVP_MD *md)
434 {
435     if (!md) {
436         EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
437         return -1;
438     }
439
440     if (md->prov != NULL && md->size != NULL)
441         return (int)md->size();
442
443     return md->md_size;
444 }
445
446 unsigned long EVP_MD_flags(const EVP_MD *md)
447 {
448     return md->flags;
449 }
450
451 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
452 {
453     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
454
455     if (md != NULL) {
456         md->type = md_type;
457         md->pkey_type = pkey_type;
458         md->lock = CRYPTO_THREAD_lock_new();
459         if (md->lock == NULL) {
460             OPENSSL_free(md);
461             return NULL;
462         }
463         md->refcnt = 1;
464     }
465     return md;
466 }
467
468 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
469 {
470     EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
471
472     if (to != NULL) {
473         CRYPTO_RWLOCK *lock = to->lock;
474         memcpy(to, md, sizeof(*to));
475         to->lock = lock;
476     }
477     return to;
478 }
479
480 int EVP_MD_up_ref(EVP_MD *md)
481 {
482     int ref = 0;
483
484     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
485     return 1;
486 }
487
488 void EVP_MD_meth_free(EVP_MD *md)
489 {
490     if (md != NULL) {
491         int i;
492
493         CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
494         if (i > 0)
495             return;
496         ossl_provider_free(md->prov);
497         CRYPTO_THREAD_lock_free(md->lock);
498         OPENSSL_free(md);
499     }
500 }
501 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
502 {
503     md->block_size = blocksize;
504     return 1;
505 }
506 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
507 {
508     md->md_size = resultsize;
509     return 1;
510 }
511 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
512 {
513     md->ctx_size = datasize;
514     return 1;
515 }
516 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
517 {
518     md->flags = flags;
519     return 1;
520 }
521 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
522 {
523     md->init = init;
524     return 1;
525 }
526 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
527                                                      const void *data,
528                                                      size_t count))
529 {
530     md->update = update;
531     return 1;
532 }
533 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
534                                                    unsigned char *md))
535 {
536     md->final = final;
537     return 1;
538 }
539 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
540                                                  const EVP_MD_CTX *from))
541 {
542     md->copy = copy;
543     return 1;
544 }
545 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
546 {
547     md->cleanup = cleanup;
548     return 1;
549 }
550 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
551                                                  int p1, void *p2))
552 {
553     md->md_ctrl = ctrl;
554     return 1;
555 }
556
557 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
558 {
559     return md->block_size;
560 }
561 int EVP_MD_meth_get_result_size(const EVP_MD *md)
562 {
563     return md->md_size;
564 }
565 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
566 {
567     return md->ctx_size;
568 }
569 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
570 {
571     return md->flags;
572 }
573 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
574 {
575     return md->init;
576 }
577 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
578                                                 const void *data,
579                                                 size_t count)
580 {
581     return md->update;
582 }
583 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
584                                                unsigned char *md)
585 {
586     return md->final;
587 }
588 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
589                                               const EVP_MD_CTX *from)
590 {
591     return md->copy;
592 }
593 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
594 {
595     return md->cleanup;
596 }
597 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
598                                               int p1, void *p2)
599 {
600     return md->md_ctrl;
601 }
602
603 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
604 {
605     if (ctx == NULL)
606         return NULL;
607     return ctx->reqdigest;
608 }
609
610 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
611 {
612     return ctx->pctx;
613 }
614
615 #if !defined(FIPS_MODE)
616 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
617 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
618 {
619     /*
620      * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
621      * we have to deal with the cleanup job here.
622      */
623     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
624         EVP_PKEY_CTX_free(ctx->pctx);
625
626     ctx->pctx = pctx;
627
628     if (pctx != NULL) {
629         /* make sure pctx is not freed when destroying EVP_MD_CTX */
630         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
631     } else {
632         EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
633     }
634 }
635 #endif /* !defined(FIPS_MODE) */
636
637 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
638 {
639     return ctx->md_data;
640 }
641
642 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
643                                              const void *data, size_t count)
644 {
645     return ctx->update;
646 }
647
648 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
649                               int (*update) (EVP_MD_CTX *ctx,
650                                              const void *data, size_t count))
651 {
652     ctx->update = update;
653 }
654
655 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
656 {
657     ctx->flags |= flags;
658 }
659
660 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
661 {
662     ctx->flags &= ~flags;
663 }
664
665 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
666 {
667     return (ctx->flags & flags);
668 }
669
670 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
671 {
672     ctx->flags |= flags;
673 }
674
675 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
676 {
677     ctx->flags &= ~flags;
678 }
679
680 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
681 {
682     return (ctx->flags & flags);
683 }
684
685 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
686                  void *ctx, int cmd, const char *value)
687 {
688     size_t len;
689
690     len = strlen(value);
691     if (len > INT_MAX)
692         return -1;
693     return cb(ctx, cmd, (void *)value, len);
694 }
695
696 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
697                  void *ctx, int cmd, const char *hex)
698 {
699     unsigned char *bin;
700     long binlen;
701     int rv = -1;
702
703     bin = OPENSSL_hexstr2buf(hex, &binlen);
704     if (bin == NULL)
705         return 0;
706     if (binlen <= INT_MAX)
707         rv = cb(ctx, cmd, bin, binlen);
708     OPENSSL_free(bin);
709     return rv;
710 }