Print this page
9642 PKCS#11 softtoken should use explicit_bzero
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Alex Wilson <alex.wilson@joyent.com>


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2012 Milan Jurik. All rights reserved.

  25  */
  26 
  27 #include <stdlib.h>
  28 #include <string.h>
  29 #include <strings.h>
  30 #include <lber.h>
  31 #include <security/cryptoki.h>
  32 #include "softDSA.h"
  33 #include "softDH.h"
  34 #include "softRSA.h"
  35 #include "softObject.h"
  36 #include "softASN1.h"
  37 
  38 #define OID_TAG                 0x06
  39 
  40 #define MAX_DH_KEY      MAX_DH_KEYLENGTH_IN_BYTES       /* bytes in DH key */
  41 static uchar_t  DH_OID[] = {
  42         /* DH key agreement OID:  1 . 2 . 840 . 113549 . 1 . 3 . 1 */
  43         0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01
  44 };


  70  */
  71 static CK_RV
  72 pad_bigint_attr(biginteger_t *src, biginteger_t *dst)
  73 {
  74         int     padding;
  75 
  76         /* Src and dst must already by previously allocated. */
  77         if (src == NULL || dst == NULL)
  78                 return (CKR_HOST_MEMORY);
  79 
  80         if (src->big_value_len == 0) {
  81                 dst->big_value = NULL;
  82                 dst->big_value_len = 0;
  83                 return (CKR_OK);
  84         }
  85         /*
  86          * Realloc() may free() or shrink previous memory location, so
  87          * clear out potentially sensitive data before that happens.
  88          */
  89         if (dst->big_value != NULL)
  90                 (void) memset(dst->big_value, 0x0, dst->big_value_len);
  91 
  92         padding = (src->big_value[0] < 0x80) ? 0 : 1;
  93         dst->big_value_len = src->big_value_len + padding;
  94 
  95         dst->big_value = realloc(dst->big_value, dst->big_value_len);
  96         if (dst->big_value == NULL)
  97                 return (CKR_HOST_MEMORY);
  98 
  99         /* Set zero-pad at first byte, then append actual big_value. */
 100         dst->big_value[0] = 0x0;
 101         (void) memcpy(&(dst->big_value[padding]), src->big_value,
 102             src->big_value_len);
 103         return (CKR_OK);
 104 }
 105 
 106 /*
 107  * Sometimes there is one bytes of zero-padding, if a big integer may
 108  * be interpreted as an ASN.1 negative number (i.e, the first bit is
 109  * non-zero, the first byte is 0x80 or greater).  Remove first byte
 110  * of zero-padding in those cases from the decoded octet strings.


 321          * buf; otherwise the user must resize buf and call again.
 322          * In either case, buf_len is reset to the corrected size.
 323          * See PKCS#11 section 11.2.
 324          */
 325 #ifdef _LP64
 326         /* LINTED E_CAST_INT_TO_SMALL_INT */
 327         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 328 #else
 329         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 330 #endif
 331                 *buf_len = p8obj_octs->bv_len;
 332                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 333                 goto cleanup_rsapri2asn;
 334         }
 335 
 336         *buf_len = p8obj_octs->bv_len;
 337         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 338 
 339 cleanup_rsapri2asn:
 340 
 341         if (tmp_pad.big_value != NULL) {
 342                 (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 343                 free(tmp_pad.big_value);
 344         }
 345 
 346         if (key_asn != NULLBER)
 347                 ber_free(key_asn, 1);
 348 
 349         if (key_octs != NULL)
 350                 ber_bvfree(key_octs);
 351 
 352         if (p8obj_asn != NULLBER)
 353                 ber_free(p8obj_asn, 1);
 354 
 355         if (p8obj_octs != NULL)
 356                 ber_bvfree(p8obj_octs);
 357 
 358         return (rv);
 359 }
 360 
 361 /* Encode DSA private key in ASN.1 BER syntax. */
 362 static CK_RV
 363 dsa_pri_to_asn1(soft_object_t *objp, uchar_t *buf, ulong_t *buf_len)
 364 {


 510          * buf; otherwise the user must resize buf and call again.
 511          * In either case, buf_len is reset to the corrected size.
 512          * See PKCS#11 section 11.2.
 513          */
 514 #ifdef _LP64
 515         /* LINTED E_CAST_INT_TO_SMALL_INT */
 516         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 517 #else
 518         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 519 #endif
 520                 *buf_len = p8obj_octs->bv_len;
 521                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 522                 goto cleanup_dsapri2asn;
 523         }
 524 
 525         *buf_len = p8obj_octs->bv_len;
 526         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 527 
 528 cleanup_dsapri2asn:
 529 
 530         if (tmp_pad.big_value != NULL) {
 531                 (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 532                 free(tmp_pad.big_value);
 533         }
 534 
 535         if (key_asn != NULLBER)
 536                 ber_free(key_asn, 1);
 537 
 538         if (key_octs != NULL)
 539                 ber_bvfree(key_octs);
 540 
 541         if (p8obj_asn != NULLBER)
 542                 ber_free(p8obj_asn, 1);
 543 
 544         if (p8obj_octs != NULL)
 545                 ber_bvfree(p8obj_octs);
 546 
 547         return (rv);
 548 }
 549 
 550 /* Encode DH private key in ASN.1 BER syntax. */
 551 static CK_RV
 552 dh_pri_to_asn1(soft_object_t *objp, uchar_t *buf, ulong_t *buf_len)
 553 {


 684          * buf; otherwise the user must resize buf and call again.
 685          * In either case, buf_len is reset to the corrected size.
 686          * See PKCS#11 section 11.2.
 687          */
 688 #ifdef _LP64
 689         /* LINTED E_CAST_INT_TO_SMALL_INT */
 690         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 691 #else
 692         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 693 #endif
 694                 *buf_len = p8obj_octs->bv_len;
 695                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 696                 goto cleanup_dhpri2asn;
 697         }
 698 
 699         *buf_len = p8obj_octs->bv_len;
 700         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 701 
 702 cleanup_dhpri2asn:
 703 
 704         if (tmp_pad.big_value != NULL) {
 705                 (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 706                 free(tmp_pad.big_value);
 707         }
 708 
 709         if (key_asn != NULLBER)
 710                 ber_free(key_asn, 1);
 711 
 712         if (key_octs != NULL)
 713                 ber_bvfree(key_octs);
 714 
 715         if (p8obj_asn != NULLBER)
 716                 ber_free(p8obj_asn, 1);
 717 
 718         if (p8obj_octs != NULL)
 719                 ber_bvfree(p8obj_octs);
 720 
 721         return (rv);
 722 }
 723 
 724 /* Encode DH X9.42 private key in ASN.1 BER syntax. */
 725 static CK_RV
 726 x942_dh_pri_to_asn1(soft_object_t *objp, uchar_t *buf, ulong_t *buf_len)
 727 {


 876          * buf; otherwise the user must resize buf and call again.
 877          * In either case, buf_len is reset to the corrected size.
 878          * See PKCS#11 section 11.2.
 879          */
 880 #ifdef _LP64
 881         /* LINTED E_CAST_INT_TO_SMALL_INT */
 882         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 883 #else
 884         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 885 #endif
 886                 *buf_len = p8obj_octs->bv_len;
 887                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 888                 goto cleanup_x942dhpri2asn;
 889         }
 890 
 891         *buf_len = p8obj_octs->bv_len;
 892         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 893 
 894 cleanup_x942dhpri2asn:
 895 
 896         if (tmp_pad.big_value != NULL) {
 897                 (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 898                 free(tmp_pad.big_value);
 899         }
 900 
 901         if (key_asn != NULLBER)
 902                 ber_free(key_asn, 1);
 903 
 904         if (key_octs != NULL)
 905                 ber_bvfree(key_octs);
 906 
 907         if (p8obj_asn != NULLBER)
 908                 ber_free(p8obj_asn, 1);
 909 
 910         if (p8obj_octs != NULL)
 911                 ber_bvfree(p8obj_octs);
 912 
 913         return (rv);
 914 }
 915 
 916 /*
 917  * Encode the object key from the soft_object_t into ASN.1 format.
 918  */
 919 CK_RV


