Print this page
8982 Support building with OpenSSL 1.1

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/krb5/plugins/preauth/pkinit/pkinit_crypto_openssl.c
          +++ new/usr/src/lib/krb5/plugins/preauth/pkinit/pkinit_crypto_openssl.c
↓ open down ↓ 23 lines elided ↑ open up ↑
  24   24   * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
  25   25   * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
  26   26   * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
  27   27   * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
  28   28   * SUCH DAMAGES.
  29   29   */
  30   30  
  31   31  /*
  32   32   * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  33   33   * Copyright (c) 2012, OmniTI Computer Consulting, Inc. All rights reserved.
       34 + * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
  34   35   */
  35   36  
  36   37  #include <errno.h>
  37   38  #include <string.h>
  38   39  #include <stdio.h>
  39   40  #include <stdlib.h>
  40   41  #include <dlfcn.h>
  41   42  #include <unistd.h>
  42   43  #include <dirent.h>
  43   44  
↓ open down ↓ 318 lines elided ↑ open up ↑
 362  363      0x23, 0x3B, 0xA1, 0x86, 0x51, 0x5B, 0xE7, 0xED,
 363  364      0x1F, 0x61, 0x29, 0x70, 0xCE, 0xE2, 0xD7, 0xAF,
 364  365      0xB8, 0x1B, 0xDD, 0x76, 0x21, 0x70, 0x48, 0x1C,
 365  366      0xD0, 0x06, 0x91, 0x27, 0xD5, 0xB0, 0x5A, 0xA9,
 366  367      0x93, 0xB4, 0xEA, 0x98, 0x8D, 0x8F, 0xDD, 0xC1,
 367  368      0x86, 0xFF, 0xB7, 0xDC, 0x90, 0xA6, 0xC0, 0x8F,
 368  369      0x4D, 0xF4, 0x35, 0xC9, 0x34, 0x06, 0x31, 0x99,
 369  370      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
 370  371  };
 371  372  
      373 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
      374 +/*
      375 + * Many things have changed in OpenSSL 1.1. The code in this file has been
      376 + * updated to use the v1.1 APIs but some are new and require emulation
      377 + * for older OpenSSL versions.
      378 + */
      379 +
      380 +/* EVP_MD_CTX construct and destructor names have changed */
      381 +
      382 +#define EVP_MD_CTX_new EVP_MD_CTX_create
      383 +#define EVP_MD_CTX_free EVP_MD_CTX_destroy
      384 +
      385 +/* ASN1_STRING_data is deprecated */
      386 +#define ASN1_STRING_get0_data ASN1_STRING_data
      387 +
      388 +/* X509_STORE_CTX_trusted_stack is deprecated */
      389 +#define X509_STORE_CTX_set0_trusted_stack X509_STORE_CTX_trusted_stack
      390 +
      391 +/* get_rfc2409_prime_1024() has been renamed. */
      392 +#define BN_get_rfc2409_prime_1024 get_rfc2409_prime_1024
      393 +
      394 +#define OBJ_get0_data(o) ((o)->data)
      395 +#define OBJ_length(o) ((o)->length)
      396 +
      397 +/* Some new DH functions that aren't in OpenSSL 1.0.x */
      398 +#define DH_bits(dh) BN_num_bits((dh)->p);
      399 +
      400 +#define DH_set0_pqg(dh, p, q, g) __DH_set0_pqg(dh, p, q, g)
      401 +static int
      402 +__DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
      403 +{
      404 +    if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
      405 +        return 0;
      406 +
      407 +    if (p != NULL) {
      408 +        BN_free(dh->p);
      409 +        dh->p = p;
      410 +    }
      411 +    if (q != NULL) {
      412 +        BN_free(dh->q);
      413 +        dh->q = q;
      414 +    }
      415 +    if (g != NULL) {
      416 +        BN_free(dh->g);
      417 +        dh->g = g;
      418 +    }
      419 +
      420 +    if (q != NULL) {
      421 +        dh->length = BN_num_bits(q);
      422 +    }
      423 +
      424 +    return 1;
      425 +}
      426 +
      427 +#define DH_get0_pqg(dh, p, q, g) __DH_get0_pqg(dh, p, q, g)
      428 +static void
      429 +__DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q,
      430 +    const BIGNUM **g)
      431 +{
      432 +    if (p != NULL)
      433 +        *p = dh->p;
      434 +    if (q != NULL)
      435 +        *q = dh->q;
      436 +    if (g != NULL)
      437 +        *g = dh->g;
      438 +}
      439 +
      440 +#define DH_set0_key(dh, pub, priv) __DH_set0_key(dh, pub, priv)
      441 +static int
      442 +__DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
      443 +{
      444 +    if (pub_key != NULL) {
      445 +        BN_free(dh->pub_key);
      446 +        dh->pub_key = pub_key;
      447 +    }
      448 +    if (priv_key != NULL) {
      449 +        BN_free(dh->priv_key);
      450 +        dh->priv_key = priv_key;
      451 +    }
      452 +
      453 +    return 1;
      454 +}
      455 +
      456 +#define DH_get0_key(dh, pub, priv) __DH_get0_key(dh, pub, priv)
      457 +static void
      458 +__DH_get0_key(const DH *dh, const BIGNUM **pub, const BIGNUM **priv)
      459 +{
      460 +    if (pub != NULL)
      461 +        *pub = dh->pub_key;
      462 +    if (priv != NULL)
      463 +        *priv = dh->priv_key;
      464 +}
      465 +
      466 +#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
      467 +
 372  468  /* Solaris Kerberos */
 373  469  static k5_mutex_t oids_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
 374  470  static int pkinit_oids_refs = 0;
 375  471  
 376  472  krb5_error_code
 377  473  pkinit_init_plg_crypto(pkinit_plg_crypto_context *cryptoctx) {
 378  474  
 379  475      krb5_error_code retval = ENOMEM;
 380  476      pkinit_plg_crypto_context ctx = NULL;
 381  477  
↓ open down ↓ 135 lines elided ↑ open up ↑
 517  613      #define CREATE_OBJ_IF_NEEDED(oid, vn, sn, ln) \
 518  614          nid = OBJ_txt2nid(oid); \
 519  615          if (nid == NID_undef) { \
 520  616              nid = OBJ_create(oid, sn, ln); \
 521  617              if (nid == NID_undef) { \
 522  618                  pkiDebug("Error creating oid object for '%s'\n", oid); \
 523  619                  goto out; \
 524  620              } \
 525  621          } \
 526  622          ctx->vn = OBJ_nid2obj(nid);
 527      -    
      623 +
 528  624      /* Solaris Kerberos */
 529  625      retval = k5_mutex_lock(&oids_mutex);
 530  626      if (retval != 0)
 531  627          goto out;
 532  628  
 533  629      CREATE_OBJ_IF_NEEDED("1.3.6.1.5.2.2", id_pkinit_san,
 534  630                           "id-pkinit-san", "KRB5PrincipalName");
 535  631  
 536  632      CREATE_OBJ_IF_NEEDED("1.3.6.1.5.2.3.1", id_pkinit_authData,
 537  633                           "id-pkinit-authdata", "PKINIT signedAuthPack");
↓ open down ↓ 22 lines elided ↑ open up ↑
 560  656                           "id-ms-kp-sc-logon EKU", "Microsoft SmartCard Login EKU");
 561  657  
 562  658      CREATE_OBJ_IF_NEEDED("1.3.6.1.4.1.311.20.2.3", id_ms_san_upn,
 563  659                           "id-ms-san-upn", "Microsoft Universal Principal Name");
 564  660  
 565  661      CREATE_OBJ_IF_NEEDED("1.3.6.1.5.5.7.3.1", id_kp_serverAuth,
 566  662                           "id-kp-serverAuth EKU", "Server Authentication EKU");
 567  663  
 568  664      /* Success */
 569  665      retval = 0;
 570      -    
      666 +
 571  667      pkinit_oids_refs++;
 572  668      /* Solaris Kerberos */
 573  669      k5_mutex_unlock(&oids_mutex);
 574  670  
 575  671  out:
 576  672      return retval;
 577  673  }
 578  674  
 579  675  static krb5_error_code
 580  676  get_cert(char *filename, X509 **retcert)
↓ open down ↓ 68 lines elided ↑ open up ↑
 649  745  
 650  746  static void
 651  747  pkinit_fini_pkinit_oids(pkinit_plg_crypto_context ctx)
 652  748  {
 653  749      if (ctx == NULL)
 654  750          return;
 655  751  
 656  752      /* Only call OBJ_cleanup once! */
 657  753      /* Solaris Kerberos: locking */
 658  754      k5_mutex_lock(&oids_mutex);
      755 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
      756 +    /*
      757 +     * In OpenSSL versions prior to 1.1.0, OBJ_cleanup() cleaned up OpenSSL's
      758 +     * internal object table. This function is deprecated in version 1.1.0.
      759 +     * No explicit de-initialisation is now required.
      760 +     */
 659  761      if (--pkinit_oids_refs == 0)
 660  762          OBJ_cleanup();
      763 +#else
      764 +    pkinit_oids_refs--;
      765 +#endif
 661  766      k5_mutex_unlock(&oids_mutex);
 662  767  }
 663  768  
      769 +/* Construct an OpenSSL DH object for an Oakley group. */
      770 +static DH *
      771 +make_dhprime(uint8_t *prime, size_t len)
      772 +{
      773 +    DH *dh = NULL;
      774 +    BIGNUM *p = NULL, *q = NULL, *g = NULL;
      775 +
      776 +    if ((p = BN_bin2bn(prime, len, NULL)) == NULL)
      777 +        goto cleanup;
      778 +    if ((q = BN_new()) == NULL)
      779 +        goto cleanup;
      780 +    if (!BN_rshift1(q, p))
      781 +        goto cleanup;
      782 +    if ((g = BN_new()) == NULL)
      783 +        goto cleanup;
      784 +    if (!BN_set_word(g, DH_GENERATOR_2))
      785 +        goto cleanup;
      786 +
      787 +    dh = DH_new();
      788 +    if (dh == NULL)
      789 +        goto cleanup;
      790 +    DH_set0_pqg(dh, p, q, g);
      791 +    p = g = q = NULL;
      792 +
      793 +cleanup:
      794 +    BN_free(p);
      795 +    BN_free(q);
      796 +    BN_free(g);
      797 +    return dh;
      798 +}
      799 +
 664  800  static krb5_error_code
 665  801  pkinit_init_dh_params(pkinit_plg_crypto_context plgctx)
 666  802  {
 667  803      krb5_error_code retval = ENOMEM;
 668  804  
 669      -    plgctx->dh_1024 = DH_new();
      805 +    plgctx->dh_1024 = make_dhprime(pkinit_1024_dhprime,
      806 +        sizeof(pkinit_1024_dhprime));
 670  807      if (plgctx->dh_1024 == NULL)
 671  808          goto cleanup;
 672      -    plgctx->dh_1024->p = BN_bin2bn(pkinit_1024_dhprime,
 673      -        sizeof(pkinit_1024_dhprime), NULL);
 674      -    if ((plgctx->dh_1024->g = BN_new()) == NULL ||
 675      -        (plgctx->dh_1024->q = BN_new()) == NULL)
 676      -        goto cleanup;
 677      -    BN_set_word(plgctx->dh_1024->g, DH_GENERATOR_2);
 678      -    BN_rshift1(plgctx->dh_1024->q, plgctx->dh_1024->p);
 679  809  
 680      -    plgctx->dh_2048 = DH_new();
      810 +    plgctx->dh_2048 = make_dhprime(pkinit_2048_dhprime,
      811 +        sizeof(pkinit_2048_dhprime));
 681  812      if (plgctx->dh_2048 == NULL)
 682  813          goto cleanup;
 683      -    plgctx->dh_2048->p = BN_bin2bn(pkinit_2048_dhprime,
 684      -        sizeof(pkinit_2048_dhprime), NULL);
 685      -    if ((plgctx->dh_2048->g = BN_new()) == NULL ||
 686      -        (plgctx->dh_2048->q = BN_new()) == NULL)
 687      -        goto cleanup;
 688      -    BN_set_word(plgctx->dh_2048->g, DH_GENERATOR_2);
 689      -    BN_rshift1(plgctx->dh_2048->q, plgctx->dh_2048->p);
 690  814  
 691      -    plgctx->dh_4096 = DH_new();
      815 +    plgctx->dh_4096 = make_dhprime(pkinit_4096_dhprime,
      816 +        sizeof(pkinit_4096_dhprime));
 692  817      if (plgctx->dh_4096 == NULL)
 693  818          goto cleanup;
 694      -    plgctx->dh_4096->p = BN_bin2bn(pkinit_4096_dhprime,
 695      -        sizeof(pkinit_4096_dhprime), NULL);
 696      -    if ((plgctx->dh_4096->g = BN_new()) == NULL ||
 697      -        (plgctx->dh_4096->q = BN_new()) == NULL)
 698      -        goto cleanup;
 699      -    BN_set_word(plgctx->dh_4096->g, DH_GENERATOR_2);
 700      -    BN_rshift1(plgctx->dh_4096->q, plgctx->dh_4096->p);
 701  819  
 702  820      retval = 0;
 703  821  
 704  822  cleanup:
 705  823      if (retval)
 706  824          pkinit_fini_dh_params(plgctx);
 707  825  
 708  826      return retval;
 709  827  }
 710  828  
