4 #include <gssapi/gssapi.h>
9 static gss_OID_desc putty_gss_mech_krb5_desc
=
10 { 9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
11 static gss_OID
const putty_gss_mech_krb5
= &putty_gss_mech_krb5_desc
;
13 typedef struct uxSsh_gss_ctx
{
19 int ssh_gss_init(void)
21 /* On Windows this tries to load the SSPI library functions. On
22 Unix we assume we have GSSAPI at runtime if we were linked with
27 Ssh_gss_stat
ssh_gss_indicate_mech(Ssh_gss_buf
*mech
)
29 /* Copy constant into mech */
30 mech
->length
= putty_gss_mech_krb5
->length
;
31 mech
->value
= putty_gss_mech_krb5
->elements
;
36 Ssh_gss_stat
ssh_gss_import_name(char *host
,
37 Ssh_gss_name
*srv_name
)
39 OM_uint32 min_stat
,maj_stat
;
40 gss_buffer_desc host_buf
;
43 pStr
= dupcat("host@", host
, NULL
);
45 host_buf
.value
= pStr
;
46 host_buf
.length
= strlen(pStr
);
48 maj_stat
= gss_import_name(&min_stat
, &host_buf
,
49 GSS_C_NT_HOSTBASED_SERVICE
, srv_name
);
52 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
53 return SSH_GSS_FAILURE
;
56 Ssh_gss_stat
ssh_gss_acquire_cred(Ssh_gss_ctx
*ctx
)
58 uxSsh_gss_ctx
*uxctx
= snew(uxSsh_gss_ctx
);
60 uxctx
->maj_stat
= uxctx
->min_stat
= GSS_S_COMPLETE
;
61 uxctx
->ctx
= GSS_C_NO_CONTEXT
;
62 *ctx
= (Ssh_gss_ctx
) uxctx
;
67 Ssh_gss_stat
ssh_gss_init_sec_context(Ssh_gss_ctx
*ctx
,
68 Ssh_gss_name srv_name
,
70 Ssh_gss_buf
*recv_tok
,
71 Ssh_gss_buf
*send_tok
)
73 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) *ctx
;
76 if (to_deleg
) to_deleg
= GSS_C_DELEG_FLAG
;
77 uxctx
->maj_stat
= gss_init_sec_context(&uxctx
->min_stat
,
81 (gss_OID
) putty_gss_mech_krb5
,
83 GSS_C_INTEG_FLAG
| to_deleg
,
85 GSS_C_NO_CHANNEL_BINDINGS
,
87 NULL
, /* ignore mech type */
90 NULL
); /* ignore time_rec */
92 if (uxctx
->maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_S_COMPLETE
;
93 if (uxctx
->maj_stat
== GSS_S_CONTINUE_NEEDED
) return SSH_GSS_S_CONTINUE_NEEDED
;
94 return SSH_GSS_FAILURE
;
97 Ssh_gss_stat
ssh_gss_display_status(Ssh_gss_ctx ctx
, Ssh_gss_buf
*buf
)
99 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) ctx
;
102 gss_buffer_desc msg_maj
=GSS_C_EMPTY_BUFFER
;
103 gss_buffer_desc msg_min
=GSS_C_EMPTY_BUFFER
;
105 /* Return empty buffer in case of failure */
106 SSH_GSS_CLEAR_BUF(buf
);
108 /* get first mesg from GSS */
110 lmax
=gss_display_status(&lmin
,uxctx
->maj_stat
,GSS_C_GSS_CODE
,(gss_OID
) putty_gss_mech_krb5
,&ccc
,&msg_maj
);
112 if (lmax
!= GSS_S_COMPLETE
) return SSH_GSS_FAILURE
;
114 /* get first mesg from Kerberos */
116 lmax
=gss_display_status(&lmin
,uxctx
->min_stat
,GSS_C_MECH_CODE
,(gss_OID
) putty_gss_mech_krb5
,&ccc
,&msg_min
);
118 if (lmax
!= GSS_S_COMPLETE
) {
119 gss_release_buffer(&lmin
, &msg_maj
);
120 return SSH_GSS_FAILURE
;
123 /* copy data into buffer */
124 buf
->length
= msg_maj
.length
+ msg_min
.length
+ 1;
125 buf
->value
= snewn(buf
->length
+ 1, char);
128 memcpy((char *)buf
->value
, msg_maj
.value
, msg_maj
.length
);
129 ((char *)buf
->value
)[msg_maj
.length
] = ' ';
130 memcpy((char *)buf
->value
+ msg_maj
.length
+ 1, msg_min
.value
, msg_min
.length
);
131 ((char *)buf
->value
)[buf
->length
] = 0;
132 /* free mem & exit */
133 gss_release_buffer(&lmin
, &msg_maj
);
134 gss_release_buffer(&lmin
, &msg_min
);
138 Ssh_gss_stat
ssh_gss_free_tok(Ssh_gss_buf
*send_tok
)
140 OM_uint32 min_stat
,maj_stat
;
141 maj_stat
= gss_release_buffer(&min_stat
, send_tok
);
143 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
144 return SSH_GSS_FAILURE
;
147 Ssh_gss_stat
ssh_gss_release_cred(Ssh_gss_ctx
*ctx
)
149 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) *ctx
;
151 OM_uint32 maj_stat
=GSS_S_COMPLETE
;
153 if (uxctx
== NULL
) return SSH_GSS_FAILURE
;
154 if (uxctx
->ctx
!= GSS_C_NO_CONTEXT
)
155 maj_stat
= gss_delete_sec_context(&min_stat
,&uxctx
->ctx
,GSS_C_NO_BUFFER
);
158 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
159 return SSH_GSS_FAILURE
;
163 Ssh_gss_stat
ssh_gss_release_name(Ssh_gss_name
*srv_name
)
165 OM_uint32 min_stat
,maj_stat
;
166 maj_stat
= gss_release_name(&min_stat
, srv_name
);
168 if (maj_stat
== GSS_S_COMPLETE
) return SSH_GSS_OK
;
169 return SSH_GSS_FAILURE
;
172 Ssh_gss_stat
ssh_gss_get_mic(Ssh_gss_ctx ctx
, Ssh_gss_buf
*buf
,
175 uxSsh_gss_ctx
*uxctx
= (uxSsh_gss_ctx
*) ctx
;
176 if (uxctx
== NULL
) return SSH_GSS_FAILURE
;
177 return gss_get_mic(&(uxctx
->min_stat
), uxctx
->ctx
, 0, buf
, hash
);
180 Ssh_gss_stat
ssh_gss_free_mic(Ssh_gss_buf
*hash
)
182 /* On Unix this is the same freeing process as ssh_gss_free_tok. */
183 return ssh_gss_free_tok(hash
);
188 /* Dummy function so this source file defines something if NO_GSSAPI
191 int ssh_gss_init(void)