Path: blob/main/crypto/krb5/src/kprop/kprop_util.c
34869 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* kprop/kprop_util.c */2/*3* Copyright (C) 2010 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/* sockaddr2krbaddr() utility function used by kprop and kpropd */2728#include "k5-int.h"29#include "kprop.h"3031/* Construct a host-based principal, similar to krb5_sname_to_principal() but32* with a specified realm. */33krb5_error_code34sn2princ_realm(krb5_context context, const char *hostname, const char *sname,35const char *realm, krb5_principal *princ_out)36{37krb5_error_code ret;38krb5_principal princ;3940*princ_out = NULL;41assert(sname != NULL && realm != NULL);4243ret = krb5_sname_to_principal(context, hostname, sname, KRB5_NT_SRV_HST,44&princ);45if (ret)46return ret;4748ret = krb5_set_principal_realm(context, princ, realm);49if (ret) {50krb5_free_principal(context, princ);51return ret;52}5354*princ_out = princ;55return 0;56}5758void59encode_database_size(uint64_t size, krb5_data *buf)60{61assert(buf->length >= 12);62if (size > 0 && size <= UINT32_MAX) {63/* Encode in 32 bits for backward compatibility. */64store_32_be(size, buf->data);65buf->length = 4;66} else {67/* Set the first 32 bits to 0 and encode in the following 64 bits. */68store_32_be(0, buf->data);69store_64_be(size, buf->data + 4);70buf->length = 12;71}72}7374krb5_error_code75decode_database_size(const krb5_data *buf, uint64_t *size_out)76{77uint64_t size;7879if (buf->length == 12) {80/* A 12-byte buffer must have the first four bytes zeroed. */81if (load_32_be(buf->data) != 0)82return KRB5KRB_ERR_GENERIC;8384/* The size is stored in the next 64 bits. Values from 1..2^32-1 must85* be encoded in four bytes. */86size = load_64_be(buf->data + 4);87if (size > 0 && size <= UINT32_MAX)88return KRB5KRB_ERR_GENERIC;89} else if (buf->length == 4) {90size = load_32_be(buf->data);91} else {92/* Invalid buffer size. */93return KRB5KRB_ERR_GENERIC;94}9596*size_out = size;97return 0;98}99100101