↓ open down ↓ 140 lines elided ↑ open up ↑
 851  969  {
 852  970      /* Solaris Kerberos */
 853  971      krb5_error_code retval = KRB5KRB_ERR_GENERIC;
 854  972      PKCS7  *p7 = NULL, *inner_p7 = NULL;
 855  973      PKCS7_SIGNED *p7s = NULL;
 856  974      PKCS7_SIGNER_INFO *p7si = NULL;
 857  975      unsigned char *p;
 858  976      ASN1_TYPE *pkinit_data = NULL;
 859  977      STACK_OF(X509) * cert_stack = NULL;
 860  978      ASN1_OCTET_STRING *digest_attr = NULL;
 861      -    EVP_MD_CTX ctx, ctx2;
      979 +    EVP_MD_CTX *ctx = NULL, *ctx2 = NULL;
 862  980      const EVP_MD *md_tmp = NULL;
 863  981      unsigned char md_data[EVP_MAX_MD_SIZE], md_data2[EVP_MAX_MD_SIZE];
 864  982      unsigned char *digestInfo_buf = NULL, *abuf = NULL;
 865  983      unsigned int md_len, md_len2, alen, digestInfo_len;
 866  984      STACK_OF(X509_ATTRIBUTE) * sk;
 867  985      unsigned char *sig = NULL;
 868  986      unsigned int sig_len = 0;
 869  987      X509_ALGOR *alg = NULL;
 870  988      ASN1_OCTET_STRING *digest = NULL;
 871  989      unsigned int alg_len = 0, digest_len = 0;
↓ open down ↓ 23 lines elided ↑ open up ↑
 895 1013      if ((cert_stack = sk_X509_new_null()) == NULL)
 896 1014          goto cleanup;
 897 1015  
 898 1016      cert = sk_X509_value(id_cryptoctx->my_certs, id_cryptoctx->cert_index);
 899 1017      if (!include_certchain) {
 900 1018          pkiDebug("only including signer's certificate\n");
 901 1019          sk_X509_push(cert_stack, X509_dup(cert));
 902 1020      } else {
 903 1021          /* create a cert chain */
 904 1022          X509_STORE *certstore = NULL;
 905      -        X509_STORE_CTX certctx;
     1023 +        X509_STORE_CTX *certctx;
 906 1024          STACK_OF(X509) *certstack = NULL;
 907 1025          char buf[DN_BUF_LEN];
 908 1026          int i = 0, size = 0;
 909 1027  
 910 1028          if ((certstore = X509_STORE_new()) == NULL)
 911 1029              goto cleanup;
     1030 +        if ((certctx = X509_STORE_CTX_new()) == NULL)
     1031 +            goto cleanup;
 912 1032          pkiDebug("building certificate chain\n");
 913      -        X509_STORE_set_verify_cb_func(certstore, openssl_callback);
 914      -        X509_STORE_CTX_init(&certctx, certstore, cert,
     1033 +        X509_STORE_set_verify_cb(certstore, openssl_callback);
     1034 +        X509_STORE_CTX_init(certctx, certstore, cert,
 915 1035                              id_cryptoctx->intermediateCAs);
 916      -        X509_STORE_CTX_trusted_stack(&certctx, id_cryptoctx->trustedCAs);
     1036 +        X509_STORE_CTX_set0_trusted_stack(certctx, id_cryptoctx->trustedCAs);
 917 1037          /* Solaris Kerberos */
 918      -        if (X509_verify_cert(&certctx) <= 0) {
 919      -            pkiDebug("failed to create a certificate chain: %s\n", 
 920      -            X509_verify_cert_error_string(X509_STORE_CTX_get_error(&certctx)));
 921      -            if (!sk_X509_num(id_cryptoctx->trustedCAs)) 
     1038 +        if (X509_verify_cert(certctx) <= 0) {
     1039 +            pkiDebug("failed to create a certificate chain: %s\n",
     1040 +            X509_verify_cert_error_string(X509_STORE_CTX_get_error(certctx)));
     1041 +            if (!sk_X509_num(id_cryptoctx->trustedCAs))
 922 1042                  pkiDebug("No trusted CAs found. Check your X509_anchors\n");
 923 1043              goto cleanup;
 924 1044          }
 925      -        certstack = X509_STORE_CTX_get1_chain(&certctx);
     1045 +        certstack = X509_STORE_CTX_get1_chain(certctx);
 926 1046          size = sk_X509_num(certstack);
 927 1047          pkiDebug("size of certificate chain = %d\n", size);
 928 1048          for(i = 0; i < size - 1; i++) {
 929 1049              X509 *x = sk_X509_value(certstack, i);
 930 1050              X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof(buf));
 931 1051              pkiDebug("cert #%d: %s\n", i, buf);
 932 1052              sk_X509_push(cert_stack, X509_dup(x));
 933 1053          }
 934      -        X509_STORE_CTX_cleanup(&certctx);
     1054 +        X509_STORE_CTX_free(certctx);
 935 1055          X509_STORE_free(certstore);
 936 1056          sk_X509_pop_free(certstack, X509_free);
 937 1057      }
 938 1058      p7s->cert = cert_stack;
 939 1059  
 940 1060      /* fill-in PKCS7_SIGNER_INFO */
 941 1061      if ((p7si = PKCS7_SIGNER_INFO_new()) == NULL)
 942 1062          goto cleanup;
 943 1063      if (!ASN1_INTEGER_set(p7si->version, 1))
 944 1064          goto cleanup;
 945 1065      if (!X509_NAME_set(&p7si->issuer_and_serial->issuer,
 946 1066                         X509_get_issuer_name(cert)))
 947 1067          goto cleanup;
 948 1068      /* because ASN1_INTEGER_set is used to set a 'long' we will do
 949 1069       * things the ugly way. */
 950      -    M_ASN1_INTEGER_free(p7si->issuer_and_serial->serial);
     1070 +    ASN1_INTEGER_free(p7si->issuer_and_serial->serial);
 951 1071      if (!(p7si->issuer_and_serial->serial =
 952      -          M_ASN1_INTEGER_dup(X509_get_serialNumber(cert))))
     1072 +          ASN1_INTEGER_dup(X509_get_serialNumber(cert))))
 953 1073          goto cleanup;
 954 1074  
 955 1075      /* will not fill-out EVP_PKEY because it's on the smartcard */
 956 1076  
 957 1077      /* Set digest algs */
 958 1078      p7si->digest_alg->algorithm = OBJ_nid2obj(NID_sha1);
 959 1079  
 960 1080      if (p7si->digest_alg->parameter != NULL)
 961 1081          ASN1_TYPE_free(p7si->digest_alg->parameter);
 962 1082      if ((p7si->digest_alg->parameter = ASN1_TYPE_new()) == NULL)