1223                 goto error_asn2rsapri;
1224         }
1225         free(tmp.big_value);
1226         copy_bigint_attr(&tmp_nopad, KEY_PRI_RSA_COEF(keyp));
1227 
1228         goto cleanup_asn2rsapri;
1229 
1230 error_asn2rsapri:
1231 
1232         bigint_attr_cleanup(KEY_PRI_RSA_MOD(keyp));
1233         bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(keyp));
1234         bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(keyp));
1235         bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(keyp));
1236         bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(keyp));
1237         bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(keyp));
1238         bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(keyp));
1239         bigint_attr_cleanup(KEY_PRI_RSA_COEF(keyp));
1240 
1241 cleanup_asn2rsapri:
1242 
1243         if (tmp_nopad.big_value != NULL) {
1244                 (void) memset(tmp_nopad.big_value, 0x0,
1245                     tmp_nopad.big_value_len);
1246                 free(tmp_nopad.big_value);
1247         }
1248 
1249         if (p8obj_asn != NULLBER)
1250                 ber_free(p8obj_asn, 1);
1251 
1252         if (key_octs.bv_val != NULL)
1253                 free(key_octs.bv_val);
1254 
1255         if (key_asn != NULLBER)
1256                 ber_free(key_asn, 1);
1257 
1258         return (rv);
1259 }
1260 
1261 /* Decode ASN.1 BER syntax into DSA private key. */
1262 static CK_RV
1263 asn1_to_dsa_pri(private_key_obj_t *keyp, uchar_t *buf, ulong_t buf_len)
1264 {
1265         CK_RV           rv = CKR_OK;
1266         BerValue        p8obj_octs, key_octs;
1267         BerElement      *p8obj_asn = NULLBER, *key_asn = NULLBER;


1431             tmp.big_value, &tmplen);
1432         tmp.big_value_len = tmplen;
1433         if ((rv = unpad_bigint_attr(tmp, &tmp_nopad)) != CKR_OK) {
1434                 free(tmp.big_value);
1435                 goto error_asn2dsapri;
1436         }
1437         free(tmp.big_value);
1438         copy_bigint_attr(&tmp_nopad, KEY_PRI_DSA_VALUE(keyp));
1439 
1440         goto cleanup_asn2dsapri;
1441 
1442 error_asn2dsapri:
1443 
1444         bigint_attr_cleanup(KEY_PRI_DSA_PRIME(keyp));
1445         bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(keyp));
1446         bigint_attr_cleanup(KEY_PRI_DSA_BASE(keyp));
1447         bigint_attr_cleanup(KEY_PRI_DSA_VALUE(keyp));
1448 
1449 cleanup_asn2dsapri:
1450 
1451         if (tmp_nopad.big_value != NULL) {
1452                 (void) memset(tmp_nopad.big_value, 0x0,
1453                     tmp_nopad.big_value_len);
1454                 free(tmp_nopad.big_value);
1455         }
1456 
1457         if (p8obj_asn != NULLBER)
1458                 ber_free(p8obj_asn, 1);
1459 
1460         if (key_octs.bv_val != NULL)
1461                 free(key_octs.bv_val);
1462 
1463         if (key_asn != NULLBER)
1464                 ber_free(key_asn, 1);
1465 
1466         return (rv);
1467 }
1468 
1469 /* Decode ASN.1 BER syntax into DH private key. */
1470 static CK_RV
1471 asn1_to_dh_pri(private_key_obj_t *keyp, uchar_t *buf, ulong_t buf_len)
1472 {
1473         CK_RV           rv = CKR_OK;
1474         BerValue        p8obj_octs, key_octs;
1475         BerElement      *p8obj_asn = NULLBER, *key_asn = NULLBER;


1615         (void) ber_scanf(key_asn, "s",          /* "s}" ? */
1616             tmp.big_value, &tmplen);
1617         tmp.big_value_len = tmplen;
1618         if ((rv = unpad_bigint_attr(tmp, &tmp_nopad)) != CKR_OK) {
1619                 free(tmp.big_value);
1620                 goto error_asn2dhpri;
1621         }
1622         free(tmp.big_value);
1623         copy_bigint_attr(&tmp_nopad, KEY_PRI_DH_VALUE(keyp));
1624 
1625         goto cleanup_asn2dhpri;
1626 
1627 error_asn2dhpri:
1628 
1629         bigint_attr_cleanup(KEY_PRI_DH_PRIME(keyp));
1630         bigint_attr_cleanup(KEY_PRI_DH_BASE(keyp));
1631         bigint_attr_cleanup(KEY_PRI_DH_VALUE(keyp));
1632 
1633 cleanup_asn2dhpri:
1634 
1635         if (tmp_nopad.big_value != NULL) {
1636                 (void) memset(tmp_nopad.big_value, 0x0,
1637                     tmp_nopad.big_value_len);
1638                 free(tmp_nopad.big_value);
1639         }
1640 
1641         if (p8obj_asn != NULLBER)
1642                 ber_free(p8obj_asn, 1);
1643 
1644         if (key_octs.bv_val != NULL)
1645                 free(key_octs.bv_val);
1646 
1647         if (key_asn != NULLBER)
1648                 ber_free(key_asn, 1);
1649 
1650         return (rv);
1651 }
1652 
1653 /* Decode ASN.1 BER syntax into DH X9.42 private key. */
1654 static CK_RV
1655 asn1_to_x942_dh_pri(private_key_obj_t *keyp, uchar_t *buf, ulong_t buf_len)
1656 {
1657         CK_RV           rv = CKR_OK;
1658         BerValue        p8obj_octs, key_octs;
1659         BerElement      *p8obj_asn = NULLBER, *key_asn = NULLBER;


1823             tmp.big_value, &tmplen);
1824         tmp.big_value_len = tmplen;
1825         if ((rv = unpad_bigint_attr(tmp, &tmp_nopad)) != CKR_OK) {
1826                 free(tmp.big_value);
1827                 goto error_asn2x942dhpri;
1828         }
1829         free(tmp.big_value);
1830         copy_bigint_attr(&tmp_nopad, KEY_PRI_DH942_VALUE(keyp));
1831 
1832         goto cleanup_asn2x942dhpri;
1833 
1834 error_asn2x942dhpri:
1835 
1836         bigint_attr_cleanup(KEY_PRI_DH942_PRIME(keyp));
1837         bigint_attr_cleanup(KEY_PRI_DH942_BASE(keyp));
1838         bigint_attr_cleanup(KEY_PRI_DH942_SUBPRIME(keyp));
1839         bigint_attr_cleanup(KEY_PRI_DH942_VALUE(keyp));
1840 
1841 cleanup_asn2x942dhpri:
1842 
1843         if (tmp_nopad.big_value != NULL) {
1844                 (void) memset(tmp_nopad.big_value, 0x0,
1845                     tmp_nopad.big_value_len);
1846                 free(tmp_nopad.big_value);
1847         }
1848 
1849         if (p8obj_asn != NULLBER)
1850                 ber_free(p8obj_asn, 1);
1851 
1852         if (key_octs.bv_val != NULL)
1853                 free(key_octs.bv_val);
1854 
1855         if (key_asn != NULLBER)
1856                 ber_free(key_asn, 1);
1857 
1858         return (rv);
1859 }
1860 
1861 /*
1862  * Decode the object key from ASN.1 format into soft_object_t.
1863  */
1864 CK_RV
1865 soft_asn1_to_object(soft_object_t *objp, uchar_t *buf, ulong_t buf_len)
1866 {
1867         CK_RV           rv = CKR_OK;




   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  * Copyright (c) 2018, Joyent. Inc.
  26  */
  27 
  28 #include <stdlib.h>
  29 #include <string.h>
  30 #include <strings.h>
  31 #include <lber.h>
  32 #include <security/cryptoki.h>
  33 #include "softDSA.h"
  34 #include "softDH.h"
  35 #include "softRSA.h"
  36 #include "softObject.h"
  37 #include "softASN1.h"
  38 
  39 #define OID_TAG                 0x06
  40 
  41 #define MAX_DH_KEY      MAX_DH_KEYLENGTH_IN_BYTES       /* bytes in DH key */
  42 static uchar_t  DH_OID[] = {
  43         /* DH key agreement OID:  1 . 2 . 840 . 113549 . 1 . 3 . 1 */
  44         0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01
  45 };


  71  */
  72 static CK_RV
  73 pad_bigint_attr(biginteger_t *src, biginteger_t *dst)
  74 {
  75         int     padding;
  76 
  77         /* Src and dst must already by previously allocated. */
  78         if (src == NULL || dst == NULL)
  79                 return (CKR_HOST_MEMORY);
  80 
  81         if (src->big_value_len == 0) {
  82                 dst->big_value = NULL;
  83                 dst->big_value_len = 0;
  84                 return (CKR_OK);
  85         }
  86         /*
  87          * Realloc() may free() or shrink previous memory location, so
  88          * clear out potentially sensitive data before that happens.
  89          */
  90         if (dst->big_value != NULL)
  91                 explicit_bzero(dst->big_value, dst->big_value_len);
  92 
  93         padding = (src->big_value[0] < 0x80) ? 0 : 1;
  94         dst->big_value_len = src->big_value_len + padding;
  95 
  96         dst->big_value = realloc(dst->big_value, dst->big_value_len);
  97         if (dst->big_value == NULL)
  98                 return (CKR_HOST_MEMORY);
  99 
 100         /* Set zero-pad at first byte, then append actual big_value. */
 101         dst->big_value[0] = 0x0;
 102         (void) memcpy(&(dst->big_value[padding]), src->big_value,
 103             src->big_value_len);
 104         return (CKR_OK);
 105 }
 106 
 107 /*
 108  * Sometimes there is one bytes of zero-padding, if a big integer may
 109  * be interpreted as an ASN.1 negative number (i.e, the first bit is
 110  * non-zero, the first byte is 0x80 or greater).  Remove first byte
 111  * of zero-padding in those cases from the decoded octet strings.


 322          * buf; otherwise the user must resize buf and call again.
 323          * In either case, buf_len is reset to the corrected size.
 324          * See PKCS#11 section 11.2.
 325          */
 326 #ifdef _LP64
 327         /* LINTED E_CAST_INT_TO_SMALL_INT */
 328         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 329 #else
 330         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 331 #endif
 332                 *buf_len = p8obj_octs->bv_len;
 333                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 334                 goto cleanup_rsapri2asn;
 335         }
 336 
 337         *buf_len = p8obj_octs->bv_len;
 338         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 339 
 340 cleanup_rsapri2asn:
 341 
 342         freezero(tmp_pad.big_value, tmp_pad.big_value_len);



 343 
 344         if (key_asn != NULLBER)
 345                 ber_free(key_asn, 1);
 346 
 347         if (key_octs != NULL)
 348                 ber_bvfree(key_octs);
 349 
 350         if (p8obj_asn != NULLBER)
 351                 ber_free(p8obj_asn, 1);
 352 
 353         if (p8obj_octs != NULL)
 354                 ber_bvfree(p8obj_octs);
 355 
 356         return (rv);
 357 }
 358 
 359 /* Encode DSA private key in ASN.1 BER syntax. */
 360 static CK_RV
 361 dsa_pri_to_asn1(soft_object_t *objp, uchar_t *buf, ulong_t *buf_len)
 362 {


 508          * buf; otherwise the user must resize buf and call again.
 509          * In either case, buf_len is reset to the corrected size.
 510          * See PKCS#11 section 11.2.
 511          */
 512 #ifdef _LP64
 513         /* LINTED E_CAST_INT_TO_SMALL_INT */
 514         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 515 #else
 516         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 517 #endif
 518                 *buf_len = p8obj_octs->bv_len;
 519                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 520                 goto cleanup_dsapri2asn;
 521         }
 522 
 523         *buf_len = p8obj_octs->bv_len;
 524         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 525 
 526 cleanup_dsapri2asn:
 527 
 528         freezero(tmp_pad.big_value, tmp_pad.big_value_len);



 529 
 530         if (key_asn != NULLBER)
 531                 ber_free(key_asn, 1);
 532 
 533         if (key_octs != NULL)
 534                 ber_bvfree(key_octs);
 535 
 536         if (p8obj_asn != NULLBER)
 537                 ber_free(p8obj_asn, 1);
 538 
 539         if (p8obj_octs != NULL)
 540                 ber_bvfree(p8obj_octs);
 541 
 542         return (rv);
 543 }
 544 
 545 /* Encode DH private key in ASN.1 BER syntax. */
 546 static CK_RV
 547 dh_pri_to_asn1(soft_object_t *objp, uchar_t *buf, ulong_t *buf_len)
 548 {


 679          * buf; otherwise the user must resize buf and call again.
 680          * In either case, buf_len is reset to the corrected size.
 681          * See PKCS#11 section 11.2.
 682          */
 683 #ifdef _LP64
 684         /* LINTED E_CAST_INT_TO_SMALL_INT */
 685         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 686 #else
 687         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 688 #endif
 689                 *buf_len = p8obj_octs->bv_len;
 690                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 691                 goto cleanup_dhpri2asn;
 692         }
 693 
 694         *buf_len = p8obj_octs->bv_len;
 695         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 696 
 697 cleanup_dhpri2asn:
 698 
 699         freezero(tmp_pad.big_value, tmp_pad.big_value_len);



 700 
 701         if (key_asn != NULLBER)
 702                 ber_free(key_asn, 1);
 703 
 704         if (key_octs != NULL)
 705                 ber_bvfree(key_octs);
 706 
 707         if (p8obj_asn != NULLBER)
 708                 ber_free(p8obj_asn, 1);
 709 
 710         if (p8obj_octs != NULL)
 711                 ber_bvfree(p8obj_octs);
 712 
 713         return (rv);
 714 }
 715 
 716 /* Encode DH X9.42 private key in ASN.1 BER syntax. */
 717 static CK_RV
 718 x942_dh_pri_to_asn1(soft_object_t *objp, uchar_t *buf, ulong_t *buf_len)
 719 {


 868          * buf; otherwise the user must resize buf and call again.
 869          * In either case, buf_len is reset to the corrected size.
 870          * See PKCS#11 section 11.2.
 871          */
 872 #ifdef _LP64
 873         /* LINTED E_CAST_INT_TO_SMALL_INT */
 874         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 875 #else
 876         if ((buf == NULL) || ((ber_len_t)(*buf_len) < p8obj_octs->bv_len)) {
 877 #endif
 878                 *buf_len = p8obj_octs->bv_len;
 879                 rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 880                 goto cleanup_x942dhpri2asn;
 881         }
 882 
 883         *buf_len = p8obj_octs->bv_len;
 884         (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 885 
 886 cleanup_x942dhpri2asn:
 887 
 888         freezero(tmp_pad.big_value, tmp_pad.big_value_len);



 889 
 890         if (key_asn != NULLBER)
 891                 ber_free(key_asn, 1);
 892 
 893         if (key_octs != NULL)
 894                 ber_bvfree(key_octs);
 895 
 896         if (p8obj_asn != NULLBER)
 897                 ber_free(p8obj_asn, 1);
 898 
 899         if (p8obj_octs != NULL)
 900                 ber_bvfree(p8obj_octs);
 901 
 902         return (rv);
 903 }
 904 
 905 /*
 906  * Encode the object key from the soft_object_t into ASN.1 format.
 907  */
 908 CK_RV


1212                 goto error_asn2rsapri;
1213         }
1214         free(tmp.big_value);
1215         copy_bigint_attr(&tmp_nopad, KEY_PRI_RSA_COEF(keyp));
1216 
1217         goto cleanup_asn2rsapri;
1218 
1219 error_asn2rsapri:
1220 
1221         bigint_attr_cleanup(KEY_PRI_RSA_MOD(keyp));
1222         bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(keyp));
1223         bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(keyp));
1224         bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(keyp));
1225         bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(keyp));
1226         bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(keyp));
1227         bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(keyp));
1228         bigint_attr_cleanup(KEY_PRI_RSA_COEF(keyp));
1229 
1230 cleanup_asn2rsapri:
1231 
1232         freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);




1233 
1234         if (p8obj_asn != NULLBER)
1235                 ber_free(p8obj_asn, 1);
1236 
1237         if (key_octs.bv_val != NULL)
1238                 free(key_octs.bv_val);
1239 
1240         if (key_asn != NULLBER)
1241                 ber_free(key_asn, 1);
1242 
1243         return (rv);
1244 }
1245 
1246 /* Decode ASN.1 BER syntax into DSA private key. */
1247 static CK_RV
1248 asn1_to_dsa_pri(private_key_obj_t *keyp, uchar_t *buf, ulong_t buf_len)
1249 {
1250         CK_RV           rv = CKR_OK;
1251         BerValue        p8obj_octs, key_octs;
1252         BerElement      *p8obj_asn = NULLBER, *key_asn = NULLBER;


1416             tmp.big_value, &tmplen);
1417         tmp.big_value_len = tmplen;
1418         if ((rv = unpad_bigint_attr(tmp, &tmp_nopad)) != CKR_OK) {
1419                 free(tmp.big_value);
1420                 goto error_asn2dsapri;
1421         }
1422         free(tmp.big_value);
1423         copy_bigint_attr(&tmp_nopad, KEY_PRI_DSA_VALUE(keyp));
1424 
1425         goto cleanup_asn2dsapri;
1426 
1427 error_asn2dsapri:
1428 
1429         bigint_attr_cleanup(KEY_PRI_DSA_PRIME(keyp));
1430         bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(keyp));
1431         bigint_attr_cleanup(KEY_PRI_DSA_BASE(keyp));
1432         bigint_attr_cleanup(KEY_PRI_DSA_VALUE(keyp));
1433 
1434 cleanup_asn2dsapri:
1435 
1436         freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);




1437 
1438         if (p8obj_asn != NULLBER)
1439                 ber_free(p8obj_asn, 1);
1440 
1441         if (key_octs.bv_val != NULL)
1442                 free(key_octs.bv_val);
1443 
1444         if (key_asn != NULLBER)
1445                 ber_free(key_asn, 1);
1446 
1447         return (rv);
1448 }
1449 
1450 /* Decode ASN.1 BER syntax into DH private key. */
1451 static CK_RV
1452 asn1_to_dh_pri(private_key_obj_t *keyp, uchar_t *buf, ulong_t buf_len)
1453 {
1454         CK_RV           rv = CKR_OK;
1455         BerValue        p8obj_octs, key_octs;
1456         BerElement      *p8obj_asn = NULLBER, *key_asn = NULLBER;


1596         (void) ber_scanf(key_asn, "s",          /* "s}" ? */
1597             tmp.big_value, &tmplen);
1598         tmp.big_value_len = tmplen;
1599         if ((rv = unpad_bigint_attr(tmp, &tmp_nopad)) != CKR_OK) {
1600                 free(tmp.big_value);
1601                 goto error_asn2dhpri;
1602         }
1603         free(tmp.big_value);
1604         copy_bigint_attr(&tmp_nopad, KEY_PRI_DH_VALUE(keyp));
1605 
1606         goto cleanup_asn2dhpri;
1607 
1608 error_asn2dhpri:
1609 
1610         bigint_attr_cleanup(KEY_PRI_DH_PRIME(keyp));
1611         bigint_attr_cleanup(KEY_PRI_DH_BASE(keyp));
1612         bigint_attr_cleanup(KEY_PRI_DH_VALUE(keyp));
1613 
1614 cleanup_asn2dhpri:
1615 
1616         freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);




