*** empty log message ***
[arla.git] / rxgk / rxgk_ticket.c
blob299dea5cf5df248992a4c63fd641560dd32ccd4a
1 /*
2 * Copyright (c) 1995 - 1998, 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "rxgk_locl.h"
36 RCSID("$Id$");
38 #include <errno.h>
40 #include "rxgk_proto.h"
42 int
43 rxgk_encrypt_ticket(struct rxgk_ticket *ticket, RXGK_Ticket_Crypt *opaque)
45 int ret;
46 char *buf;
47 size_t len;
48 RXGK_Token clear, crypt;
49 struct rxgk_keyblock key;
51 len = RXGK_TICKET_MAX_SIZE;
52 buf = malloc(RXGK_TICKET_MAX_SIZE);
54 if (ydr_encode_rxgk_ticket(ticket, buf, &len) == NULL) {
55 return errno;
58 clear.val = buf;
59 clear.len = RXGK_TICKET_MAX_SIZE - len;
61 #if DEBUG
62 fprintf(stderr, "before encrypt:");
63 print_chararray(clear.val, clear.len);
64 #endif
66 ret = rxgk_get_server_ticket_key(&key);
67 if (ret) {
68 free(buf);
69 return EINVAL;
72 ret = rxgk_encrypt_buffer(&clear, &crypt, &key, RXGK_SERVER_ENC_TICKET);
73 if (ret) {
74 free(key.data);
75 free(buf);
76 return EINVAL;
79 opaque->val = crypt.val;
80 opaque->len = crypt.len;
82 #if DEBUG
83 fprintf(stderr, "after encrypt:");
84 print_chararray(crypt.val, crypt.len);
85 #endif
87 return 0;
90 int
91 rxgk_decrypt_ticket(RXGK_Ticket_Crypt *opaque, struct rxgk_ticket *ticket)
93 size_t len;
94 int ret;
95 RXGK_Token clear, crypt;
96 struct rxgk_keyblock key;
98 ret = rxgk_get_server_ticket_key(&key);
99 if (ret) {
100 return EINVAL;
103 crypt.val = opaque->val;
104 crypt.len = opaque->len;
106 #if DEBUG
107 fprintf(stderr, "before decrypt:");
108 print_chararray(crypt.val, crypt.len);
109 #endif
111 ret = rxgk_decrypt_buffer(&crypt, &clear, &key, RXGK_SERVER_ENC_TICKET);
112 if (ret) {
113 free(key.data);
114 return EINVAL;
117 #if DEBUG
118 fprintf(stderr, "after decrypt:");
119 print_chararray(clear.val, clear.len);
120 #endif
122 len = clear.len;
123 if (ydr_decode_rxgk_ticket(ticket, clear.val, &len) == NULL) {
124 return errno;
127 return 0;
131 rxgk_get_server_ticket_key(struct rxgk_keyblock *key)
133 int i;
134 /* XXX get real key */
136 key->length = 32;
137 key->enctype = RXGK_CRYPTO_AES256_CTS_HMAC_SHA1_96;
139 key->data = malloc(key->length);
140 if (key->data == NULL)
141 return ENOMEM;
143 for (i = 0; i < key->length; i++) {
144 ((unsigned char *)key->data)[i] = 0x23 + i * 47;
147 return 0;