LCOV - code coverage report
Current view: top level - source4/auth/kerberos - kerberos_pac.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 119 190 62.6 %
Date: 2021-09-23 10:06:22 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Create and parse the krb5 PAC
       5             : 
       6             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005,2008
       7             :    Copyright (C) Andrew Tridgell 2001
       8             :    Copyright (C) Luke Howard 2002-2003
       9             :    Copyright (C) Stefan Metzmacher 2004-2005
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             : 
      22             :    You should have received a copy of the GNU General Public License
      23             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      24             : */
      25             : 
      26             : #include "includes.h"
      27             : #include "system/kerberos.h"
      28             : #include "auth/auth.h"
      29             : #include "auth/kerberos/kerberos.h"
      30             : #include "librpc/gen_ndr/ndr_krb5pac.h"
      31             : #include <ldb.h>
      32             : #include "auth/auth_sam_reply.h"
      33             : #include "auth/credentials/credentials.h"
      34             : #include "auth/kerberos/kerberos_util.h"
      35             : #include "auth/kerberos/pac_utils.h"
      36             : 
      37           3 :  krb5_error_code kerberos_encode_pac(TALLOC_CTX *mem_ctx,
      38             :                                     struct PAC_DATA *pac_data,
      39             :                                     krb5_context context,
      40             :                                     const krb5_keyblock *krbtgt_keyblock,
      41             :                                     const krb5_keyblock *service_keyblock,
      42             :                                     DATA_BLOB *pac)
      43             : {
      44             :         NTSTATUS nt_status;
      45             :         krb5_error_code ret;
      46             :         enum ndr_err_code ndr_err;
      47           3 :         DATA_BLOB zero_blob = data_blob(NULL, 0);
      48           3 :         DATA_BLOB tmp_blob = data_blob(NULL, 0);
      49           3 :         struct PAC_SIGNATURE_DATA *kdc_checksum = NULL;
      50           3 :         struct PAC_SIGNATURE_DATA *srv_checksum = NULL;
      51             :         uint32_t i;
      52             : 
      53             :         /* First, just get the keytypes filled in (and lengths right, eventually) */
      54          15 :         for (i=0; i < pac_data->num_buffers; i++) {
      55          12 :                 if (pac_data->buffers[i].type != PAC_TYPE_KDC_CHECKSUM) {
      56           9 :                         continue;
      57             :                 }
      58           3 :                 kdc_checksum = &pac_data->buffers[i].info->kdc_cksum,
      59           3 :                 ret = smb_krb5_make_pac_checksum(mem_ctx,
      60             :                                                  &zero_blob,
      61             :                                                  context,
      62             :                                                  krbtgt_keyblock,
      63             :                                                  &kdc_checksum->type,
      64             :                                                  &kdc_checksum->signature);
      65           3 :                 if (ret) {
      66           0 :                         DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
      67             :                                   smb_get_krb5_error_message(context, ret, mem_ctx)));
      68           0 :                         talloc_free(pac_data);
      69           0 :                         return ret;
      70             :                 }
      71             :         }
      72             : 
      73          12 :         for (i=0; i < pac_data->num_buffers; i++) {
      74          12 :                 if (pac_data->buffers[i].type != PAC_TYPE_SRV_CHECKSUM) {
      75           9 :                         continue;
      76             :                 }
      77           3 :                 srv_checksum = &pac_data->buffers[i].info->srv_cksum;
      78           3 :                 ret = smb_krb5_make_pac_checksum(mem_ctx,
      79             :                                                  &zero_blob,
      80             :                                                  context,
      81             :                                                  service_keyblock,
      82             :                                                  &srv_checksum->type,
      83             :                                                  &srv_checksum->signature);
      84           3 :                 if (ret) {
      85           0 :                         DEBUG(2, ("making service PAC checksum failed: %s\n",
      86             :                                   smb_get_krb5_error_message(context, ret, mem_ctx)));
      87           0 :                         talloc_free(pac_data);
      88           0 :                         return ret;
      89             :                 }
      90             :         }
      91             : 
      92           3 :         if (!kdc_checksum) {
      93           0 :                 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
      94           0 :                 return EINVAL;
      95             :         }
      96           3 :         if (!srv_checksum) {
      97           0 :                 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
      98           0 :                 return EINVAL;
      99             :         }
     100             : 
     101             :         /* But wipe out the actual signatures */
     102           6 :         memset(kdc_checksum->signature.data, '\0', kdc_checksum->signature.length);
     103           6 :         memset(srv_checksum->signature.data, '\0', srv_checksum->signature.length);
     104             : 
     105           3 :         ndr_err = ndr_push_struct_blob(&tmp_blob, mem_ctx,
     106             :                                        pac_data,
     107             :                                        (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
     108           3 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     109           0 :                 nt_status = ndr_map_error2ntstatus(ndr_err);
     110           0 :                 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
     111           0 :                 talloc_free(pac_data);
     112           0 :                 return EINVAL;
     113             :         }
     114             : 
     115             :         /* Then sign the result of the previous push, where the sig was zero'ed out */
     116           3 :         ret = smb_krb5_make_pac_checksum(mem_ctx,
     117             :                                          &tmp_blob,
     118             :                                          context,
     119             :                                          service_keyblock,
     120             :                                          &srv_checksum->type,
     121             :                                          &srv_checksum->signature);
     122             : 
     123           3 :         if (ret) {
     124           0 :                 DBG_WARNING("making krbtgt PAC srv_checksum failed: %s\n",
     125             :                             smb_get_krb5_error_message(context, ret, mem_ctx));
     126           0 :                 talloc_free(pac_data);
     127           0 :                 return ret;
     128             :         }
     129             : 
     130             :         /* Then sign Server checksum */
     131           3 :         ret = smb_krb5_make_pac_checksum(mem_ctx,
     132             :                                          &srv_checksum->signature,
     133             :                                          context,
     134             :                                          krbtgt_keyblock,
     135             :                                          &kdc_checksum->type,
     136             :                                          &kdc_checksum->signature);
     137           3 :         if (ret) {
     138           0 :                 DBG_WARNING("making krbtgt PAC kdc_checksum failed: %s\n",
     139             :                             smb_get_krb5_error_message(context, ret, mem_ctx));
     140           0 :                 talloc_free(pac_data);
     141           0 :                 return ret;
     142             :         }
     143             : 
     144             :         /* And push it out again, this time to the world.  This relies on determanistic pointer values */
     145           3 :         ndr_err = ndr_push_struct_blob(&tmp_blob, mem_ctx,
     146             :                                        pac_data,
     147             :                                        (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
     148           3 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     149           0 :                 nt_status = ndr_map_error2ntstatus(ndr_err);
     150           0 :                 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status)));
     151           0 :                 talloc_free(pac_data);
     152           0 :                 return EINVAL;
     153             :         }
     154             : 
     155           3 :         *pac = tmp_blob;
     156             : 
     157           3 :         return ret;
     158             : }
     159             : 
     160             : 
     161           2 :  krb5_error_code kerberos_create_pac(TALLOC_CTX *mem_ctx,
     162             :                                      struct auth_user_info_dc *user_info_dc,
     163             :                                      krb5_context context,
     164             :                                      const krb5_keyblock *krbtgt_keyblock,
     165             :                                      const krb5_keyblock *service_keyblock,
     166             :                                      krb5_principal client_principal,
     167             :                                      time_t tgs_authtime,
     168             :                                      DATA_BLOB *pac)
     169             : {
     170             :         NTSTATUS nt_status;
     171             :         krb5_error_code ret;
     172           2 :         struct PAC_DATA *pac_data = talloc(mem_ctx, struct PAC_DATA);
     173             :         struct netr_SamInfo3 *sam3;
     174             :         union PAC_INFO *u_LOGON_INFO;
     175             :         struct PAC_LOGON_INFO *LOGON_INFO;
     176             :         union PAC_INFO *u_LOGON_NAME;
     177             :         struct PAC_LOGON_NAME *LOGON_NAME;
     178             :         union PAC_INFO *u_KDC_CHECKSUM;
     179             :         union PAC_INFO *u_SRV_CHECKSUM;
     180             : 
     181             :         char *name;
     182             : 
     183             :         enum {
     184             :                 PAC_BUF_LOGON_INFO = 0,
     185             :                 PAC_BUF_LOGON_NAME = 1,
     186             :                 PAC_BUF_SRV_CHECKSUM = 2,
     187             :                 PAC_BUF_KDC_CHECKSUM = 3,
     188             :                 PAC_BUF_NUM_BUFFERS = 4
     189             :         };
     190             : 
     191           2 :         if (!pac_data) {
     192           0 :                 return ENOMEM;
     193             :         }
     194             : 
     195           2 :         pac_data->num_buffers = PAC_BUF_NUM_BUFFERS;
     196           2 :         pac_data->version = 0;
     197             : 
     198           2 :         pac_data->buffers = talloc_array(pac_data,
     199             :                                          struct PAC_BUFFER,
     200             :                                          pac_data->num_buffers);
     201           2 :         if (!pac_data->buffers) {
     202           0 :                 talloc_free(pac_data);
     203           0 :                 return ENOMEM;
     204             :         }
     205             : 
     206             :         /* LOGON_INFO */
     207           2 :         u_LOGON_INFO = talloc_zero(pac_data->buffers, union PAC_INFO);
     208           2 :         if (!u_LOGON_INFO) {
     209           0 :                 talloc_free(pac_data);
     210           0 :                 return ENOMEM;
     211             :         }
     212           2 :         pac_data->buffers[PAC_BUF_LOGON_INFO].type = PAC_TYPE_LOGON_INFO;
     213           2 :         pac_data->buffers[PAC_BUF_LOGON_INFO].info = u_LOGON_INFO;
     214             : 
     215             :         /* LOGON_NAME */
     216           2 :         u_LOGON_NAME = talloc_zero(pac_data->buffers, union PAC_INFO);
     217           2 :         if (!u_LOGON_NAME) {
     218           0 :                 talloc_free(pac_data);
     219           0 :                 return ENOMEM;
     220             :         }
     221           2 :         pac_data->buffers[PAC_BUF_LOGON_NAME].type = PAC_TYPE_LOGON_NAME;
     222           2 :         pac_data->buffers[PAC_BUF_LOGON_NAME].info = u_LOGON_NAME;
     223           2 :         LOGON_NAME = &u_LOGON_NAME->logon_name;
     224             : 
     225             :         /* SRV_CHECKSUM */
     226           2 :         u_SRV_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
     227           2 :         if (!u_SRV_CHECKSUM) {
     228           0 :                 talloc_free(pac_data);
     229           0 :                 return ENOMEM;
     230             :         }
     231           2 :         pac_data->buffers[PAC_BUF_SRV_CHECKSUM].type = PAC_TYPE_SRV_CHECKSUM;
     232           2 :         pac_data->buffers[PAC_BUF_SRV_CHECKSUM].info = u_SRV_CHECKSUM;
     233             : 
     234             :         /* KDC_CHECKSUM */
     235           2 :         u_KDC_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
     236           2 :         if (!u_KDC_CHECKSUM) {
     237           0 :                 talloc_free(pac_data);
     238           0 :                 return ENOMEM;
     239             :         }
     240           2 :         pac_data->buffers[PAC_BUF_KDC_CHECKSUM].type = PAC_TYPE_KDC_CHECKSUM;
     241           2 :         pac_data->buffers[PAC_BUF_KDC_CHECKSUM].info = u_KDC_CHECKSUM;
     242             : 
     243             :         /* now the real work begins... */
     244             : 
     245           2 :         LOGON_INFO = talloc_zero(u_LOGON_INFO, struct PAC_LOGON_INFO);
     246           2 :         if (!LOGON_INFO) {
     247           0 :                 talloc_free(pac_data);
     248           0 :                 return ENOMEM;
     249             :         }
     250           2 :         nt_status = auth_convert_user_info_dc_saminfo3(LOGON_INFO, user_info_dc, &sam3);
     251           2 :         if (!NT_STATUS_IS_OK(nt_status)) {
     252           0 :                 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status)));
     253           0 :                 talloc_free(pac_data);
     254           0 :                 return EINVAL;
     255             :         }
     256             : 
     257           2 :         u_LOGON_INFO->logon_info.info                = LOGON_INFO;
     258           2 :         LOGON_INFO->info3 = *sam3;
     259             : 
     260           2 :         ret = krb5_unparse_name_flags(context, client_principal,
     261             :                                       KRB5_PRINCIPAL_UNPARSE_NO_REALM |
     262             :                                       KRB5_PRINCIPAL_UNPARSE_DISPLAY,
     263             :                                       &name);
     264           2 :         if (ret) {
     265           0 :                 return ret;
     266             :         }
     267           2 :         LOGON_NAME->account_name     = talloc_strdup(LOGON_NAME, name);
     268           2 :         free(name);
     269             :         /*
     270             :           this logon_time field is absolutely critical. This is what
     271             :           caused all our PAC troubles :-)
     272             :         */
     273           2 :         unix_to_nt_time(&LOGON_NAME->logon_time, tgs_authtime);
     274             : 
     275           2 :         ret = kerberos_encode_pac(mem_ctx,
     276             :                                   pac_data,
     277             :                                   context,
     278             :                                   krbtgt_keyblock,
     279             :                                   service_keyblock,
     280             :                                   pac);
     281           2 :         talloc_free(pac_data);
     282           2 :         return ret;
     283             : }
     284             : 
     285       67374 : krb5_error_code kerberos_pac_to_user_info_dc(TALLOC_CTX *mem_ctx,
     286             :                                              krb5_pac pac,
     287             :                                              krb5_context context,
     288             :                                              struct auth_user_info_dc **user_info_dc,
     289             :                                              struct PAC_SIGNATURE_DATA *pac_srv_sig,
     290             :                                              struct PAC_SIGNATURE_DATA *pac_kdc_sig)
     291             : {
     292             :         NTSTATUS nt_status;
     293             :         enum ndr_err_code ndr_err;
     294             :         krb5_error_code ret;
     295             : 
     296             :         DATA_BLOB pac_logon_info_in, pac_srv_checksum_in, pac_kdc_checksum_in;
     297             :         krb5_data k5pac_logon_info_in, k5pac_srv_checksum_in, k5pac_kdc_checksum_in;
     298             :         DATA_BLOB pac_upn_dns_info_in;
     299             :         krb5_data k5pac_upn_dns_info_in;
     300             : 
     301             :         union PAC_INFO info;
     302             :         union PAC_INFO _upn_dns_info;
     303       67374 :         const struct PAC_UPN_DNS_INFO *upn_dns_info = NULL;
     304             :         struct auth_user_info_dc *user_info_dc_out;
     305             : 
     306       67374 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     307             : 
     308       67374 :         if (!tmp_ctx) {
     309           0 :                 return ENOMEM;
     310             :         }
     311             : 
     312       67374 :         ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_LOGON_INFO, &k5pac_logon_info_in);
     313       67374 :         if (ret != 0) {
     314           0 :                 talloc_free(tmp_ctx);
     315           0 :                 return EINVAL;
     316             :         }
     317             : 
     318       67374 :         pac_logon_info_in = data_blob_const(k5pac_logon_info_in.data, k5pac_logon_info_in.length);
     319             : 
     320       67374 :         ndr_err = ndr_pull_union_blob(&pac_logon_info_in, tmp_ctx, &info,
     321             :                                       PAC_TYPE_LOGON_INFO,
     322             :                                       (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
     323       67374 :         smb_krb5_free_data_contents(context, &k5pac_logon_info_in);
     324       67374 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     325           0 :                 nt_status = ndr_map_error2ntstatus(ndr_err);
     326           0 :                 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
     327           0 :                 talloc_free(tmp_ctx);
     328           0 :                 return EINVAL;
     329             :         }
     330       67374 :         if (info.logon_info.info == NULL) {
     331           0 :                 DEBUG(0,("can't parse the PAC LOGON_INFO: missing info pointer\n"));
     332           0 :                 talloc_free(tmp_ctx);
     333           0 :                 return EINVAL;
     334             :         }
     335             : 
     336       67374 :         ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_UPN_DNS_INFO,
     337             :                                   &k5pac_upn_dns_info_in);
     338       67374 :         if (ret == ENOENT) {
     339           2 :                 ZERO_STRUCT(k5pac_upn_dns_info_in);
     340           2 :                 ret = 0;
     341             :         }
     342       67372 :         if (ret != 0) {
     343           0 :                 talloc_free(tmp_ctx);
     344           0 :                 return EINVAL;
     345             :         }
     346             : 
     347       67374 :         pac_upn_dns_info_in = data_blob_const(k5pac_upn_dns_info_in.data,
     348        6362 :                                               k5pac_upn_dns_info_in.length);
     349             : 
     350       67374 :         if (pac_upn_dns_info_in.length != 0) {
     351       67372 :                 ndr_err = ndr_pull_union_blob(&pac_upn_dns_info_in, tmp_ctx,
     352             :                                               &_upn_dns_info,
     353             :                                               PAC_TYPE_UPN_DNS_INFO,
     354             :                                               (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
     355       67372 :                 smb_krb5_free_data_contents(context, &k5pac_upn_dns_info_in);
     356       67372 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     357           0 :                         nt_status = ndr_map_error2ntstatus(ndr_err);
     358           0 :                         DEBUG(0,("can't parse the PAC UPN_DNS_INFO: %s\n",
     359             :                                  nt_errstr(nt_status)));
     360           0 :                         talloc_free(tmp_ctx);
     361           0 :                         return EINVAL;
     362             :                 }
     363       65545 :                 upn_dns_info = &_upn_dns_info.upn_dns_info;
     364             :         }
     365             : 
     366             :         /* Pull this right into the normal auth sysstem structures */
     367       67374 :         nt_status = make_user_info_dc_pac(mem_ctx,
     368       67374 :                                          info.logon_info.info,
     369             :                                          upn_dns_info,
     370             :                                          &user_info_dc_out);
     371       67374 :         if (!NT_STATUS_IS_OK(nt_status)) {
     372           0 :                 talloc_free(tmp_ctx);
     373           0 :                 return EINVAL;
     374             :         }
     375             : 
     376       67374 :         if (pac_srv_sig) {
     377       39949 :                 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_SRV_CHECKSUM, &k5pac_srv_checksum_in);
     378       39949 :                 if (ret != 0) {
     379           0 :                         talloc_free(tmp_ctx);
     380           0 :                         return ret;
     381             :                 }
     382             : 
     383       39949 :                 pac_srv_checksum_in = data_blob_const(k5pac_srv_checksum_in.data, k5pac_srv_checksum_in.length);
     384             : 
     385       39949 :                 ndr_err = ndr_pull_struct_blob(&pac_srv_checksum_in, pac_srv_sig,
     386             :                                                pac_srv_sig,
     387             :                                                (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
     388       39949 :                 smb_krb5_free_data_contents(context, &k5pac_srv_checksum_in);
     389       39949 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     390           0 :                         nt_status = ndr_map_error2ntstatus(ndr_err);
     391           0 :                         DEBUG(0,("can't parse the KDC signature: %s\n",
     392             :                                  nt_errstr(nt_status)));
     393           0 :                         return EINVAL;
     394             :                 }
     395             :         }
     396             : 
     397       67374 :         if (pac_kdc_sig) {
     398       39949 :                 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_KDC_CHECKSUM, &k5pac_kdc_checksum_in);
     399       39949 :                 if (ret != 0) {
     400           0 :                         talloc_free(tmp_ctx);
     401           0 :                         return ret;
     402             :                 }
     403             : 
     404       39949 :                 pac_kdc_checksum_in = data_blob_const(k5pac_kdc_checksum_in.data, k5pac_kdc_checksum_in.length);
     405             : 
     406       39949 :                 ndr_err = ndr_pull_struct_blob(&pac_kdc_checksum_in, pac_kdc_sig,
     407             :                                                pac_kdc_sig,
     408             :                                                (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
     409       39949 :                 smb_krb5_free_data_contents(context, &k5pac_kdc_checksum_in);
     410       39949 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     411           0 :                         nt_status = ndr_map_error2ntstatus(ndr_err);
     412           0 :                         DEBUG(0,("can't parse the KDC signature: %s\n",
     413             :                                  nt_errstr(nt_status)));
     414           0 :                         return EINVAL;
     415             :                 }
     416             :         }
     417       67374 :         *user_info_dc = user_info_dc_out;
     418             : 
     419       67374 :         return 0;
     420             : }
     421             : 
     422             : 
     423       27525 : NTSTATUS kerberos_pac_blob_to_user_info_dc(TALLOC_CTX *mem_ctx,
     424             :                                            DATA_BLOB pac_blob,
     425             :                                            krb5_context context,
     426             :                                            struct auth_user_info_dc **user_info_dc,
     427             :                                            struct PAC_SIGNATURE_DATA *pac_srv_sig,
     428             :                                            struct PAC_SIGNATURE_DATA *pac_kdc_sig)
     429             : {
     430             :         krb5_error_code ret;
     431             :         krb5_pac pac;
     432       48688 :         ret = krb5_pac_parse(context,
     433       27525 :                              pac_blob.data, pac_blob.length,
     434             :                              &pac);
     435       27525 :         if (ret) {
     436           0 :                 return map_nt_error_from_unix_common(ret);
     437             :         }
     438             : 
     439             : 
     440       27525 :         ret = kerberos_pac_to_user_info_dc(mem_ctx, pac, context, user_info_dc, pac_srv_sig, pac_kdc_sig);
     441       27525 :         krb5_pac_free(context, pac);
     442       27525 :         if (ret) {
     443           0 :                 return map_nt_error_from_unix_common(ret);
     444             :         }
     445       27525 :         return NT_STATUS_OK;
     446             : }

Generated by: LCOV version 1.13