xref: /illumos-gate/usr/src/lib/libslp/clib/slp_auth.c (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file contains all authentication-related functionality for
297c478bd9Sstevel@tonic-gate  * SLP. Two interfaces are exported:
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  *  slp_sign:		Creates auth blocks for a given piece of data
327c478bd9Sstevel@tonic-gate  *  slp_verify:		Verifies an auth block for a given piece of data.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * A shared object which provides crypto-suites and key management
357c478bd9Sstevel@tonic-gate  * functionality is dynamically linked in during intialization. If
367c478bd9Sstevel@tonic-gate  * the shared object cannot be found, the authentication code aborts
377c478bd9Sstevel@tonic-gate  * and an SLP_AUTHENTICATION_FAILED error is returned. Which shared
387c478bd9Sstevel@tonic-gate  * object is actually loaded is controlled by the property
397c478bd9Sstevel@tonic-gate  * sun.net.slp.authBackend; the value of this property should contain
407c478bd9Sstevel@tonic-gate  * either the name of a shared object which implements the necessary
417c478bd9Sstevel@tonic-gate  * interfaces, or a full or relative path to such an object. This value
42*bbf21555SRichard Lowe  * will be passed to dlopen(3C) to resolve the symbols.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The shared object must implement the following AMI interfaces:
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *  ami_init
477c478bd9Sstevel@tonic-gate  *  ami_sign
487c478bd9Sstevel@tonic-gate  *  ami_verify
497c478bd9Sstevel@tonic-gate  *  ami_get_cert
507c478bd9Sstevel@tonic-gate  *  ami_get_cert_chain
517c478bd9Sstevel@tonic-gate  *  ami_strerror
527c478bd9Sstevel@tonic-gate  *  ami_end
537c478bd9Sstevel@tonic-gate  *  AMI_MD5WithRSAEncryption_AID
547c478bd9Sstevel@tonic-gate  *  AMI_SHA1WithDSASignature_AID
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * See security/ami.h for more info on these interfaces.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <stdio.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <syslog.h>
637c478bd9Sstevel@tonic-gate #include <synch.h>
647c478bd9Sstevel@tonic-gate #include <dlfcn.h>
657c478bd9Sstevel@tonic-gate #include <slp-internal.h>
667c478bd9Sstevel@tonic-gate #include "slp_ami.h"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* Prototypes for dynamically loaded (dl'd) AMI functions */
697c478bd9Sstevel@tonic-gate static ami_algid **ami_rsa_aid, **ami_dsa_aid;
707c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_init)(ami_handle_t **, const char *,
717c478bd9Sstevel@tonic-gate 				    const char *, const u_int, const u_int,
727c478bd9Sstevel@tonic-gate 				    const char *);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_sign)(ami_handle_t *,
757c478bd9Sstevel@tonic-gate 				    const uchar_t *,
767c478bd9Sstevel@tonic-gate 				    const size_t,
777c478bd9Sstevel@tonic-gate 				    const int,
787c478bd9Sstevel@tonic-gate 				    const ami_algid *,
797c478bd9Sstevel@tonic-gate 				    const uchar_t *,
807c478bd9Sstevel@tonic-gate 				    const size_t,
817c478bd9Sstevel@tonic-gate 				    const ami_algid *,
827c478bd9Sstevel@tonic-gate 				    uchar_t **,
837c478bd9Sstevel@tonic-gate 				    size_t *);
847c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_verify)(ami_handle_t *,
857c478bd9Sstevel@tonic-gate 				    const uchar_t *,
867c478bd9Sstevel@tonic-gate 				    const size_t,
877c478bd9Sstevel@tonic-gate 				    const int,
887c478bd9Sstevel@tonic-gate 				    const ami_algid *,
897c478bd9Sstevel@tonic-gate 				    const uchar_t *,
907c478bd9Sstevel@tonic-gate 				    const size_t,
917c478bd9Sstevel@tonic-gate 				    const ami_algid *,
927c478bd9Sstevel@tonic-gate 				    const uchar_t *,
937c478bd9Sstevel@tonic-gate 				    const size_t);
947c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_get_cert)(const ami_handle_t *,
957c478bd9Sstevel@tonic-gate 				    const char *,
967c478bd9Sstevel@tonic-gate 				    ami_cert **,
977c478bd9Sstevel@tonic-gate 				    int *);
987c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_get_cert_chain)(const ami_handle_t *,
997c478bd9Sstevel@tonic-gate 					    const ami_cert *,
1007c478bd9Sstevel@tonic-gate 					    const char **,
1017c478bd9Sstevel@tonic-gate 					    int flags,
1027c478bd9Sstevel@tonic-gate 					    ami_cert **,
1037c478bd9Sstevel@tonic-gate 					    int *);
1047c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_str2dn)(const ami_handle_t *,
1057c478bd9Sstevel@tonic-gate 				    char *, ami_name **);
1067c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_dn2str)(const ami_handle_t *,
1077c478bd9Sstevel@tonic-gate 				    ami_name *, char **);
1087c478bd9Sstevel@tonic-gate static void (*dld_ami_free_cert_list)(ami_cert **, int);
1097c478bd9Sstevel@tonic-gate static void (*dld_ami_free_dn)(ami_name **);
1107c478bd9Sstevel@tonic-gate static char *(*dld_ami_strerror)(const ami_handle_t *, const AMI_STATUS);
1117c478bd9Sstevel@tonic-gate static AMI_STATUS (*dld_ami_end)(ami_handle_t *);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /* local utilities */
1147c478bd9Sstevel@tonic-gate static SLPError get_security_backend();
1157c478bd9Sstevel@tonic-gate static SLPError make_tbs(const char *, struct iovec *, int,
1167c478bd9Sstevel@tonic-gate 			    unsigned int, unsigned char **, size_t *);
1177c478bd9Sstevel@tonic-gate static SLPError make_authblock(struct iovec *, int, const char *,
1187c478bd9Sstevel@tonic-gate 				time_t, caddr_t *, size_t *);
1197c478bd9Sstevel@tonic-gate static SLPError do_verify(unsigned char *, size_t, unsigned short,
1207c478bd9Sstevel@tonic-gate 				const unsigned char *, size_t, const char *);
1217c478bd9Sstevel@tonic-gate static char *alias2dn(ami_handle_t *);
1227c478bd9Sstevel@tonic-gate static SLPError check_spis(ami_handle_t *, ami_cert *, int, const char *);
1237c478bd9Sstevel@tonic-gate static int dncmp(ami_handle_t *, const char *, const char *);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * Creates a cryptographic signature over the components of authiov, and
1277c478bd9Sstevel@tonic-gate  * creates an auth block from the signature. The auth block is placed
1287c478bd9Sstevel@tonic-gate  * into msgiov at the index specified by msgiov_index. The timestamp
1297c478bd9Sstevel@tonic-gate  * for the auth block is given in ts. Caller must free the auth block
1307c478bd9Sstevel@tonic-gate  * when finished.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * Returns SLP_OK on success, SLP_AUTHENTICATION_FAILED on failure.
1337c478bd9Sstevel@tonic-gate  */
slp_sign(struct iovec * authiov,int authiov_len,time_t ts,struct iovec * msgiov,int msg_index)1347c478bd9Sstevel@tonic-gate SLPError slp_sign(struct iovec *authiov, int authiov_len, time_t ts,
1357c478bd9Sstevel@tonic-gate 		    struct iovec *msgiov, int msg_index) {
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	char *sign_as = NULL;
1387c478bd9Sstevel@tonic-gate 	char *alias, *aliasp;
1397c478bd9Sstevel@tonic-gate 	SLPError err = SLP_OK;
1407c478bd9Sstevel@tonic-gate 	unsigned char num_auths = 0;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* This auth block is always at least 1 byte long, for num auths */
1437c478bd9Sstevel@tonic-gate 	msgiov[msg_index].iov_base = calloc(1, 1);
1447c478bd9Sstevel@tonic-gate 	msgiov[msg_index].iov_len = 1;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* if security is off, just return the empty auth block */
1477c478bd9Sstevel@tonic-gate 	if (!slp_get_security_on() || slp_get_bypass_auth()) {
1487c478bd9Sstevel@tonic-gate 	    return (SLP_OK);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Security is disabled in Solaris 8 due to AMI trouble.
1537c478bd9Sstevel@tonic-gate 	 * The pragmas and LINTED suppress "statement not reached"
1547c478bd9Sstevel@tonic-gate 	 * compiler and lint warnings, and should be removed when
1557c478bd9Sstevel@tonic-gate 	 * security is re-enabled.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	return (SLP_SECURITY_UNAVAILABLE);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #pragma	error_messages(off, E_STATEMENT_NOT_REACHED)
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/* else we should sign this advert */
1627c478bd9Sstevel@tonic-gate 	if (!(sign_as = (char *)SLPGetProperty(SLP_CONFIG_SIGN_AS)) ||
1637c478bd9Sstevel@tonic-gate /*LINTED statement not reached*/
1647c478bd9Sstevel@tonic-gate 		!*sign_as) {
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "slp_sign", "No signing identity given");
1677c478bd9Sstevel@tonic-gate 	    return (SLP_AUTHENTICATION_FAILED);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Try to initialize security backend */
1717c478bd9Sstevel@tonic-gate 	if (!(err = get_security_backend()) == SLP_OK) {
1727c478bd9Sstevel@tonic-gate 	    return (SLP_AUTHENTICATION_FAILED);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* dup SPI list so we can destructively modify it */
1767c478bd9Sstevel@tonic-gate 	if (!(sign_as = strdup(sign_as))) {
1777c478bd9Sstevel@tonic-gate 	    slp_err(LOG_CRIT, 0, "slp_sign", "out of memory");
1787c478bd9Sstevel@tonic-gate 	    return (SLP_MEMORY_ALLOC_FAILED);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* For each SPI, create an auth block */
1827c478bd9Sstevel@tonic-gate 	for (aliasp = sign_as; aliasp; ) {
1837c478bd9Sstevel@tonic-gate 	    alias = aliasp;
1847c478bd9Sstevel@tonic-gate 	    aliasp = slp_utf_strchr(aliasp, ',');
1857c478bd9Sstevel@tonic-gate 	    if (aliasp) {
1867c478bd9Sstevel@tonic-gate 		*aliasp++ = 0;
1877c478bd9Sstevel@tonic-gate 	    }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	    /* create an auth block for this SPI */
1907c478bd9Sstevel@tonic-gate 	    err = make_authblock(authiov, authiov_len, alias, ts,
1917c478bd9Sstevel@tonic-gate 				    &(msgiov[msg_index].iov_base),
1927c478bd9Sstevel@tonic-gate 				    (size_t *)&(msgiov[msg_index].iov_len));
1937c478bd9Sstevel@tonic-gate 	    if (err == SLP_MEMORY_ALLOC_FAILED) {
1947c478bd9Sstevel@tonic-gate 		goto done;
1957c478bd9Sstevel@tonic-gate 	    } else if (err != SLP_OK) {
1967c478bd9Sstevel@tonic-gate 		/* else skip and keep going */
1977c478bd9Sstevel@tonic-gate 		continue;
1987c478bd9Sstevel@tonic-gate 	    }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	    num_auths++;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate done:
2047c478bd9Sstevel@tonic-gate 	if (sign_as) free(sign_as);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (err != SLP_OK) {
2077c478bd9Sstevel@tonic-gate 	    return (err);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (num_auths == 0) {
2117c478bd9Sstevel@tonic-gate 	    return (SLP_AUTHENTICATION_FAILED);
2127c478bd9Sstevel@tonic-gate 	} else {
2137c478bd9Sstevel@tonic-gate 	    size_t off = 0;
2147c478bd9Sstevel@tonic-gate 	    /* Lay in number of auth blocks created */
2157c478bd9Sstevel@tonic-gate 	    err = slp_add_byte(msgiov[msg_index].iov_base, 1, num_auths, &off);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (err);
2197c478bd9Sstevel@tonic-gate #pragma	error_messages(on, E_STATEMENT_NOT_REACHED)
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Verifies that the signature(s) contained in authblocks validates
2247c478bd9Sstevel@tonic-gate  * the data in authiov. slp_verify will not read more than len bytes
2257c478bd9Sstevel@tonic-gate  * from authblocks. n is the stated number of authblocks in authblock.
2267c478bd9Sstevel@tonic-gate  * The total length of all auth blocks read is placed in *total.
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * Returns SLP_OK if the verification succeeds.
2297c478bd9Sstevel@tonic-gate  */
slp_verify(struct iovec * authiov,int authiov_len,const char * authblocks,size_t len,int n,size_t * total)2307c478bd9Sstevel@tonic-gate SLPError slp_verify(struct iovec *authiov, int authiov_len,
2317c478bd9Sstevel@tonic-gate 		    const char *authblocks, size_t len, int n, size_t *total) {
2327c478bd9Sstevel@tonic-gate 	int i;
2337c478bd9Sstevel@tonic-gate 	size_t off, this_ab;
2347c478bd9Sstevel@tonic-gate 	unsigned short bsd, ablen;
2357c478bd9Sstevel@tonic-gate 	unsigned int timestamp;
2367c478bd9Sstevel@tonic-gate 	char *spi = NULL;
2377c478bd9Sstevel@tonic-gate 	SLPError err = SLP_AUTHENTICATION_FAILED;
2387c478bd9Sstevel@tonic-gate 	unsigned char *inbytes = NULL;
2397c478bd9Sstevel@tonic-gate 	size_t inbytes_len;
2407c478bd9Sstevel@tonic-gate 	unsigned char *sig;
2417c478bd9Sstevel@tonic-gate 	size_t siglen;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/* 1st: if bypass_auth == true, just return SLP_OK */
2447c478bd9Sstevel@tonic-gate 	if (slp_get_bypass_auth()) {
2457c478bd9Sstevel@tonic-gate 	    return (SLP_OK);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* 2nd: If security is off, and there are no auth blocks, OK */
2497c478bd9Sstevel@tonic-gate 	if (!slp_get_security_on() && n == 0) {
2507c478bd9Sstevel@tonic-gate 	    return (SLP_OK);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * Security is disabled in Solaris 8 due to AMI trouble.
2557c478bd9Sstevel@tonic-gate 	 * The pragmas and LINTED suppress "statement not reached"
2567c478bd9Sstevel@tonic-gate 	 * compiler and lint warnings, and should be removed when
2577c478bd9Sstevel@tonic-gate 	 * security is re-enabled.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	return (SLP_SECURITY_UNAVAILABLE);
2607c478bd9Sstevel@tonic-gate #pragma	error_messages(off, E_STATEMENT_NOT_REACHED)
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/* For all other scenarios, we must verify the auth blocks */
2637c478bd9Sstevel@tonic-gate /*LINTED statement not reached*/
2647c478bd9Sstevel@tonic-gate 	if (get_security_backend() != SLP_OK || n == 0) {
2657c478bd9Sstevel@tonic-gate 	    return (SLP_AUTHENTICATION_FAILED);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * If we get here, the backend is available and there are auth
2707c478bd9Sstevel@tonic-gate 	 * blocks to verify. Verify each input auth block.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	off = 0;	/* offset into raw auth blocks */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	for (i = 0; i < n && off <= len; i++) {
2757c478bd9Sstevel@tonic-gate 	    this_ab = off;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	    /* BSD */
2787c478bd9Sstevel@tonic-gate 	    if ((err = slp_get_sht(authblocks, len, &off, &bsd)) != SLP_OK) {
2797c478bd9Sstevel@tonic-gate 		slp_err(LOG_INFO, 0, "slp_verify", "corrupt auth block");
2807c478bd9Sstevel@tonic-gate 		goto done;
2817c478bd9Sstevel@tonic-gate 	    }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	    /* Auth block length */
2847c478bd9Sstevel@tonic-gate 	    if ((err = slp_get_sht(authblocks, len, &off, &ablen)) != SLP_OK) {
2857c478bd9Sstevel@tonic-gate 		slp_err(LOG_INFO, 0, "slp_verify", "corrupt auth block");
2867c478bd9Sstevel@tonic-gate 		goto done;
2877c478bd9Sstevel@tonic-gate 	    }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	    /* Time stamp */
2907c478bd9Sstevel@tonic-gate 	    if ((err = slp_get_int32(authblocks, len, &off, &timestamp))
2917c478bd9Sstevel@tonic-gate 		!= SLP_OK) {
2927c478bd9Sstevel@tonic-gate 		slp_err(LOG_INFO, 0, "slp_verify", "corrupt auth block");
2937c478bd9Sstevel@tonic-gate 		goto done;
2947c478bd9Sstevel@tonic-gate 	    }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	    /* SPI string */
2977c478bd9Sstevel@tonic-gate 	    if ((err = slp_get_string(authblocks, len, &off, &spi))
2987c478bd9Sstevel@tonic-gate 		!= SLP_OK) {
2997c478bd9Sstevel@tonic-gate 		slp_err(LOG_INFO, 0, "slp_verify", "corrupt auth block");
3007c478bd9Sstevel@tonic-gate 		goto done;
3017c478bd9Sstevel@tonic-gate 	    }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	    err = make_tbs(
3047c478bd9Sstevel@tonic-gate 		spi, authiov, authiov_len, timestamp, &inbytes, &inbytes_len);
3057c478bd9Sstevel@tonic-gate 	    if (err != SLP_OK) {
3067c478bd9Sstevel@tonic-gate 		goto done;
3077c478bd9Sstevel@tonic-gate 	    }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	    sig = (unsigned char *)(authblocks + off);
3107c478bd9Sstevel@tonic-gate 	    siglen = ablen - (off - this_ab);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	    off += siglen;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	    err =  do_verify(inbytes, inbytes_len, bsd, sig, siglen, spi);
3157c478bd9Sstevel@tonic-gate 	    if (err != SLP_OK) {
3167c478bd9Sstevel@tonic-gate 		free(spi);
3177c478bd9Sstevel@tonic-gate 		goto done;
3187c478bd9Sstevel@tonic-gate 	    }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	    free(spi);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate done:
3247c478bd9Sstevel@tonic-gate 	if (inbytes) free(inbytes);
3257c478bd9Sstevel@tonic-gate 	*total = off;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return (err);
3287c478bd9Sstevel@tonic-gate #pragma	error_messages(on, E_STATEMENT_NOT_REACHED)
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * When first called, attempts to dlopen a security shared library
3337c478bd9Sstevel@tonic-gate  * and dlsym in the necessary interfaces. The library remains mapped
3347c478bd9Sstevel@tonic-gate  * in, so successive calls just return SLP_OK.
3357c478bd9Sstevel@tonic-gate  */
get_security_backend()3367c478bd9Sstevel@tonic-gate static SLPError get_security_backend() {
3377c478bd9Sstevel@tonic-gate 	static mutex_t be_lock = DEFAULTMUTEX;
3387c478bd9Sstevel@tonic-gate 	static void *dl = NULL;
3397c478bd9Sstevel@tonic-gate 	static int got_backend = 0;
3407c478bd9Sstevel@tonic-gate 	SLPError err = SLP_SECURITY_UNAVAILABLE;
3417c478bd9Sstevel@tonic-gate 	const char *libname;
3427c478bd9Sstevel@tonic-gate 	char *dlerr;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&be_lock);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (got_backend) {
3477c478bd9Sstevel@tonic-gate 	    (void) mutex_unlock(&be_lock);
3487c478bd9Sstevel@tonic-gate 	    return (SLP_OK);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (!(libname = SLPGetProperty(SLP_CONFIG_AUTH_BACKEND)) ||
3527c478bd9Sstevel@tonic-gate 	    !*libname) {
3537c478bd9Sstevel@tonic-gate 	    /* revert to default */
3547c478bd9Sstevel@tonic-gate 	    libname = "libami.so.1";
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (!(dl = dlopen(libname, RTLD_LAZY))) {
3587c478bd9Sstevel@tonic-gate 	    dlerr = dlerror();
3597c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
3607c478bd9Sstevel@tonic-gate 				"Could not dlopen AMI library: %s",
3617c478bd9Sstevel@tonic-gate 				(dlerr ? dlerr : "unknown DL error"));
3627c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
3637c478bd9Sstevel@tonic-gate 				"Is AMI installed?");
3647c478bd9Sstevel@tonic-gate 	    goto done;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/* Relocate AMI's statically initialized AIDs we need */
3687c478bd9Sstevel@tonic-gate 	if (!(ami_rsa_aid =
3697c478bd9Sstevel@tonic-gate 		dlsym(dl, "AMI_MD5WithRSAEncryption_AID"))) {
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	    dlerr = dlerror();
3727c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
3737c478bd9Sstevel@tonic-gate 		    "Could not relocate AMI_MD5WithRSAEncryption_AID: %s",
3747c478bd9Sstevel@tonic-gate 				(dlerr ? dlerr : "unknown DL error"));
3757c478bd9Sstevel@tonic-gate 	    goto done;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (!(ami_dsa_aid =
3797c478bd9Sstevel@tonic-gate 		dlsym(dl, "AMI_SHA1WithDSASignature_AID"))) {
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	    dlerr = dlerror();
3827c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
3837c478bd9Sstevel@tonic-gate 		    "Could not relocate AMI_SHA1WithDSASignature_AID: %s",
3847c478bd9Sstevel@tonic-gate 				(dlerr ? dlerr : "unknown DL error"));
3857c478bd9Sstevel@tonic-gate 	    goto done;
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/* Bring in the functions we need */
3897c478bd9Sstevel@tonic-gate 	if (!(dld_ami_init = (AMI_STATUS (*)(ami_handle_t **,
3907c478bd9Sstevel@tonic-gate 					    const char *,
3917c478bd9Sstevel@tonic-gate 					    const char *,
3927c478bd9Sstevel@tonic-gate 					    const u_int,
3937c478bd9Sstevel@tonic-gate 					    const u_int,
3947c478bd9Sstevel@tonic-gate 					    const char *))dlsym(
3957c478bd9Sstevel@tonic-gate 						    dl, "ami_init"))) {
3967c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
3977c478bd9Sstevel@tonic-gate 		    "Could not load ami_init");
3987c478bd9Sstevel@tonic-gate 	    goto done;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (!(dld_ami_sign = (AMI_STATUS (*)(ami_handle_t *,
4027c478bd9Sstevel@tonic-gate 						const uchar_t *,
4037c478bd9Sstevel@tonic-gate 						const size_t,
4047c478bd9Sstevel@tonic-gate 						const int,
4057c478bd9Sstevel@tonic-gate 						const ami_algid *,
4067c478bd9Sstevel@tonic-gate 						const uchar_t *,
4077c478bd9Sstevel@tonic-gate 						const size_t,
4087c478bd9Sstevel@tonic-gate 						const ami_algid *,
4097c478bd9Sstevel@tonic-gate 						uchar_t **,
4107c478bd9Sstevel@tonic-gate 						size_t *))dlsym(
4117c478bd9Sstevel@tonic-gate 							dl, "ami_sign"))) {
4127c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4137c478bd9Sstevel@tonic-gate 		    "Could not load ami_sign");
4147c478bd9Sstevel@tonic-gate 	    goto done;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (!(dld_ami_verify = (AMI_STATUS (*)(ami_handle_t *,
4187c478bd9Sstevel@tonic-gate 						const uchar_t *,
4197c478bd9Sstevel@tonic-gate 						const size_t,
4207c478bd9Sstevel@tonic-gate 						const int,
4217c478bd9Sstevel@tonic-gate 						const ami_algid *,
4227c478bd9Sstevel@tonic-gate 						const uchar_t *,
4237c478bd9Sstevel@tonic-gate 						const size_t,
4247c478bd9Sstevel@tonic-gate 						const ami_algid *,
4257c478bd9Sstevel@tonic-gate 						const uchar_t *,
4267c478bd9Sstevel@tonic-gate 						const size_t))dlsym(
4277c478bd9Sstevel@tonic-gate 							dl, "ami_verify"))) {
4287c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4297c478bd9Sstevel@tonic-gate 		    "Could not load ami_verify");
4307c478bd9Sstevel@tonic-gate 	    goto done;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (!(dld_ami_get_cert = (AMI_STATUS (*)(const ami_handle_t *,
4347c478bd9Sstevel@tonic-gate 						const char *,
4357c478bd9Sstevel@tonic-gate 						ami_cert **,
4367c478bd9Sstevel@tonic-gate 						int *))dlsym(
4377c478bd9Sstevel@tonic-gate 							dl, "ami_get_cert"))) {
4387c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4397c478bd9Sstevel@tonic-gate 		    "Could not load ami_get_cert");
4407c478bd9Sstevel@tonic-gate 	    goto done;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (!(dld_ami_get_cert_chain = (AMI_STATUS (*)(const ami_handle_t *,
4447c478bd9Sstevel@tonic-gate 					    const ami_cert *,
4457c478bd9Sstevel@tonic-gate 					    const char **,
4467c478bd9Sstevel@tonic-gate 					    int flags,
4477c478bd9Sstevel@tonic-gate 					    ami_cert **,
4487c478bd9Sstevel@tonic-gate 					    int *))dlsym(
4497c478bd9Sstevel@tonic-gate 						dl, "ami_get_cert_chain"))) {
4507c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4517c478bd9Sstevel@tonic-gate 		    "Could not load ami_get_cert_chain");
4527c478bd9Sstevel@tonic-gate 	    goto done;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (!(dld_ami_str2dn = (AMI_STATUS (*)(const ami_handle_t *,
4567c478bd9Sstevel@tonic-gate 						char *, ami_name **))dlsym(
4577c478bd9Sstevel@tonic-gate 							dl, "ami_str2dn"))) {
4587c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4597c478bd9Sstevel@tonic-gate 		    "Could not load ami_str2dn");
4607c478bd9Sstevel@tonic-gate 	    goto done;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (!(dld_ami_dn2str = (AMI_STATUS (*)(const ami_handle_t *,
4647c478bd9Sstevel@tonic-gate 						ami_name *, char **))dlsym(
4657c478bd9Sstevel@tonic-gate 							dl, "ami_dn2str"))) {
4667c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4677c478bd9Sstevel@tonic-gate 		    "Could not load ami_dn2str");
4687c478bd9Sstevel@tonic-gate 	    goto done;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (!(dld_ami_free_cert_list = (void (*)(ami_cert **, int))dlsym(
4727c478bd9Sstevel@tonic-gate 						dl, "ami_free_cert_list"))) {
4737c478bd9Sstevel@tonic-gate 		    slp_err(LOG_INFO, 0, "get_security_backend",
4747c478bd9Sstevel@tonic-gate 		    "Could not load ami_free_cert_list");
4757c478bd9Sstevel@tonic-gate 	    goto done;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (!(dld_ami_free_dn = (void (*)(ami_name **))dlsym(
4797c478bd9Sstevel@tonic-gate 							dl, "ami_free_dn"))) {
4807c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4817c478bd9Sstevel@tonic-gate 		    "Could not load ami_free_dn");
4827c478bd9Sstevel@tonic-gate 	    goto done;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (!(dld_ami_strerror = (char *(*)(const ami_handle_t *,
4867c478bd9Sstevel@tonic-gate 					    const AMI_STATUS))dlsym(
4877c478bd9Sstevel@tonic-gate 						dl, "ami_strerror"))) {
4887c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4897c478bd9Sstevel@tonic-gate 		    "Could not load ami_strerror");
4907c478bd9Sstevel@tonic-gate 	    goto done;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (!(dld_ami_end = (AMI_STATUS (*)(ami_handle_t *))dlsym(
4947c478bd9Sstevel@tonic-gate 							dl, "ami_end"))) {
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "get_security_backend",
4977c478bd9Sstevel@tonic-gate 		    "Could not load ami_end");
4987c478bd9Sstevel@tonic-gate 	    goto done;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	got_backend = 1;
5027c478bd9Sstevel@tonic-gate 	err = SLP_OK;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate done:
5057c478bd9Sstevel@tonic-gate 	if (!got_backend && dl) {
5067c478bd9Sstevel@tonic-gate 	    (void) dlclose(dl);
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&be_lock);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	return (err);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * Creates a bytes to-be-signed buffer suitable for input
5157c478bd9Sstevel@tonic-gate  * a signature algorithm.
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  * The only backend currently available is AMI, which does
5187c478bd9Sstevel@tonic-gate  * not support incremental updates for digesting. Hence we
5197c478bd9Sstevel@tonic-gate  * must copy all elements of the input iovec into one buffer.
5207c478bd9Sstevel@tonic-gate  *
5217c478bd9Sstevel@tonic-gate  * This function allocates a single buffer into *buf big enough
5227c478bd9Sstevel@tonic-gate  * to hold all necessary elements, sets *buflen to this length, and
5237c478bd9Sstevel@tonic-gate  * makes a bytes-to-be-signed buffer. Into this buffer is placed
5247c478bd9Sstevel@tonic-gate  * first the SPI string, then all elements of iov, and finally
5257c478bd9Sstevel@tonic-gate  * the timestamp. Caller must free *buf.
5267c478bd9Sstevel@tonic-gate  *
5277c478bd9Sstevel@tonic-gate  * Returns err != SLP_OK only on catastrophic error.
5287c478bd9Sstevel@tonic-gate  */
make_tbs(const char * spi,struct iovec * iov,int iovlen,unsigned int timestamp,unsigned char ** buf,size_t * buflen)5297c478bd9Sstevel@tonic-gate static SLPError make_tbs(const char *spi,
5307c478bd9Sstevel@tonic-gate 			    struct iovec *iov,
5317c478bd9Sstevel@tonic-gate 			    int iovlen,
5327c478bd9Sstevel@tonic-gate 			    unsigned int timestamp,
5337c478bd9Sstevel@tonic-gate 			    unsigned char **buf,
5347c478bd9Sstevel@tonic-gate 			    size_t *buflen) {
5357c478bd9Sstevel@tonic-gate 	int i;
5367c478bd9Sstevel@tonic-gate 	caddr_t p;
5377c478bd9Sstevel@tonic-gate 	size_t off;
5387c478bd9Sstevel@tonic-gate 	SLPError err;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	*buflen = 2 + strlen(spi);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	for (i = 0; i < iovlen; i++) {
5437c478bd9Sstevel@tonic-gate 	    *buflen += iov[i].iov_len;
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	*buflen += sizeof (timestamp);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (!(*buf = malloc(*buflen))) {
5497c478bd9Sstevel@tonic-gate 	    slp_err(LOG_CRIT, 0, "slp_sign", "out of memory");
5507c478bd9Sstevel@tonic-gate 	    return (SLP_MEMORY_ALLOC_FAILED);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/* @@@ ok to use caddr_t? */
5547c478bd9Sstevel@tonic-gate 	p = (caddr_t)*buf;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/* Lay in SPI string */
5577c478bd9Sstevel@tonic-gate 	off = 0;
5587c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(p, *buflen, spi, &off)) != SLP_OK) {
5597c478bd9Sstevel@tonic-gate 		return (err);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	p += off;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* Copy in elements of iov */
5657c478bd9Sstevel@tonic-gate 	for (i = 0; i < iovlen; i++) {
5667c478bd9Sstevel@tonic-gate 	    (void) memcpy(p, iov[i].iov_base, iov[i].iov_len);
5677c478bd9Sstevel@tonic-gate 	    p += iov[i].iov_len;
5687c478bd9Sstevel@tonic-gate 	    off += iov[i].iov_len;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/* Lay in timestamp */
5727c478bd9Sstevel@tonic-gate 	return (slp_add_int32((char *)*buf, *buflen, timestamp, &off));
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * Creates an auth block from the given parameters:
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  *   sig_in	IN	Data to be signed
5797c478bd9Sstevel@tonic-gate  *   sig_in_len	IN	Length of sig_in
5807c478bd9Sstevel@tonic-gate  *   alias	IN	signing alias for this auth block
5817c478bd9Sstevel@tonic-gate  *   timestamp	IN	Timestamp for this auth block
5827c478bd9Sstevel@tonic-gate  *   abs	IN/OUT	Buffer of accumulated auth blocks
5837c478bd9Sstevel@tonic-gate  *   abs_len	IN/OUT	Length of abs
5847c478bd9Sstevel@tonic-gate  *
5857c478bd9Sstevel@tonic-gate  * For each new auth block, abs is resized as necessary, and the
5867c478bd9Sstevel@tonic-gate  * new auth block is appended. abs_len is updated accordingly.
5877c478bd9Sstevel@tonic-gate  *
5887c478bd9Sstevel@tonic-gate  * Returns SLP_OK if the signing and auth block creation succeeded.
5897c478bd9Sstevel@tonic-gate  */
make_authblock(struct iovec * authiov,int authiov_len,const char * alias,time_t timestamp,caddr_t * abs,size_t * abs_len)5907c478bd9Sstevel@tonic-gate static SLPError make_authblock(struct iovec *authiov, int authiov_len,
5917c478bd9Sstevel@tonic-gate 				const char *alias, time_t timestamp,
5927c478bd9Sstevel@tonic-gate 				caddr_t *abs, size_t *abs_len) {
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	unsigned char *sig_out = NULL;
5957c478bd9Sstevel@tonic-gate 	size_t sig_out_len = 0;
5967c478bd9Sstevel@tonic-gate 	ami_handle_t *amih = NULL;
5977c478bd9Sstevel@tonic-gate 	AMI_STATUS ami_err;
5987c478bd9Sstevel@tonic-gate 	size_t off = 0;
5997c478bd9Sstevel@tonic-gate 	SLPError err = SLP_OK;
6007c478bd9Sstevel@tonic-gate 	caddr_t ab;
6017c478bd9Sstevel@tonic-gate 	size_t ab_len;
6027c478bd9Sstevel@tonic-gate 	unsigned short bsd;
6037c478bd9Sstevel@tonic-gate 	ami_algid *aid;
6047c478bd9Sstevel@tonic-gate 	char *dn = NULL;
6057c478bd9Sstevel@tonic-gate 	unsigned char *sig_in = NULL;
6067c478bd9Sstevel@tonic-gate 	size_t sig_in_len;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* Create the signature */
6097c478bd9Sstevel@tonic-gate 	if ((ami_err = dld_ami_init(&amih, alias, NULL, 0, 0, NULL))
6107c478bd9Sstevel@tonic-gate 	    != AMI_OK) {
6117c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "make_authblock", "ami_init failed: %s",
6127c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, ami_err));
6137c478bd9Sstevel@tonic-gate 	    return (SLP_AUTHENTICATION_FAILED);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/* determine our DN, to be used as the SPI */
6177c478bd9Sstevel@tonic-gate 	if (!(dn = alias2dn(amih))) {
6187c478bd9Sstevel@tonic-gate 	    err = SLP_AUTHENTICATION_FAILED;
6197c478bd9Sstevel@tonic-gate 	    goto done;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/* make bytes to-be-signed */
6237c478bd9Sstevel@tonic-gate 	err = make_tbs(
6247c478bd9Sstevel@tonic-gate 		dn, authiov, authiov_len, timestamp, &sig_in, &sig_in_len);
6257c478bd9Sstevel@tonic-gate 	if (err != SLP_OK) {
6267c478bd9Sstevel@tonic-gate 	    goto done;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/* @@@ determine the AID and BSD for this alias */
6307c478bd9Sstevel@tonic-gate 	bsd = 1;
6317c478bd9Sstevel@tonic-gate 	aid = *ami_rsa_aid;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if ((ami_err = dld_ami_sign(amih, sig_in, sig_in_len, AMI_END_DATA,
6347c478bd9Sstevel@tonic-gate 				NULL, NULL, 0, aid, &sig_out, &sig_out_len))
6357c478bd9Sstevel@tonic-gate 	    != AMI_OK) {
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		slp_err(LOG_INFO, 0, "make_authblock", "ami_sign failed: %s",
6387c478bd9Sstevel@tonic-gate 			dld_ami_strerror(amih, ami_err));
6397c478bd9Sstevel@tonic-gate 		err = SLP_AUTHENTICATION_FAILED;
6407c478bd9Sstevel@tonic-gate 		goto done;
6417c478bd9Sstevel@tonic-gate 	    }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* We can now calculate the length of the auth block */
6447c478bd9Sstevel@tonic-gate 	ab_len =
6457c478bd9Sstevel@tonic-gate 		2 +			/* BSD */
6467c478bd9Sstevel@tonic-gate 		2 +			/* length */
6477c478bd9Sstevel@tonic-gate 		4 +			/* timestamp */
6487c478bd9Sstevel@tonic-gate 		2 + strlen(dn) +	/* SPI string */
6497c478bd9Sstevel@tonic-gate 		sig_out_len;		/* the signature */
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* Grow buffer for already-created auth blocks, if necessary */
6527c478bd9Sstevel@tonic-gate 	if (*abs_len != 0) {
6537c478bd9Sstevel@tonic-gate 	    if (!(*abs = realloc(*abs, *abs_len + ab_len))) {
6547c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "make_authblock", "out of memory");
6557c478bd9Sstevel@tonic-gate 		err = SLP_MEMORY_ALLOC_FAILED;
6567c478bd9Sstevel@tonic-gate 		goto done;
6577c478bd9Sstevel@tonic-gate 	    }
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 	ab = *abs + *abs_len;
6607c478bd9Sstevel@tonic-gate 	*abs_len += ab_len;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/* BSD */
6637c478bd9Sstevel@tonic-gate 	err = slp_add_sht(ab, ab_len, bsd, &off);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/* Auth block length */
6667c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
6677c478bd9Sstevel@tonic-gate 	    err = slp_add_sht(ab, ab_len, ab_len, &off);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* timestamp */
6717c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
6727c478bd9Sstevel@tonic-gate 	    err = slp_add_int32(ab, ab_len, timestamp, &off);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	/* SPI string */
6767c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
6777c478bd9Sstevel@tonic-gate 	    err = slp_add_string(ab, ab_len, dn, &off);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/* Signature */
6817c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
6827c478bd9Sstevel@tonic-gate 	    (void) memcpy(ab + off, sig_out, sig_out_len);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate done:
6867c478bd9Sstevel@tonic-gate 	if (amih) {
6877c478bd9Sstevel@tonic-gate 	    dld_ami_end(amih);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	if (dn) free(dn);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (sig_in) free(sig_in);
6927c478bd9Sstevel@tonic-gate 	if (sig_out) free(sig_out);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (err == SLP_MEMORY_ALLOC_FAILED) {
6957c478bd9Sstevel@tonic-gate 	    /* critical error; abort */
6967c478bd9Sstevel@tonic-gate 	    free(*abs);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	return (err);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate  * The actual verification routine which interacts with the security
7047c478bd9Sstevel@tonic-gate  * backend to get a certificate for the given SPI and use that cert
7057c478bd9Sstevel@tonic-gate  * to verify the signature contained in the auth block.
7067c478bd9Sstevel@tonic-gate  *
7077c478bd9Sstevel@tonic-gate  * inbytes	IN	bytes to be verified
7087c478bd9Sstevel@tonic-gate  * inbytes_len	IN	length of inbytes
7097c478bd9Sstevel@tonic-gate  * bsd		IN	BSD for this signature
7107c478bd9Sstevel@tonic-gate  * sig		IN	the signature
7117c478bd9Sstevel@tonic-gate  * siglen	IN	length of sig
7127c478bd9Sstevel@tonic-gate  * spi		IN	SPI for this signature, not escaped
7137c478bd9Sstevel@tonic-gate  *
7147c478bd9Sstevel@tonic-gate  * Returns SLP_OK if the signature is verified, or SLP_AUTHENTICATION_FAILED
7157c478bd9Sstevel@tonic-gate  * if any error occured.
7167c478bd9Sstevel@tonic-gate  */
do_verify(unsigned char * inbytes,size_t inbytes_len,unsigned short bsd,const unsigned char * sig,size_t siglen,const char * esc_spi)7177c478bd9Sstevel@tonic-gate static SLPError do_verify(unsigned char *inbytes, size_t inbytes_len,
7187c478bd9Sstevel@tonic-gate 			    unsigned short bsd, const unsigned char *sig,
7197c478bd9Sstevel@tonic-gate 			    size_t siglen, const char *esc_spi) {
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	AMI_STATUS ami_err;
7227c478bd9Sstevel@tonic-gate 	ami_handle_t *amih = NULL;
7237c478bd9Sstevel@tonic-gate 	SLPError err;
7247c478bd9Sstevel@tonic-gate 	ami_cert *certs = NULL;
7257c478bd9Sstevel@tonic-gate 	int icert, ccnt;
7267c478bd9Sstevel@tonic-gate 	ami_algid *aid;
7277c478bd9Sstevel@tonic-gate 	char *spi = NULL;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	/* Get the right AID */
7307c478bd9Sstevel@tonic-gate 	switch (bsd) {
7317c478bd9Sstevel@tonic-gate 	case 1:
7327c478bd9Sstevel@tonic-gate 		aid = *ami_rsa_aid;
7337c478bd9Sstevel@tonic-gate 		break;
7347c478bd9Sstevel@tonic-gate 	case 2:
7357c478bd9Sstevel@tonic-gate 		aid = *ami_dsa_aid;
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 	default:
7387c478bd9Sstevel@tonic-gate 		slp_err(LOG_INFO, 0, "do_verify",
7397c478bd9Sstevel@tonic-gate 			"Unsupported BSD %d for given SPI %s", bsd, spi);
7407c478bd9Sstevel@tonic-gate 		return (SLP_AUTHENTICATION_FAILED);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if ((ami_err = dld_ami_init(&amih, spi, NULL, 0, 0, NULL)) != AMI_OK) {
7447c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "do_verify", "ami_init failed: %s",
7457c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, ami_err));
7467c478bd9Sstevel@tonic-gate 	    return (SLP_AUTHENTICATION_FAILED);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/* unescape SPI */
7507c478bd9Sstevel@tonic-gate 	if ((err = SLPUnescape(esc_spi, &spi, SLP_FALSE))) {
7517c478bd9Sstevel@tonic-gate 	    goto done;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/* get certificate */
7557c478bd9Sstevel@tonic-gate 	if ((ami_err = dld_ami_get_cert(amih, spi, &certs, &ccnt)) != AMI_OK) {
7567c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "do_verify",
7577c478bd9Sstevel@tonic-gate 		    "Can not get certificate for %s: %s",
7587c478bd9Sstevel@tonic-gate 		    spi, dld_ami_strerror(amih, ami_err));
7597c478bd9Sstevel@tonic-gate 	    err = SLP_AUTHENTICATION_FAILED;
7607c478bd9Sstevel@tonic-gate 	    goto done;
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/* @@@ select the right cert, if more than one */
7647c478bd9Sstevel@tonic-gate 	icert = 0;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if ((ami_err = dld_ami_verify(amih, inbytes, inbytes_len, AMI_END_DATA,
7677c478bd9Sstevel@tonic-gate 				certs[icert].info.pubKeyInfo->algorithm,
7687c478bd9Sstevel@tonic-gate 				certs[icert].info.pubKeyInfo->pubKey.value,
7697c478bd9Sstevel@tonic-gate 				certs[icert].info.pubKeyInfo->pubKey.length,
7707c478bd9Sstevel@tonic-gate 				aid, sig, siglen)) != AMI_OK) {
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "do_verify", "ami_verify failed: %s",
7737c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, ami_err));
7747c478bd9Sstevel@tonic-gate 	    err = SLP_AUTHENTICATION_FAILED;
7757c478bd9Sstevel@tonic-gate 	    goto done;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	err = check_spis(amih, certs, icert, spi);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate done:
7817c478bd9Sstevel@tonic-gate 	if (certs) {
7827c478bd9Sstevel@tonic-gate 	    dld_ami_free_cert_list(&certs, ccnt);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	if (amih) {
7867c478bd9Sstevel@tonic-gate 	    dld_ami_end(amih);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (spi) free(spi);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	return (err);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate /*
7957c478bd9Sstevel@tonic-gate  * Gets this process' DN, or returns NULL on failure. Caller must free
7967c478bd9Sstevel@tonic-gate  * the result. The reslting DN will be escaped.
7977c478bd9Sstevel@tonic-gate  */
alias2dn(ami_handle_t * amih)7987c478bd9Sstevel@tonic-gate static char *alias2dn(ami_handle_t *amih) {
7997c478bd9Sstevel@tonic-gate 	ami_cert *certs;
8007c478bd9Sstevel@tonic-gate 	int ccnt;
8017c478bd9Sstevel@tonic-gate 	AMI_STATUS status;
8027c478bd9Sstevel@tonic-gate 	char *answer = NULL;
8037c478bd9Sstevel@tonic-gate 	char *esc_answer;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if ((status = dld_ami_get_cert(amih, NULL, &certs, &ccnt)) != AMI_OK) {
8067c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "alias2dn",
8077c478bd9Sstevel@tonic-gate 		    "Can not get my DN: %s",
8087c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, status));
8097c478bd9Sstevel@tonic-gate 	    return (NULL);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (ccnt == 0) {
8137c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "alias2dn",
8147c478bd9Sstevel@tonic-gate 		    "No cert found for myself");
8157c478bd9Sstevel@tonic-gate 	    return (NULL);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if ((status = dld_ami_dn2str(amih, certs[0].info.subject, &answer))
8197c478bd9Sstevel@tonic-gate 	    != AMI_OK) {
8207c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "alias2dn",
8217c478bd9Sstevel@tonic-gate 		    "Can not convert DN to string: %s",
8227c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, status));
8237c478bd9Sstevel@tonic-gate 	    answer = NULL;
8247c478bd9Sstevel@tonic-gate 	    goto done;
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (SLPEscape(answer, &esc_answer, SLP_FALSE) != SLP_OK) {
8287c478bd9Sstevel@tonic-gate 	    free(answer);
8297c478bd9Sstevel@tonic-gate 	    answer = NULL;
8307c478bd9Sstevel@tonic-gate 	} else {
8317c478bd9Sstevel@tonic-gate 	    free(answer);
8327c478bd9Sstevel@tonic-gate 	    answer = esc_answer;
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate done:
8367c478bd9Sstevel@tonic-gate 	dld_ami_free_cert_list(&certs, ccnt);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	return (answer);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
check_spis(ami_handle_t * amih,ami_cert * certs,int icert,const char * spi)8417c478bd9Sstevel@tonic-gate static SLPError check_spis(ami_handle_t *amih,
8427c478bd9Sstevel@tonic-gate 			    ami_cert *certs,
8437c478bd9Sstevel@tonic-gate 			    int icert,
8447c478bd9Sstevel@tonic-gate 			    const char *spi) {
8457c478bd9Sstevel@tonic-gate 	ami_cert *chain = NULL;
8467c478bd9Sstevel@tonic-gate 	int ccnt;
8477c478bd9Sstevel@tonic-gate 	const char *cas[2];
8487c478bd9Sstevel@tonic-gate 	char *prop_spi;
8497c478bd9Sstevel@tonic-gate 	char *ue_spi;
8507c478bd9Sstevel@tonic-gate 	char *p;
8517c478bd9Sstevel@tonic-gate 	SLPError err;
8527c478bd9Sstevel@tonic-gate 	AMI_STATUS ami_err;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/* If configured SPI == authblock SPI, we are done */
8557c478bd9Sstevel@tonic-gate 	prop_spi = (char *)SLPGetProperty(SLP_CONFIG_SPI);
8567c478bd9Sstevel@tonic-gate 	if (!prop_spi || !*prop_spi) {
8577c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "do_verify", "no SPI configured");
8587c478bd9Sstevel@tonic-gate 	    err = SLP_AUTHENTICATION_FAILED;
8597c478bd9Sstevel@tonic-gate 	    goto done;
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/* dup it so we can modify it */
8637c478bd9Sstevel@tonic-gate 	if (!(prop_spi = strdup(prop_spi))) {
8647c478bd9Sstevel@tonic-gate 	    slp_err(LOG_CRIT, 0, "do_verify", "out of memory");
8657c478bd9Sstevel@tonic-gate 	    err = SLP_MEMORY_ALLOC_FAILED;
8667c478bd9Sstevel@tonic-gate 	    goto done;
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/* if more than one SPI given, discard all but first */
8707c478bd9Sstevel@tonic-gate 	if ((p = slp_utf_strchr(prop_spi, ','))) {
8717c478bd9Sstevel@tonic-gate 	    *p = 0;
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/* unescape configured DNs */
8757c478bd9Sstevel@tonic-gate 	if ((err = SLPUnescape(prop_spi, &ue_spi, SLP_FALSE)) != SLP_OK) {
8767c478bd9Sstevel@tonic-gate 	    goto done;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	free(prop_spi);
8797c478bd9Sstevel@tonic-gate 	prop_spi = ue_spi;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (dncmp(amih, prop_spi, spi) == 0) {
8827c478bd9Sstevel@tonic-gate 	    /* they match, so we are done */
8837c478bd9Sstevel@tonic-gate 	    err = SLP_OK;
8847c478bd9Sstevel@tonic-gate 	    goto done;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	/*
8887c478bd9Sstevel@tonic-gate 	 * Else we need to traverse the cert chain. ami_get_cert_chain
8897c478bd9Sstevel@tonic-gate 	 * verifies each link in the chain, so no need to do it again.
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 	cas[0] = prop_spi;
8927c478bd9Sstevel@tonic-gate 	cas[1] = NULL;
8937c478bd9Sstevel@tonic-gate 	ami_err = dld_ami_get_cert_chain(amih, certs + icert, cas, 0,
8947c478bd9Sstevel@tonic-gate 						&chain, &ccnt);
8957c478bd9Sstevel@tonic-gate 	if (ami_err != AMI_OK) {
8967c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "do_verify",
8977c478bd9Sstevel@tonic-gate 		    "can not get cert chain: %s",
8987c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, ami_err));
8997c478bd9Sstevel@tonic-gate 	    err = SLP_AUTHENTICATION_FAILED;
9007c478bd9Sstevel@tonic-gate 	    goto done;
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	err = SLP_OK;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate done:
9067c478bd9Sstevel@tonic-gate 	if (chain) {
9077c478bd9Sstevel@tonic-gate 	    dld_ami_free_cert_list(&chain, ccnt);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if (prop_spi) free(prop_spi);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	return (err);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
dncmp(ami_handle_t * amih,const char * s1,const char * s2)9157c478bd9Sstevel@tonic-gate static int dncmp(ami_handle_t *amih, const char *s1, const char *s2) {
9167c478bd9Sstevel@tonic-gate 	AMI_STATUS status;
9177c478bd9Sstevel@tonic-gate 	ami_name *dn1 = NULL;
9187c478bd9Sstevel@tonic-gate 	ami_name *dn2 = NULL;
9197c478bd9Sstevel@tonic-gate 	char *dnstr1 = NULL;
9207c478bd9Sstevel@tonic-gate 	char *dnstr2 = NULL;
9217c478bd9Sstevel@tonic-gate 	int answer;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/* Normalize: convert to DN structs and back to strings */
9247c478bd9Sstevel@tonic-gate 	if ((status = dld_ami_str2dn(amih, (char *)s1, &dn1)) != AMI_OK) {
9257c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "dncmp",
9267c478bd9Sstevel@tonic-gate 		    "can not create DN structure for %s: %s",
9277c478bd9Sstevel@tonic-gate 		    s1,
9287c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, status));
9297c478bd9Sstevel@tonic-gate 	    answer = 1;
9307c478bd9Sstevel@tonic-gate 	    goto done;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if ((status = dld_ami_str2dn(amih, (char *)s2, &dn2)) != AMI_OK) {
9347c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "dncmp",
9357c478bd9Sstevel@tonic-gate 		    "can not create DN structure for %s: %s",
9367c478bd9Sstevel@tonic-gate 		    s2,
9377c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, status));
9387c478bd9Sstevel@tonic-gate 	    answer = 1;
9397c478bd9Sstevel@tonic-gate 	    goto done;
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/* convert back to strings */
9437c478bd9Sstevel@tonic-gate 	if ((status = dld_ami_dn2str(amih, dn1, &dnstr1)) != AMI_OK) {
9447c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "dncmp",
9457c478bd9Sstevel@tonic-gate 		    "can not convert DN to string: %s",
9467c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, status));
9477c478bd9Sstevel@tonic-gate 	    answer = 1;
9487c478bd9Sstevel@tonic-gate 	    goto done;
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	if ((status = dld_ami_dn2str(amih, dn2, &dnstr2)) != AMI_OK) {
9527c478bd9Sstevel@tonic-gate 	    slp_err(LOG_INFO, 0, "dncmp",
9537c478bd9Sstevel@tonic-gate 		    "can not convert DN to string: %s",
9547c478bd9Sstevel@tonic-gate 		    dld_ami_strerror(amih, status));
9557c478bd9Sstevel@tonic-gate 	    answer = 1;
9567c478bd9Sstevel@tonic-gate 	    goto done;
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	answer = strcasecmp(dnstr1, dnstr2);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate done:
9627c478bd9Sstevel@tonic-gate 	if (dn1) {
9637c478bd9Sstevel@tonic-gate 	    dld_ami_free_dn(&dn1);
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (dn2) {
9677c478bd9Sstevel@tonic-gate 	    dld_ami_free_dn(&dn2);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (dnstr1) free(dnstr1);
9717c478bd9Sstevel@tonic-gate 	if (dnstr2) free(dnstr2);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	return (answer);
9747c478bd9Sstevel@tonic-gate }
975