File: encrypt.c

package info (click to toggle)
kftgt 1.6-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 276 kB
  • ctags: 110
  • sloc: ansic: 901; perl: 298; sh: 166; makefile: 138
file content (100 lines) | stat: -rw-r--r-- 2,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***********************************************************************
 *
 * encrypt.c - routines to send and receive encrypted chunks using
 *             the krb_mk_priv and krb_rd_priv routines. A chunk
 *             consists of a network long "n" (the length) followed
 *             by "n" bytes of encrypted data.
 *
 * $Id: encrypt.c,v 1.2 2001/03/06 15:02:11 bbense Exp $
 *
 * $Log: encrypt.c,v $
 * Revision 1.2  2001/03/06 15:02:11  bbense
 * Ported to Solaris 2.7 and new krb5 k4 libraries.
 *
 * Revision 1.1  1994/06/08 17:21:41  schemers
 * Initial revision
 *
 *
 *----------------------------------------------------------------------
 * Copyright (c) 1994 Board of Trustees, Leland Stanford Jr. University
 ***********************************************************************/
#ifndef lint
static char _rcs_id[]="$Id: encrypt.c,v 1.2 2001/03/06 15:02:11 bbense Exp $";
#endif


#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <krb.h>
#include <errno.h>

#include <memory.h>
#include <stdlib.h>
#include <string.h>

#include "kftgt.h"

int 
send_encrypted_chunk(
         char *in,
         int   in_len,
         int   sock,
         des_cblock session,
         des_key_schedule sched,
         struct sockaddr_in *sender,
         struct sockaddr_in *receiver)
{
      char buffer[KFTGT_MAX_BUFFER*2];
      int elen;
      KRB_UINT32 nlen;

      elen = krb_mk_priv(in, buffer, in_len, sched, session, sender, receiver);

      if (elen >KFTGT_MAX_BUFFER) return -1;

      nlen = htonl(elen);

      if (
              (krb_net_write(sock,(char *)&nlen,sizeof(nlen)) != sizeof(nlen))
          ||  (krb_net_write(sock,buffer,elen) != elen)
             ) {
                return -1;
      }
      return elen;
}

int 
receive_encrypted_chunk(
         MSG_DAT *m_data,
         char *buffer,
         int   buffer_max,
         int   sock,
         des_cblock session,
         des_key_schedule sched,
         struct sockaddr_in *sender,
         struct sockaddr_in *receiver)

{
     KRB_UINT32 nlen,elen;
     int status;

     if (krb_net_read(sock,(char *)&nlen, sizeof(nlen)) != sizeof(nlen)) {
         return -1;
     }
     elen=ntohl(nlen);

     if (elen> buffer_max)  return -1;

     if ( krb_net_read(sock, buffer, elen) != elen) return -1;

     /* decrypt */
     
     status = krb_rd_priv(buffer,elen,sched, session,sender,receiver,m_data);

     if (status != KSUCCESS) return -1;
     else return m_data->app_length;
 
}