xref: /freebsd/crypto/krb5/src/lib/krb5/krb/mk_rep.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/mk_rep.c */
3 /*
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 /*
27  * Copyright (c) 2006-2008, Novell, Inc.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions are met:
32  *
33  *   * Redistributions of source code must retain the above copyright notice,
34  *       this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *       notice, this list of conditions and the following disclaimer in the
37  *       documentation and/or other materials provided with the distribution.
38  *   * The copyright holder's name is not used to endorse or promote products
39  *       derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  */
53 
54 #include "k5-int.h"
55 #include "int-proto.h"
56 #include "auth_con.h"
57 
58 /*
59   Formats a KRB_AP_REP message into outbuf.
60 
61   The outbuf buffer storage is allocated, and should be freed by the
62   caller when finished.
63 
64   returns system errors
65 */
66 
67 static krb5_error_code
k5_mk_rep(krb5_context context,krb5_auth_context auth_context,krb5_data * outbuf,int dce_style)68 k5_mk_rep(krb5_context context, krb5_auth_context auth_context,
69           krb5_data *outbuf, int dce_style)
70 {
71     krb5_error_code       retval;
72     krb5_ap_rep_enc_part  repl;
73     krb5_ap_rep           reply;
74     krb5_data           * scratch;
75     krb5_data           * toutbuf;
76 
77     /* Make the reply */
78     if (((auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) ||
79          (auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_SEQUENCE)) &&
80         (auth_context->local_seq_number == 0)) {
81         if ((retval = krb5_generate_seq_number(context,
82                                                &auth_context->key->keyblock,
83                                                &auth_context->local_seq_number)))
84             return(retval);
85     }
86 
87     if (dce_style) {
88         krb5_us_timeofday(context, &repl.ctime, &repl.cusec);
89     } else {
90         repl.ctime = auth_context->authentp->ctime;
91         repl.cusec = auth_context->authentp->cusec;
92     }
93 
94     if (dce_style)
95         repl.subkey = NULL;
96     else if (auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_USE_SUBKEY) {
97         assert(auth_context->negotiated_etype != ENCTYPE_NULL);
98 
99         retval = k5_generate_and_save_subkey(context, auth_context,
100                                              &auth_context->key->keyblock,
101                                              auth_context->negotiated_etype);
102         if (retval)
103             return retval;
104         repl.subkey = &auth_context->send_subkey->keyblock;
105     } else
106         repl.subkey = auth_context->authentp->subkey;
107 
108     if (dce_style)
109         repl.seq_number = auth_context->remote_seq_number;
110     else
111         repl.seq_number = auth_context->local_seq_number;
112 
113     TRACE_MK_REP(context, repl.ctime, repl.cusec, repl.subkey,
114                  repl.seq_number);
115 
116     /* encode it before encrypting */
117     if ((retval = encode_krb5_ap_rep_enc_part(&repl, &scratch)))
118         return retval;
119 
120     if ((retval = k5_encrypt_keyhelper(context, auth_context->key,
121                                        KRB5_KEYUSAGE_AP_REP_ENCPART, scratch,
122                                        &reply.enc_part)))
123         goto cleanup_scratch;
124 
125     if (!(retval = encode_krb5_ap_rep(&reply, &toutbuf))) {
126         *outbuf = *toutbuf;
127         free(toutbuf);
128     }
129 
130     memset(reply.enc_part.ciphertext.data, 0, reply.enc_part.ciphertext.length);
131     free(reply.enc_part.ciphertext.data);
132     reply.enc_part.ciphertext.length = 0;
133     reply.enc_part.ciphertext.data = 0;
134 
135 cleanup_scratch:
136     memset(scratch->data, 0, scratch->length);
137     krb5_free_data(context, scratch);
138 
139     return retval;
140 }
141 
142 krb5_error_code KRB5_CALLCONV
krb5_mk_rep(krb5_context context,krb5_auth_context auth_context,krb5_data * outbuf)143 krb5_mk_rep(krb5_context context, krb5_auth_context auth_context, krb5_data *outbuf)
144 {
145     return k5_mk_rep(context, auth_context, outbuf, 0);
146 }
147 
148 krb5_error_code KRB5_CALLCONV
krb5_mk_rep_dce(krb5_context context,krb5_auth_context auth_context,krb5_data * outbuf)149 krb5_mk_rep_dce(krb5_context context, krb5_auth_context auth_context, krb5_data *outbuf)
150 {
151     return k5_mk_rep(context, auth_context, outbuf, 1);
152 }
153