crypt_client.c revision 278039
126219Swpaul/*
226219Swpaul * Copyright (c) 1996
326219Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
426219Swpaul *
526219Swpaul * Redistribution and use in source and binary forms, with or without
626219Swpaul * modification, are permitted provided that the following conditions
726219Swpaul * are met:
826219Swpaul * 1. Redistributions of source code must retain the above copyright
926219Swpaul *    notice, this list of conditions and the following disclaimer.
1026219Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1126219Swpaul *    notice, this list of conditions and the following disclaimer in the
1226219Swpaul *    documentation and/or other materials provided with the distribution.
1326219Swpaul * 3. All advertising materials mentioning features or use of this software
1426219Swpaul *    must display the following acknowledgement:
1526219Swpaul *	This product includes software developed by Bill Paul.
1626219Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1726219Swpaul *    may be used to endorse or promote products derived from this software
1826219Swpaul *    without specific prior written permission.
1926219Swpaul *
2026219Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2126219Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2226219Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2326219Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2426219Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2526219Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2626219Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2726219Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2826219Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2926219Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3026219Swpaul * SUCH DAMAGE.
3126219Swpaul */
3226219Swpaul
3392990Sobrien#include <sys/cdefs.h>
3492990Sobrien__FBSDID("$FreeBSD: head/lib/libc/rpc/crypt_client.c 278039 2015-02-01 23:19:06Z pfg $");
3592990Sobrien
3674462Salfred#include "namespace.h"
37111010Snectar#include <err.h>
3826219Swpaul#include <sys/types.h>
3926219Swpaul#include <rpc/des_crypt.h>
4026219Swpaul#include <rpc/des.h>
4126219Swpaul#include <string.h>
4226219Swpaul#include <rpcsvc/crypt.h>
4374462Salfred#include "un-namespace.h"
4426219Swpaul
4526219Swpaulint
4626219Swpaul_des_crypt_call(buf, len, dparms)
4726219Swpaul	char *buf;
4826219Swpaul	int len;
4926219Swpaul	struct desparams *dparms;
5026219Swpaul{
5126219Swpaul	CLIENT *clnt;
5226219Swpaul	desresp  *result_1;
5326219Swpaul	desargs  des_crypt_1_arg;
5490256Salfred	struct netconfig *nconf;
5590256Salfred	void *localhandle;
5690256Salfred	int stat;
5726219Swpaul
5890256Salfred	nconf = NULL;
5990256Salfred	localhandle = setnetconfig();
6090256Salfred	while ((nconf = getnetconfig(localhandle)) != NULL) {
6190256Salfred		if (nconf->nc_protofmly != NULL &&
6290256Salfred		     strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
6390256Salfred			break;
6490256Salfred	}
6590256Salfred	if (nconf == NULL) {
6690256Salfred		warnx("getnetconfig: %s", nc_sperror());
67278039Spfg		endnetconfig(localhandle);
6890256Salfred		return(DESERR_HWERROR);
6990256Salfred	}
7090256Salfred	clnt = clnt_tp_create(NULL, CRYPT_PROG, CRYPT_VERS, nconf);
7126219Swpaul	if (clnt == (CLIENT *) NULL) {
7290256Salfred		endnetconfig(localhandle);
7326219Swpaul		return(DESERR_HWERROR);
7426219Swpaul	}
7590256Salfred	endnetconfig(localhandle);
7626219Swpaul
7726219Swpaul	des_crypt_1_arg.desbuf.desbuf_len = len;
7826219Swpaul	des_crypt_1_arg.desbuf.desbuf_val = buf;
79228538Sdim	des_crypt_1_arg.des_dir = (dparms->des_dir == ENCRYPT) ? ENCRYPT_DES : DECRYPT_DES;
80228538Sdim	des_crypt_1_arg.des_mode = (dparms->des_mode == CBC) ? CBC_DES : ECB_DES;
8126219Swpaul	bcopy(dparms->des_ivec, des_crypt_1_arg.des_ivec, 8);
8226219Swpaul	bcopy(dparms->des_key, des_crypt_1_arg.des_key, 8);
8326219Swpaul
8426219Swpaul	result_1 = des_crypt_1(&des_crypt_1_arg, clnt);
8526219Swpaul	if (result_1 == (desresp *) NULL) {
8636816Swpaul		clnt_destroy(clnt);
8726219Swpaul		return(DESERR_HWERROR);
8826219Swpaul	}
8926219Swpaul
9026219Swpaul	stat = result_1->stat;
9126219Swpaul
9226219Swpaul	if (result_1->stat == DESERR_NONE ||
9326219Swpaul	    result_1->stat == DESERR_NOHWDEVICE) {
9426219Swpaul		bcopy(result_1->desbuf.desbuf_val, buf, len);
9526219Swpaul		bcopy(result_1->des_ivec, dparms->des_ivec, 8);
9626219Swpaul	}
9726219Swpaul
9895658Sdes	clnt_freeres(clnt, (xdrproc_t)xdr_desresp, result_1);
9926219Swpaul	clnt_destroy(clnt);
10026219Swpaul
10126219Swpaul	return(stat);
10226219Swpaul}
103