↓ open down ↓ 13 lines elided ↑ open up ↑
 976 1096      if (oid == NULL)
 977 1097          goto cleanup;
 978 1098  
 979 1099      if (cms_msg_type == CMS_SIGN_DRAFT9) {
 980 1100          /* don't include signed attributes for pa-type 15 request */
 981 1101          abuf = data;
 982 1102          alen = data_len;
 983 1103      } else {
 984 1104          /* add signed attributes */
 985 1105          /* compute sha1 digest over the EncapsulatedContentInfo */
 986      -        EVP_MD_CTX_init(&ctx);
 987      -        EVP_DigestInit_ex(&ctx, EVP_sha1(), NULL);
 988      -        EVP_DigestUpdate(&ctx, data, data_len);
 989      -        md_tmp = EVP_MD_CTX_md(&ctx);
 990      -        EVP_DigestFinal_ex(&ctx, md_data, &md_len);
     1106 +        ctx = EVP_MD_CTX_new();
     1107 +        if (ctx == NULL)
     1108 +            goto cleanup2;
     1109 +        EVP_MD_CTX_init(ctx);
     1110 +        EVP_DigestInit_ex(ctx, EVP_sha1(), NULL);
     1111 +        EVP_DigestUpdate(ctx, data, data_len);
     1112 +        md_tmp = EVP_MD_CTX_md(ctx);
     1113 +        EVP_DigestFinal_ex(ctx, md_data, &md_len);
     1114 +        EVP_MD_CTX_free(ctx);
     1115 +        ctx = NULL;
 991 1116  
 992 1117          /* create a message digest attr */
 993 1118          digest_attr = ASN1_OCTET_STRING_new();
 994 1119          ASN1_OCTET_STRING_set(digest_attr, md_data, (int)md_len);
 995 1120          PKCS7_add_signed_attribute(p7si, NID_pkcs9_messageDigest,
 996 1121                                     V_ASN1_OCTET_STRING, (char *) digest_attr);
 997 1122  
 998 1123          /* create a content-type attr */
 999      -        PKCS7_add_signed_attribute(p7si, NID_pkcs9_contentType, 
     1124 +        PKCS7_add_signed_attribute(p7si, NID_pkcs9_contentType,
1000 1125                                     V_ASN1_OBJECT, oid);
1001 1126  
1002 1127          /* create the signature over signed attributes. get DER encoded value */
1003 1128          /* This is the place where smartcard signature needs to be calculated */
1004 1129          sk = p7si->auth_attr;
1005 1130          alen = ASN1_item_i2d((ASN1_VALUE *) sk, &abuf,
1006 1131                               ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
1007 1132          if (abuf == NULL)
1008 1133              goto cleanup2;
1009 1134      }
1010 1135  
1011 1136  #ifndef WITHOUT_PKCS11
1012 1137      /* Some tokens can only do RSAEncryption without sha1 hash */
1013 1138      /* to compute sha1WithRSAEncryption, encode the algorithm ID for the hash
1014 1139       * function and the hash value into an ASN.1 value of type DigestInfo
1015 1140       * DigestInfo::=SEQUENCE {
1016 1141       *  digestAlgorithm  AlgorithmIdentifier,
1017 1142       *  digest OCTET STRING }
1018 1143       */
1019      -    if (id_cryptoctx->pkcs11_method == 1 && 
     1144 +    if (id_cryptoctx->pkcs11_method == 1 &&
1020 1145              id_cryptoctx->mech == CKM_RSA_PKCS) {
1021 1146          pkiDebug("mech = CKM_RSA_PKCS\n");
1022      -        EVP_MD_CTX_init(&ctx2);
     1147 +        ctx2 = EVP_MD_CTX_new();
     1148 +        if (ctx2 == NULL)
     1149 +            goto cleanup2;
     1150 +        EVP_MD_CTX_init(ctx2);
1023 1151          /* if this is not draft9 request, include digest signed attribute */
1024      -        if (cms_msg_type != CMS_SIGN_DRAFT9) 
1025      -            EVP_DigestInit_ex(&ctx2, md_tmp, NULL);
     1152 +        if (cms_msg_type != CMS_SIGN_DRAFT9)
     1153 +            EVP_DigestInit_ex(ctx2, md_tmp, NULL);
1026 1154          else
1027      -            EVP_DigestInit_ex(&ctx2, EVP_sha1(), NULL);
1028      -        EVP_DigestUpdate(&ctx2, abuf, alen);
1029      -        EVP_DigestFinal_ex(&ctx2, md_data2, &md_len2);
     1155 +            EVP_DigestInit_ex(ctx2, EVP_sha1(), NULL);
     1156 +        EVP_DigestUpdate(ctx2, abuf, alen);
     1157 +        EVP_DigestFinal_ex(ctx2, md_data2, &md_len2);
     1158 +        EVP_MD_CTX_free(ctx2);
     1159 +        ctx2 = NULL;
1030 1160  
1031 1161          alg = X509_ALGOR_new();
1032 1162          if (alg == NULL)
1033 1163              goto cleanup2;
1034 1164          alg->algorithm = OBJ_nid2obj(NID_sha1);
1035 1165          alg->parameter = NULL;
1036 1166          alg_len = i2d_X509_ALGOR(alg, NULL);
1037 1167          alg_buf = (unsigned char *)malloc(alg_len);
1038 1168          if (alg_buf == NULL)
1039 1169              goto cleanup2;
↓ open down ↓ 27 lines elided ↑ open up ↑
1067 1197  #endif
1068 1198      {
1069 1199          pkiDebug("mech = %s\n",
1070 1200              id_cryptoctx->pkcs11_method == 1 ? "CKM_SHA1_RSA_PKCS" : "FS");
1071 1201          retval = pkinit_sign_data(context, id_cryptoctx, abuf, alen,
1072 1202                                    &sig, &sig_len);
1073 1203      }
1074 1204  #ifdef DEBUG_SIG
1075 1205      print_buffer(sig, sig_len);
1076 1206  #endif
1077      -    if (cms_msg_type != CMS_SIGN_DRAFT9) 
     1207 +    if (cms_msg_type != CMS_SIGN_DRAFT9)
1078 1208          free(abuf);
1079 1209      if (retval)
1080 1210          goto cleanup2;
1081 1211  
1082 1212      /* Add signature */
1083 1213      if (!ASN1_STRING_set(p7si->enc_digest, (unsigned char *) sig,
1084 1214                           (int)sig_len)) {
1085 1215          unsigned long err = ERR_peek_error();
1086 1216          retval = KRB5KDC_ERR_PREAUTH_FAILED;
1087 1217          krb5_set_error_message(context, retval, "%s\n",
↓ open down ↓ 64 lines elided ↑ open up ↑
1152 1282              print_buffer_bin(*signed_data, *signed_data_len,
1153 1283                               "/tmp/kdc_pkcs7_signeddata");
1154 1284          } else {
1155 1285              print_buffer_bin(*signed_data, *signed_data_len,
1156 1286                               "/tmp/draft9_pkcs7_signeddata");
1157 1287          }
1158 1288      }
1159 1289  #endif
1160 1290  
1161 1291    cleanup2:
1162      -    if (cms_msg_type != CMS_SIGN_DRAFT9) 
1163      -        EVP_MD_CTX_cleanup(&ctx);
     1292 +    if (cms_msg_type != CMS_SIGN_DRAFT9)
     1293 +        if (ctx != NULL)
     1294 +                EVP_MD_CTX_free(ctx);
1164 1295  #ifndef WITHOUT_PKCS11
1165      -    if (id_cryptoctx->pkcs11_method == 1 && 
     1296 +    if (id_cryptoctx->pkcs11_method == 1 &&
1166 1297              id_cryptoctx->mech == CKM_RSA_PKCS) {
1167      -        EVP_MD_CTX_cleanup(&ctx2);
     1298 +        if (ctx2 != NULL)
     1299 +                EVP_MD_CTX_free(ctx2);
1168 1300          if (digest_buf != NULL)
1169 1301              free(digest_buf);
1170 1302          if (digestInfo_buf != NULL)
1171 1303              free(digestInfo_buf);
1172 1304          if (alg_buf != NULL)
1173 1305              free(alg_buf);
1174 1306          if (digest != NULL)
1175 1307              ASN1_OCTET_STRING_free(digest);
1176 1308      }
1177 1309  #endif
1178 1310      if (alg != NULL)
1179 1311          X509_ALGOR_free(alg);
1180 1312    cleanup:
1181      -    if (p7 != NULL) 
     1313 +    if (p7 != NULL)
1182 1314          PKCS7_free(p7);
1183 1315      if (sig != NULL)
1184 1316          free(sig);
1185 1317  
1186 1318      return retval;
1187 1319  }
1188 1320  
1189 1321  krb5_error_code
1190 1322  cms_signeddata_verify(krb5_context context,
1191 1323                        pkinit_plg_crypto_context plgctx,
↓ open down ↓ 11 lines elided ↑ open up ↑
1203 1335      krb5_error_code retval = KRB5KDC_ERR_PREAUTH_FAILED;
1204 1336      PKCS7 *p7 = NULL;
1205 1337      BIO *out = NULL;
1206 1338      int flags = PKCS7_NOVERIFY, i = 0;
1207 1339      unsigned int vflags = 0, size = 0;
1208 1340      const unsigned char *p = signed_data;
1209 1341      STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
1210 1342      PKCS7_SIGNER_INFO *si = NULL;
1211 1343      X509 *x = NULL;
1212 1344      X509_STORE *store = NULL;
1213      -    X509_STORE_CTX cert_ctx;
     1345 +    X509_STORE_CTX *cert_ctx;
1214 1346      STACK_OF(X509) *intermediateCAs = NULL;
1215 1347      STACK_OF(X509_CRL) *revoked = NULL;
1216 1348      STACK_OF(X509) *verified_chain = NULL;
1217 1349      ASN1_OBJECT *oid = NULL;
1218 1350      krb5_external_principal_identifier **krb5_verified_chain = NULL;
1219 1351      krb5_data *authz = NULL;
1220 1352      char buf[DN_BUF_LEN];
1221 1353  
1222 1354  #ifdef DEBUG_ASN1
1223 1355      print_buffer_bin(signed_data, signed_data_len,
↓ open down ↓ 23 lines elided ↑ open up ↑
1247 1379          goto cleanup;
1248 1380      }
1249 1381  
1250 1382      /* setup to verify X509 certificate used to sign PKCS7 message */
1251 1383      if (!(store = X509_STORE_new()))
1252 1384          goto cleanup;
1253 1385  
1254 1386      /* check if we are inforcing CRL checking */
1255 1387      vflags = X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
1256 1388      if (require_crl_checking)
1257      -        X509_STORE_set_verify_cb_func(store, openssl_callback);
     1389 +        X509_STORE_set_verify_cb(store, openssl_callback);
1258 1390      else
1259      -        X509_STORE_set_verify_cb_func(store, openssl_callback_ignore_crls);
     1391 +        X509_STORE_set_verify_cb(store, openssl_callback_ignore_crls);
1260 1392      X509_STORE_set_flags(store, vflags);
1261 1393  
1262 1394      /* get the signer's information from the PKCS7 message */
1263 1395      if ((si_sk = PKCS7_get_signer_info(p7)) == NULL)
1264 1396          goto cleanup;
1265 1397      if ((si = sk_PKCS7_SIGNER_INFO_value(si_sk, 0)) == NULL)
1266 1398          goto cleanup;
1267 1399      if ((x = PKCS7_cert_from_signer_info(p7, si)) == NULL)
1268 1400          goto cleanup;
1269 1401  
↓ open down ↓ 30 lines elided ↑ open up ↑
1300 1432          }
1301 1433          size = sk_X509_num(p7->d.sign->cert);
1302 1434          for (i = 0; i < size; i++) {
1303 1435              sk_X509_push(intermediateCAs, sk_X509_value(p7->d.sign->cert, i));
1304 1436          }
1305 1437      }
1306 1438  
1307 1439      /* initialize x509 context with the received certificate and
1308 1440       * trusted and intermediate CA chains and CRLs
1309 1441       */
1310      -    if (!X509_STORE_CTX_init(&cert_ctx, store, x, intermediateCAs))
     1442 +    if ((cert_ctx = X509_STORE_CTX_new()) == NULL)
1311 1443          goto cleanup;
     1444 +    if (!X509_STORE_CTX_init(cert_ctx, store, x, intermediateCAs))
     1445 +        goto cleanup;
1312 1446  
1313      -    X509_STORE_CTX_set0_crls(&cert_ctx, revoked);
     1447 +    X509_STORE_CTX_set0_crls(cert_ctx, revoked);
1314 1448  
1315 1449      /* add trusted CAs certificates for cert verification */
1316 1450      if (idctx->trustedCAs != NULL)
1317      -        X509_STORE_CTX_trusted_stack(&cert_ctx, idctx->trustedCAs);
     1451 +        X509_STORE_CTX_set0_trusted_stack(cert_ctx, idctx->trustedCAs);
1318 1452      else {
1319 1453          pkiDebug("unable to find any trusted CAs\n");
1320 1454          goto cleanup;
1321 1455      }
1322 1456  #ifdef DEBUG_CERTCHAIN
1323 1457      if (intermediateCAs != NULL) {
1324 1458          size = sk_X509_num(intermediateCAs);
1325 1459          pkiDebug("untrusted cert chain of size %d\n", size);
1326 1460          for (i = 0; i < size; i++) {
1327 1461              X509_NAME_oneline(X509_get_subject_name(
↓ open down ↓ 14 lines elided ↑ open up ↑
1342 1476          size = sk_X509_CRL_num(revoked);
1343 1477          pkiDebug("CRL chain of size %d\n", size);
1344 1478          for (i = 0; i < size; i++) {
1345 1479              X509_CRL *crl = sk_X509_CRL_value(revoked, i);
1346 1480              X509_NAME_oneline(X509_CRL_get_issuer(crl), buf, sizeof(buf));
1347 1481              pkiDebug("crls by CA #%d: %s\n", i , buf);
1348 1482          }
1349 1483      }
1350 1484  #endif
1351 1485  
1352      -    i = X509_verify_cert(&cert_ctx);
     1486 +    i = X509_verify_cert(cert_ctx);
1353 1487      if (i <= 0) {
1354      -        int j = X509_STORE_CTX_get_error(&cert_ctx);
     1488 +        int j = X509_STORE_CTX_get_error(cert_ctx);
1355 1489  
1356      -        reqctx->received_cert = X509_dup(cert_ctx.current_cert);
     1490 +        reqctx->received_cert = X509_dup(
     1491 +            X509_STORE_CTX_get_current_cert(cert_ctx));
1357 1492          switch(j) {
1358 1493              case X509_V_ERR_CERT_REVOKED:
1359 1494                  retval = KRB5KDC_ERR_REVOKED_CERTIFICATE;
1360 1495                  break;
1361 1496              case X509_V_ERR_UNABLE_TO_GET_CRL:
1362 1497                  retval = KRB5KDC_ERR_REVOCATION_STATUS_UNKNOWN;
1363 1498                  break;
1364 1499              case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1365 1500              case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1366 1501                  retval = KRB5KDC_ERR_CANT_VERIFY_CERTIFICATE;
↓ open down ↓ 11 lines elided ↑ open up ↑
1378 1513          size = sk_X509_num(p7->d.sign->cert);
1379 1514          pkiDebug("received cert chain of size %d\n", size);
1380 1515          for (j = 0; j < size; j++) {
1381 1516              X509 *tmp_cert = sk_X509_value(p7->d.sign->cert, j);
1382 1517              X509_NAME_oneline(X509_get_subject_name(tmp_cert), buf, sizeof(buf));
1383 1518              pkiDebug("cert #%d: %s\n", j, buf);
1384 1519          }
1385 1520  #endif
1386 1521      } else {
1387 1522          /* retrieve verified certificate chain */
1388      -        if (cms_msg_type == CMS_SIGN_CLIENT || cms_msg_type == CMS_SIGN_DRAFT9) 
1389      -            verified_chain = X509_STORE_CTX_get1_chain(&cert_ctx);
     1523 +        if (cms_msg_type == CMS_SIGN_CLIENT || cms_msg_type == CMS_SIGN_DRAFT9)
     1524 +            verified_chain = X509_STORE_CTX_get1_chain(cert_ctx);
1390 1525      }
1391      -    X509_STORE_CTX_cleanup(&cert_ctx);
     1526 +    X509_STORE_CTX_free(cert_ctx);
1392 1527      if (i <= 0)
1393 1528          goto cleanup;
1394 1529  
1395 1530      out = BIO_new(BIO_s_mem());
1396 1531      if (cms_msg_type == CMS_SIGN_DRAFT9)
1397 1532          flags |= PKCS7_NOATTR;
1398 1533      if (PKCS7_verify(p7, NULL, store, NULL, out, flags)) {
1399 1534          int valid_oid = 0;
1400 1535  
1401      -        if (!OBJ_cmp(p7->d.sign->contents->type, oid)) 
     1536 +        if (!OBJ_cmp(p7->d.sign->contents->type, oid))
1402 1537              valid_oid = 1;
1403 1538          else if (cms_msg_type == CMS_SIGN_DRAFT9) {
1404 1539              /*
1405 1540               * Various implementations of the pa-type 15 request use
1406 1541               * different OIDS.  We check that the returned object
1407 1542               * has any of the acceptable OIDs
1408 1543               */
1409 1544              ASN1_OBJECT *client_oid = NULL, *server_oid = NULL, *rsa_oid = NULL;
1410 1545              client_oid = pkinit_pkcs7type2oid(plgctx, CMS_SIGN_CLIENT);
1411 1546              server_oid = pkinit_pkcs7type2oid(plgctx, CMS_SIGN_SERVER);
1412 1547              rsa_oid = pkinit_pkcs7type2oid(plgctx, CMS_ENVEL_SERVER);
1413 1548              if (!OBJ_cmp(p7->d.sign->contents->type, client_oid) ||
1414 1549                  !OBJ_cmp(p7->d.sign->contents->type, server_oid) ||
1415 1550                  !OBJ_cmp(p7->d.sign->contents->type, rsa_oid))
1416 1551                  valid_oid = 1;
1417 1552          }
1418 1553  
1419      -        if (valid_oid) 
     1554 +        if (valid_oid)
1420 1555              pkiDebug("PKCS7 Verification successful\n");
1421 1556          else {
     1557 +            const ASN1_OBJECT *etype = p7->d.sign->contents->type;
1422 1558              pkiDebug("wrong oid in eContentType\n");
1423      -            print_buffer((unsigned char *)p7->d.sign->contents->type->data, 
1424      -                (unsigned int)p7->d.sign->contents->type->length);
     1559 +            print_buffer((unsigned char *)OBJ_get0_data(etype),
     1560 +                OBJ_length(etype));
1425 1561              retval = KRB5KDC_ERR_PREAUTH_FAILED;
1426 1562              krb5_set_error_message(context, retval, "wrong oid\n");
1427 1563              goto cleanup;
1428 1564          }
1429 1565      }
1430 1566      else {
1431 1567          unsigned long err = ERR_peek_error();
1432 1568          switch(ERR_GET_REASON(err)) {
1433 1569              case PKCS7_R_DIGEST_FAILURE:
1434 1570                  retval = KRB5KDC_ERR_DIGEST_IN_SIGNED_DATA_NOT_ACCEPTED;
↓ open down ↓ 18 lines elided ↑ open up ↑
1453 1589          else
1454 1590              size += i;
1455 1591      }
1456 1592      *data_len = size;
1457 1593  
1458 1594      reqctx->received_cert = X509_dup(x);
1459 1595  
1460 1596      /* generate authorization data */
1461 1597      if (cms_msg_type == CMS_SIGN_CLIENT || cms_msg_type == CMS_SIGN_DRAFT9) {
1462 1598  
1463      -        if (authz_data == NULL || authz_data_len == NULL) 
     1599 +        if (authz_data == NULL || authz_data_len == NULL)
1464 1600              goto out;
1465 1601  
1466 1602          *authz_data = NULL;
1467      -        retval = create_identifiers_from_stack(verified_chain, 
     1603 +        retval = create_identifiers_from_stack(verified_chain,
1468 1604                                                 &krb5_verified_chain);
1469 1605          if (retval) {
1470 1606              pkiDebug("create_identifiers_from_stack failed\n");
1471 1607              goto cleanup;
1472 1608          }
1473 1609  
1474 1610          retval = k5int_encode_krb5_td_trusted_certifiers((const krb5_external_principal_identifier **)krb5_verified_chain, &authz);
1475 1611          if (retval) {
1476 1612              pkiDebug("encode_krb5_td_trusted_certifiers failed\n");
1477 1613              goto cleanup;
↓ open down ↓ 117 lines elided ↑ open up ↑
1595 1731      }
1596 1732  
1597 1733      p7 = PKCS7_encrypt(encerts, in, cipher, flags);
1598 1734      if (p7 == NULL) {
1599 1735          pkiDebug("failed to encrypt PKCS7 object\n");
1600 1736          retval = -1;
1601 1737          goto cleanup;
1602 1738      }
1603 1739      switch (pa_type) {
1604 1740          case KRB5_PADATA_PK_AS_REQ:
1605      -            p7->d.enveloped->enc_data->content_type = 
     1741 +            p7->d.enveloped->enc_data->content_type =
1606 1742                  OBJ_nid2obj(NID_pkcs7_signed);
1607 1743              break;
1608 1744          case KRB5_PADATA_PK_AS_REP_OLD:
1609 1745          case KRB5_PADATA_PK_AS_REQ_OLD:
1610      -            p7->d.enveloped->enc_data->content_type = 
     1746 +            p7->d.enveloped->enc_data->content_type =
1611 1747                  OBJ_nid2obj(NID_pkcs7_data);
1612 1748              break;
1613      -    } 
     1749 +    }
1614 1750  
1615 1751      *out_len = i2d_PKCS7(p7, NULL);
1616 1752      if (!*out_len || (p = *out = (unsigned char *)malloc(*out_len)) == NULL) {
1617 1753          retval = ENOMEM;
1618 1754          goto cleanup;
1619 1755      }
1620 1756      retval = i2d_PKCS7(p7, &p);
1621 1757      if (!retval) {
1622 1758          pkiDebug("unable to write pkcs7 object\n");
1623 1759          goto cleanup;
↓ open down ↓ 293 lines elided ↑ open up ↑
1917 2053                               __FUNCTION__);
1918 2054                      continue;
1919 2055                  }
1920 2056  
1921 2057                  break;
1922 2058              case GEN_DNS:
1923 2059                  if (dnss != NULL) {
1924 2060                      pkiDebug("%s: found dns name = %s\n",
1925 2061                               __FUNCTION__, gen->d.dNSName->data);
1926 2062                      dnss[d] = (unsigned char *)
1927      -                                    strdup((char *)gen->d.dNSName->data); 
     2063 +                                    strdup((char *)gen->d.dNSName->data);
1928 2064                      if (dnss[d] == NULL) {
1929 2065                          pkiDebug("%s: failed to duplicate dns name\n",
1930 2066                                   __FUNCTION__);
1931 2067                      } else {
1932 2068                          d++;
1933 2069                          num_found++;
1934 2070                      }
1935 2071                  }
1936 2072                  break;
1937 2073              default:
↓ open down ↓ 218 lines elided ↑ open up ↑
2156 2292                   pkinit_identity_crypto_context id_cryptoctx,
2157 2293                   int dh_size,
2158 2294                   unsigned char **dh_params,
2159 2295                   unsigned int *dh_params_len,
2160 2296                   unsigned char **dh_pubkey,
2161 2297                   unsigned int *dh_pubkey_len)
2162 2298  {
2163 2299      krb5_error_code retval = KRB5KDC_ERR_PREAUTH_FAILED;
2164 2300      unsigned char *buf = NULL;
2165 2301      int dh_err = 0;
2166      -    ASN1_INTEGER *pub_key = NULL;
     2302 +    ASN1_INTEGER *asn_pub_key = NULL;
     2303 +    BIGNUM *p, *g, *q;
     2304 +    const BIGNUM *pub_key;
2167 2305  
2168 2306      if (cryptoctx->dh == NULL) {
2169 2307          if ((cryptoctx->dh = DH_new()) == NULL)
2170 2308              goto cleanup;
2171      -        if ((cryptoctx->dh->g = BN_new()) == NULL ||
2172      -            (cryptoctx->dh->q = BN_new()) == NULL)
     2309 +        if ((g = BN_new()) == NULL || (q = BN_new()) == NULL)
2173 2310              goto cleanup;
2174 2311  
2175 2312          switch(dh_size) {
2176 2313              case 1024:
2177 2314                  pkiDebug("client uses 1024 DH keys\n");
2178      -                cryptoctx->dh->p = get_rfc2409_prime_1024(NULL);
     2315 +                cryptoctx->dh = make_dhprime(pkinit_1024_dhprime,
     2316 +                    sizeof(pkinit_1024_dhprime));
2179 2317                  break;
2180 2318              case 2048:
2181 2319                  pkiDebug("client uses 2048 DH keys\n");
2182      -                cryptoctx->dh->p = BN_bin2bn(pkinit_2048_dhprime,
2183      -                    sizeof(pkinit_2048_dhprime), NULL);
     2320 +                cryptoctx->dh = make_dhprime(pkinit_2048_dhprime,
     2321 +                    sizeof(pkinit_2048_dhprime));
2184 2322                  break;
2185 2323              case 4096:
2186 2324                  pkiDebug("client uses 4096 DH keys\n");
2187      -                cryptoctx->dh->p = BN_bin2bn(pkinit_4096_dhprime,
2188      -                    sizeof(pkinit_4096_dhprime), NULL);
     2325 +                cryptoctx->dh = make_dhprime(pkinit_4096_dhprime,
     2326 +                    sizeof(pkinit_4096_dhprime));
2189 2327                  break;
2190      -            default:
2191      -                goto cleanup;
2192 2328          }
2193      -
2194      -        BN_set_word((cryptoctx->dh->g), DH_GENERATOR_2);
2195      -        BN_rshift1(cryptoctx->dh->q, cryptoctx->dh->p);
     2329 +        if (cryptoctx->dh == NULL)
     2330 +                goto cleanup;
2196 2331      }
2197 2332  
2198 2333      DH_generate_key(cryptoctx->dh);
     2334 +    DH_get0_key(cryptoctx->dh, &pub_key, NULL);
     2335 +
2199 2336  /* Solaris Kerberos */
2200 2337  #ifdef DEBUG
2201 2338      DH_check(cryptoctx->dh, &dh_err);
2202 2339      if (dh_err != 0) {
2203 2340          pkiDebug("Warning: dh_check failed with %d\n", dh_err);
2204 2341          if (dh_err & DH_CHECK_P_NOT_PRIME)
2205 2342              pkiDebug("p value is not prime\n");
2206 2343          if (dh_err & DH_CHECK_P_NOT_SAFE_PRIME)
2207 2344              pkiDebug("p value is not a safe prime\n");
2208 2345          if (dh_err & DH_UNABLE_TO_CHECK_GENERATOR)
2209 2346              pkiDebug("unable to check the generator value\n");
2210 2347          if (dh_err & DH_NOT_SUITABLE_GENERATOR)
2211 2348              pkiDebug("the g value is not a generator\n");
2212 2349      }
2213 2350  #endif
2214 2351  #ifdef DEBUG_DH
2215 2352      print_dh(cryptoctx->dh, "client's DH params\n");
2216      -    print_pubkey(cryptoctx->dh->pub_key, "client's pub_key=");
     2353 +    print_pubkey(pub_key, "client's pub_key=");
2217 2354  #endif
2218 2355  
2219      -    DH_check_pub_key(cryptoctx->dh, cryptoctx->dh->pub_key, &dh_err);
     2356 +    DH_check_pub_key(cryptoctx->dh, pub_key, &dh_err);
2220 2357      if (dh_err != 0) {
2221 2358          pkiDebug("dh_check_pub_key failed with %d\n", dh_err);
2222 2359          goto cleanup;
2223 2360      }
2224 2361  
2225 2362      /* pack DHparams */
2226 2363      /* aglo: usually we could just call i2d_DHparams to encode DH params
2227 2364       * however, PKINIT requires RFC3279 encoding and openssl does pkcs#3.
2228 2365       */
2229      -    retval = pkinit_encode_dh_params(cryptoctx->dh->p, cryptoctx->dh->g,
2230      -        cryptoctx->dh->q, dh_params, dh_params_len);
     2366 +    DH_get0_pqg(cryptoctx->dh, (const BIGNUM **)&p, (const BIGNUM **)&q,
     2367 +        (const BIGNUM **)&g);
     2368 +    retval = pkinit_encode_dh_params(p, g, q, dh_params, dh_params_len);
2231 2369      if (retval)
2232 2370          goto cleanup;
2233 2371  
2234 2372      /* pack DH public key */
2235 2373      /* Diffie-Hellman public key must be ASN1 encoded as an INTEGER; this
2236 2374       * encoding shall be used as the contents (the value) of the
2237 2375       * subjectPublicKey component (a BIT STRING) of the SubjectPublicKeyInfo
2238 2376       * data element
2239 2377       */
2240      -    if ((pub_key = BN_to_ASN1_INTEGER(cryptoctx->dh->pub_key, NULL)) == NULL)
     2378 +    if ((asn_pub_key = BN_to_ASN1_INTEGER(pub_key, NULL)) == NULL)
2241 2379          goto cleanup;
2242      -    *dh_pubkey_len = i2d_ASN1_INTEGER(pub_key, NULL);
     2380 +    *dh_pubkey_len = i2d_ASN1_INTEGER(asn_pub_key, NULL);
2243 2381      if ((buf = *dh_pubkey = (unsigned char *)
2244      -            malloc((size_t) *dh_pubkey_len)) == NULL) {
2245      -        retval  = ENOMEM;
2246      -        goto cleanup;
     2382 +        malloc((size_t) *dh_pubkey_len)) == NULL) {
     2383 +            retval = ENOMEM;
     2384 +            goto cleanup;
2247 2385      }
2248      -    i2d_ASN1_INTEGER(pub_key, &buf);
     2386 +    i2d_ASN1_INTEGER(asn_pub_key, &buf);
2249 2387  
2250      -    if (pub_key != NULL)
2251      -        ASN1_INTEGER_free(pub_key);
     2388 +    if (asn_pub_key != NULL)
     2389 +        ASN1_INTEGER_free(asn_pub_key);
2252 2390  
2253 2391      retval = 0;
2254 2392      return retval;
2255 2393  
2256 2394    cleanup:
2257 2395      if (cryptoctx->dh != NULL)
2258 2396          DH_free(cryptoctx->dh);
2259 2397      cryptoctx->dh = NULL;
2260 2398      if (*dh_params != NULL)
2261 2399          free(*dh_params);
2262 2400      *dh_params = NULL;
2263 2401      if (*dh_pubkey != NULL)
2264 2402          free(*dh_pubkey);
2265 2403      *dh_pubkey = NULL;
2266      -    if (pub_key != NULL)
2267      -        ASN1_INTEGER_free(pub_key);
     2404 +    if (asn_pub_key != NULL)
     2405 +        ASN1_INTEGER_free(asn_pub_key);
2268 2406  
2269 2407      return retval;
2270 2408  }
2271 2409  
2272 2410  /* ARGSUSED */
2273 2411  krb5_error_code
2274 2412  client_process_dh(krb5_context context,
2275 2413                    pkinit_plg_crypto_context plg_cryptoctx,
2276 2414                    pkinit_req_crypto_context cryptoctx,
2277 2415                    pkinit_identity_crypto_context id_cryptoctx,
↓ open down ↓ 67 lines elided ↑ open up ↑
2345 2483                  pkinit_plg_crypto_context cryptoctx,
2346 2484                  pkinit_req_crypto_context req_cryptoctx,
2347 2485                  pkinit_identity_crypto_context id_cryptoctx,
2348 2486                  krb5_octet_data *dh_params,
2349 2487                  int minbits)
2350 2488  {
2351 2489      DH *dh = NULL;
2352 2490      unsigned char *tmp = NULL;
2353 2491      int dh_prime_bits;
2354 2492      krb5_error_code retval = KRB5KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED;
     2493 +    const BIGNUM *p, *g, *q, *p2;
2355 2494  
2356 2495      tmp = dh_params->data;
2357 2496      dh = DH_new();
2358 2497      dh = pkinit_decode_dh_params(&dh, &tmp, dh_params->length);
2359 2498      if (dh == NULL) {
2360 2499          pkiDebug("failed to decode dhparams\n");
2361 2500          goto cleanup;
2362 2501      }
2363 2502  
     2503 +    DH_get0_pqg(dh, &p, &q, &g);
     2504 +
2364 2505      /* KDC SHOULD check to see if the key parameters satisfy its policy */
2365      -    dh_prime_bits = BN_num_bits(dh->p);
     2506 +    dh_prime_bits = BN_num_bits(p);
2366 2507      if (minbits && dh_prime_bits < minbits) {
2367 2508          pkiDebug("client sent dh params with %d bits, we require %d\n",
2368 2509                   dh_prime_bits, minbits);
2369 2510          goto cleanup;
2370 2511      }
2371 2512  
2372 2513      /* check dhparams is group 2 */
2373      -    if (pkinit_check_dh_params(cryptoctx->dh_1024->p,
2374      -                               dh->p, dh->g, dh->q) == 0) {
     2514 +    DH_get0_pqg(cryptoctx->dh_1024, &p2, NULL, NULL);
     2515 +    if (pkinit_check_dh_params(p2, p, g, q) == 0) {
2375 2516          retval = 0;
2376 2517          goto cleanup;
2377 2518      }
2378 2519  
2379 2520      /* check dhparams is group 14 */
2380      -    if (pkinit_check_dh_params(cryptoctx->dh_2048->p,
2381      -                               dh->p, dh->g, dh->q) == 0) {
     2521 +    DH_get0_pqg(cryptoctx->dh_2048, &p2, NULL, NULL);
     2522 +    if (pkinit_check_dh_params(p2, p, g, q) == 0) {
2382 2523          retval = 0;
2383 2524          goto cleanup;
2384 2525      }
2385 2526  
2386 2527      /* check dhparams is group 16 */
2387      -    if (pkinit_check_dh_params(cryptoctx->dh_4096->p,
2388      -                               dh->p, dh->g, dh->q) == 0) {
     2528 +    DH_get0_pqg(cryptoctx->dh_4096, &p2, NULL, NULL);
     2529 +    if (pkinit_check_dh_params(p2, p, g, q) == 0) {
2389 2530          retval = 0;
2390 2531          goto cleanup;
2391 2532      }
2392 2533  
2393 2534    cleanup:
2394 2535      if (retval == 0)
2395 2536          req_cryptoctx->dh = dh;
2396 2537      else
2397 2538          DH_free(dh);
2398 2539  
↓ open down ↓ 10 lines elided ↑ open up ↑
2409 2550                    unsigned char *data,
2410 2551                    unsigned int data_len,
2411 2552                    unsigned char **dh_pubkey,
2412 2553                    unsigned int *dh_pubkey_len,
2413 2554                    unsigned char **server_key,
2414 2555                    unsigned int *server_key_len)
2415 2556  {
2416 2557      /* Solaris Kerberos */
2417 2558      krb5_error_code retval = KRB5KRB_ERR_GENERIC;
2418 2559      DH *dh = NULL, *dh_server = NULL;
2419      -    unsigned char *p = NULL;
2420      -    ASN1_INTEGER *pub_key = NULL;
     2560 +    const BIGNUM *p, *g, *q, *s_pub_key;
     2561 +    BIGNUM *pub_key;
     2562 +    unsigned char *s = NULL;
     2563 +    ASN1_INTEGER *asn_pub_key = NULL;
2421 2564  
2422 2565      /* get client's received DH parameters that we saved in server_check_dh */
2423 2566      dh = cryptoctx->dh;
2424 2567  
2425 2568      dh_server = DH_new();
2426 2569      if (dh_server == NULL)
2427 2570          goto cleanup;
2428      -    dh_server->p = BN_dup(dh->p);
2429      -    dh_server->g = BN_dup(dh->g);
2430      -    dh_server->q = BN_dup(dh->q);
     2571 +    DH_get0_pqg(dh, &p, &g, &q);
     2572 +    DH_set0_pqg(dh_server, BN_dup(p), BN_dup(g), BN_dup(q));
2431 2573  
2432 2574      /* decode client's public key */
2433      -    p = data;
2434      -    pub_key = d2i_ASN1_INTEGER(NULL, (const unsigned char **)&p, (int)data_len);
     2575 +    s = data;
     2576 +    asn_pub_key = d2i_ASN1_INTEGER(NULL,
     2577 +        (const unsigned char **)&s, (int)data_len);
     2578 +    if (asn_pub_key == NULL)
     2579 +        goto cleanup;
     2580 +    pub_key = ASN1_INTEGER_to_BN(asn_pub_key, NULL);
2435 2581      if (pub_key == NULL)
2436 2582          goto cleanup;
2437      -    dh->pub_key = ASN1_INTEGER_to_BN(pub_key, NULL);
2438      -    if (dh->pub_key == NULL)
2439      -        goto cleanup;
2440      -    ASN1_INTEGER_free(pub_key);
     2583 +    DH_set0_key(dh, pub_key, NULL);
     2584 +    ASN1_INTEGER_free(asn_pub_key);
2441 2585  
2442 2586      if (!DH_generate_key(dh_server))
2443 2587          goto cleanup;
2444 2588  
2445 2589      /* generate DH session key */
2446 2590      *server_key_len = DH_size(dh_server);
2447      -    if ((*server_key = (unsigned char *) malloc((size_t)*server_key_len)) == NULL)
2448      -        goto cleanup;
2449      -    DH_compute_key(*server_key, dh->pub_key, dh_server);
     2591 +    if ((*server_key = (unsigned char *) malloc((size_t)*server_key_len))
     2592 +        == NULL)
     2593 +            goto cleanup;
     2594 +    DH_compute_key(*server_key, pub_key, dh_server);
     2595 +    DH_get0_key(dh_server, &s_pub_key, NULL);
2450 2596  
2451 2597  #ifdef DEBUG_DH
2452 2598      print_dh(dh_server, "client&server's DH params\n");
2453      -    print_pubkey(dh->pub_key, "client's pub_key=");
2454      -    print_pubkey(dh_server->pub_key, "server's pub_key=");
     2599 +    print_pubkey(pub_key, "client's pub_key=");
     2600 +    print_pubkey(s_pub_key, "server's pub_key=");
2455 2601      pkiDebug("server secret key=");
2456 2602      print_buffer(*server_key, *server_key_len);
2457 2603  #endif
2458 2604  
2459 2605      /* KDC reply */
2460 2606      /* pack DH public key */
2461 2607      /* Diffie-Hellman public key must be ASN1 encoded as an INTEGER; this
2462 2608       * encoding shall be used as the contents (the value) of the
2463 2609       * subjectPublicKey component (a BIT STRING) of the SubjectPublicKeyInfo
2464 2610       * data element
2465 2611       */
2466      -    if ((pub_key = BN_to_ASN1_INTEGER(dh_server->pub_key, NULL)) == NULL)
     2612 +    if ((asn_pub_key = BN_to_ASN1_INTEGER(s_pub_key, NULL)) == NULL)
2467 2613          goto cleanup;
2468      -    *dh_pubkey_len = i2d_ASN1_INTEGER(pub_key, NULL);
2469      -    if ((p = *dh_pubkey = (unsigned char *) malloc((size_t)*dh_pubkey_len)) == NULL)
2470      -        goto cleanup;
2471      -    i2d_ASN1_INTEGER(pub_key, &p);
2472      -    if (pub_key != NULL)
2473      -        ASN1_INTEGER_free(pub_key);
     2614 +    *dh_pubkey_len = i2d_ASN1_INTEGER(asn_pub_key, NULL);
     2615 +    if ((s = *dh_pubkey = (unsigned char *) malloc((size_t)*dh_pubkey_len))
     2616 +        == NULL)
     2617 +            goto cleanup;
     2618 +    i2d_ASN1_INTEGER(asn_pub_key, &s);
     2619 +    if (asn_pub_key != NULL)
     2620 +        ASN1_INTEGER_free(asn_pub_key);
2474 2621  
2475 2622      retval = 0;
2476 2623  
2477 2624      if (dh_server != NULL)
2478 2625          DH_free(dh_server);
2479 2626      return retval;
2480 2627  
2481 2628    cleanup:
2482 2629      if (dh_server != NULL)
2483 2630          DH_free(dh_server);
↓ open down ↓ 13 lines elided ↑ open up ↑
2497 2644  openssl_init()
2498 2645  {
2499 2646      krb5_error_code ret = 0;
2500 2647      static int did_init = 0;
2501 2648      static k5_mutex_t init_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
2502 2649  
2503 2650      ret = k5_mutex_lock(&init_mutex);
2504 2651      if (ret == 0) {
2505 2652          if (!did_init) {
2506 2653              /* initialize openssl routines */
     2654 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     2655 +            /*
     2656 +             * As of version 1.1.0, OpenSSL will automatically allocate
     2657 +             * resources as-needed.
     2658 +             */
2507 2659              CRYPTO_malloc_init();
2508 2660              ERR_load_crypto_strings();
2509 2661              OpenSSL_add_all_algorithms();
     2662 +#endif
2510 2663              did_init++;
2511 2664          }
2512 2665          k5_mutex_unlock(&init_mutex);
2513 2666      }
2514 2667      return (ret);
2515 2668  }
2516 2669  
2517 2670  static krb5_error_code
2518      -pkinit_encode_dh_params(BIGNUM *p, BIGNUM *g, BIGNUM *q,
     2671 +pkinit_encode_dh_params(const BIGNUM *p, const BIGNUM *g, const BIGNUM *q,
2519 2672                          unsigned char **buf, unsigned int *buf_len)
2520 2673  {
2521 2674      krb5_error_code retval = KRB5KDC_ERR_PREAUTH_FAILED;
2522 2675      int bufsize = 0, r = 0;
2523 2676      unsigned char *tmp = NULL;
2524 2677      ASN1_INTEGER *ap = NULL, *ag = NULL, *aq = NULL;
2525 2678  
2526 2679      if ((ap = BN_to_ASN1_INTEGER(p, NULL)) == NULL)
2527 2680          goto cleanup;
2528 2681      if ((ag = BN_to_ASN1_INTEGER(g, NULL)) == NULL)
↓ open down ↓ 24 lines elided ↑ open up ↑
2553 2706      if (ap != NULL)
2554 2707          ASN1_INTEGER_free(ap);
2555 2708      if (ag != NULL)
2556 2709          ASN1_INTEGER_free(ag);
2557 2710      if (aq != NULL)
2558 2711          ASN1_INTEGER_free(aq);
2559 2712  
2560 2713      return retval;
2561 2714  }
2562 2715  
     2716 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     2717 +
2563 2718  static DH *
2564 2719  pkinit_decode_dh_params(DH ** a, unsigned char **pp, unsigned int len)
2565 2720  {
2566 2721      ASN1_INTEGER ai, *aip = NULL;
2567 2722      long length = (long) len;
2568 2723  
2569 2724      M_ASN1_D2I_vars(a, DH *, DH_new);
2570 2725  
2571 2726      M_ASN1_D2I_Init();
2572 2727      M_ASN1_D2I_start_sequence();
↓ open down ↓ 39 lines elided ↑ open up ↑
2612 2767              ai.data = NULL;
2613 2768              ai.length = 0;
2614 2769          }
2615 2770  
2616 2771      }
2617 2772      M_ASN1_D2I_end_sequence();
2618 2773      M_ASN1_D2I_Finish(a, DH_free, 0);
2619 2774  
2620 2775  }
2621 2776  
     2777 +#else
     2778 +
     2779 +/*
     2780 + * This is taken from the internal dh_asn1.c file in OpenSSL 1.1, modified to
     2781 + * make q an optional field.
     2782 + */
     2783 +
     2784 +typedef struct {
     2785 +    ASN1_BIT_STRING *seed;
     2786 +    BIGNUM *counter;
     2787 +} int_dhvparams;
     2788 +
     2789 +typedef struct {
     2790 +    BIGNUM *p;
     2791 +    BIGNUM *q;
     2792 +    BIGNUM *g;
     2793 +    BIGNUM *j;
     2794 +    int_dhvparams *vparams;
     2795 +} int_dhx942_dh;
     2796 +
     2797 +ASN1_SEQUENCE(DHvparams) = {
     2798 +    ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
     2799 +    ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
     2800 +} static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
     2801 +
     2802 +ASN1_SEQUENCE(DHxparams) = {
     2803 +    ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
     2804 +    ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
     2805 +    ASN1_OPT(int_dhx942_dh, q, BIGNUM),
     2806 +    ASN1_OPT(int_dhx942_dh, j, BIGNUM),
     2807 +    ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
     2808 +} static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
     2809 +
     2810 +static DH *
     2811 +pkinit_decode_dh_params(DH **a, unsigned char **pp, unsigned int len)
     2812 +{
     2813 +        int_dhx942_dh *params;
     2814 +        DH *dh = *a;
     2815 +
     2816 +        if (dh == NULL)
     2817 +                return NULL;
     2818 +
     2819 +        params = (int_dhx942_dh *)ASN1_item_d2i(NULL,
     2820 +            (const unsigned char **)pp, len, ASN1_ITEM_rptr(DHxparams));
     2821 +        if (params == NULL) {
     2822 +                DH_free(dh);
     2823 +                return NULL;
     2824 +        }
     2825 +
     2826 +        DH_set0_pqg(dh, params->p, params->q, params->g);
     2827 +        params->p = params->q = params->g = NULL;
     2828 +        ASN1_item_free((ASN1_VALUE *)params, ASN1_ITEM_rptr(DHxparams));
     2829 +        return dh;
     2830 +}
     2831 +
     2832 +#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
     2833 +
2622 2834  static krb5_error_code
2623 2835  pkinit_create_sequence_of_principal_identifiers(
2624 2836      krb5_context context,
2625 2837      pkinit_plg_crypto_context plg_cryptoctx,
2626 2838      pkinit_req_crypto_context req_cryptoctx,
2627 2839      pkinit_identity_crypto_context id_cryptoctx,
2628 2840      int type,
2629 2841      krb5_data **out_data)
2630 2842  {
2631 2843      krb5_error_code retval = KRB5KRB_ERR_GENERIC;
↓ open down ↓ 124 lines elided ↑ open up ↑
2756 2968                                 pkinit_plg_opts *opts,
2757 2969                                 krb5_data **out_data)
2758 2970  {
2759 2971      /* Solaris Kerberos */
2760 2972      krb5_error_code retval = KRB5KRB_ERR_GENERIC;
2761 2973      unsigned int buf1_len = 0, buf2_len = 0, buf3_len = 0, i = 0;
2762 2974      unsigned char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
2763 2975      krb5_typed_data **typed_data = NULL;
2764 2976      krb5_data *data = NULL, *encoded_algId = NULL;
2765 2977      krb5_algorithm_identifier **algId = NULL;
     2978 +    const BIGNUM *p, *q, *g;
2766 2979  
2767 2980      /* Solaris Kerberos */
2768 2981      if (opts->dh_min_bits > 4096) {
2769 2982          retval = EINVAL;
2770 2983          goto cleanup;
2771 2984      }
2772 2985  
2773 2986      if (opts->dh_min_bits <= 1024) {
2774      -        retval = pkinit_encode_dh_params(plg_cryptoctx->dh_1024->p,
2775      -            plg_cryptoctx->dh_1024->g, plg_cryptoctx->dh_1024->q,
2776      -            &buf1, &buf1_len);
     2987 +        DH_get0_pqg(plg_cryptoctx->dh_1024, &p, &q, &g);
     2988 +        retval = pkinit_encode_dh_params(p, g, q, &buf1, &buf1_len);
2777 2989          if (retval)
2778 2990              goto cleanup;
2779 2991      }
2780 2992      if (opts->dh_min_bits <= 2048) {
2781      -        retval = pkinit_encode_dh_params(plg_cryptoctx->dh_2048->p,
2782      -            plg_cryptoctx->dh_2048->g, plg_cryptoctx->dh_2048->q,
2783      -            &buf2, &buf2_len);
     2993 +        DH_get0_pqg(plg_cryptoctx->dh_2048, &p, &q, &g);
     2994 +        retval = pkinit_encode_dh_params(p, g, q, &buf2, &buf2_len);
2784 2995          if (retval)
2785 2996              goto cleanup;
2786 2997      }
2787      -    retval = pkinit_encode_dh_params(plg_cryptoctx->dh_4096->p,
2788      -        plg_cryptoctx->dh_4096->g, plg_cryptoctx->dh_4096->q,
2789      -        &buf3, &buf3_len);
     2998 +    DH_get0_pqg(plg_cryptoctx->dh_4096, &p, &q, &g);
     2999 +    retval = pkinit_encode_dh_params(p, g, q, &buf3, &buf3_len);
2790 3000      if (retval)
2791 3001          goto cleanup;
2792 3002  
2793 3003      if (opts->dh_min_bits <= 1024) {
2794 3004          algId = malloc(4 * sizeof(krb5_algorithm_identifier *));
2795 3005          if (algId == NULL)
2796 3006              goto cleanup;
2797 3007          algId[3] = NULL;
2798 3008          algId[0] = (krb5_algorithm_identifier *)malloc(sizeof(krb5_algorithm_identifier));
2799 3009          if (algId[0] == NULL)
↓ open down ↓ 173 lines elided ↑ open up ↑
2973 3183      retval = 0;
2974 3184  cleanup:
2975 3185      X509_NAME_free(is->issuer);
2976 3186      ASN1_INTEGER_free(is->serial);
2977 3187      free(is);
2978 3188  
2979 3189      return retval;
2980 3190  }
2981 3191  
2982 3192  static int
2983      -pkinit_check_dh_params(BIGNUM * p1, BIGNUM * p2, BIGNUM * g1, BIGNUM * q1)
     3193 +pkinit_check_dh_params(const BIGNUM *p1, const BIGNUM *p2, const BIGNUM *g1,
     3194 +    const BIGNUM *q1)
2984 3195  {
2985 3196      BIGNUM *g2 = NULL, *q2 = NULL;
2986 3197      /* Solaris Kerberos */
2987 3198      int retval = EINVAL;
2988 3199  
2989 3200      if (!BN_cmp(p1, p2)) {
2990 3201          g2 = BN_new();
2991 3202          BN_set_word(g2, DH_GENERATOR_2);
2992 3203          if (!BN_cmp(g1, g2)) {
2993 3204              q2 = BN_new();
↓ open down ↓ 23 lines elided ↑ open up ↑
3017 3228                              int *new_dh_size)
3018 3229  {
3019 3230      krb5_error_code retval = KRB5KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED;
3020 3231      int i = 0, use_sent_dh = 0, ok = 0;
3021 3232  
3022 3233      pkiDebug("dh parameters\n");
3023 3234  
3024 3235      while (algId[i] != NULL) {
3025 3236          DH *dh = NULL;
3026 3237          unsigned char *tmp = NULL;
     3238 +        const BIGNUM *p, *g, *q, *p2;
3027 3239          int dh_prime_bits = 0;
3028 3240  
3029 3241          if (algId[i]->algorithm.length != dh_oid.length ||
3030 3242              memcmp(algId[i]->algorithm.data, dh_oid.data, dh_oid.length))
3031 3243              goto cleanup;
3032 3244  
3033 3245          tmp = algId[i]->parameters.data;
3034 3246          dh = DH_new();
3035 3247          dh = pkinit_decode_dh_params(&dh, &tmp, algId[i]->parameters.length);
3036      -        dh_prime_bits = BN_num_bits(dh->p);
     3248 +        dh_prime_bits = DH_bits(dh);
3037 3249          pkiDebug("client sent %d DH bits server prefers %d DH bits\n",
3038 3250                   *new_dh_size, dh_prime_bits);
     3251 +        DH_get0_pqg(dh, &p, &q, &g);
3039 3252          switch(dh_prime_bits) {
3040 3253              case 1024:
3041      -                if (pkinit_check_dh_params(cryptoctx->dh_1024->p, dh->p,
3042      -                        dh->g, dh->q) == 0) {
     3254 +                DH_get0_pqg(cryptoctx->dh_1024, &p2, NULL, NULL);
     3255 +                if (pkinit_check_dh_params(p2, p, g, q) == 0) {
3043 3256                      *new_dh_size = 1024;
3044 3257                      ok = 1;
3045 3258                  }
3046 3259                  break;
3047 3260              case 2048:
3048      -                if (pkinit_check_dh_params(cryptoctx->dh_2048->p, dh->p,
3049      -                        dh->g, dh->q) == 0) {
     3261 +                DH_get0_pqg(cryptoctx->dh_2048, &p2, NULL, NULL);
     3262 +                if (pkinit_check_dh_params(p2, p, g, q) == 0) {
3050 3263                      *new_dh_size = 2048;
3051 3264                      ok = 1;
3052 3265                  }
3053 3266                  break;
3054 3267              case 4096:
3055      -                if (pkinit_check_dh_params(cryptoctx->dh_4096->p, dh->p,
3056      -                        dh->g, dh->q) == 0) {
     3268 +                DH_get0_pqg(cryptoctx->dh_4096, &p2, NULL, NULL);
     3269 +                if (pkinit_check_dh_params(p2, p, g, q) == 0) {
3057 3270                      *new_dh_size = 4096;
3058 3271                      ok = 1;
3059 3272                  }
3060 3273                  break;
3061 3274              default:
3062 3275                  break;
3063 3276          }
3064 3277          if (!ok) {
3065 3278              DH_check(dh, &retval);
3066 3279              if (retval != 0) {
↓ open down ↓ 19 lines elided ↑ open up ↑
3086 3299          i++;
3087 3300      }
3088 3301  
3089 3302      if (ok)
3090 3303          retval = 0;
3091 3304  
3092 3305  cleanup:
3093 3306      return retval;
3094 3307  }
3095 3308  
3096      -/* ARGSUSED */ 
     3309 +/* ARGSUSED */
3097 3310  static int
3098 3311  openssl_callback(int ok, X509_STORE_CTX * ctx)
3099 3312  {
3100 3313  #ifdef DEBUG
3101 3314      if (!ok) {
3102 3315          char buf[DN_BUF_LEN];
3103 3316  
3104 3317          X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), buf, sizeof(buf));
3105 3318          pkiDebug("cert = %s\n", buf);
3106 3319          pkiDebug("callback function: %d (%s)\n", ctx->error,
3107 3320                  X509_verify_cert_error_string(ctx->error));
3108 3321      }
3109 3322  #endif
3110 3323      return ok;
3111 3324  }
3112 3325  
3113 3326  static int
3114 3327  openssl_callback_ignore_crls(int ok, X509_STORE_CTX * ctx)
3115 3328  {
3116      -    if (!ok) {
3117      -        switch (ctx->error) {
3118      -            case X509_V_ERR_UNABLE_TO_GET_CRL:
3119      -                return 1;
3120      -            default:
3121      -                return 0;
3122      -        }
3123      -    }
     3329 +    if (!ok)
     3330 +        return (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_UNABLE_TO_GET_CRL);
3124 3331      return ok;
3125 3332  }
3126 3333  
3127 3334  static ASN1_OBJECT *
3128 3335  pkinit_pkcs7type2oid(pkinit_plg_crypto_context cryptoctx, int pkcs7_type)
3129 3336  {
3130 3337      int nid;
3131 3338  
3132 3339      switch (pkcs7_type) {
3133 3340          case CMS_SIGN_CLIENT:
3134 3341              return cryptoctx->id_pkinit_authData;
3135 3342          case CMS_SIGN_DRAFT9:
3136 3343              /*
3137 3344               * Delay creating this OID until we know we need it.
3138 3345               * It shadows an existing OpenSSL oid.  If it
3139 3346               * is created too early, it breaks things like
3140 3347               * the use of pkcs12 (which uses pkcs7 structures).
3141 3348               * We need this shadow version because our code
3142 3349               * depends on the "other" type to be unknown to the
3143 3350               * OpenSSL code.
3144      -             */ 
     3351 +             */
3145 3352              if (cryptoctx->id_pkinit_authData9 == NULL) {
3146 3353                  pkiDebug("%s: Creating shadow instance of pkcs7-data oid\n",
3147 3354                           __FUNCTION__);
3148 3355                  nid = OBJ_create("1.2.840.113549.1.7.1", "id-pkcs7-data",
3149 3356                                   "PKCS7 data");
3150 3357                  if (nid == NID_undef)
3151 3358                      return NULL;
3152 3359                  cryptoctx->id_pkinit_authData9 = OBJ_nid2obj(nid);
3153 3360              }
3154 3361              return cryptoctx->id_pkinit_authData9;
↓ open down ↓ 147 lines elided ↑ open up ↑
3302 3509      return 0;
3303 3510  }
3304 3511  #endif
3305 3512  
3306 3513  static int
3307 3514  prepare_enc_data(unsigned char *indata,
3308 3515                   int indata_len,
3309 3516                   unsigned char **outdata,
3310 3517                   int *outdata_len)
3311 3518  {
3312      -    /* Solaris Kerberos */
3313      -    ASN1_const_CTX c;
3314      -    long length = indata_len;
3315      -    int Ttag, Tclass;
3316      -    long Tlen;
     3519 +    int tag, class;
     3520 +    long tlen, slen;
     3521 +    const uint8_t *p = indata, *oldp;
3317 3522  
3318      -    c.pp = (const unsigned char **)&indata;
3319      -    c.q = *(const unsigned char **)&indata;
3320      -    c.error = ERR_R_NESTED_ASN1_ERROR;
3321      -    c.p= *(const unsigned char **)&indata;
3322      -    c.max = (length == 0)?0:(c.p+length);
     3523 +    /* Top-bit set means that the conversion failed. */
     3524 +    if (ASN1_get_object(&p, &slen, &tag, &class, indata_len) & 0x80)
     3525 +        return EINVAL;
     3526 +    if (tag != V_ASN1_SEQUENCE)
     3527 +        return EINVAL;
3323 3528  
3324      -    asn1_GetSequence(&c,&length);
     3529 +    oldp = p;
     3530 +    if (ASN1_get_object(&p, &tlen, &tag, &class, slen) & 0x80)
     3531 +        return EINVAL;
     3532 +    p += tlen;
     3533 +    slen -= (p - oldp);
3325 3534  
3326      -    ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen);
3327      -    c.p += Tlen;
3328      -    ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen);
     3535 +    if (ASN1_get_object(&p, &tlen, &tag, &class, slen) & 0x80)
     3536 +        return EINVAL;
3329 3537  
3330      -    asn1_const_Finish(&c);
     3538 +    *outdata = malloc(tlen);
     3539 +    if (*outdata == NULL)
     3540 +        return ENOMEM;
     3541 +    memcpy(*outdata, p, tlen);
     3542 +    *outdata_len = tlen;
3331 3543  
3332      -    *outdata = (unsigned char *)malloc((size_t)Tlen);
3333      -    /* Solaris Kerberos */
3334      -    if (outdata == NULL)
3335      -        return ENOMEM;
3336      -    
3337      -    (void) memcpy(*outdata, c.p, (size_t)Tlen);
3338      -    *outdata_len = Tlen;
3339      -
3340 3544      return 0;
3341 3545  }
3342 3546  
3343 3547  #ifndef WITHOUT_PKCS11
3344 3548  static void *
3345 3549  pkinit_C_LoadModule(const char *modname, CK_FUNCTION_LIST_PTR_PTR p11p)
3346 3550  {
3347 3551      void *handle;
3348 3552      CK_RV (*getflist)(CK_FUNCTION_LIST_PTR_PTR);
3349 3553  
↓ open down ↓ 544 lines elided ↑ open up ↑
3894 4098           */
3895 4099          (void) crypto_free_cert_info(context, plg_cryptoctx, req_cryptoctx,
3896 4100                                       id_cryptoctx);
3897 4101      }
3898 4102  
3899 4103      if (cert)
3900 4104          free(cert);
3901 4105  
3902 4106      if (cert_id)
3903 4107          free(cert_id);
3904      -    
     4108 +
3905 4109      return (r);
3906 4110  }
3907 4111  
3908 4112  /*
3909 4113   * Solaris Kerberos: this function has been significantly modified to prompt
3910 4114   * the user in certain cases so defer to this version when resyncing MIT code.
3911 4115   *
3912 4116   * pkinit_open_session now does several things including prompting the user if
3913 4117   * do_matching is set which indicates the code is executing in a client
3914 4118   * context.  This function fills out a pkinit_identity_crypto_context with a
↓ open down ↓ 43 lines elided ↑ open up ↑
3958 4162          return KRB5KDC_ERR_PREAUTH_FAILED;
3959 4163      }
3960 4164  
3961 4165      (void) memset(&token_choices, 0, sizeof(token_choices));
3962 4166  
3963 4167      /*
3964 4168       * Solaris Kerberos:
3965 4169       * If C_Initialize was already called by the process before the pkinit
3966 4170       * module was loaded then record that fact.
3967 4171       * "finalize_pkcs11" is used by pkinit_fini_pkcs11 to determine whether
3968      -     * or not C_Finalize() should be called. 
     4172 +     * or not C_Finalize() should be called.
3969 4173       */
3970 4174       cctx->finalize_pkcs11 =
3971 4175          (r == CKR_CRYPTOKI_ALREADY_INITIALIZED ? FALSE : TRUE);
3972 4176      /*
3973 4177       * First make sure that is an applicable slot otherwise fail.
3974 4178       *
3975 4179       * Start by getting a count of all slots with or without tokens.
3976 4180       */
3977 4181  
3978 4182      if ((r = cctx->p11->C_GetSlotList(FALSE, NULL, &count)) != CKR_OK) {
↓ open down ↓ 37 lines elided ↑ open up ↑
4016 4220              /* no slots match */
4017 4221              r = KRB5KDC_ERR_PREAUTH_FAILED;
4018 4222              krb5_set_error_message(context, r,
4019 4223                                     gettext("Requested PKCS11 slot ID %d not found"),
4020 4224                                     cctx->slotid);
4021 4225              pkiDebug("open_session: no matching slot found for slotID %d\n",
4022 4226                       cctx->slotid);
4023 4227              goto out;
4024 4228          }
4025 4229      }
4026      -    
     4230 +
4027 4231  tryagain:
4028 4232      /* get count of slots that have tokens */
4029 4233      if ((r = cctx->p11->C_GetSlotList(TRUE, NULL, &count)) != CKR_OK) {
4030 4234          pkiDebug("C_GetSlotList: %s\n", pkinit_pkcs11_code_to_text(r));
4031 4235          krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
4032 4236                                 gettext("Error trying to get PKCS11 slot list: %s"),
4033 4237                                 pkinit_pkcs11_code_to_text(r));
4034 4238          r = KRB5KDC_ERR_PREAUTH_FAILED;
4035 4239          goto out;
4036 4240      }
↓ open down ↓ 21 lines elided ↑ open up ↑
4058 4262              r = KRB5KDC_ERR_PREAUTH_FAILED;
4059 4263              krb5_set_error_message(context, r,
4060 4264                                     gettext("No smart card tokens found"));
4061 4265              pkiDebug("pkinit_open_session: no token, already prompted\n");
4062 4266              goto out;
4063 4267          }
4064 4268      }
4065 4269  
4066 4270      if (slotlist != NULL)
4067 4271          free(slotlist);
4068      - 
     4272 +
4069 4273      slotlist = malloc(count * sizeof (CK_SLOT_ID));
4070 4274      if (slotlist == NULL) {
4071 4275          krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
4072 4276                                 gettext("Memory allocation error"));
4073 4277          r = KRB5KDC_ERR_PREAUTH_FAILED;
4074 4278          goto out;
4075 4279      }
4076 4280      /*
4077 4281       * Solaris Kerberos: get list of PKCS11 slotid's that have tokens.
4078 4282       */
↓ open down ↓ 282 lines elided ↑ open up ↑
4361 4565  /* ARGSUSED */
4362 4566  krb5_error_code
4363 4567  pkinit_find_private_key(pkinit_identity_crypto_context id_cryptoctx,
4364 4568                          CK_ATTRIBUTE_TYPE usage,
4365 4569                          CK_OBJECT_HANDLE *objp)
4366 4570  {
4367 4571      CK_OBJECT_CLASS cls;
4368 4572      CK_ATTRIBUTE attrs[4];
4369 4573      CK_ULONG count;
4370 4574      CK_KEY_TYPE keytype;
     4575 +    RSA *rsa;
4371 4576      unsigned int nattrs = 0;
4372 4577      int r;
4373 4578  #ifdef PKINIT_USE_KEY_USAGE
4374 4579      CK_BBOOL true_false;
4375 4580  #endif
4376 4581  
4377 4582      cls = CKO_PRIVATE_KEY;
4378 4583      attrs[nattrs].type = CKA_CLASS;
4379 4584      attrs[nattrs].pValue = &cls;
4380 4585      attrs[nattrs].ulValueLen = sizeof cls;
↓ open down ↓ 40 lines elided ↑ open up ↑
4421 4626       * The CKA_ID may not be correctly set for the private key. For e.g. when
4422 4627       * storing a private key in softtoken pktool(1) doesn't generate or store
4423 4628       * a CKA_ID for the private key. Another way to identify the private key is
4424 4629       * to look for a private key with the same RSA modulus as the public key
4425 4630       * in the certificate.
4426 4631       */
4427 4632      if (r == CKR_OK && count != 1) {
4428 4633  
4429 4634          EVP_PKEY *priv;
4430 4635          X509 *cert;
     4636 +        const BIGNUM *rsan;
4431 4637          unsigned int n_len;
4432 4638          unsigned char *n_bytes;
4433 4639  
4434 4640          cert = sk_X509_value(id_cryptoctx->my_certs, 0);
4435 4641          priv = X509_get_pubkey(cert);
4436 4642          if (priv == NULL) {
4437 4643                  pkiDebug("Failed to extract pub key from cert\n");
4438 4644                  return KRB5KDC_ERR_PREAUTH_FAILED;
4439 4645          }
4440 4646  
↓ open down ↓ 11 lines elided ↑ open up ↑
4452 4658          attrs[nattrs].ulValueLen = sizeof true_false;
4453 4659          nattrs++;
4454 4660  #endif
4455 4661  
4456 4662          keytype = CKK_RSA;
4457 4663          attrs[nattrs].type = CKA_KEY_TYPE;
4458 4664          attrs[nattrs].pValue = &keytype;
4459 4665          attrs[nattrs].ulValueLen = sizeof keytype;
4460 4666          nattrs++;
4461 4667  
4462      -        n_len = BN_num_bytes(priv->pkey.rsa->n);
     4668 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
     4669 +        rsa = priv->pkey.rsa;
     4670 +        rsan = rsa->n;
     4671 +        n_len = BN_num_bytes(rsan);
     4672 +#else
     4673 +        rsa = EVP_PKEY_get0_RSA(priv);
     4674 +        RSA_get0_key(rsa, &rsan, NULL, NULL);
     4675 +        n_len = RSA_size(rsa);
     4676 +#endif
4463 4677          n_bytes = (unsigned char *) malloc((size_t) n_len);
4464 4678          if (n_bytes == NULL) {
4465 4679                  return (ENOMEM);
4466 4680          }
4467 4681  
4468      -        if (BN_bn2bin(priv->pkey.rsa->n, n_bytes) == 0) {
     4682 +        if (BN_bn2bin(rsan, n_bytes) == 0) {
4469 4683                  free (n_bytes);
4470      -                pkiDebug("zero-byte key modulus\n");
     4684 +                pkiDebug("zero-byte key modulus\n");
4471 4685                  return KRB5KDC_ERR_PREAUTH_FAILED;
4472 4686          }
4473 4687  
4474 4688          attrs[nattrs].type = CKA_MODULUS;
4475      -        attrs[nattrs].ulValueLen = n_len; 
     4689 +        attrs[nattrs].ulValueLen = n_len;
4476 4690          attrs[nattrs].pValue = n_bytes;
4477 4691  
4478 4692          nattrs++;
4479 4693  
4480 4694          r = id_cryptoctx->p11->C_FindObjectsInit(id_cryptoctx->session, attrs, nattrs);
4481 4695          free (n_bytes);
4482 4696          if (r != CKR_OK) {
4483 4697                  pkiDebug("krb5_pkinit_sign_data: C_FindObjectsInit: %s\n",
4484 4698                          pkinit_pkcs11_code_to_text(r));
4485 4699                  return KRB5KDC_ERR_PREAUTH_FAILED;
↓ open down ↓ 281 lines elided ↑ open up ↑
4767 4981          pkiDebug("private key does not match certificate\n");
4768 4982          /* Solaris Kerberos */
4769 4983          return EINVAL;
4770 4984      }
4771 4985  
4772 4986      buf_len = EVP_PKEY_size(pkey);
4773 4987      buf = (unsigned char *)malloc((size_t) buf_len + 10);
4774 4988      if (buf == NULL)
4775 4989          return ENOMEM;
4776 4990  
4777      -#if OPENSSL_VERSION_NUMBER < 0x10000000L
4778      -    len = EVP_PKEY_decrypt(buf, data, (int)data_len, pkey);
4779      -#else
4780 4991      len = EVP_PKEY_decrypt_old(buf, data, (int)data_len, pkey);
4781      -#endif
4782 4992      if (len <= 0) {
4783 4993          pkiDebug("unable to decrypt received data (len=%d)\n", data_len);
4784 4994          /* Solaris Kerberos */
4785 4995          free(buf);
4786 4996          return KRB5KRB_ERR_GENERIC;
4787 4997      }
4788 4998      *out_data = buf;
4789 4999      *out_data_len = len;
4790 5000  
4791 5001      return 0;
4792 5002  }
4793 5003  
4794 5004  static krb5_error_code
4795 5005  create_signature(unsigned char **sig, unsigned int *sig_len,
4796 5006                   unsigned char *data, unsigned int data_len, EVP_PKEY *pkey)
4797 5007  {
4798 5008      krb5_error_code retval = ENOMEM;
4799      -    EVP_MD_CTX md_ctx;
     5009 +    EVP_MD_CTX *md_ctx;
4800 5010  
4801 5011      if (pkey == NULL)
4802 5012          /* Solaris Kerberos */
4803 5013          return EINVAL;
4804 5014  
4805      -    EVP_VerifyInit(&md_ctx, EVP_sha1());
4806      -    EVP_SignUpdate(&md_ctx, data, data_len);
     5015 +    if ((md_ctx = EVP_MD_CTX_new()) == NULL)
     5016 +        return EINVAL;
     5017 +
     5018 +    EVP_VerifyInit(md_ctx, EVP_sha1());
     5019 +    EVP_SignUpdate(md_ctx, data, data_len);
4807 5020      *sig_len = EVP_PKEY_size(pkey);
4808 5021      if ((*sig = (unsigned char *) malloc((size_t) *sig_len)) == NULL)
4809 5022          goto cleanup;
4810      -    EVP_SignFinal(&md_ctx, *sig, sig_len, pkey);
     5023 +    EVP_SignFinal(md_ctx, *sig, sig_len, pkey);
4811 5024  
4812 5025      retval = 0;
4813 5026  
4814 5027    cleanup:
4815      -    EVP_MD_CTX_cleanup(&md_ctx);
     5028 +    EVP_MD_CTX_free(md_ctx);
4816 5029  
4817 5030      return retval;
4818 5031  }
4819 5032  
4820 5033  /*
4821 5034   * Note:
4822 5035   * This is not the routine the KDC uses to get its certificate.
4823 5036   * This routine is intended to be called by the client
4824 5037   * to obtain the KDC's certificate from some local storage
4825 5038   * to be sent as a hint in its request to the KDC.
↓ open down ↓ 320 lines elided ↑ open up ↑
5146 5359              idopts->cert_filename);
5147 5360          pkiDebug("%s: No cert/key pairs found in directory '%s'\n",
5148 5361                   __FUNCTION__, idopts->cert_filename);
5149 5362          retval = ENOENT;
5150 5363          goto cleanup;
5151 5364      }
5152 5365  
5153 5366      retval = 0;
5154 5367  
5155 5368    cleanup:
5156      -    if (d) 
     5369 +    if (d)
5157 5370          (void) closedir(d);
5158 5371  
5159 5372      return retval;
5160 5373  }
5161 5374  
5162 5375  #ifndef WITHOUT_PKCS11
5163 5376  /* ARGSUSED */
5164 5377  static krb5_error_code
5165 5378  pkinit_get_certs_pkcs11(krb5_context context,
5166 5379                          pkinit_plg_crypto_context plg_cryptoctx,
↓ open down ↓ 276 lines elided ↑ open up ↑
5443 5656  
5444 5657      if (ch_ret == NULL)
5445 5658          return EINVAL;
5446 5659  
5447 5660      id_cryptoctx = id->idctx;
5448 5661      if (id_cryptoctx == NULL)
5449 5662          return EINVAL;
5450 5663  
5451 5664      if (id_cryptoctx->creds[id->index] == NULL)
5452 5665          return PKINIT_ITER_NO_MORE;
5453      -    
     5666 +
5454 5667      cd = calloc(1, sizeof(*cd));
5455 5668      if (cd == NULL)
5456 5669          return ENOMEM;
5457 5670  
5458 5671      cd->magic = CERT_MAGIC;
5459 5672      cd->plgctx = id->plgctx;
5460 5673      cd->reqctx = id->reqctx;
5461 5674      cd->idctx = id->idctx;
5462 5675      cd->index = id->index;
5463 5676      cd->cred = id_cryptoctx->creds[id->index++];
↓ open down ↓ 35 lines elided ↑ open up ↑
5499 5712  
5500 5713      if (ret_ku_bits == NULL && ret_eku_bits == NULL)
5501 5714          return EINVAL;
5502 5715  
5503 5716      if (ret_eku_bits)
5504 5717          *ret_eku_bits = 0;
5505 5718      else {
5506 5719          pkiDebug("%s: EKUs not requested, not checking\n", __FUNCTION__);
5507 5720          goto check_kus;
5508 5721      }
5509      -    
     5722 +
5510 5723      /* Start with Extended Key usage */
5511 5724      i = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
5512 5725      if (i >= 0) {
5513 5726          EXTENDED_KEY_USAGE *eku;
5514 5727  
5515 5728          eku = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL);
5516 5729          if (eku) {
5517 5730              for (i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
5518 5731                  ASN1_OBJECT *certoid;
5519 5732                  certoid = sk_ASN1_OBJECT_value(eku, i);
↓ open down ↓ 200 lines elided ↑ open up ↑
5720 5933  crypto_cert_select(krb5_context context,
5721 5934                     pkinit_cert_matching_data *md)
5722 5935  {
5723 5936      struct _pkinit_cert_data *cd;
5724 5937      if (md == NULL)
5725 5938          return EINVAL;
5726 5939  
5727 5940      cd = (struct _pkinit_cert_data *)md->ch;
5728 5941      if (cd == NULL || cd->magic != CERT_MAGIC)
5729 5942          return EINVAL;
5730      -    
5731      -    /* copy the selected cert into our id_cryptoctx */ 
     5943 +
     5944 +    /* copy the selected cert into our id_cryptoctx */
5732 5945      if (cd->idctx->my_certs != NULL) {
5733 5946          sk_X509_pop_free(cd->idctx->my_certs, X509_free);
5734 5947      }
5735 5948      cd->idctx->my_certs = sk_X509_new_null();   
5736 5949      sk_X509_push(cd->idctx->my_certs, cd->cred->cert);
5737 5950      cd->idctx->creds[cd->index]->cert = NULL;       /* Don't free it twice */
5738 5951      cd->idctx->cert_index = 0;
5739 5952  
5740 5953      if (cd->idctx->pkcs11_method != 1) {
5741 5954          cd->idctx->my_key = cd->cred->key;
5742 5955          cd->idctx->creds[cd->index]->key = NULL;    /* Don't free it twice */
5743      -    } 
     5956 +    }
5744 5957  #ifndef WITHOUT_PKCS11
5745 5958      else {
5746 5959          cd->idctx->cert_id = cd->cred->cert_id;
5747 5960          cd->idctx->creds[cd->index]->cert_id = NULL; /* Don't free it twice */
5748 5961          cd->idctx->cert_id_len = cd->cred->cert_id_len;
5749 5962      }
5750 5963  #endif
5751 5964      return 0;
5752 5965  }
5753 5966  
↓ open down ↓ 21 lines elided ↑ open up ↑
5775 5988          retval = EINVAL;
5776 5989          krb5_set_error_message(context, retval,
5777 5990              gettext("Failed to select default certificate: "
5778 5991                  "found %d certs to choose from but there must be exactly one"),
5779 5992              cert_count);
5780 5993          pkiDebug("%s: ERROR: There are %d certs to choose from, "
5781 5994                   "but there must be exactly one.\n",
5782 5995                   __FUNCTION__, cert_count);
5783 5996          goto errout;
5784 5997      }
5785      -    /* copy the selected cert into our id_cryptoctx */ 
     5998 +    /* copy the selected cert into our id_cryptoctx */
5786 5999      if (id_cryptoctx->my_certs != NULL) {
5787 6000          sk_X509_pop_free(id_cryptoctx->my_certs, X509_free);
5788 6001      }
5789 6002      id_cryptoctx->my_certs = sk_X509_new_null();        
5790 6003      sk_X509_push(id_cryptoctx->my_certs, id_cryptoctx->creds[0]->cert);
5791 6004      id_cryptoctx->creds[0]->cert = NULL;        /* Don't free it twice */
5792 6005      id_cryptoctx->cert_index = 0;
5793 6006  
5794 6007      if (id_cryptoctx->pkcs11_method != 1) {
5795 6008          id_cryptoctx->my_key = id_cryptoctx->creds[0]->key;
5796 6009          id_cryptoctx->creds[0]->key = NULL;     /* Don't free it twice */
5797      -    } 
     6010 +    }
5798 6011  #ifndef WITHOUT_PKCS11
5799 6012      else {
5800 6013          id_cryptoctx->cert_id = id_cryptoctx->creds[0]->cert_id;
5801 6014          id_cryptoctx->creds[0]->cert_id = NULL; /* Don't free it twice */
5802 6015          id_cryptoctx->cert_id_len = id_cryptoctx->creds[0]->cert_id_len;
5803 6016      }
5804 6017  #endif
5805 6018      retval = 0;
5806 6019  errout:
5807 6020      return retval;
↓ open down ↓ 63 lines elided ↑ open up ↑
5871 6084          pkiDebug("%s: error reading file '%s'\n", __FUNCTION__, filename);
5872 6085          retval = EIO;
5873 6086          goto cleanup;
5874 6087      }
5875 6088  
5876 6089      /* scan over the stack created from loading the file contents,
5877 6090       * weed out duplicates, and push new ones onto the return stack
5878 6091       */
5879 6092      for (i = 0; i < sk_X509_INFO_num(sk); i++) {
5880 6093          X509_INFO *xi = sk_X509_INFO_value(sk, i);
5881      -        if (xi != NULL && xi->x509 != NULL && catype != CATYPE_CRLS) { 
     6094 +        if (xi != NULL && xi->x509 != NULL && catype != CATYPE_CRLS) {
5882 6095              int j = 0, size = sk_X509_num(ca_certs), flag = 0;
5883 6096  
5884 6097              if (!size) {
5885 6098                  sk_X509_push(ca_certs, xi->x509);
5886 6099                  xi->x509 = NULL;
5887 6100                  continue;
5888 6101              }
5889 6102              for (j = 0; j < size; j++) {
5890 6103                  X509 *x = sk_X509_value(ca_certs, j);
5891 6104                  flag = X509_cmp(x, xi->x509);
5892 6105                  if (flag == 0)
5893 6106                      break;
5894      -                else 
     6107 +                else
5895 6108                      continue;
5896 6109              }
5897 6110              if (flag != 0) {
5898 6111                  sk_X509_push(ca_certs, X509_dup(xi->x509));
5899 6112              }
5900 6113          } else if (xi != NULL && xi->crl != NULL && catype == CATYPE_CRLS) {
5901 6114              int j = 0, size = sk_X509_CRL_num(ca_crls), flag = 0;
5902 6115              if (!size) {
5903 6116                  sk_X509_CRL_push(ca_crls, xi->crl);
5904 6117                  xi->crl = NULL;
↓ open down ↓ 13 lines elided ↑ open up ↑
5918 6131          }
5919 6132      }
5920 6133  
5921 6134      /* If we added something and there wasn't a stack in the
5922 6135       * context before, add the temporary stack to the context.
5923 6136       */
5924 6137      switch(catype) {
5925 6138      case CATYPE_ANCHORS:
5926 6139          if (sk_X509_num(ca_certs) == 0) {
5927 6140              pkiDebug("no anchors in file, %s\n", filename);
5928      -            if (id_cryptoctx->trustedCAs == NULL) 
     6141 +            if (id_cryptoctx->trustedCAs == NULL)
5929 6142                  sk_X509_free(ca_certs);
5930 6143          } else {
5931 6144              if (id_cryptoctx->trustedCAs == NULL)
5932 6145                  id_cryptoctx->trustedCAs = ca_certs;
5933 6146          }
5934 6147          break;
5935 6148      case CATYPE_INTERMEDIATES:
5936 6149          if (sk_X509_num(ca_certs) == 0) {
5937 6150              pkiDebug("no intermediates in file, %s\n", filename);
5938      -            if (id_cryptoctx->intermediateCAs == NULL) 
     6151 +            if (id_cryptoctx->intermediateCAs == NULL)
5939 6152                  sk_X509_free(ca_certs);
5940 6153          } else {
5941 6154              if (id_cryptoctx->intermediateCAs == NULL)
5942 6155                  id_cryptoctx->intermediateCAs = ca_certs;
5943 6156          }
5944 6157          break;
5945 6158      case CATYPE_CRLS:
5946 6159          if (sk_X509_CRL_num(ca_crls) == 0) {
5947 6160              pkiDebug("no crls in file, %s\n", filename);
5948 6161              if (id_cryptoctx->revoked == NULL)
↓ open down ↓ 20 lines elided ↑ open up ↑
5969 6182  
5970 6183      return retval;
5971 6184  }
5972 6185  
5973 6186  static krb5_error_code
5974 6187  load_cas_and_crls_dir(krb5_context context,
5975 6188                        pkinit_plg_crypto_context plg_cryptoctx,
5976 6189                        pkinit_req_crypto_context req_cryptoctx,
5977 6190                        pkinit_identity_crypto_context id_cryptoctx,
5978 6191                        int catype,
5979      -                      char *dirname) 
     6192 +                      char *dirname)
5980 6193  {
5981 6194      krb5_error_code retval = EINVAL;
5982 6195      DIR *d = NULL;
5983 6196      struct dirent *dentry = NULL;
5984 6197      char filename[1024];
5985 6198  
5986 6199      if (dirname == NULL)
5987 6200          return EINVAL;
5988 6201  
5989 6202      d = opendir(dirname);
5990      -    if (d == NULL) 
     6203 +    if (d == NULL)
5991 6204          return ENOENT;
5992 6205  
5993 6206      while ((dentry = readdir(d))) {
5994 6207          if (strlen(dirname) + strlen(dentry->d_name) + 2 > sizeof(filename)) {
5995 6208              pkiDebug("%s: Path too long -- directory '%s' and file '%s'\n",
5996 6209                       __FUNCTION__, dirname, dentry->d_name);
5997 6210              goto cleanup;
5998 6211          }
5999 6212          /* Ignore subdirectories and anything starting with a dot */
6000 6213  #ifdef DT_DIR
↓ open down ↓ 6 lines elided ↑ open up ↑
6007 6220  
6008 6221          retval = load_cas_and_crls(context, plg_cryptoctx, req_cryptoctx,
6009 6222                                     id_cryptoctx, catype, filename);
6010 6223          if (retval)
6011 6224              goto cleanup;
6012 6225      }
6013 6226  
6014 6227      retval = 0;
6015 6228  
6016 6229    cleanup:
6017      -    if (d != NULL) 
     6230 +    if (d != NULL)
6018 6231          (void) closedir(d);
6019 6232  
6020 6233      return retval;
6021 6234  }
6022 6235  
6023 6236  /* ARGSUSED */
6024 6237  krb5_error_code
6025 6238  crypto_load_cas_and_crls(krb5_context context,
6026 6239                           pkinit_plg_crypto_context plg_cryptoctx,
6027 6240                           pkinit_req_crypto_context req_cryptoctx,
6028 6241                           pkinit_identity_opts *idopts,
6029 6242                           pkinit_identity_crypto_context id_cryptoctx,
6030 6243                           int idtype,
6031 6244                           int catype,
6032      -                         char *id) 
     6245 +                         char *id)
6033 6246  {
6034 6247      pkiDebug("%s: called with idtype %s and catype %s\n",
6035 6248               __FUNCTION__, idtype2string(idtype), catype2string(catype));
6036 6249      /* Solaris Kerberos: Removed "break"'s as they are never reached */
6037 6250      switch (idtype) {
6038 6251      case IDTYPE_FILE:
6039 6252          return load_cas_and_crls(context, plg_cryptoctx, req_cryptoctx,
6040 6253                                   id_cryptoctx, catype, id);
6041 6254      case IDTYPE_DIR:
6042 6255          return load_cas_and_crls_dir(context, plg_cryptoctx, req_cryptoctx,
↓ open down ↓ 48 lines elided ↑ open up ↑
6091 6304          /* fill-in issuerAndSerialNumber */
6092 6305          krb5_cas[i]->issuerAndSerialNumber.length = 0;
6093 6306          krb5_cas[i]->issuerAndSerialNumber.magic = 0;
6094 6307          krb5_cas[i]->issuerAndSerialNumber.data = NULL;
6095 6308  
6096 6309  #ifdef LONGHORN_BETA_COMPAT
6097 6310  if (longhorn == 0) { /* XXX Longhorn doesn't like this */
6098 6311  #endif
6099 6312          is = PKCS7_ISSUER_AND_SERIAL_new();
6100 6313          X509_NAME_set(&is->issuer, X509_get_issuer_name(x));
6101      -        M_ASN1_INTEGER_free(is->serial);
6102      -        is->serial = M_ASN1_INTEGER_dup(X509_get_serialNumber(x));
     6314 +        ASN1_INTEGER_free(is->serial);
     6315 +        is->serial = ASN1_INTEGER_dup(X509_get_serialNumber(x));
6103 6316          len = i2d_PKCS7_ISSUER_AND_SERIAL(is, NULL);
6104 6317          if ((p = krb5_cas[i]->issuerAndSerialNumber.data =
6105 6318               (unsigned char *)malloc((size_t) len)) == NULL)
6106 6319              goto cleanup;
6107 6320          i2d_PKCS7_ISSUER_AND_SERIAL(is, &p);
6108 6321          krb5_cas[i]->issuerAndSerialNumber.length = len;
6109 6322  #ifdef LONGHORN_BETA_COMPAT
6110 6323  }
6111 6324  #endif
6112 6325  
↓ open down ↓ 53 lines elided ↑ open up ↑
6166 6379  {
6167 6380  
6168 6381      krb5_error_code retval = ENOMEM;
6169 6382      STACK_OF(X509) *sk = NULL;
6170 6383  
6171 6384      *ids = NULL;
6172 6385      if (req_cryptoctx->received_cert == NULL)
6173 6386          return KRB5KDC_ERR_PREAUTH_FAILED;
6174 6387  
6175 6388      sk = sk_X509_new_null();
6176      -    if (sk == NULL) 
     6389 +    if (sk == NULL)
6177 6390          goto cleanup;
6178 6391      sk_X509_push(sk, req_cryptoctx->received_cert);
6179 6392  
6180 6393      retval = create_identifiers_from_stack(sk, ids);
6181 6394  
6182 6395      sk_X509_free(sk);
6183 6396  cleanup:
6184 6397  
6185 6398      return retval;
6186 6399  }
↓ open down ↓ 47 lines elided ↑ open up ↑
6234 6447  {
6235 6448  
6236 6449      /* Solaris Kerberos */
6237 6450      STACK_OF(X509) *sk = id_cryptoctx->trustedCAs;
6238 6451  
6239 6452      *ids = NULL;
6240 6453      if (id_cryptoctx->trustedCAs == NULL)
6241 6454          return KRB5KDC_ERR_PREAUTH_FAILED;
6242 6455  
6243 6456      return create_identifiers_from_stack(sk, ids);
6244      -    
     6457 +
6245 6458  }
6246 6459  
6247 6460  /* ARGSUSED */
6248 6461  krb5_error_code
6249 6462  create_krb5_trustedCas(krb5_context context,
6250 6463                         pkinit_plg_crypto_context plg_cryptoctx,
6251 6464                         pkinit_req_crypto_context req_cryptoctx,
6252 6465                         pkinit_identity_crypto_context id_cryptoctx,
6253 6466                         int flag,
6254 6467                         krb5_trusted_ca *** ids)
↓ open down ↓ 41 lines elided ↑ open up ↑
6296 6509                      goto cleanup;
6297 6510                  i2d_X509_NAME(xn, &p);
6298 6511                  krb5_cas[i]->u.caName.length = len;
6299 6512                  break;
6300 6513              case choice_trusted_cas_issuerAndSerial:
6301 6514                  krb5_cas[i]->choice = choice_trusted_cas_issuerAndSerial;
6302 6515                  krb5_cas[i]->u.issuerAndSerial.data = NULL;
6303 6516                  krb5_cas[i]->u.issuerAndSerial.length = 0;
6304 6517                  is = PKCS7_ISSUER_AND_SERIAL_new();
6305 6518                  X509_NAME_set(&is->issuer, X509_get_issuer_name(x));
6306      -                M_ASN1_INTEGER_free(is->serial);
6307      -                is->serial = M_ASN1_INTEGER_dup(X509_get_serialNumber(x));
     6519 +                ASN1_INTEGER_free(is->serial);
     6520 +                is->serial = ASN1_INTEGER_dup(X509_get_serialNumber(x));
6308 6521                  len = i2d_PKCS7_ISSUER_AND_SERIAL(is, NULL);
6309 6522                  if ((p = krb5_cas[i]->u.issuerAndSerial.data =
6310 6523                      (unsigned char *)malloc((size_t) len)) == NULL)
6311 6524                      goto cleanup;
6312 6525                  i2d_PKCS7_ISSUER_AND_SERIAL(is, &p);
6313 6526                  krb5_cas[i]->u.issuerAndSerial.length = len;
6314 6527                  if (is != NULL) {
6315 6528                      if (is->issuer != NULL)
6316 6529                          X509_NAME_free(is->issuer);
6317 6530                      if (is->serial != NULL)
↓ open down ↓ 28 lines elided ↑ open up ↑
6346 6559      krb5_error_code retval = ENOMEM;
6347 6560      X509 *cert = req_cryptoctx->received_cert;
6348 6561  
6349 6562      *out = NULL;
6350 6563      *out_len = 0;
6351 6564      if (req_cryptoctx->received_cert == NULL)
6352 6565          return 0;
6353 6566  
6354 6567      is = PKCS7_ISSUER_AND_SERIAL_new();
6355 6568      X509_NAME_set(&is->issuer, X509_get_issuer_name(cert));
6356      -    M_ASN1_INTEGER_free(is->serial);
6357      -    is->serial = M_ASN1_INTEGER_dup(X509_get_serialNumber(cert));
     6569 +    ASN1_INTEGER_free(is->serial);
     6570 +    is->serial = ASN1_INTEGER_dup(X509_get_serialNumber(cert));
6358 6571      len = i2d_PKCS7_ISSUER_AND_SERIAL(is, NULL);
6359 6572      if ((p = *out = (unsigned char *)malloc((size_t) len)) == NULL)
6360 6573          goto cleanup;
6361 6574      i2d_PKCS7_ISSUER_AND_SERIAL(is, &p);
6362 6575      *out_len = len;
6363 6576      retval = 0;
6364 6577  
6365 6578  cleanup:
6366 6579      X509_NAME_free(is->issuer);
6367 6580      ASN1_INTEGER_free(is->serial);
↓ open down ↓ 29 lines elided ↑ open up ↑
6397 6610  #pragma error_messages (off, E_END_OF_LOOP_CODE_NOT_REACHED)
6398 6611      for(;;) {
6399 6612          i = BIO_read(tmpmem, buf, sizeof(buf));
6400 6613          if (i <= 0) break;
6401 6614          BIO_write(data, buf, i);
6402 6615          BIO_free_all(tmpmem);
6403 6616          return 1;
6404 6617      }
6405 6618  #pragma error_messages (default, E_END_OF_LOOP_CODE_NOT_REACHED)
6406 6619  
6407      -    return 0; 
     6620 +    return 0;
6408 6621  }
6409 6622  
6410 6623  krb5_error_code
6411 6624  pkinit_process_td_trusted_certifiers(
6412 6625      krb5_context context,
6413 6626      pkinit_plg_crypto_context plg_cryptoctx,
6414 6627      pkinit_req_crypto_context req_cryptoctx,
6415 6628      pkinit_identity_crypto_context id_cryptoctx,
6416 6629      krb5_external_principal_identifier **krb5_trusted_certifiers,
6417 6630      int td_type)
↓ open down ↓ 114 lines elided ↑ open up ↑
6532 6745  
6533 6746      /* Find the recipientInfo which matches the passed certificate
6534 6747       * (if any)
6535 6748       */
6536 6749  
6537 6750      if (cert) {
6538 6751          for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) {
6539 6752              int tmp_ret = 0;
6540 6753              ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
6541 6754              tmp_ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
6542      -                                    cert->cert_info->issuer);
     6755 +                X509_get_issuer_name(cert));
6543 6756              if (!tmp_ret) {
6544      -                tmp_ret = M_ASN1_INTEGER_cmp(cert->cert_info->serialNumber,
     6757 +                tmp_ret = ASN1_INTEGER_cmp(X509_get_serialNumber(cert),
6545 6758                                               ri->issuer_and_serial->serial);
6546 6759                  if (!tmp_ret)
6547 6760                      break;
6548 6761              }
6549 6762              ri=NULL;
6550 6763          }
6551 6764          if (ri == NULL) {
6552 6765              PKCS7err(PKCS7_F_PKCS7_DATADECODE,
6553 6766                       PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
6554 6767              goto cleanup;
6555 6768          }
6556 6769          
6557 6770      }
6558 6771  
6559 6772      /* If we haven't got a certificate try each ri in turn */
6560 6773  
6561 6774      if (cert == NULL) {
6562 6775          for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) {
6563 6776              ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
6564 6777              jj = pkinit_decode_data(context, id_cryptoctx,
6565      -                M_ASN1_STRING_data(ri->enc_key),
6566      -                (unsigned int) M_ASN1_STRING_length(ri->enc_key),
     6778 +                (unsigned char *)ASN1_STRING_get0_data(ri->enc_key),
     6779 +                ASN1_STRING_length(ri->enc_key),
6567 6780                  &tmp, &tmp_len);
6568 6781              if (jj) {
6569 6782                  PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_EVP_LIB);
6570 6783                  goto cleanup;
6571 6784              }
6572 6785  
6573 6786              if (!jj && tmp_len > 0) {
6574 6787                  jj = tmp_len;
6575 6788                  break;
6576 6789              }
6577 6790  
6578 6791              ERR_clear_error();
6579 6792              ri = NULL;
6580 6793          }
6581 6794  
6582 6795          if (ri == NULL) {
6583      -            PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_RECIPIENT_MATCHES_KEY);
     6796 +            PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
6584 6797              goto cleanup;
6585 6798          }
6586 6799      }
6587 6800      else {
6588 6801          jj = pkinit_decode_data(context, id_cryptoctx,
6589      -            M_ASN1_STRING_data(ri->enc_key),
6590      -            (unsigned int) M_ASN1_STRING_length(ri->enc_key),
     6802 +            (unsigned char *)ASN1_STRING_get0_data(ri->enc_key),
     6803 +            ASN1_STRING_length(ri->enc_key),
6591 6804              &tmp, &tmp_len);
6592 6805          /* Solaris Kerberos: tmp_len is unsigned. Cannot be < 0 */
6593 6806          if (jj || tmp_len == 0) {
6594 6807              PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_EVP_LIB);
6595 6808              goto cleanup;
6596 6809          }
6597 6810          jj = tmp_len;
6598 6811      }
6599 6812  
6600 6813      evp_ctx=NULL;
↓ open down ↓ 3 lines elided ↑ open up ↑
6604 6817      if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0)
6605 6818          goto cleanup;
6606 6819  
6607 6820      if (jj != EVP_CIPHER_CTX_key_length(evp_ctx)) {
6608 6821          /* Some S/MIME clients don't use the same key
6609 6822           * and effective key length. The key length is
6610 6823           * determined by the size of the decrypted RSA key.
6611 6824           */
6612 6825          if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, (int)jj)) {
6613 6826              PKCS7err(PKCS7_F_PKCS7_DATADECODE,
6614      -                     PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH);
     6827 +                     PKCS7_R_DECRYPT_ERROR);
6615 6828              goto cleanup;
6616 6829          }
6617 6830      }
6618 6831      if (EVP_CipherInit_ex(evp_ctx,NULL,NULL,tmp,NULL,0) <= 0)
6619 6832          goto cleanup;
6620 6833  
6621 6834      OPENSSL_cleanse(tmp,jj);
6622 6835  
6623 6836      if (out == NULL)
6624 6837          out=etmp;
↓ open down ↓ 121 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX