Annotation of embedaddon/strongswan/src/libstrongswan/plugins/dnskey/dnskey_encoder.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2013 Andreas Steffen
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the
                      7:  * Free Software Foundation; either version 2 of the License, or (at your
                      8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful, but
                     11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     13:  * for more details.
                     14:  */
                     15: 
                     16: #include "dnskey_encoder.h"
                     17: 
                     18: #include <utils/debug.h>
                     19: 
                     20: /**
                     21:  * Encode an RSA public key in DNSKEY format (RFC 3110)
                     22:  */
                     23: static bool build_pub(chunk_t *encoding, va_list args)
                     24: {
                     25:        chunk_t n, e, pubkey;
                     26:        size_t exp_len;
                     27:        u_char *pos;
                     28: 
                     29:        if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
                     30:                                                   CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
                     31:        {
                     32:                /* remove leading zeros in exponent and modulus */
                     33:                while (*e.ptr == 0)
                     34:                {
                     35:                        e = chunk_skip(e, 1);
                     36:                }
                     37:                while (*n.ptr == 0)
                     38:                {
                     39:                        n = chunk_skip(n, 1);
                     40:                }
                     41: 
                     42:                if (e.len < 256)
                     43:                {
                     44:                        /* exponent length fits into a single octet */
                     45:                        exp_len = 1;
                     46:                        pubkey = chunk_alloc(exp_len + e.len + n.len);
                     47:                        pubkey.ptr[0] = (char)e.len;
                     48:                }
                     49:                else if (e.len < 65536)
                     50:                {
                     51:                        /* exponent length fits into two octets preceded by zero octet */
                     52:                        exp_len = 3;
                     53:                        pubkey = chunk_alloc(exp_len + e.len + n.len);
                     54:                        pubkey.ptr[0] = 0x00;
                     55:                        htoun16(pubkey.ptr + 1, e.len);
                     56:                }
                     57:                else
                     58:                {
                     59:                        /* exponent length is too large */
                     60:                        return FALSE;
                     61:                }
                     62: 
                     63:                /* copy exponent and modulus and convert to base64 format */
                     64:                pos = pubkey.ptr + exp_len;
                     65:                memcpy(pos, e.ptr, e.len);
                     66:                pos += e.len;
                     67:                memcpy(pos, n.ptr, n.len);
                     68:                *encoding = chunk_to_base64(pubkey, NULL);
                     69:                chunk_free(&pubkey);
                     70: 
                     71:                return TRUE;
                     72:        }
                     73:        return FALSE;
                     74: }
                     75: 
                     76: /**
                     77:  * See header.
                     78:  */
                     79: bool dnskey_encoder_encode(cred_encoding_type_t type, chunk_t *encoding,
                     80:                                                   va_list args)
                     81: {
                     82:        switch (type)
                     83:        {
                     84:                case PUBKEY_DNSKEY:
                     85:                        return build_pub(encoding, args);
                     86:                default:
                     87:                        return FALSE;
                     88:        }
                     89: }
                     90: 
                     91: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>