Print this page
8982 Support building with OpenSSL 1.1

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libkmf/plugins/kmf_openssl/common/openssl_spi.c
          +++ new/usr/src/lib/libkmf/plugins/kmf_openssl/common/openssl_spi.c
   1    1  /*
   2    2   * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
   3    3   *
   4    4   * Use is subject to license terms.
   5    5   */
   6    6  /*
   7    7   * Copyright (c) 2012, OmniTI Computer Consulting, Inc. All rights reserved.
        8 + * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
   8    9   */
   9   10  /*
  10   11   * Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  11   12   * project 2000.
  12   13   */
  13   14  /*
  14   15   * ====================================================================
  15   16   * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
  16   17   *
  17   18   * Redistribution and use in source and binary forms, with or without
↓ open down ↓ 55 lines elided ↑ open up ↑
  73   74  #include <dirent.h>
  74   75  #include <cryptoutil.h>
  75   76  #include <synch.h>
  76   77  #include <thread.h>
  77   78  
  78   79  /* OPENSSL related headers */
  79   80  #include <openssl/bio.h>
  80   81  #include <openssl/bn.h>
  81   82  #include <openssl/asn1.h>
  82   83  #include <openssl/err.h>
  83      -#include <openssl/bn.h>
  84   84  #include <openssl/x509.h>
  85   85  #include <openssl/rsa.h>
  86   86  #include <openssl/dsa.h>
  87   87  #include <openssl/x509v3.h>
  88   88  #include <openssl/objects.h>
  89   89  #include <openssl/pem.h>
  90   90  #include <openssl/pkcs12.h>
  91   91  #include <openssl/ocsp.h>
  92   92  #include <openssl/des.h>
  93   93  #include <openssl/rand.h>
       94 +#include "compat.h"
  94   95  
  95   96  #define PRINT_ANY_EXTENSION (\
  96   97          KMF_X509_EXT_KEY_USAGE |\
  97   98          KMF_X509_EXT_CERT_POLICIES |\
  98   99          KMF_X509_EXT_SUBJALTNAME |\
  99  100          KMF_X509_EXT_BASIC_CONSTRAINTS |\
 100  101          KMF_X509_EXT_NAME_CONSTRAINTS |\
 101  102          KMF_X509_EXT_POLICY_CONSTRAINTS |\
 102  103          KMF_X509_EXT_EXT_KEY_USAGE |\
 103  104          KMF_X509_EXT_INHIBIT_ANY_POLICY |\
↓ open down ↓ 26 lines elided ↑ open up ↑
 130  131          0x02 };
 131  132  
 132  133  #define SET_ERROR(h, c) h->lasterr.kstype = KMF_KEYSTORE_OPENSSL; \
 133  134          h->lasterr.errcode = c;
 134  135  
 135  136  #define SET_SYS_ERROR(h, c) h->lasterr.kstype = -1; h->lasterr.errcode = c;
 136  137  
 137  138  /*
 138  139   * Declare some new macros for managing stacks of EVP_PKEYS.
 139  140   */
      141 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
 140  142  DECLARE_STACK_OF(EVP_PKEY)
 141  143  
 142  144  #define sk_EVP_PKEY_new_null() SKM_sk_new_null(EVP_PKEY)
 143  145  #define sk_EVP_PKEY_free(st) SKM_sk_free(EVP_PKEY, (st))
 144  146  #define sk_EVP_PKEY_num(st) SKM_sk_num(EVP_PKEY, (st))
 145  147  #define sk_EVP_PKEY_value(st, i) SKM_sk_value(EVP_PKEY, (st), (i))
 146  148  #define sk_EVP_PKEY_push(st, val) SKM_sk_push(EVP_PKEY, (st), (val))
 147  149  #define sk_EVP_PKEY_pop_free(st, free_func) SKM_sk_pop_free(EVP_PKEY, (st), \
 148  150          (free_func))
 149  151  
      152 +#else
      153 +/* LINTED E_STATIC_UNUSED */
      154 +DEFINE_STACK_OF(EVP_PKEY)
      155 +#endif
      156 +
 150  157  mutex_t init_lock = DEFAULTMUTEX;
 151  158  static int ssl_initialized = 0;
 152  159  static BIO *bio_err = NULL;
 153  160  
 154  161  static int
 155  162  test_for_file(char *, mode_t);
 156  163  static KMF_RETURN
 157  164  openssl_parse_bag(PKCS12_SAFEBAG *, char *, int,
 158  165      STACK_OF(EVP_PKEY) *, STACK_OF(X509) *);
 159  166  