1617 
1618         if (p8obj_asn != NULLBER)
1619                 ber_free(p8obj_asn, 1);
1620 
1621         if (key_octs.bv_val != NULL)
1622                 free(key_octs.bv_val);
1623 
1624         if (key_asn != NULLBER)
1625                 ber_free(key_asn, 1);
1626 
1627         return (rv);
1628 }
1629 
1630 /* Decode ASN.1 BER syntax into DH X9.42 private key. */
1631 static CK_RV
1632 asn1_to_x942_dh_pri(private_key_obj_t *keyp, uchar_t *buf, ulong_t buf_len)
1633 {
1634         CK_RV           rv = CKR_OK;
1635         BerValue        p8obj_octs, key_octs;
1636         BerElement      *p8obj_asn = NULLBER, *key_asn = NULLBER;


1800             tmp.big_value, &tmplen);
1801         tmp.big_value_len = tmplen;
1802         if ((rv = unpad_bigint_attr(tmp, &tmp_nopad)) != CKR_OK) {
1803                 free(tmp.big_value);
1804                 goto error_asn2x942dhpri;
1805         }
1806         free(tmp.big_value);
1807         copy_bigint_attr(&tmp_nopad, KEY_PRI_DH942_VALUE(keyp));
1808 
1809         goto cleanup_asn2x942dhpri;
1810 
1811 error_asn2x942dhpri:
1812 
1813         bigint_attr_cleanup(KEY_PRI_DH942_PRIME(keyp));
1814         bigint_attr_cleanup(KEY_PRI_DH942_BASE(keyp));
1815         bigint_attr_cleanup(KEY_PRI_DH942_SUBPRIME(keyp));
1816         bigint_attr_cleanup(KEY_PRI_DH942_VALUE(keyp));
1817 
1818 cleanup_asn2x942dhpri:
1819 
1820         freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);




1821 
1822         if (p8obj_asn != NULLBER)
1823                 ber_free(p8obj_asn, 1);
1824 
1825         if (key_octs.bv_val != NULL)
1826                 free(key_octs.bv_val);
1827 
1828         if (key_asn != NULLBER)
1829                 ber_free(key_asn, 1);
1830 
1831         return (rv);
1832 }
1833 
1834 /*
1835  * Decode the object key from ASN.1 format into soft_object_t.
1836  */
1837 CK_RV
1838 soft_asn1_to_object(soft_object_t *objp, uchar_t *buf, ulong_t buf_len)
1839 {
1840         CK_RV           rv = CKR_OK;