↓ open down ↓ 126 lines elided ↑ open up ↑
 286  293          OpenSSL_FindPrikeyByCert,
 287  294          OpenSSL_DecryptData,
 288  295          OpenSSL_ExportPK12,
 289  296          OpenSSL_CreateSymKey,
 290  297          OpenSSL_GetSymKeyValue,
 291  298          NULL,   /* SetTokenPin */
 292  299          OpenSSL_StoreKey,
 293  300          NULL    /* Finalize */
 294  301  };
 295  302  
      303 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
 296  304  static mutex_t *lock_cs;
 297  305  static long *lock_count;
 298  306  
 299  307  static void
 300  308  /* ARGSUSED1 */
 301  309  locking_cb(int mode, int type, char *file, int line)
 302  310  {
 303  311          if (mode & CRYPTO_LOCK) {
 304  312                  (void) mutex_lock(&(lock_cs[type]));
 305  313                  lock_count[type]++;
 306  314          } else {
 307  315                  (void) mutex_unlock(&(lock_cs[type]));
 308  316          }
 309  317  }
 310  318  
 311  319  static unsigned long
 312  320  thread_id()
 313  321  {
 314  322          return ((unsigned long)thr_self());
 315  323  }
      324 +#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
 316  325  
 317  326  KMF_PLUGIN_FUNCLIST *
 318  327  KMF_Plugin_Initialize()
 319  328  {
      329 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
 320  330          int i;
      331 +#endif
 321  332  
 322  333          (void) mutex_lock(&init_lock);
 323  334          if (!ssl_initialized) {
 324  335                  /*
 325  336                   * Add support for extension OIDs that are not yet in the
 326  337                   * openssl default set.
 327  338                   */
 328  339                  (void) OBJ_create("2.5.29.30", "nameConstraints",
 329  340                      "X509v3 Name Constraints");
 330  341                  (void) OBJ_create("2.5.29.33", "policyMappings",
 331  342                      "X509v3 Policy Mappings");
 332  343                  (void) OBJ_create("2.5.29.36", "policyConstraints",
 333  344                      "X509v3 Policy Constraints");
 334  345                  (void) OBJ_create("2.5.29.46", "freshestCRL",
 335  346                      "X509v3 Freshest CRL");
 336  347                  (void) OBJ_create("2.5.29.54", "inhibitAnyPolicy",
 337  348                      "X509v3 Inhibit Any-Policy");
      349 +
      350 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
 338  351                  /*
 339  352                   * Set up for thread-safe operation.
      353 +                 * This is not required for OpenSSL 1.1
 340  354                   */
 341  355                  lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof (mutex_t));
 342  356                  if (lock_cs == NULL) {
 343  357                          (void) mutex_unlock(&init_lock);
 344  358                          return (NULL);
 345  359                  }
 346  360  
 347  361                  lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof (long));
 348  362                  if (lock_count == NULL) {
 349  363                          OPENSSL_free(lock_cs);
↓ open down ↓ 3 lines elided ↑ open up ↑
 353  367  
 354  368                  for (i = 0; i < CRYPTO_num_locks(); i++) {
 355  369                          lock_count[i] = 0;
 356  370                          (void) mutex_init(&lock_cs[i], USYNC_THREAD, NULL);
 357  371                  }
 358  372  
 359  373                  CRYPTO_set_id_callback((unsigned long (*)())thread_id);
 360  374                  if (CRYPTO_get_locking_callback() == NULL)
 361  375                          CRYPTO_set_locking_callback((void (*)())locking_cb);
 362  376  
 363      -                OpenSSL_add_all_algorithms();
      377 +                (void) OpenSSL_add_all_algorithms();
 364  378  
 365  379                  /* Enable error strings for reporting */
 366      -                ERR_load_crypto_strings();
      380 +                (void) ERR_load_crypto_strings();
      381 +#endif
 367  382  
 368  383                  ssl_initialized = 1;
 369  384          }
 370  385          (void) mutex_unlock(&init_lock);
 371  386  
 372  387          return (&openssl_plugin_table);
 373  388  }
      389 +
 374  390  /*
 375  391   * Convert an SSL DN to a KMF DN.
 376  392   */
 377  393  static KMF_RETURN
 378  394  get_x509_dn(X509_NAME *sslDN, KMF_X509_NAME *kmfDN)
 379  395  {
 380  396          KMF_DATA derdata;
 381  397          KMF_RETURN rv = KMF_OK;
 382  398          uchar_t *tmp;
 383  399  
↓ open down ↓ 94 lines elided ↑ open up ↑
 478  494          (void) memset(&issuerDN, 0, sizeof (KMF_X509_NAME));
 479  495          (void) memset(&subjectDN, 0, sizeof (KMF_X509_NAME));
 480  496          (void) memset(&certIssuerDN, 0, sizeof (KMF_X509_NAME));
 481  497          (void) memset(&certSubjectDN, 0, sizeof (KMF_X509_NAME));
 482  498  
 483  499          if (issuer != NULL && strlen(issuer)) {
 484  500                  rv = kmf_dn_parser(issuer, &issuerDN);
 485  501                  if (rv != KMF_OK)
 486  502                          return (KMF_ERR_BAD_PARAMETER);
 487  503  
 488      -                rv = get_x509_dn(xcert->cert_info->issuer, &certIssuerDN);
      504 +                rv = get_x509_dn(X509_get_issuer_name(xcert), &certIssuerDN);
 489  505                  if (rv != KMF_OK) {
 490  506                          kmf_free_dn(&issuerDN);
 491  507                          return (KMF_ERR_BAD_PARAMETER);
 492  508                  }
 493  509  
 494  510                  findIssuer = TRUE;
 495  511          }
 496  512          if (subject != NULL && strlen(subject)) {
 497  513                  rv = kmf_dn_parser(subject, &subjectDN);
 498  514                  if (rv != KMF_OK) {
 499  515                          rv = KMF_ERR_BAD_PARAMETER;
 500  516                          goto cleanup;
 501  517                  }
 502  518  
 503      -                rv = get_x509_dn(xcert->cert_info->subject, &certSubjectDN);
      519 +                rv = get_x509_dn(X509_get_subject_name(xcert), &certSubjectDN);
 504  520                  if (rv != KMF_OK) {
 505  521                          rv = KMF_ERR_BAD_PARAMETER;
 506  522                          goto cleanup;
 507  523                  }
 508  524                  findSubject = TRUE;
 509  525          }
 510  526          if (serial != NULL && serial->val != NULL)
 511  527                  findSerial = TRUE;
 512  528  
 513  529          if (findSerial) {
 514  530                  BIGNUM *bn;
 515  531  
 516  532                  /* Comparing BIGNUMs is a pain! */
 517      -                bn = ASN1_INTEGER_to_BN(xcert->cert_info->serialNumber, NULL);
      533 +                bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(xcert), NULL);
 518  534                  if (bn != NULL) {
 519  535                          int bnlen = BN_num_bytes(bn);
 520  536  
 521  537                          if (bnlen == serial->len) {
 522  538                                  uchar_t *a = malloc(bnlen);
 523  539                                  if (a == NULL) {
 524  540                                          rv = KMF_ERR_MEMORY;
 525  541                                          BN_free(bn);
 526  542                                          goto cleanup;
 527  543                                  }
↓ open down ↓ 885 lines elided ↑ open up ↑
1413 1429          RSA *rsa;
1414 1430          DSA *dsa;
1415 1431  
1416 1432          if (pkey == NULL || out == NULL)
1417 1433                  return (KMF_ERR_BAD_PARAMETER);
1418 1434  
1419 1435          switch (format) {
1420 1436                  case KMF_FORMAT_RAWKEY:
1421 1437                          /* same as ASN.1 */
1422 1438                  case KMF_FORMAT_ASN1:
1423      -                        if (pkey->type == EVP_PKEY_RSA) {
1424      -                                rsa = EVP_PKEY_get1_RSA(pkey);
     1439 +                        if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) {
1425 1440                                  if (private)
1426 1441                                          rv = i2d_RSAPrivateKey_bio(out, rsa);
1427 1442                                  else
1428 1443                                          rv = i2d_RSAPublicKey_bio(out, rsa);
1429      -                                RSA_free(rsa);
1430      -                        } else if (pkey->type == EVP_PKEY_DSA) {
1431      -                                dsa = EVP_PKEY_get1_DSA(pkey);
     1444 +                        } else if ((dsa = EVP_PKEY_get0_DSA(pkey)) != NULL) {
1432 1445                                  rv = i2d_DSAPrivateKey_bio(out, dsa);
1433      -                                DSA_free(dsa);
1434 1446                          }
1435 1447                          if (rv == 1) {
1436 1448                                  rv = KMF_OK;
1437 1449                          } else {
1438 1450                                  SET_ERROR(kmfh, rv);
1439 1451                          }
1440 1452                          break;
1441 1453                  case KMF_FORMAT_PEM:
1442      -                        if (pkey->type == EVP_PKEY_RSA) {
1443      -                                rsa = EVP_PKEY_get1_RSA(pkey);
     1454 +                        if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) {
1444 1455                                  if (private)
1445 1456                                          rv = PEM_write_bio_RSAPrivateKey(out,
1446 1457                                              rsa, NULL, NULL, 0, NULL,
1447 1458                                              (cred != NULL ? cred->cred : NULL));
1448 1459                                  else
1449 1460                                          rv = PEM_write_bio_RSAPublicKey(out,
1450 1461                                              rsa);
1451      -                                RSA_free(rsa);
1452      -                        } else if (pkey->type == EVP_PKEY_DSA) {
1453      -                                dsa = EVP_PKEY_get1_DSA(pkey);
     1462 +                        } else if ((dsa = EVP_PKEY_get0_DSA(pkey)) != NULL) {
1454 1463                                  rv = PEM_write_bio_DSAPrivateKey(out,
1455 1464                                      dsa, NULL, NULL, 0, NULL,
1456 1465                                      (cred != NULL ? cred->cred : NULL));
1457      -                                DSA_free(dsa);
1458 1466                          }
1459 1467  
1460 1468                          if (rv == 1) {
1461 1469                                  rv = KMF_OK;
1462 1470                          } else {
1463 1471                                  SET_ERROR(kmfh, rv);
1464 1472                          }
1465 1473                          break;
1466 1474  
1467 1475                  default:
↓ open down ↓ 2 lines elided ↑ open up ↑
1470 1478  
1471 1479          return (rv);
1472 1480  }
1473 1481  
1474 1482  KMF_RETURN
1475 1483  OpenSSL_CreateKeypair(KMF_HANDLE_T handle, int numattr,
1476 1484          KMF_ATTRIBUTE *attrlist)
1477 1485  {
1478 1486          KMF_RETURN rv = KMF_OK;
1479 1487          KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
1480      -        uint32_t eValue = 0x010001;
     1488 +        uint32_t eValue = RSA_F4;
     1489 +        BIGNUM *eValue_bn = NULL;
1481 1490          RSA *sslPrivKey = NULL;
1482 1491          DSA *sslDSAKey = NULL;
1483 1492          EVP_PKEY *eprikey = NULL;
1484 1493          EVP_PKEY *epubkey = NULL;
1485 1494          BIO *out = NULL;
1486 1495          KMF_KEY_HANDLE *pubkey = NULL, *privkey = NULL;
1487 1496          uint32_t keylen = 1024;
1488 1497          uint32_t keylen_size = sizeof (uint32_t);
1489 1498          boolean_t storekey = TRUE;
1490 1499          KMF_KEY_ALG keytype = KMF_RSA;
1491 1500  
     1501 +        eValue_bn = BN_new();
     1502 +        if (eValue_bn == NULL)
     1503 +                return (KMF_ERR_MEMORY);
     1504 +        if (BN_set_word(eValue_bn, eValue) == 0) {
     1505 +                rv = KMF_ERR_KEYGEN_FAILED;
     1506 +                goto cleanup;
     1507 +        }
     1508 +
1492 1509          rv = kmf_get_attr(KMF_STOREKEY_BOOL_ATTR, attrlist, numattr,
1493 1510              &storekey, NULL);
1494 1511          if (rv != KMF_OK) {
1495 1512                  /* "storekey" is optional. Default is TRUE */
1496 1513                  rv = KMF_OK;
1497 1514          }
1498 1515  
1499 1516          rv = kmf_get_attr(KMF_KEYALG_ATTR, attrlist, numattr,
1500 1517              (void *)&keytype, NULL);
1501 1518          if (rv != KMF_OK)
1502 1519                  /* keytype is optional.  KMF_RSA is default */
1503 1520                  rv = KMF_OK;
1504 1521  
1505 1522          pubkey = kmf_get_attr_ptr(KMF_PUBKEY_HANDLE_ATTR, attrlist, numattr);
1506      -        if (pubkey == NULL)
1507      -                return (KMF_ERR_BAD_PARAMETER);
     1523 +        if (pubkey == NULL) {
     1524 +                rv = KMF_ERR_BAD_PARAMETER;
     1525 +                goto cleanup;
     1526 +        }
1508 1527  
1509 1528          privkey = kmf_get_attr_ptr(KMF_PRIVKEY_HANDLE_ATTR, attrlist, numattr);
1510      -        if (privkey == NULL)
1511      -                return (KMF_ERR_BAD_PARAMETER);
     1529 +        if (privkey == NULL) {
     1530 +                rv = KMF_ERR_BAD_PARAMETER;
     1531 +                goto cleanup;
     1532 +        }
1512 1533  
1513 1534          (void) memset(pubkey, 0, sizeof (KMF_KEY_HANDLE));
1514 1535          (void) memset(privkey, 0, sizeof (KMF_KEY_HANDLE));
1515 1536  
1516 1537          eprikey = EVP_PKEY_new();
1517 1538          if (eprikey == NULL) {
1518 1539                  SET_ERROR(kmfh, ERR_get_error());
1519 1540                  rv = KMF_ERR_KEYGEN_FAILED;
1520 1541                  goto cleanup;
1521 1542          }
↓ open down ↓ 6 lines elided ↑ open up ↑
1528 1549          if (keytype == KMF_RSA) {
1529 1550                  KMF_BIGINT *rsaexp = NULL;
1530 1551  
1531 1552                  rsaexp = kmf_get_attr_ptr(KMF_RSAEXP_ATTR, attrlist, numattr);
1532 1553                  if (rsaexp != NULL) {
1533 1554                          if (rsaexp->len > 0 &&
1534 1555                              rsaexp->len <= sizeof (eValue) &&
1535 1556                              rsaexp->val != NULL) {
1536 1557                                  /* LINTED E_BAD_PTR_CAST_ALIGN */
1537 1558                                  eValue = *(uint32_t *)rsaexp->val;
     1559 +                                if (BN_set_word(eValue_bn, eValue) == 0) {
     1560 +                                        rv = KMF_ERR_BAD_PARAMETER;
     1561 +                                        goto cleanup;
     1562 +                                }
1538 1563                          } else {
1539 1564                                  rv = KMF_ERR_BAD_PARAMETER;
1540 1565                                  goto cleanup;
1541 1566                          }
1542 1567                  } else {
1543 1568                          /* RSA Exponent is optional. Default is 0x10001 */
1544 1569                          rv = KMF_OK;
1545 1570                  }
1546 1571  
1547 1572                  rv = kmf_get_attr(KMF_KEYLENGTH_ATTR, attrlist, numattr,
1548 1573                      &keylen, &keylen_size);
1549 1574                  if (rv == KMF_ERR_ATTR_NOT_FOUND)
1550 1575                          /* keylen is optional, default is 1024 */
1551 1576                          rv = KMF_OK;
1552 1577                  if (rv != KMF_OK) {
1553 1578                          rv = KMF_ERR_BAD_PARAMETER;
1554 1579                          goto cleanup;
1555 1580                  }
1556 1581  
1557      -                sslPrivKey = RSA_generate_key(keylen, eValue, NULL, NULL);
1558      -                if (sslPrivKey == NULL) {
     1582 +                sslPrivKey = RSA_new();
     1583 +                if (sslPrivKey == NULL ||
     1584 +                    RSA_generate_key_ex(sslPrivKey, keylen, eValue_bn, NULL)
     1585 +                    == 0) {
1559 1586                          SET_ERROR(kmfh, ERR_get_error());
1560 1587                          rv = KMF_ERR_KEYGEN_FAILED;
1561 1588                  } else {
1562 1589                          (void) EVP_PKEY_set1_RSA(eprikey, sslPrivKey);
1563 1590                          privkey->kstype = KMF_KEYSTORE_OPENSSL;
1564 1591                          privkey->keyalg = KMF_RSA;
1565 1592                          privkey->keyclass = KMF_ASYM_PRI;
1566 1593                          privkey->israw = FALSE;
1567 1594                          privkey->keyp = (void *)eprikey;
1568 1595  
1569 1596                          /* OpenSSL derives the public key from the private */
1570 1597                          (void) EVP_PKEY_set1_RSA(epubkey, sslPrivKey);
1571 1598                          pubkey->kstype = KMF_KEYSTORE_OPENSSL;
1572 1599                          pubkey->keyalg = KMF_RSA;
1573 1600                          pubkey->israw = FALSE;
1574 1601                          pubkey->keyclass = KMF_ASYM_PUB;
1575 1602                          pubkey->keyp = (void *)epubkey;
1576 1603                  }
1577 1604          } else if (keytype == KMF_DSA) {
1578      -                DSA *dp;
     1605 +                BIGNUM *p, *q, *g;
     1606 +
1579 1607                  sslDSAKey = DSA_new();
1580 1608                  if (sslDSAKey == NULL) {
1581 1609                          SET_ERROR(kmfh, ERR_get_error());
1582 1610                          return (KMF_ERR_MEMORY);
1583 1611                  }
1584 1612  
1585      -                if ((sslDSAKey->p = BN_bin2bn(P, sizeof (P), sslDSAKey->p)) ==
1586      -                    NULL) {
     1613 +                p = BN_bin2bn(P, sizeof (P), NULL);
     1614 +                q = BN_bin2bn(Q, sizeof (Q), NULL);
     1615 +                g = BN_bin2bn(G, sizeof (G), NULL);
     1616 +                if (p == NULL || q == NULL || g == NULL) {
     1617 +                        BN_free(p);
     1618 +                        BN_free(q);
     1619 +                        BN_free(g);
1587 1620                          SET_ERROR(kmfh, ERR_get_error());
1588 1621                          rv = KMF_ERR_KEYGEN_FAILED;
1589 1622                          goto cleanup;
1590 1623                  }
1591      -                if ((sslDSAKey->q = BN_bin2bn(Q, sizeof (Q), sslDSAKey->q)) ==
1592      -                    NULL) {
     1624 +
     1625 +                if (DSA_set0_pqg(sslDSAKey, p, q, g) == 0) {
1593 1626                          SET_ERROR(kmfh, ERR_get_error());
1594 1627                          rv = KMF_ERR_KEYGEN_FAILED;
1595 1628                          goto cleanup;
1596 1629                  }
1597      -                if ((sslDSAKey->g = BN_bin2bn(G, sizeof (G), sslDSAKey->g)) ==
1598      -                    NULL) {
1599      -                        SET_ERROR(kmfh, ERR_get_error());
1600      -                        rv = KMF_ERR_KEYGEN_FAILED;
1601      -                        goto cleanup;
1602      -                }
1603 1630  
1604 1631                  if (!DSA_generate_key(sslDSAKey)) {
1605 1632                          SET_ERROR(kmfh, ERR_get_error());
1606 1633                          rv = KMF_ERR_KEYGEN_FAILED;
1607 1634                          goto cleanup;
1608 1635                  }
1609 1636  
1610 1637                  privkey->kstype = KMF_KEYSTORE_OPENSSL;
1611 1638                  privkey->keyalg = KMF_DSA;
1612 1639                  privkey->keyclass = KMF_ASYM_PRI;
1613 1640                  privkey->israw = FALSE;
1614 1641                  if (EVP_PKEY_set1_DSA(eprikey, sslDSAKey)) {
1615 1642                          privkey->keyp = (void *)eprikey;
1616 1643                  } else {
1617 1644                          SET_ERROR(kmfh, ERR_get_error());
1618 1645                          rv = KMF_ERR_KEYGEN_FAILED;
1619 1646                          goto cleanup;
1620 1647                  }
1621      -                dp = DSA_new();
1622      -                /* Make a copy for the public key */
1623      -                if (dp != NULL) {
1624      -                        if ((dp->p = BN_new()) == NULL) {
1625      -                                SET_ERROR(kmfh, ERR_get_error());
1626      -                                rv = KMF_ERR_MEMORY;
1627      -                                DSA_free(dp);
1628      -                                goto cleanup;
1629      -                        }
1630      -                        if ((dp->q = BN_new()) == NULL) {
1631      -                                SET_ERROR(kmfh, ERR_get_error());
1632      -                                rv = KMF_ERR_MEMORY;
1633      -                                BN_free(dp->p);
1634      -                                DSA_free(dp);
1635      -                                goto cleanup;
1636      -                        }
1637      -                        if ((dp->g = BN_new()) == NULL) {
1638      -                                SET_ERROR(kmfh, ERR_get_error());
1639      -                                rv = KMF_ERR_MEMORY;
1640      -                                BN_free(dp->q);
1641      -                                BN_free(dp->p);
1642      -                                DSA_free(dp);
1643      -                                goto cleanup;
1644      -                        }
1645      -                        if ((dp->pub_key = BN_new()) == NULL) {
1646      -                                SET_ERROR(kmfh, ERR_get_error());
1647      -                                rv = KMF_ERR_MEMORY;
1648      -                                BN_free(dp->q);
1649      -                                BN_free(dp->p);
1650      -                                BN_free(dp->g);
1651      -                                DSA_free(dp);
1652      -                                goto cleanup;
1653      -                        }
1654      -                        (void) BN_copy(dp->p, sslDSAKey->p);
1655      -                        (void) BN_copy(dp->q, sslDSAKey->q);
1656      -                        (void) BN_copy(dp->g, sslDSAKey->g);
1657      -                        (void) BN_copy(dp->pub_key, sslDSAKey->pub_key);
1658 1648  
1659      -                        pubkey->kstype = KMF_KEYSTORE_OPENSSL;
1660      -                        pubkey->keyalg = KMF_DSA;
1661      -                        pubkey->keyclass = KMF_ASYM_PUB;
1662      -                        pubkey->israw = FALSE;
     1649 +                pubkey->kstype = KMF_KEYSTORE_OPENSSL;
     1650 +                pubkey->keyalg = KMF_DSA;
     1651 +                pubkey->keyclass = KMF_ASYM_PUB;
     1652 +                pubkey->israw = FALSE;
1663 1653  
1664      -                        if (EVP_PKEY_set1_DSA(epubkey, sslDSAKey)) {
1665      -                                pubkey->keyp = (void *)epubkey;
1666      -                        } else {
1667      -                                SET_ERROR(kmfh, ERR_get_error());
1668      -                                rv = KMF_ERR_KEYGEN_FAILED;
1669      -                                goto cleanup;
1670      -                        }
     1654 +                if (EVP_PKEY_set1_DSA(epubkey, sslDSAKey)) {
     1655 +                        pubkey->keyp = (void *)epubkey;
     1656 +                } else {
     1657 +                        SET_ERROR(kmfh, ERR_get_error());
     1658 +                        rv = KMF_ERR_KEYGEN_FAILED;
     1659 +                        goto cleanup;
1671 1660                  }
1672 1661          }
1673 1662  
1674 1663          if (rv != KMF_OK) {
1675 1664                  goto cleanup;
1676 1665          }
1677 1666  
1678 1667          if (storekey) {
1679 1668                  KMF_ATTRIBUTE storeattrs[4]; /* max. 4 attributes needed */
1680 1669                  int i = 0;
↓ open down ↓ 31 lines elided ↑ open up ↑
1712 1701                          storeattrs[i].type = KMF_ENCODE_FORMAT_ATTR;
1713 1702                          storeattrs[i].pValue = &format;
1714 1703                          storeattrs[i].valueLen = sizeof (format);
1715 1704                          i++;
1716 1705                  }
1717 1706  
1718 1707                  rv = OpenSSL_StoreKey(handle, i, storeattrs);
1719 1708          }
1720 1709  
1721 1710  cleanup:
     1711 +        if (eValue_bn != NULL)
     1712 +                BN_free(eValue_bn);
     1713 +
1722 1714          if (rv != KMF_OK) {
1723 1715                  if (eprikey != NULL)
1724 1716                          EVP_PKEY_free(eprikey);
1725 1717  
1726 1718                  if (epubkey != NULL)
1727 1719                          EVP_PKEY_free(epubkey);
1728 1720  
1729 1721                  if (pubkey->keylabel) {
1730 1722                          free(pubkey->keylabel);
1731 1723                          pubkey->keylabel = NULL;
↓ open down ↓ 20 lines elided ↑ open up ↑
1752 1744          return (rv);
1753 1745  }
1754 1746  
1755 1747  /*
1756 1748   * Make sure the BN conversion is properly padded with 0x00
1757 1749   * bytes.  If not, signature verification for DSA signatures
1758 1750   * may fail in the case where the bignum value does not use
1759 1751   * all of the bits.
1760 1752   */
1761 1753  static int
1762      -fixbnlen(BIGNUM *bn, unsigned char *buf, int len) {
     1754 +fixbnlen(const BIGNUM *bn, unsigned char *buf, int len) {
1763 1755          int bytes = len - BN_num_bytes(bn);
1764 1756  
1765 1757          /* prepend with leading 0x00 if necessary */
1766 1758          while (bytes-- > 0)
1767 1759                  *buf++ = 0;
1768 1760  
1769 1761          (void) BN_bn2bin(bn, buf);
1770 1762          /*
1771 1763           * Return the desired length since we prepended it
1772 1764           * with the necessary 0x00 padding.
↓ open down ↓ 1 lines elided ↑ open up ↑
1774 1766          return (len);
1775 1767  }
1776 1768  
1777 1769  KMF_RETURN
1778 1770  OpenSSL_SignData(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key,
1779 1771          KMF_OID *AlgOID, KMF_DATA *tobesigned, KMF_DATA *output)
1780 1772  {
1781 1773          KMF_RETURN ret = KMF_OK;
1782 1774          KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
1783 1775          KMF_ALGORITHM_INDEX             AlgId;
1784      -        EVP_MD_CTX ctx;
     1776 +        EVP_MD_CTX *ctx;
1785 1777          const EVP_MD *md;
1786 1778  
1787 1779          if (key == NULL || AlgOID == NULL ||
1788 1780              tobesigned == NULL || output == NULL ||
1789 1781              tobesigned->Data == NULL ||
1790 1782              output->Data == NULL)
1791 1783                  return (KMF_ERR_BAD_PARAMETER);
1792 1784  
1793 1785          /* Map the OID to an OpenSSL algorithm */
1794 1786          AlgId = x509_algoid_to_algid(AlgOID);
↓ open down ↓ 24 lines elided ↑ open up ↑
1819 1811  
1820 1812                          p = output->Data;
1821 1813                          if ((len = RSA_private_encrypt(tobesigned->Length,
1822 1814                              tobesigned->Data, p, rsa,
1823 1815                              RSA_PKCS1_PADDING)) <= 0) {
1824 1816                                  SET_ERROR(kmfh, ERR_get_error());
1825 1817                                  ret = KMF_ERR_INTERNAL;
1826 1818                          }
1827 1819                          output->Length = len;
1828 1820                  } else {
1829      -                        (void) EVP_MD_CTX_init(&ctx);
1830      -                        (void) EVP_SignInit_ex(&ctx, md, NULL);
1831      -                        (void) EVP_SignUpdate(&ctx, tobesigned->Data,
     1821 +                        if ((ctx = EVP_MD_CTX_new()) == NULL)
     1822 +                                return (KMF_ERR_MEMORY);
     1823 +                        (void) EVP_SignInit_ex(ctx, md, NULL);
     1824 +                        (void) EVP_SignUpdate(ctx, tobesigned->Data,
1832 1825                              (uint32_t)tobesigned->Length);
1833 1826                          len = (uint32_t)output->Length;
1834 1827                          p = output->Data;
1835      -                        if (!EVP_SignFinal(&ctx, p, (uint32_t *)&len, pkey)) {
     1828 +                        if (!EVP_SignFinal(ctx, p, (uint32_t *)&len, pkey)) {
1836 1829                                  SET_ERROR(kmfh, ERR_get_error());
1837 1830                                  len = 0;
1838 1831                                  ret = KMF_ERR_INTERNAL;
1839 1832                          }
1840 1833                          output->Length = len;
1841      -                        (void) EVP_MD_CTX_cleanup(&ctx);
     1834 +                        EVP_MD_CTX_free(ctx);
1842 1835                  }
1843 1836          } else if (key->keyalg == KMF_DSA) {
1844 1837                  DSA *dsa = EVP_PKEY_get1_DSA(key->keyp);
1845 1838  
1846 1839                  uchar_t hash[EVP_MAX_MD_SIZE];
1847 1840                  uint32_t hashlen;
1848 1841                  DSA_SIG *dsasig;
1849 1842  
1850 1843                  if (AlgId == KMF_ALGID_DSA ||
1851 1844                      AlgId == KMF_ALGID_SHA1WithDSA)
↓ open down ↓ 4 lines elided ↑ open up ↑
1856 1849                          return (KMF_ERR_BAD_ALGORITHM);
1857 1850  
1858 1851                  /*
1859 1852                   * OpenSSL EVP_Sign operation automatically converts to
1860 1853                   * ASN.1 output so we do the operations separately so we
1861 1854                   * are assured of NOT getting ASN.1 output returned.
1862 1855                   * KMF does not want ASN.1 encoded results because
1863 1856                   * not all mechanisms return ASN.1 encodings (PKCS#11
1864 1857                   * and NSS return raw signature data).
1865 1858                   */
1866      -                EVP_MD_CTX_init(&ctx);
1867      -                (void) EVP_DigestInit_ex(&ctx, md, NULL);
1868      -                (void) EVP_DigestUpdate(&ctx, tobesigned->Data,
     1859 +                if ((ctx = EVP_MD_CTX_new()) == NULL)
     1860 +                        return (KMF_ERR_MEMORY);
     1861 +                (void) EVP_DigestInit_ex(ctx, md, NULL);
     1862 +                (void) EVP_DigestUpdate(ctx, tobesigned->Data,
1869 1863                      tobesigned->Length);
1870      -                (void) EVP_DigestFinal_ex(&ctx, hash, &hashlen);
     1864 +                (void) EVP_DigestFinal_ex(ctx, hash, &hashlen);
1871 1865  
1872 1866                  /* Only sign first 20 bytes for SHA2 */
1873 1867                  if (AlgId == KMF_ALGID_SHA256WithDSA)
1874 1868                          hashlen = 20;
1875 1869                  dsasig = DSA_do_sign(hash, hashlen, dsa);
1876 1870                  if (dsasig != NULL) {
1877 1871                          int i;
1878      -                        output->Length = i = fixbnlen(dsasig->r, output->Data,
     1872 +                        const BIGNUM *r, *s;
     1873 +
     1874 +                        DSA_SIG_get0(dsasig, &r, &s);
     1875 +                        output->Length = i = fixbnlen(r, output->Data,
1879 1876                              hashlen);
1880 1877  
1881      -                        output->Length += fixbnlen(dsasig->s, &output->Data[i],
     1878 +                        output->Length += fixbnlen(s, &output->Data[i],
1882 1879                              hashlen);
1883 1880  
1884 1881                          DSA_SIG_free(dsasig);
1885 1882                  } else {
1886 1883                          SET_ERROR(kmfh, ERR_get_error());
1887 1884                  }
1888      -                (void) EVP_MD_CTX_cleanup(&ctx);
     1885 +                EVP_MD_CTX_free(ctx);
1889 1886          } else {
1890 1887                  return (KMF_ERR_BAD_PARAMETER);
1891 1888          }
1892 1889  cleanup:
1893 1890          return (ret);
1894 1891  }
1895 1892  
1896 1893  KMF_RETURN
1897 1894  /*ARGSUSED*/
1898 1895  OpenSSL_DeleteKey(KMF_HANDLE_T handle,
↓ open down ↓ 117 lines elided ↑ open up ↑
2016 2013  
2017 2014  KMF_RETURN
2018 2015  OpenSSL_CertGetPrintable(KMF_HANDLE_T handle, const KMF_DATA *pcert,
2019 2016          KMF_PRINTABLE_ITEM flag, char *resultStr)
2020 2017  {
2021 2018          KMF_RETURN ret = KMF_OK;
2022 2019          KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
2023 2020          X509 *xcert = NULL;
2024 2021          unsigned char *outbuf = NULL;
2025 2022          unsigned char *outbuf_p;
2026      -        char *tmpstr = NULL;
2027 2023          int j;
2028 2024          int ext_index, nid, len;
2029 2025          BIO *mem = NULL;
2030      -#if OPENSSL_VERSION_NUMBER < 0x10000000L
2031      -        STACK *emlst = NULL;
2032      -#else
2033 2026          STACK_OF(OPENSSL_STRING) *emlst = NULL;
2034      -#endif
2035 2027          X509_EXTENSION *ex;
2036      -        X509_CINF *ci;
2037 2028  
2038 2029          if (pcert == NULL || pcert->Data == NULL || pcert->Length == 0) {
2039 2030                  return (KMF_ERR_BAD_PARAMETER);
2040 2031          }
2041 2032  
2042 2033          /* copy cert data to outbuf */
2043 2034          outbuf = malloc(pcert->Length);
2044 2035          if (outbuf == NULL) {
2045 2036                  return (KMF_ERR_MEMORY);
2046 2037          }
↓ open down ↓ 21 lines elided ↑ open up ↑
2068 2059                  len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2069 2060                  break;
2070 2061  
2071 2062          case KMF_CERT_SUBJECT:
2072 2063                  (void) X509_NAME_print_ex(mem, X509_get_subject_name(xcert), 0,
2073 2064                      XN_FLAG_SEP_CPLUS_SPC);
2074 2065                  len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2075 2066                  break;
2076 2067  
2077 2068          case KMF_CERT_VERSION:
2078      -                tmpstr = i2s_ASN1_INTEGER(NULL, xcert->cert_info->version);
2079      -                (void) strncpy(resultStr, tmpstr, KMF_CERT_PRINTABLE_LEN);
2080      -                OPENSSL_free(tmpstr);
     2069 +                (void) snprintf(resultStr, KMF_CERT_PRINTABLE_LEN,
     2070 +                    "%ld", X509_get_version(xcert));
2081 2071                  len = strlen(resultStr);
2082 2072                  break;
2083 2073  
2084 2074          case KMF_CERT_SERIALNUM:
2085 2075                  if (i2a_ASN1_INTEGER(mem, X509_get_serialNumber(xcert)) > 0) {
2086 2076                          (void) strcpy(resultStr, "0x");
2087 2077                          len = BIO_gets(mem, &resultStr[2],
2088 2078                              KMF_CERT_PRINTABLE_LEN - 2);
2089 2079                  }
2090 2080                  break;
2091 2081  
2092 2082          case KMF_CERT_NOTBEFORE:
2093      -                (void) ASN1_TIME_print(mem, X509_get_notBefore(xcert));
     2083 +                (void) ASN1_TIME_print(mem, X509_getm_notBefore(xcert));
2094 2084                  len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2095 2085                  break;
2096 2086  
2097 2087          case KMF_CERT_NOTAFTER:
2098      -                (void) ASN1_TIME_print(mem, X509_get_notAfter(xcert));
     2088 +                (void) ASN1_TIME_print(mem, X509_getm_notAfter(xcert));
2099 2089                  len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2100 2090                  break;
2101 2091  
2102 2092          case KMF_CERT_PUBKEY_DATA:
2103 2093                  {
     2094 +                        RSA *rsa;
     2095 +                        DSA *dsa;
     2096 +
2104 2097                          EVP_PKEY *pkey = X509_get_pubkey(xcert);
2105 2098                          if (pkey == NULL) {
2106 2099                                  SET_ERROR(kmfh, ERR_get_error());
2107 2100                                  ret = KMF_ERR_ENCODING;
2108 2101                                  goto out;
2109 2102                          }
2110 2103  
2111      -                        if (pkey->type == EVP_PKEY_RSA) {
     2104 +                        if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) {
2112 2105                                  (void) BIO_printf(mem,
2113 2106                                      "RSA Public Key: (%d bit)\n",
2114      -                                    BN_num_bits(pkey->pkey.rsa->n));
2115      -                                (void) RSA_print(mem, pkey->pkey.rsa, 0);
2116      -                        } else if (pkey->type == EVP_PKEY_DSA) {
     2107 +                                    RSA_bits(rsa));
     2108 +                                (void) RSA_print(mem, rsa, 0);
     2109 +
     2110 +                        } else if ((dsa = EVP_PKEY_get0_DSA(pkey)) != NULL) {
2117 2111                                  (void) BIO_printf(mem,
2118 2112                                      "%12sDSA Public Key:\n", "");
2119      -                                (void) DSA_print(mem, pkey->pkey.dsa, 0);
     2113 +                                (void) DSA_print(mem, dsa, 0);
2120 2114                          } else {
2121 2115                                  (void) BIO_printf(mem,
2122 2116                                      "%12sUnknown Public Key:\n", "");
2123 2117                          }
2124 2118                          (void) BIO_printf(mem, "\n");
2125 2119                          EVP_PKEY_free(pkey);
2126 2120                  }
2127 2121                  len = BIO_read(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2128 2122                  break;
2129 2123          case KMF_CERT_SIGNATURE_ALG:
2130 2124          case KMF_CERT_PUBKEY_ALG:
2131      -                if (flag == KMF_CERT_SIGNATURE_ALG) {
2132      -                        len = i2a_ASN1_OBJECT(mem,
2133      -                            xcert->sig_alg->algorithm);
2134      -                } else {
2135      -                        len = i2a_ASN1_OBJECT(mem,
2136      -                            xcert->cert_info->key->algor->algorithm);
2137      -                }
     2125 +                {
     2126 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     2127 +                        ASN1_OBJECT *alg = NULL;
     2128 +#else
     2129 +                        const ASN1_OBJECT *alg = NULL;
     2130 +#endif
2138 2131  
2139      -                if (len > 0) {
2140      -                        len = BIO_read(mem, resultStr,
2141      -                            KMF_CERT_PRINTABLE_LEN);
     2132 +                        if (flag == KMF_CERT_SIGNATURE_ALG) {
     2133 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     2134 +                                alg = xcert->sig_alg->algorithm;
     2135 +#else
     2136 +                                const X509_ALGOR *sig_alg = NULL;
     2137 +
     2138 +                                X509_get0_signature(NULL, &sig_alg, xcert);
     2139 +                                if (sig_alg != NULL)
     2140 +                                        X509_ALGOR_get0(&alg, NULL, NULL,
     2141 +                                            sig_alg);
     2142 +#endif
     2143 +                        } else {
     2144 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     2145 +                                alg = xcert->cert_info->key->algor->algorithm;
     2146 +#else
     2147 +                                X509_PUBKEY *key = X509_get_X509_PUBKEY(xcert);
     2148 +
     2149 +                                if (key != NULL)
     2150 +                                        (void) X509_PUBKEY_get0_param(
     2151 +                                            (ASN1_OBJECT **)&alg, NULL, 0,
     2152 +                                            NULL, key);
     2153 +#endif
     2154 +                        }
     2155 +
     2156 +                        if (alg == NULL)
     2157 +                                len = -1;
     2158 +                        else if ((len = i2a_ASN1_OBJECT(mem, alg)) > 0)
     2159 +                                len = BIO_read(mem, resultStr,
     2160 +                                    KMF_CERT_PRINTABLE_LEN);
2142 2161                  }
2143 2162                  break;
2144 2163  
2145 2164          case KMF_CERT_EMAIL:
2146 2165                  emlst = X509_get1_email(xcert);
2147      -#if OPENSSL_VERSION_NUMBER < 0x10000000L
2148      -                for (j = 0; j < sk_num(emlst); j++)
2149      -                        (void) BIO_printf(mem, "%s\n", sk_value(emlst, j));
2150      -#else
2151 2166                  for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
2152 2167                          (void) BIO_printf(mem, "%s\n",
2153 2168                              sk_OPENSSL_STRING_value(emlst, j));
2154      -#endif
2155 2169  
2156 2170                  len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2157 2171                  X509_email_free(emlst);
2158 2172                  break;
2159 2173          case KMF_X509_EXT_ISSUER_ALTNAME:
2160 2174          case KMF_X509_EXT_SUBJ_ALTNAME:
2161 2175          case KMF_X509_EXT_KEY_USAGE:
2162 2176          case KMF_X509_EXT_PRIV_KEY_USAGE_PERIOD:
2163 2177          case KMF_X509_EXT_CERT_POLICIES:
2164 2178          case KMF_X509_EXT_BASIC_CONSTRAINTS:
↓ open down ↓ 4 lines elided ↑ open up ↑
2169 2183          case KMF_X509_EXT_AUTH_KEY_ID:
2170 2184          case KMF_X509_EXT_SUBJ_KEY_ID:
2171 2185          case KMF_X509_EXT_POLICY_MAPPINGS:
2172 2186          case KMF_X509_EXT_CRL_DIST_POINTS:
2173 2187          case KMF_X509_EXT_FRESHEST_CRL:
2174 2188                  nid = ext2NID(flag);
2175 2189                  if (nid == NID_undef) {
2176 2190                          ret = KMF_ERR_EXTENSION_NOT_FOUND;
2177 2191                          goto out;
2178 2192                  }
2179      -                ci = xcert->cert_info;
2180 2193  
2181      -                ext_index = X509v3_get_ext_by_NID(ci->extensions, nid, -1);
     2194 +                ext_index = X509_get_ext_by_NID(xcert, nid, -1);
2182 2195                  if (ext_index == -1) {
2183 2196                          SET_ERROR(kmfh, ERR_get_error());
2184 2197  
2185 2198                          ret = KMF_ERR_EXTENSION_NOT_FOUND;
2186 2199                          goto out;
2187 2200                  }
2188      -                ex = X509v3_get_ext(ci->extensions, ext_index);
     2201 +                ex = X509_get_ext(xcert, ext_index);
2189 2202  
2190 2203                  (void) i2a_ASN1_OBJECT(mem, X509_EXTENSION_get_object(ex));
2191 2204  
2192 2205                  if (BIO_printf(mem, ": %s\n",
2193 2206                      X509_EXTENSION_get_critical(ex) ? "critical" : "") <= 0) {
2194 2207                          SET_ERROR(kmfh, ERR_get_error());
2195 2208                          ret = KMF_ERR_ENCODING;
2196 2209                          goto out;
2197 2210                  }
2198 2211                  if (!X509V3_EXT_print(mem, ex, X509V3_EXT_DUMP_UNKNOWN, 4)) {
2199 2212                          (void) BIO_printf(mem, "%*s", 4, "");
2200      -                        (void) M_ASN1_OCTET_STRING_print(mem, ex->value);
     2213 +                        (void) ASN1_STRING_print(mem,
     2214 +                            X509_EXTENSION_get_data(ex));
2201 2215                  }
2202 2216                  if (BIO_write(mem, "\n", 1) <= 0) {
2203 2217                          SET_ERROR(kmfh, ERR_get_error());
2204 2218                          ret = KMF_ERR_ENCODING;
2205 2219                          goto out;
2206 2220                  }
2207 2221                  len = BIO_read(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
2208 2222          }
2209 2223          if (len <= 0) {
2210 2224                  SET_ERROR(kmfh, ERR_get_error());
↓ open down ↓ 260 lines elided ↑ open up ↑
2471 2485          }
2472 2486  
2473 2487          if (derbio != NULL) {
2474 2488                  (void) BIO_free(derbio);
2475 2489          }
2476 2490  
2477 2491          return (ret);
2478 2492  }
2479 2493  
2480 2494  /* ocsp_find_signer_sk() is copied from openssl source */
2481      -static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
     2495 +static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_BASICRESP *bs)
2482 2496  {
2483 2497          int i;
2484 2498          unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
     2499 +        const ASN1_OCTET_STRING *pid;
2485 2500  
2486      -        /* Easy if lookup by name */
     2501 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     2502 +        OCSP_RESPID *id = bs->tbsResponseData->responderId;
     2503 +
2487 2504          if (id->type == V_OCSP_RESPID_NAME)
2488 2505                  return (X509_find_by_subject(certs, id->value.byName));
2489 2506  
     2507 +        pid = id->value.byKey;
     2508 +#else
     2509 +        const X509_NAME *pname;
     2510 +
     2511 +        if (OCSP_resp_get0_id(bs, &pid, &pname) == 0)
     2512 +                return (NULL);
     2513 +
     2514 +        if (pname != NULL)
     2515 +                return (X509_find_by_subject(certs, (X509_NAME *)pname));
     2516 +#endif
     2517 +
2490 2518          /* Lookup by key hash */
2491 2519  
2492 2520          /* If key hash isn't SHA1 length then forget it */
2493      -        if (id->value.byKey->length != SHA_DIGEST_LENGTH)
     2521 +        if (pid->length != SHA_DIGEST_LENGTH)
2494 2522                  return (NULL);
2495 2523  
2496      -        keyhash = id->value.byKey->data;
     2524 +        keyhash = pid->data;
2497 2525          /* Calculate hash of each key and compare */
2498 2526          for (i = 0; i < sk_X509_num(certs); i++) {
2499 2527                  /* LINTED E_BAD_PTR_CAST_ALIGN */
2500 2528                  X509 *x = sk_X509_value(certs, i);
2501 2529                  /* Use pubkey_digest to get the key ID value */
2502 2530                  (void) X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL);
2503 2531                  if (!memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH))
2504 2532                          return (x);
2505 2533          }
2506 2534          return (NULL);
2507 2535  }
2508 2536  
2509 2537  /* ocsp_find_signer() is copied from openssl source */
2510 2538  /* ARGSUSED2 */
2511 2539  static int
2512 2540  ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
2513 2541      X509_STORE *st, unsigned long flags)
2514 2542  {
2515 2543          X509 *signer;
2516      -        OCSP_RESPID *rid = bs->tbsResponseData->responderId;
2517      -        if ((signer = ocsp_find_signer_sk(certs, rid))) {
     2544 +        if ((signer = ocsp_find_signer_sk(certs, bs)))  {
2518 2545                  *psigner = signer;
2519 2546                  return (2);
2520 2547          }
     2548 +
2521 2549          if (!(flags & OCSP_NOINTERN) &&
2522      -            (signer = ocsp_find_signer_sk(bs->certs, rid))) {
     2550 +            (signer = ocsp_find_signer_sk(
     2551 +            (STACK_OF(X509) *)OCSP_resp_get0_certs(bs), bs))) {
2523 2552                  *psigner = signer;
2524 2553                  return (1);
2525 2554          }
2526 2555          /* Maybe lookup from store if by subject name */
2527 2556  
2528 2557          *psigner = NULL;
2529 2558          return (0);
2530 2559  }
2531 2560  
2532 2561  /*
↓ open down ↓ 2 lines elided ↑ open up ↑
2535 2564   */
2536 2565  static KMF_RETURN
2537 2566  check_response_signature(KMF_HANDLE_T handle, OCSP_BASICRESP *bs,
2538 2567      KMF_DATA *signer_cert, KMF_DATA *issuer_cert)
2539 2568  {
2540 2569          KMF_RETURN ret = KMF_OK;
2541 2570          KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
2542 2571          STACK_OF(X509) *cert_stack = NULL;
2543 2572          X509 *signer = NULL;
2544 2573          X509 *issuer = NULL;
     2574 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
2545 2575          EVP_PKEY *skey = NULL;
     2576 +#else
     2577 +        STACK_OF(X509) *cert_stack2 = NULL;
     2578 +#endif
2546 2579          unsigned char *ptmp;
2547 2580  
2548      -
2549 2581          if (bs == NULL || issuer_cert == NULL)
2550 2582                  return (KMF_ERR_BAD_PARAMETER);
2551 2583  
2552 2584          /*
2553 2585           * Find the certificate that signed the basic response.
2554 2586           *
2555 2587           * If signer_cert is not NULL, we will use that as the signer cert.
2556 2588           * Otherwise, we will check if the issuer cert is actually the signer.
2557 2589           * If we still do not find a signer, we will look for it from the
2558 2590           * certificate list came with the response file.
↓ open down ↓ 33 lines elided ↑ open up ↑
2592 2624  
2593 2625                  ret = ocsp_find_signer(&signer, bs, cert_stack, NULL, 0);
2594 2626                  if (!ret) {
2595 2627                          /* can not find the signer */
2596 2628                          ret = KMF_ERR_OCSP_BAD_SIGNER;
2597 2629                          goto end;
2598 2630                  }
2599 2631          }
2600 2632  
2601 2633          /* Verify the signature of the response */
     2634 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
2602 2635          skey = X509_get_pubkey(signer);
2603 2636          if (skey == NULL) {
2604 2637                  ret = KMF_ERR_OCSP_BAD_SIGNER;
2605 2638                  goto end;
2606 2639          }
2607 2640  
2608 2641          ret = OCSP_BASICRESP_verify(bs, skey, 0);
     2642 +#else
     2643 +        /*
     2644 +         * Technique based on
     2645 +         * https://mta.openssl.org/pipermail/openssl-users/
     2646 +         *      2017-October/006814.html
     2647 +         */
     2648 +        if ((cert_stack2 = sk_X509_new_null()) == NULL) {
     2649 +                ret = KMF_ERR_INTERNAL;
     2650 +                goto end;
     2651 +        }
     2652 +
     2653 +        if (sk_X509_push(cert_stack2, signer) == NULL) {
     2654 +                ret = KMF_ERR_INTERNAL;
     2655 +                goto end;
     2656 +        }
     2657 +
     2658 +        ret = OCSP_basic_verify(bs, cert_stack2, NULL, OCSP_NOVERIFY);
     2659 +#endif
     2660 +
2609 2661          if (ret == 0) {
2610 2662                  ret = KMF_ERR_OCSP_RESPONSE_SIGNATURE;
2611 2663                  goto end;
2612 2664          }
2613 2665  
2614 2666  end:
2615 2667          if (issuer != NULL) {
2616 2668                  X509_free(issuer);
2617 2669          }
2618 2670  
2619 2671          if (signer != NULL) {
2620 2672                  X509_free(signer);
2621 2673          }
2622 2674  
     2675 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
2623 2676          if (skey != NULL) {
2624 2677                  EVP_PKEY_free(skey);
2625 2678          }
     2679 +#else
     2680 +        if (cert_stack2 != NULL) {
     2681 +                sk_X509_free(cert_stack2);
     2682 +        }
     2683 +#endif
2626 2684  
2627 2685          if (cert_stack != NULL) {
2628 2686                  sk_X509_free(cert_stack);
2629 2687          }
2630 2688  
2631 2689          return (ret);
2632 2690  }
2633 2691  
2634      -
2635      -
2636 2692  KMF_RETURN
2637 2693  OpenSSL_GetOCSPStatusForCert(KMF_HANDLE_T handle,
2638 2694          int numattr, KMF_ATTRIBUTE *attrlist)
2639 2695  {
2640 2696          KMF_RETURN ret = KMF_OK;
2641 2697          BIO *derbio = NULL;
2642 2698          OCSP_RESPONSE *resp = NULL;
2643 2699          OCSP_BASICRESP *bs = NULL;
2644 2700          OCSP_CERTID *id = NULL;
2645 2701          OCSP_SINGLERESP *single = NULL;
↓ open down ↓ 168 lines elided ↑ open up ↑
2814 2870          EVP_PKEY *pkey = NULL;
2815 2871          KMF_RAW_SYM_KEY *rkey = NULL;
2816 2872  
2817 2873          if (keyclass == KMF_ASYM_PRI ||
2818 2874              keyclass == KMF_ASYM_PUB) {
2819 2875                  pkey = openssl_load_key(handle, path);
2820 2876                  if (pkey == NULL) {
2821 2877                          return (KMF_ERR_KEY_NOT_FOUND);
2822 2878                  }
2823 2879                  if (key != NULL) {
2824      -                        if (pkey->type == EVP_PKEY_RSA)
     2880 +                        if (EVP_PKEY_get0_RSA(pkey) != NULL)
2825 2881                                  key->keyalg = KMF_RSA;
2826      -                        else if (pkey->type == EVP_PKEY_DSA)
     2882 +                        else if (EVP_PKEY_get0_DSA(pkey) != NULL)
2827 2883                                  key->keyalg = KMF_DSA;
2828 2884  
2829 2885                          key->kstype = KMF_KEYSTORE_OPENSSL;
2830 2886                          key->keyclass = keyclass;
2831 2887                          key->keyp = (void *)pkey;
2832 2888                          key->israw = FALSE;
2833 2889                          if (path != NULL &&
2834 2890                              ((key->keylabel = strdup(path)) == NULL)) {
2835 2891                                  EVP_PKEY_free(pkey);
2836 2892                                  return (KMF_ERR_MEMORY);
↓ open down ↓ 176 lines elided ↑ open up ↑
3013 3069  
3014 3070  #define HANDLE_PK12_ERROR { \
3015 3071          SET_ERROR(kmfh, ERR_get_error()); \
3016 3072          rv = KMF_ERR_ENCODING; \
3017 3073          goto out; \
3018 3074  }
3019 3075  
3020 3076  static int
3021 3077  add_alias_to_bag(PKCS12_SAFEBAG *bag, X509 *xcert)
3022 3078  {
3023      -        if (xcert != NULL && xcert->aux != NULL &&
3024      -            xcert->aux->alias != NULL) {
     3079 +        unsigned char *alias;
     3080 +        int len;
     3081 +
     3082 +        if (xcert != NULL && (alias = X509_alias_get0(xcert, &len)) != NULL) {
3025 3083                  if (PKCS12_add_friendlyname_asc(bag,
3026      -                    (const char *)xcert->aux->alias->data,
3027      -                    xcert->aux->alias->length) == 0)
     3084 +                    (const char *)alias, len) == 0)
3028 3085                          return (0);
3029 3086          }
3030 3087          return (1);
3031 3088  }
3032 3089  
3033 3090  static PKCS7 *
3034 3091  add_cert_to_safe(X509 *sslcert, KMF_CREDENTIAL *cred,
3035 3092          uchar_t *keyid, unsigned int keyidlen)
3036 3093  {
3037 3094          PKCS12_SAFEBAG *bag = NULL;
3038 3095          PKCS7 *cert_authsafe = NULL;
3039 3096          STACK_OF(PKCS12_SAFEBAG) *bag_stack;
3040 3097  
3041 3098          bag_stack = sk_PKCS12_SAFEBAG_new_null();
3042 3099          if (bag_stack == NULL)
3043 3100                  return (NULL);
3044 3101  
3045 3102          /* Convert cert from X509 struct to PKCS#12 bag */
3046      -        bag = PKCS12_x5092certbag(sslcert);
     3103 +        bag = PKCS12_SAFEBAG_create_cert(sslcert);
3047 3104          if (bag == NULL) {
3048 3105                  goto out;
3049 3106          }
3050 3107  
3051 3108          /* Add the key id to the certificate bag. */
3052 3109          if (keyidlen > 0 && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) {
3053 3110                  goto out;
3054 3111          }
3055 3112  
3056 3113          if (!add_alias_to_bag(bag, sslcert))
↓ open down ↓ 24 lines elided ↑ open up ↑
3081 3138          PKCS8_PRIV_KEY_INFO *p8 = NULL;
3082 3139          STACK_OF(PKCS12_SAFEBAG) *bag_stack = NULL;
3083 3140          PKCS12_SAFEBAG *bag = NULL;
3084 3141          PKCS7 *key_authsafe = NULL;
3085 3142  
3086 3143          p8 = EVP_PKEY2PKCS8(pkey);
3087 3144          if (p8 == NULL) {
3088 3145                  return (NULL);
3089 3146          }
3090 3147          /* Put the shrouded key into a PKCS#12 bag. */
3091      -        bag = PKCS12_MAKE_SHKEYBAG(
     3148 +        bag = PKCS12_SAFEBAG_create_pkcs8_encrypt(
3092 3149              NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
3093 3150              cred->cred, cred->credlen,
3094 3151              NULL, 0, PKCS12_DEFAULT_ITER, p8);
3095 3152  
3096 3153          /* Clean up the PKCS#8 shrouded key, don't need it now. */
3097 3154          PKCS8_PRIV_KEY_INFO_free(p8);
3098 3155          p8 = NULL;
3099 3156  
3100 3157          if (bag == NULL) {
3101 3158                  return (NULL);
↓ open down ↓ 18 lines elided ↑ open up ↑
3120 3177          if (bag_stack != NULL)
3121 3178                  sk_PKCS12_SAFEBAG_pop_free(bag_stack, PKCS12_SAFEBAG_free);
3122 3179          bag_stack = NULL;
3123 3180          return (key_authsafe);
3124 3181  }
3125 3182  
3126 3183  static EVP_PKEY *
3127 3184  ImportRawRSAKey(KMF_RAW_RSA_KEY *key)
3128 3185  {
3129 3186          RSA             *rsa = NULL;
3130      -        EVP_PKEY        *newkey = NULL;
     3187 +        EVP_PKEY        *newkey = NULL;
     3188 +        BIGNUM          *n = NULL, *e = NULL, *d = NULL,
     3189 +                        *p = NULL, *q = NULL,
     3190 +                        *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
3131 3191  
3132 3192          if ((rsa = RSA_new()) == NULL)
3133      -                return (NULL);
     3193 +                goto cleanup;
3134 3194  
3135      -        if ((rsa->n = BN_bin2bn(key->mod.val, key->mod.len, rsa->n)) == NULL)
3136      -                return (NULL);
     3195 +        if ((n = BN_bin2bn(key->mod.val, key->mod.len, NULL)) == NULL)
     3196 +                goto cleanup;
3137 3197  
3138      -        if ((rsa->e = BN_bin2bn(key->pubexp.val, key->pubexp.len, rsa->e)) ==
3139      -            NULL)
3140      -                return (NULL);
     3198 +        if ((e = BN_bin2bn(key->pubexp.val, key->pubexp.len, NULL)) == NULL)
     3199 +                goto cleanup;
3141 3200  
3142      -        if (key->priexp.val != NULL)
3143      -                if ((rsa->d = BN_bin2bn(key->priexp.val, key->priexp.len,
3144      -                    rsa->d)) == NULL)
3145      -                        return (NULL);
     3201 +        if (key->priexp.val != NULL &&
     3202 +            (d = BN_bin2bn(key->priexp.val, key->priexp.len, NULL)) == NULL)
     3203 +                goto cleanup;
3146 3204  
3147      -        if (key->prime1.val != NULL)
3148      -                if ((rsa->p = BN_bin2bn(key->prime1.val, key->prime1.len,
3149      -                    rsa->p)) == NULL)
3150      -                        return (NULL);
     3205 +        if (key->prime1.val != NULL &&
     3206 +            (p = BN_bin2bn(key->prime1.val, key->prime1.len, NULL)) == NULL)
     3207 +                goto cleanup;
3151 3208  
3152      -        if (key->prime2.val != NULL)
3153      -                if ((rsa->q = BN_bin2bn(key->prime2.val, key->prime2.len,
3154      -                    rsa->q)) == NULL)
3155      -                        return (NULL);
     3209 +        if (key->prime2.val != NULL &&
     3210 +            (q = BN_bin2bn(key->prime2.val, key->prime2.len, NULL)) == NULL)
     3211 +                goto cleanup;
3156 3212  
3157      -        if (key->exp1.val != NULL)
3158      -                if ((rsa->dmp1 = BN_bin2bn(key->exp1.val, key->exp1.len,
3159      -                    rsa->dmp1)) == NULL)
3160      -                        return (NULL);
     3213 +        if (key->exp1.val != NULL &&
     3214 +            (dmp1 = BN_bin2bn(key->exp1.val, key->exp1.len, NULL)) == NULL)
     3215 +                goto cleanup;
3161 3216  
3162      -        if (key->exp2.val != NULL)
3163      -                if ((rsa->dmq1 = BN_bin2bn(key->exp2.val, key->exp2.len,
3164      -                    rsa->dmq1)) == NULL)
3165      -                        return (NULL);
     3217 +        if (key->exp2.val != NULL &&
     3218 +            (dmq1 = BN_bin2bn(key->exp2.val, key->exp2.len, NULL)) == NULL)
     3219 +                goto cleanup;
3166 3220  
3167      -        if (key->coef.val != NULL)
3168      -                if ((rsa->iqmp = BN_bin2bn(key->coef.val, key->coef.len,
3169      -                    rsa->iqmp)) == NULL)
3170      -                        return (NULL);
     3221 +        if (key->coef.val != NULL &&
     3222 +            (iqmp = BN_bin2bn(key->coef.val, key->coef.len, NULL)) == NULL)
     3223 +                goto cleanup;
3171 3224  
     3225 +        if (RSA_set0_key(rsa, n, e, d) == 0)
     3226 +                goto cleanup;
     3227 +        n = e = d = NULL;
     3228 +        if (RSA_set0_factors(rsa, p, q) == 0)
     3229 +                goto cleanup;
     3230 +        p = q = NULL;
     3231 +        if (RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp) == 0)
     3232 +                goto cleanup;
     3233 +        dmp1 = dmq1 = iqmp = NULL;
     3234 +
3172 3235          if ((newkey = EVP_PKEY_new()) == NULL)
3173      -                return (NULL);
     3236 +                goto cleanup;
3174 3237  
3175 3238          (void) EVP_PKEY_set1_RSA(newkey, rsa);
3176 3239  
     3240 +cleanup:
3177 3241          /* The original key must be freed once here or it leaks memory */
3178      -        RSA_free(rsa);
     3242 +        if (rsa)
     3243 +                RSA_free(rsa);
     3244 +        BN_free(n);
     3245 +        BN_free(e);
     3246 +        BN_free(d);
     3247 +        BN_free(p);
     3248 +        BN_free(q);
     3249 +        BN_free(dmp1);
     3250 +        BN_free(dmq1);
     3251 +        BN_free(iqmp);
3179 3252  
3180 3253          return (newkey);
3181 3254  }
3182 3255  
3183 3256  static EVP_PKEY *
3184 3257  ImportRawDSAKey(KMF_RAW_DSA_KEY *key)
3185 3258  {
3186 3259          DSA             *dsa = NULL;
3187      -        EVP_PKEY        *newkey = NULL;
     3260 +        EVP_PKEY        *newkey = NULL;
     3261 +        BIGNUM          *p = NULL, *q = NULL, *g = NULL,
     3262 +                        *priv_key = NULL, *pub_key = NULL;
3188 3263  
3189 3264          if ((dsa = DSA_new()) == NULL)
3190      -                return (NULL);
     3265 +                goto cleanup;
3191 3266  
3192      -        if ((dsa->p = BN_bin2bn(key->prime.val, key->prime.len,
3193      -            dsa->p)) == NULL)
3194      -                return (NULL);
     3267 +        if ((p = BN_bin2bn(key->prime.val, key->prime.len, NULL)) == NULL)
     3268 +                goto cleanup;
3195 3269  
3196      -        if ((dsa->q = BN_bin2bn(key->subprime.val, key->subprime.len,
3197      -            dsa->q)) == NULL)
3198      -                return (NULL);
     3270 +        if ((q = BN_bin2bn(key->subprime.val, key->subprime.len, NULL)) == NULL)
     3271 +                goto cleanup;
3199 3272  
3200      -        if ((dsa->g = BN_bin2bn(key->base.val, key->base.len,
3201      -            dsa->g)) == NULL)
3202      -                return (NULL);
     3273 +        if ((g = BN_bin2bn(key->base.val, key->base.len, NULL)) == NULL)
     3274 +                goto cleanup;
3203 3275  
3204      -        if ((dsa->priv_key = BN_bin2bn(key->value.val, key->value.len,
3205      -            dsa->priv_key)) == NULL)
3206      -                return (NULL);
     3276 +        if ((priv_key = BN_bin2bn(key->value.val, key->value.len,
     3277 +            NULL)) == NULL)
     3278 +                goto cleanup;
3207 3279  
3208      -        if (key->pubvalue.val != NULL) {
3209      -                if ((dsa->pub_key = BN_bin2bn(key->pubvalue.val,
3210      -                    key->pubvalue.len, dsa->pub_key)) == NULL)
3211      -                        return (NULL);
3212      -        }
     3280 +        if (key->pubvalue.val != NULL && (pub_key =
     3281 +            BN_bin2bn(key->pubvalue.val, key->pubvalue.len, NULL)) == NULL)
     3282 +                goto cleanup;
3213 3283  
     3284 +        if (DSA_set0_pqg(dsa, p, q, g) == 0)
     3285 +                goto cleanup;
     3286 +        p = q = g = NULL;
     3287 +        if (DSA_set0_key(dsa, pub_key, priv_key) == 0)
     3288 +                goto cleanup;
     3289 +        pub_key = priv_key = 0;
     3290 +
3214 3291          if ((newkey = EVP_PKEY_new()) == NULL)
3215      -                return (NULL);
     3292 +                goto cleanup;
3216 3293  
3217 3294          (void) EVP_PKEY_set1_DSA(newkey, dsa);
3218 3295  
     3296 +cleanup:
3219 3297          /* The original key must be freed once here or it leaks memory */
3220      -        DSA_free(dsa);
     3298 +        if (dsa)
     3299 +                DSA_free(dsa);
     3300 +        BN_free(p);
     3301 +        BN_free(q);
     3302 +        BN_free(g);
     3303 +        BN_free(priv_key);
     3304 +        BN_free(pub_key);
     3305 +
3221 3306          return (newkey);
3222 3307  }
3223 3308  
3224 3309  static EVP_PKEY *
3225 3310  raw_key_to_pkey(KMF_KEY_HANDLE *key)
3226 3311  {
3227 3312          EVP_PKEY *pkey = NULL;
3228 3313          KMF_RAW_KEY_DATA *rawkey;
3229 3314          ASN1_TYPE *attr = NULL;
3230 3315          KMF_RETURN ret;
↓ open down ↓ 494 lines elided ↑ open up ↑
3725 3810          if (x509_info_stack)
3726 3811                  sk_X509_INFO_free(x509_info_stack);
3727 3812  
3728 3813          if (cert_infos != NULL)
3729 3814                  free(cert_infos);
3730 3815  
3731 3816          return (rv);
3732 3817  }
3733 3818  
3734 3819  static KMF_RETURN
3735      -openssl_parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *pin,
     3820 +openssl_parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, char *pin,
3736 3821          STACK_OF(EVP_PKEY) *keys, STACK_OF(X509) *certs)
3737 3822  {
3738 3823          KMF_RETURN ret;
3739 3824          int i;
3740 3825  
3741 3826          for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
3742 3827                  /* LINTED E_BAD_PTR_CAST_ALIGN */
3743 3828                  PKCS12_SAFEBAG *bag = sk_PKCS12_SAFEBAG_value(bags, i);
3744 3829                  ret = openssl_parse_bag(bag, pin, (pin ? strlen(pin) : 0),
3745 3830                      keys, certs);
↓ open down ↓ 6 lines elided ↑ open up ↑
3752 3837  }
3753 3838  
3754 3839  static KMF_RETURN
3755 3840  set_pkey_attrib(EVP_PKEY *pkey, ASN1_TYPE *attrib, int nid)
3756 3841  {
3757 3842          X509_ATTRIBUTE *attr = NULL;
3758 3843  
3759 3844          if (pkey == NULL || attrib == NULL)
3760 3845                  return (KMF_ERR_BAD_PARAMETER);
3761 3846  
3762      -        if (pkey->attributes == NULL) {
3763      -                pkey->attributes = sk_X509_ATTRIBUTE_new_null();
3764      -                if (pkey->attributes == NULL)
3765      -                        return (KMF_ERR_MEMORY);
3766      -        }
3767 3847          attr = X509_ATTRIBUTE_create(nid, attrib->type, attrib->value.ptr);
3768 3848          if (attr != NULL) {
3769 3849                  int i;
3770      -                X509_ATTRIBUTE *a;
3771      -                for (i = 0;
3772      -                    i < sk_X509_ATTRIBUTE_num(pkey->attributes); i++) {
3773      -                        /* LINTED E_BAD_PTR_CASE_ALIGN */
3774      -                        a = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
3775      -                        if (OBJ_obj2nid(a->object) == nid) {
3776      -                                X509_ATTRIBUTE_free(a);
3777      -                                /* LINTED E_BAD_PTR_CAST_ALIGN */
3778      -                                (void) sk_X509_ATTRIBUTE_set(pkey->attributes,
3779      -                                    i, attr);
3780      -                                return (KMF_OK);
3781      -                        }
3782      -                }
3783      -                if (sk_X509_ATTRIBUTE_push(pkey->attributes, attr) == NULL) {
     3850 +
     3851 +                if ((i = EVP_PKEY_get_attr_by_NID(pkey, nid, -1)) != -1)
     3852 +                        (void) EVP_PKEY_delete_attr(pkey, i);
     3853 +                if (EVP_PKEY_add1_attr(pkey, attr) == 0) {
3784 3854                          X509_ATTRIBUTE_free(attr);
3785 3855                          return (KMF_ERR_MEMORY);
3786 3856                  }
3787 3857          } else {
3788 3858                  return (KMF_ERR_MEMORY);
3789 3859          }
3790 3860  
3791 3861          return (KMF_OK);
3792 3862  }
3793 3863  
3794 3864  static KMF_RETURN
3795 3865  openssl_parse_bag(PKCS12_SAFEBAG *bag, char *pass, int passlen,
3796 3866          STACK_OF(EVP_PKEY) *keylist, STACK_OF(X509) *certlist)
3797 3867  {
3798 3868          KMF_RETURN ret = KMF_OK;
3799 3869          PKCS8_PRIV_KEY_INFO *p8 = NULL;
3800 3870          EVP_PKEY *pkey = NULL;
3801 3871          X509 *xcert = NULL;
3802      -        ASN1_TYPE *keyid = NULL;
3803      -        ASN1_TYPE *fname = NULL;
     3872 +        const ASN1_TYPE *keyid = NULL;
     3873 +        const ASN1_TYPE *fname = NULL;
3804 3874          uchar_t *data = NULL;
3805 3875  
3806      -        keyid = PKCS12_get_attr(bag, NID_localKeyID);
3807      -        fname = PKCS12_get_attr(bag, NID_friendlyName);
     3876 +        keyid = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID);
     3877 +        fname = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName);
3808 3878  
3809      -        switch (M_PKCS12_bag_type(bag)) {
     3879 +        switch (PKCS12_SAFEBAG_get_nid(bag)) {
3810 3880                  case NID_keyBag:
3811 3881                          if (keylist == NULL)
3812 3882                                  goto end;
3813      -                        pkey = EVP_PKCS82PKEY(bag->value.keybag);
     3883 +                        pkey = EVP_PKCS82PKEY(
     3884 +                            PKCS12_SAFEBAG_get0_p8inf(bag));
3814 3885                          if (pkey == NULL)
3815 3886                                  ret = KMF_ERR_PKCS12_FORMAT;
3816 3887  
3817 3888                          break;
3818 3889                  case NID_pkcs8ShroudedKeyBag:
3819 3890                          if (keylist == NULL)
3820 3891                                  goto end;
3821      -                        p8 = M_PKCS12_decrypt_skey(bag, pass, passlen);
     3892 +                        p8 = PKCS12_decrypt_skey(bag, pass, passlen);
3822 3893                          if (p8 == NULL)
3823 3894                                  return (KMF_ERR_AUTH_FAILED);
3824 3895                          pkey = EVP_PKCS82PKEY(p8);
3825 3896                          PKCS8_PRIV_KEY_INFO_free(p8);
3826 3897                          if (pkey == NULL)
3827 3898                                  ret = KMF_ERR_PKCS12_FORMAT;
3828 3899                          break;
3829 3900                  case NID_certBag:
3830 3901                          if (certlist == NULL)
3831 3902                                  goto end;
3832      -                        if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
     3903 +                        if (PKCS12_SAFEBAG_get_bag_nid(bag) !=
     3904 +                            NID_x509Certificate)
3833 3905                                  return (KMF_ERR_PKCS12_FORMAT);
3834      -                        xcert = M_PKCS12_certbag2x509(bag);
     3906 +                        xcert = PKCS12_SAFEBAG_get1_cert(bag);
3835 3907                          if (xcert == NULL) {
3836 3908                                  ret = KMF_ERR_PKCS12_FORMAT;
3837 3909                                  goto end;
3838 3910                          }
3839 3911                          if (keyid != NULL) {
3840 3912                                  if (X509_keyid_set1(xcert,
3841 3913                                      keyid->value.octet_string->data,
3842 3914                                      keyid->value.octet_string->length) == 0) {
3843 3915                                          ret = KMF_ERR_PKCS12_FORMAT;
3844 3916                                          goto end;
↓ open down ↓ 13 lines elided ↑ open up ↑
3858 3930                                          ret = KMF_ERR_PKCS12_FORMAT;
3859 3931                                          goto end;
3860 3932                                  }
3861 3933                          }
3862 3934                          if (sk_X509_push(certlist, xcert) == 0)
3863 3935                                  ret = KMF_ERR_MEMORY;
3864 3936                          else
3865 3937                                  xcert = NULL;
3866 3938                          break;
3867 3939                  case NID_safeContentsBag:
3868      -                        return (openssl_parse_bags(bag->value.safes, pass,
3869      -                            keylist, certlist));
     3940 +                        return (openssl_parse_bags(
     3941 +                            PKCS12_SAFEBAG_get0_safes(bag),
     3942 +                            pass, keylist, certlist));
3870 3943                  default:
3871 3944                          ret = KMF_ERR_PKCS12_FORMAT;
3872 3945                          break;
3873 3946          }
3874 3947  
3875 3948          /*
3876 3949           * Set the ID and/or FriendlyName attributes on the key.
3877 3950           * If converting to PKCS11 objects, these can translate to CKA_ID
3878 3951           * and CKA_LABEL values.
3879 3952           */
↓ open down ↓ 189 lines elided ↑ open up ↑
4069 4142  
4070 4143          return (rv);
4071 4144  }
4072 4145  
4073 4146  static KMF_RETURN
4074 4147  exportRawRSAKey(RSA *rsa, KMF_RAW_KEY_DATA *key)
4075 4148  {
4076 4149          KMF_RETURN rv;
4077 4150          KMF_RAW_RSA_KEY *kmfkey = &key->rawdata.rsa;
4078 4151  
     4152 +        const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmpq, *iqmp;
     4153 +
     4154 +        RSA_get0_key(rsa, &n, &e, &d);
     4155 +        RSA_get0_factors(rsa, &p, &q);
     4156 +        RSA_get0_crt_params(rsa, &dmp1, &dmpq, &iqmp);
     4157 +
4079 4158          (void) memset(kmfkey, 0, sizeof (KMF_RAW_RSA_KEY));
4080      -        if ((rv = sslBN2KMFBN(rsa->n, &kmfkey->mod)) != KMF_OK)
     4159 +        if ((rv = sslBN2KMFBN((BIGNUM *)n, &kmfkey->mod)) != KMF_OK)
4081 4160                  goto cleanup;
4082 4161  
4083      -        if ((rv = sslBN2KMFBN(rsa->e, &kmfkey->pubexp)) != KMF_OK)
     4162 +        if ((rv = sslBN2KMFBN((BIGNUM *)e, &kmfkey->pubexp)) != KMF_OK)
4084 4163                  goto cleanup;
4085 4164  
4086      -        if (rsa->d != NULL)
4087      -                if ((rv = sslBN2KMFBN(rsa->d, &kmfkey->priexp)) != KMF_OK)
     4165 +        if (d != NULL)
     4166 +                if ((rv = sslBN2KMFBN((BIGNUM *)d, &kmfkey->priexp)) != KMF_OK)
4088 4167                          goto cleanup;
4089 4168  
4090      -        if (rsa->p != NULL)
4091      -                if ((rv = sslBN2KMFBN(rsa->p, &kmfkey->prime1)) != KMF_OK)
     4169 +        if (p != NULL)
     4170 +                if ((rv = sslBN2KMFBN((BIGNUM *)p, &kmfkey->prime1)) != KMF_OK)
4092 4171                          goto cleanup;
4093 4172  
4094      -        if (rsa->q != NULL)
4095      -                if ((rv = sslBN2KMFBN(rsa->q, &kmfkey->prime2)) != KMF_OK)
     4173 +        if (q != NULL)
     4174 +                if ((rv = sslBN2KMFBN((BIGNUM *)q, &kmfkey->prime2)) != KMF_OK)
4096 4175                          goto cleanup;
4097 4176  
4098      -        if (rsa->dmp1 != NULL)
4099      -                if ((rv = sslBN2KMFBN(rsa->dmp1, &kmfkey->exp1)) != KMF_OK)
     4177 +        if (dmp1 != NULL)
     4178 +                if ((rv = sslBN2KMFBN((BIGNUM *)dmp1, &kmfkey->exp1)) != KMF_OK)
4100 4179                          goto cleanup;
4101 4180  
4102      -        if (rsa->dmq1 != NULL)
4103      -                if ((rv = sslBN2KMFBN(rsa->dmq1, &kmfkey->exp2)) != KMF_OK)
     4181 +        if (dmpq != NULL)
     4182 +                if ((rv = sslBN2KMFBN((BIGNUM *)dmpq, &kmfkey->exp2)) != KMF_OK)
4104 4183                          goto cleanup;
4105 4184  
4106      -        if (rsa->iqmp != NULL)
4107      -                if ((rv = sslBN2KMFBN(rsa->iqmp, &kmfkey->coef)) != KMF_OK)
     4185 +        if (iqmp != NULL)
     4186 +                if ((rv = sslBN2KMFBN((BIGNUM *)iqmp, &kmfkey->coef)) != KMF_OK)
4108 4187                          goto cleanup;
4109 4188  cleanup:
4110 4189          if (rv != KMF_OK)
4111 4190                  kmf_free_raw_key(key);
4112 4191          else
4113 4192                  key->keytype = KMF_RSA;
4114 4193  
4115 4194          /*
4116 4195           * Free the reference to this key, SSL will not actually free
4117 4196           * the memory until the refcount == 0, so this is safe.
↓ open down ↓ 1 lines elided ↑ open up ↑
4119 4198          RSA_free(rsa);
4120 4199  
4121 4200          return (rv);
4122 4201  }
4123 4202  
4124 4203  static KMF_RETURN
4125 4204  exportRawDSAKey(DSA *dsa, KMF_RAW_KEY_DATA *key)
4126 4205  {
4127 4206          KMF_RETURN rv;
4128 4207          KMF_RAW_DSA_KEY *kmfkey = &key->rawdata.dsa;
     4208 +        const BIGNUM *p, *q, *g, *priv_key;
4129 4209  
     4210 +        DSA_get0_pqg(dsa, &p, &q, &g);
     4211 +        DSA_get0_key(dsa, NULL, &priv_key);
     4212 +
4130 4213          (void) memset(kmfkey, 0, sizeof (KMF_RAW_DSA_KEY));
4131      -        if ((rv = sslBN2KMFBN(dsa->p, &kmfkey->prime)) != KMF_OK)
     4214 +        if ((rv = sslBN2KMFBN((BIGNUM *)p, &kmfkey->prime)) != KMF_OK)
4132 4215                  goto cleanup;
4133 4216  
4134      -        if ((rv = sslBN2KMFBN(dsa->q, &kmfkey->subprime)) != KMF_OK)
     4217 +        if ((rv = sslBN2KMFBN((BIGNUM *)q, &kmfkey->subprime)) != KMF_OK)
4135 4218                  goto cleanup;
4136 4219  
4137      -        if ((rv = sslBN2KMFBN(dsa->g, &kmfkey->base)) != KMF_OK)
     4220 +        if ((rv = sslBN2KMFBN((BIGNUM *)g, &kmfkey->base)) != KMF_OK)
4138 4221                  goto cleanup;
4139 4222  
4140      -        if ((rv = sslBN2KMFBN(dsa->priv_key, &kmfkey->value)) != KMF_OK)
     4223 +        if ((rv = sslBN2KMFBN((BIGNUM *)priv_key, &kmfkey->value)) != KMF_OK)
4141 4224                  goto cleanup;
4142 4225  
4143 4226  cleanup:
4144 4227          if (rv != KMF_OK)
4145 4228                  kmf_free_raw_key(key);
4146 4229          else
4147 4230                  key->keytype = KMF_DSA;
4148 4231  
4149 4232          /*
4150 4233           * Free the reference to this key, SSL will not actually free
↓ open down ↓ 62 lines elided ↑ open up ↑
4213 4296                  return (KMF_ERR_MEMORY);
4214 4297  
4215 4298          list[n] = *newkey;
4216 4299          (*nkeys) = n + 1;
4217 4300  
4218 4301          *keylist = list;
4219 4302  
4220 4303          return (KMF_OK);
4221 4304  }
4222 4305  
4223      -static X509_ATTRIBUTE *
4224      -find_attr(STACK_OF(X509_ATTRIBUTE) *attrs, int nid)
4225      -{
4226      -        X509_ATTRIBUTE *a;
4227      -        int i;
4228      -
4229      -        if (attrs == NULL)
4230      -                return (NULL);
4231      -
4232      -        for (i = 0; i < sk_X509_ATTRIBUTE_num(attrs); i++) {
4233      -                /* LINTED E_BAD_PTR_CAST_ALIGN */
4234      -                a = sk_X509_ATTRIBUTE_value(attrs, i);
4235      -                if (OBJ_obj2nid(a->object) == nid)
4236      -                        return (a);
4237      -        }
4238      -        return (NULL);
4239      -}
4240      -
4241 4306  static KMF_RETURN
4242 4307  convertToRawKey(EVP_PKEY *pkey, KMF_RAW_KEY_DATA *key)
4243 4308  {
4244 4309          KMF_RETURN rv = KMF_OK;
4245 4310          X509_ATTRIBUTE *attr;
     4311 +        RSA *rsa;
     4312 +        DSA *dsa;
     4313 +        int loc;
4246 4314  
4247 4315          if (pkey == NULL || key == NULL)
4248 4316                  return (KMF_ERR_BAD_PARAMETER);
4249 4317          /* Convert SSL key to raw key */
4250      -        switch (pkey->type) {
4251      -                case EVP_PKEY_RSA:
4252      -                        rv = exportRawRSAKey(EVP_PKEY_get1_RSA(pkey),
4253      -                            key);
4254      -                        if (rv != KMF_OK)
4255      -                                return (rv);
4256      -                        break;
4257      -                case EVP_PKEY_DSA:
4258      -                        rv = exportRawDSAKey(EVP_PKEY_get1_DSA(pkey),
4259      -                            key);
4260      -                        if (rv != KMF_OK)
4261      -                                return (rv);
4262      -                        break;
4263      -                default:
4264      -                        return (KMF_ERR_BAD_PARAMETER);
4265      -        }
     4318 +        if ((rsa = EVP_PKEY_get1_RSA(pkey)) != NULL) {
     4319 +                rv = exportRawRSAKey(rsa, key);
     4320 +                if (rv != KMF_OK)
     4321 +                        return (rv);
     4322 +        } else if ((dsa = EVP_PKEY_get1_DSA(pkey)) != NULL) {
     4323 +                rv = exportRawDSAKey(dsa, key);
     4324 +                if (rv != KMF_OK)
     4325 +                        return (rv);
     4326 +        } else
     4327 +                return (KMF_ERR_BAD_PARAMETER);
     4328 +
4266 4329          /*
4267 4330           * If friendlyName, add it to record.
4268 4331           */
4269      -        attr = find_attr(pkey->attributes, NID_friendlyName);
4270      -        if (attr != NULL) {
     4332 +
     4333 +        if ((loc = EVP_PKEY_get_attr_by_NID(pkey,
     4334 +            NID_friendlyName, -1)) != -1 &&
     4335 +            (attr = EVP_PKEY_get_attr(pkey, loc))) {
4271 4336                  ASN1_TYPE *ty = NULL;
4272      -                int numattr = sk_ASN1_TYPE_num(attr->value.set);
4273      -                if (attr->single == 0 && numattr > 0) {
4274      -                        /* LINTED E_BAD_PTR_CAST_ALIGN */
4275      -                        ty = sk_ASN1_TYPE_value(attr->value.set, 0);
     4337 +                int numattr = X509_ATTRIBUTE_count(attr);
     4338 +                if (numattr > 0) {
     4339 +                        ty = X509_ATTRIBUTE_get0_type(attr, 0);
4276 4340                  }
4277 4341                  if (ty != NULL) {
4278      -#if OPENSSL_VERSION_NUMBER < 0x10000000L
4279      -                        key->label = uni2asc(ty->value.bmpstring->data,
4280      -                            ty->value.bmpstring->length);
4281      -#else
4282 4342                          key->label = OPENSSL_uni2asc(ty->value.bmpstring->data,
4283 4343                              ty->value.bmpstring->length);
4284      -#endif
4285 4344                  }
4286 4345          } else {
4287 4346                  key->label = NULL;
4288 4347          }
4289 4348  
4290 4349          /*
4291 4350           * If KeyID, add it to record as a KMF_DATA object.
4292 4351           */
4293      -        attr = find_attr(pkey->attributes, NID_localKeyID);
4294      -        if (attr != NULL) {
     4352 +        if ((loc = EVP_PKEY_get_attr_by_NID(pkey,
     4353 +            NID_localKeyID, -1)) != -1 &&
     4354 +            (attr = EVP_PKEY_get_attr(pkey, loc)) != NULL) {
4295 4355                  ASN1_TYPE *ty = NULL;
4296      -                int numattr = sk_ASN1_TYPE_num(attr->value.set);
4297      -                if (attr->single == 0 && numattr > 0) {
4298      -                        /* LINTED E_BAD_PTR_CAST_ALIGN */
4299      -                        ty = sk_ASN1_TYPE_value(attr->value.set, 0);
4300      -                }
     4356 +                int numattr = X509_ATTRIBUTE_count(attr);
     4357 +                if (numattr > 0)
     4358 +                        ty = X509_ATTRIBUTE_get0_type(attr, 0);
4301 4359                  key->id.Data = (uchar_t *)malloc(
4302 4360                      ty->value.octet_string->length);
4303 4361                  if (key->id.Data == NULL)
4304 4362                          return (KMF_ERR_MEMORY);
4305 4363                  (void) memcpy(key->id.Data, ty->value.octet_string->data,
4306 4364                      ty->value.octet_string->length);
4307 4365                  key->id.Length = ty->value.octet_string->length;
4308 4366          } else {
4309 4367                  (void) memset(&key->id, 0, sizeof (KMF_DATA));
4310 4368          }
↓ open down ↓ 1088 lines elided ↑ open up ↑
5399 5457                  xcert = PEM_read_bio_X509(in, NULL, NULL, NULL);
5400 5458          }
5401 5459  
5402 5460          if (xcert == NULL) {
5403 5461                  SET_ERROR(kmfh, ERR_get_error());
5404 5462                  ret = KMF_ERR_BAD_CERTFILE;
5405 5463                  goto end;
5406 5464          }
5407 5465  
5408 5466          /* Check if the certificate and the CRL have same issuer */
5409      -        if (X509_NAME_cmp(xcert->cert_info->issuer, xcrl->crl->issuer) != 0) {
     5467 +        if (X509_NAME_cmp(X509_get_issuer_name(xcert),
     5468 +            X509_CRL_get_issuer(xcrl)) != 0) {
5410 5469                  ret = KMF_ERR_ISSUER;
5411 5470                  goto end;
5412 5471          }
5413 5472  
5414 5473          /* Check to see if the certificate serial number is revoked */
5415 5474          revoke_stack = X509_CRL_get_REVOKED(xcrl);
5416 5475          if (sk_X509_REVOKED_num(revoke_stack) <= 0) {
5417 5476                  /* No revoked certificates in the CRL file */
5418 5477                  SET_ERROR(kmfh, ERR_get_error());
5419 5478                  ret = KMF_ERR_EMPTY_CRL;
5420 5479                  goto end;
5421 5480          }
5422 5481  
5423 5482          for (i = 0; i < sk_X509_REVOKED_num(revoke_stack); i++) {
5424 5483                  /* LINTED E_BAD_PTR_CAST_ALIGN */
5425 5484                  revoke = sk_X509_REVOKED_value(revoke_stack, i);
5426      -                if (ASN1_INTEGER_cmp(xcert->cert_info->serialNumber,
5427      -                    revoke->serialNumber) == 0) {
     5485 +                if (ASN1_INTEGER_cmp(X509_get_serialNumber(xcert),
     5486 +                    X509_REVOKED_get0_serialNumber(revoke)) == 0) {
5428 5487                          break;
5429 5488                  }
5430 5489          }
5431 5490  
5432 5491          if (i < sk_X509_REVOKED_num(revoke_stack)) {
5433 5492                  ret = KMF_OK;
5434 5493          } else {
5435 5494                  ret = KMF_ERR_NOT_REVOKED;
5436 5495          }
5437 5496  
↓ open down ↓ 122 lines elided ↑ open up ↑
5560 5619          if (crl_format == KMF_FORMAT_ASN1)
5561 5620                  xcrl = d2i_X509_CRL_bio(bcrl, NULL);
5562 5621          else if (crl_format == KMF_FORMAT_PEM)
5563 5622                  xcrl = PEM_read_bio_X509_CRL(bcrl, NULL, NULL, NULL);
5564 5623  
5565 5624          if (xcrl == NULL) {
5566 5625                  SET_ERROR(kmfh, ERR_get_error());
5567 5626                  ret = KMF_ERR_BAD_CRLFILE;
5568 5627                  goto cleanup;
5569 5628          }
5570      -        i = X509_cmp_time(X509_CRL_get_lastUpdate(xcrl), NULL);
     5629 +        i = X509_cmp_time(X509_CRL_get0_lastUpdate(xcrl), NULL);
5571 5630          if (i >= 0) {
5572 5631                  ret = KMF_ERR_VALIDITY_PERIOD;
5573 5632                  goto cleanup;
5574 5633          }
5575      -        if (X509_CRL_get_nextUpdate(xcrl)) {
5576      -                i = X509_cmp_time(X509_CRL_get_nextUpdate(xcrl), NULL);
     5634 +        if (X509_CRL_get0_nextUpdate(xcrl)) {
     5635 +                i = X509_cmp_time(X509_CRL_get0_nextUpdate(xcrl), NULL);
5577 5636  
5578 5637                  if (i <= 0) {
5579 5638                          ret = KMF_ERR_VALIDITY_PERIOD;
5580 5639                          goto cleanup;
5581 5640                  }
5582 5641          }
5583 5642  
5584 5643          ret = KMF_OK;
5585 5644  
5586 5645  cleanup:
5587 5646          if (bcrl != NULL)
5588 5647                  (void) BIO_free(bcrl);
5589 5648  
5590 5649          if (xcrl != NULL)
5591 5650                  X509_CRL_free(xcrl);
5592 5651  
5593 5652          return (ret);
5594 5653  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX