dst_parse.c revision a91029a00e83e3933046cc9354357bccaa66c272
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/*
a7038d1a0513c8e804937ebc95fc9cb3a46c04f5Mark Andrews * Portions Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Portions Copyright (C) 1999-2002 Internet Software Consortium.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Permission to use, copy, modify, and/or distribute this software for any
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
15a44745412679c30a6d022733925af70a38b715David Lawrence * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
15a44745412679c30a6d022733925af70a38b715David Lawrence * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15a44745412679c30a6d022733925af70a38b715David Lawrence * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews *
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Permission to use, copy, modify, and/or distribute this software for any
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff * purpose with or without fee is hereby granted, provided that the above
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff * copyright notice and this permission notice appear in all copies.
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
364a82f7c25b62967678027043425201a5e5171aBob Halley * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews/*%
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Principal Author: Brian Wellington
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * $Id: dst_parse.c,v 1.26 2010/01/11 10:49:14 fdupont Exp $
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <config.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/base64.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/dir.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/fsaccess.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/lex.h>
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley#include <isc/mem.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/stdtime.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/string.h>
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley#include <isc/util.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <dns/time.h>
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley#include "dst_internal.h"
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff#include "dst_parse.h"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include "dst/result.h"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DST_AS_STR(t) ((t).value.as_textregion.base)
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley#define PRIVATE_KEY_STR "Private-key-format:"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define ALGORITHM_STR "Algorithm:"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define TIMING_NTAGS (DST_MAX_TIMES + 1)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic const char *timetags[TIMING_NTAGS] = {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Created:",
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Publish:",
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Activate:",
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Revoke:",
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Inactive:",
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley "Delete:",
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "DSPublish:"
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence};
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define NUMERIC_NTAGS (DST_MAX_NUMERIC + 1)
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halleystatic const char *numerictags[NUMERIC_NTAGS] = {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Predecessor:",
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley "Successor:",
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley "MaxTTL:",
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley "RollPeriod:"
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff};
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystruct parse_map {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley const int value;
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews const char *tag;
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley};
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic struct parse_map map[] = {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_MODULUS, "Modulus:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_PUBLICEXPONENT, "PublicExponent:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_PRIVATEEXPONENT, "PrivateExponent:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_PRIME1, "Prime1:"},
2cd0c38115b1efb043ed3104c0d08e51ceade0d7Bob Halley {TAG_RSA_PRIME2, "Prime2:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_EXPONENT1, "Exponent1:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_EXPONENT2, "Exponent2:"},
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley {TAG_RSA_COEFFICIENT, "Coefficient:"},
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley {TAG_RSA_ENGINE, "Engine:" },
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_LABEL, "Label:" },
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_RSA_PIN, "PIN:" },
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_DH_PRIME, "Prime(p):"},
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley {TAG_DH_GENERATOR, "Generator(g):"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_DH_PRIVATE, "Private_value(x):"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_DH_PUBLIC, "Public_value(y):"},
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley {TAG_DSA_PRIME, "Prime(p):"},
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff {TAG_DSA_SUBPRIME, "Subprime(q):"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_DSA_BASE, "Base(g):"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_DSA_PRIVATE, "Private_value(x):"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_DSA_PUBLIC, "Public_value(y):"},
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley {TAG_HMACMD5_KEY, "Key:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley {TAG_HMACMD5_BITS, "Bits:"},
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA1_KEY, "Key:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA1_BITS, "Bits:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA224_KEY, "Key:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA224_BITS, "Bits:"},
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA256_KEY, "Key:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA256_BITS, "Bits:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA384_KEY, "Key:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA384_BITS, "Bits:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA512_KEY, "Key:"},
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley {TAG_HMACSHA512_BITS, "Bits:"},
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley {0, NULL}
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson};
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic int
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Grafffind_value(const char *s, const unsigned int alg) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley int i;
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson for (i = 0; map[i].tag != NULL; i++) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (strcasecmp(s, map[i].tag) == 0 &&
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley (TAG_ALG(map[i].value) == alg))
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (map[i].value);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (-1);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley}
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic const char *
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafssonfind_tag(const int value) {
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson int i;
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley for (i = 0; ; i++) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (map[i].tag == NULL)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (NULL);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley else if (value == map[i].value)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (map[i].tag);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley }
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley}
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleystatic int
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleyfind_metadata(const char *s, const char *tags[], int ntags) {
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley int i;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley for (i = 0; i < ntags; i++) {
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson if (strcasecmp(s, tags[i]) == 0)
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (i);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley }
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (-1);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley}
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic int
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleyfind_timedata(const char *s) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (find_metadata(s, timetags, TIMING_NTAGS));
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley}
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleystatic int
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleyfind_numericdata(const char *s) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (find_metadata(s, numerictags, NUMERIC_NTAGS));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley}
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic int
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleycheck_rsa(const dst_private_t *priv) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley int i, j;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley isc_boolean_t have[RSA_NTAGS];
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley isc_boolean_t ok;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley unsigned int mask;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley for (i = 0; i < RSA_NTAGS; i++)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley have[i] = ISC_FALSE;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley for (j = 0; j < priv->nelements; j++) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley for (i = 0; i < RSA_NTAGS; i++)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (priv->elements[j].tag == TAG(DST_ALG_RSAMD5, i))
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (i == RSA_NTAGS)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (-1);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley have[i] = ISC_TRUE;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley mask = ~0;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley mask <<= sizeof(mask) * 8 - TAG_SHIFT;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley mask >>= sizeof(mask) * 8 - TAG_SHIFT;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley if (have[TAG_RSA_ENGINE & mask])
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ok = have[TAG_RSA_MODULUS & mask] &&
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley have[TAG_RSA_PUBLICEXPONENT & mask] &&
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley have[TAG_RSA_LABEL & mask];
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley else
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ok = have[TAG_RSA_MODULUS & mask] &&
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley have[TAG_RSA_PUBLICEXPONENT & mask] &&
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley have[TAG_RSA_PRIVATEEXPONENT & mask] &&
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley have[TAG_RSA_PRIME1 & mask] &&
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley have[TAG_RSA_PRIME2 & mask] &&
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley have[TAG_RSA_EXPONENT1 & mask] &&
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley have[TAG_RSA_EXPONENT2 & mask] &&
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley have[TAG_RSA_COEFFICIENT & mask];
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (ok ? 0 : -1 );
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley}
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleystatic int
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleycheck_dh(const dst_private_t *priv) {
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley int i, j;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (priv->nelements != DH_NTAGS)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (-1);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley for (i = 0; i < DH_NTAGS; i++) {
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley for (j = 0; j < priv->nelements; j++)
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (priv->elements[j].tag == TAG(DST_ALG_DH, i))
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley break;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (j == priv->nelements)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (-1);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley }
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (0);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halleystatic int
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleycheck_dsa(const dst_private_t *priv) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley int i, j;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (priv->nelements != DSA_NTAGS)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (-1);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff for (i = 0; i < DSA_NTAGS; i++) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley for (j = 0; j < priv->nelements; j++)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (priv->elements[j].tag == TAG(DST_ALG_DSA, i))
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley break;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (j == priv->nelements)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (-1);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley }
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley return (0);
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley}
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic int
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleycheck_hmac_md5(const dst_private_t *priv, isc_boolean_t old) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley int i, j;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (priv->nelements != HMACMD5_NTAGS) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /*
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * If this is a good old format and we are accepting
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * the old format return success.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (old && priv->nelements == OLD_HMACMD5_NTAGS &&
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley priv->elements[0].tag == TAG_HMACMD5_KEY)
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley return (0);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (-1);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley }
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /*
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * We must be new format at this point.
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley */
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley for (i = 0; i < HMACMD5_NTAGS; i++) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews for (j = 0; j < priv->nelements; j++)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (priv->elements[j].tag == TAG(DST_ALG_HMACMD5, i))
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson break;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (j == priv->nelements)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (-1);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (0);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonstatic int
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssoncheck_hmac_sha(const dst_private_t *priv, unsigned int ntags,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson unsigned int alg)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson{
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley unsigned int i, j;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews if (priv->nelements != ntags)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (-1);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews for (i = 0; i < ntags; i++) {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews for (j = 0; j < priv->nelements; j++)
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington if (priv->elements[j].tag == TAG(alg, i))
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington break;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (j == priv->nelements)
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews return (-1);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff }
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff return (0);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews}
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graffstatic int
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleycheck_data(const dst_private_t *priv, const unsigned int alg,
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley isc_boolean_t old)
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley{
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson /* XXXVIX this switch statement is too sparse to gen a jump table. */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley switch (alg) {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews case DST_ALG_RSAMD5:
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews case DST_ALG_RSASHA1:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (check_rsa(priv));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case DST_ALG_DH:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (check_dh(priv));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case DST_ALG_DSA:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (check_dsa(priv));
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley case DST_ALG_HMACMD5:
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley return (check_hmac_md5(priv, old));
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson case DST_ALG_HMACSHA1:
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley return (check_hmac_sha(priv, HMACSHA1_NTAGS, alg));
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley case DST_ALG_HMACSHA224:
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley return (check_hmac_sha(priv, HMACSHA224_NTAGS, alg));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case DST_ALG_HMACSHA256:
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (check_hmac_sha(priv, HMACSHA256_NTAGS, alg));
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley case DST_ALG_HMACSHA384:
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (check_hmac_sha(priv, HMACSHA384_NTAGS, alg));
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley case DST_ALG_HMACSHA512:
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley return (check_hmac_sha(priv, HMACSHA512_NTAGS, alg));
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley default:
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley return (DST_R_UNSUPPORTEDALG);
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington }
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington}
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellingtonvoid
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellingtondst__privstruct_free(dst_private_t *priv, isc_mem_t *mctx) {
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley int i;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (priv == NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff for (i = 0; i < priv->nelements; i++) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (priv->elements[i].data == NULL)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley continue;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley memset(priv->elements[i].data, 0, MAXFIELDSIZE);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley isc_mem_put(mctx, priv->elements[i].data, MAXFIELDSIZE);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson priv->nelements = 0;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonisc_result_t
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssondst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_mem_t *mctx, dst_private_t *priv)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson{
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews int n = 0, major, minor;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_buffer_t b;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_token_t token;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson unsigned char *data = NULL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson unsigned int opt = ISC_LEXOPT_EOL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_stdtime_t when;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_result_t ret;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson REQUIRE(priv != NULL);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson priv->nelements = 0;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson memset(priv->elements, 0, sizeof(priv->elements));
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson#define NEXTTOKEN(lex, opt, token) \
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson do { \
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ret = isc_lex_gettoken(lex, opt, token); \
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (ret != ISC_R_SUCCESS) \
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson goto fail; \
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson } while (0)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley#define READLINE(lex, opt, token) \
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson do { \
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley ret = isc_lex_gettoken(lex, opt, token); \
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (ret == ISC_R_EOF) \
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley break; \
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews else if (ret != ISC_R_SUCCESS) \
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley goto fail; \
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews } while ((*token).type != isc_tokentype_eol)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson /*
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson * Read the description line.
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson NEXTTOKEN(lex, opt, &token);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (token.type != isc_tokentype_string ||
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews strcmp(DST_AS_STR(token), PRIVATE_KEY_STR) != 0)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ret = DST_R_INVALIDPRIVATEKEY;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews goto fail;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews NEXTTOKEN(lex, opt, &token);
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews if (token.type != isc_tokentype_string ||
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews (DST_AS_STR(token))[0] != 'v')
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ret = DST_R_INVALIDPRIVATEKEY;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews goto fail;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (sscanf(DST_AS_STR(token), "v%d.%d", &major, &minor) != 2)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ret = DST_R_INVALIDPRIVATEKEY;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews goto fail;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (major > DST_MAJOR_VERSION) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley ret = DST_R_INVALIDPRIVATEKEY;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson goto fail;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley }
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews /*
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews * Store the private key format version number
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews */
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews dst_key_setprivateformat(key, major, minor);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews READLINE(lex, opt, &token);
abc93ada82b37282122f8162f48761eb99736571Mark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews /*
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews * Read the algorithm line.
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson NEXTTOKEN(lex, opt, &token);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (token.type != isc_tokentype_string ||
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson strcmp(DST_AS_STR(token), ALGORITHM_STR) != 0)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley ret = DST_R_INVALIDPRIVATEKEY;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley goto fail;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews NEXTTOKEN(lex, opt | ISC_LEXOPT_NUMBER, &token);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (token.type != isc_tokentype_number ||
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews token.value.as_ulong != (unsigned long) dst_key_alg(key))
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ret = DST_R_INVALIDPRIVATEKEY;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley goto fail;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley }
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson READLINE(lex, opt, &token);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley /*
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff * Read the key data.
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence for (n = 0; n < MAXFIELDS; n++) {
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff int tag;
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews isc_region_t r;
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews do {
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson ret = isc_lex_gettoken(lex, opt, &token);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff if (ret == ISC_R_EOF)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto done;
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (ret != ISC_R_SUCCESS)
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff goto fail;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley } while (token.type == isc_tokentype_eol);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff if (token.type != isc_tokentype_string) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ret = DST_R_INVALIDPRIVATEKEY;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff goto fail;
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley }
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews /* Numeric metadata */
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff tag = find_numericdata(DST_AS_STR(token));
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley if (tag >= 0) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley INSIST(tag < NUMERIC_NTAGS);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff NEXTTOKEN(lex, opt | ISC_LEXOPT_NUMBER, &token);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff if (token.type != isc_tokentype_number) {
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff ret = DST_R_INVALIDPRIVATEKEY;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff goto fail;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff }
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff dst_key_setnum(key, tag, token.value.as_ulong);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff goto next;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley /* Timing metadata */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson tag = find_timedata(DST_AS_STR(token));
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews if (tag >= 0) {
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews INSIST(tag < TIMING_NTAGS);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews NEXTTOKEN(lex, opt, &token);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff if (token.type != isc_tokentype_string) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ret = DST_R_INVALIDPRIVATEKEY;
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley goto fail;
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence }
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff ret = dns_time32_fromtext(DST_AS_STR(token), &when);
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence if (ret != ISC_R_SUCCESS)
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence goto fail;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews dst_key_settime(key, tag, when);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley goto next;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley }
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff /* Key data */
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley tag = find_value(DST_AS_STR(token), alg);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley if (tag < 0 && minor > DST_MINOR_VERSION)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson goto next;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley else if (tag < 0) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ret = DST_R_INVALIDPRIVATEKEY;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto fail;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley priv->elements[n].tag = tag;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley data = (unsigned char *) isc_mem_get(mctx, MAXFIELDSIZE);
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley if (data == NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto fail;
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley isc_buffer_init(&b, data, MAXFIELDSIZE);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews ret = isc_base64_tobuffer(lex, &b, -1);
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews if (ret != ISC_R_SUCCESS)
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews goto fail;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
92296c744ef9ef36de499eb10b3e27800d8b24a5Mark Andrews isc_buffer_usedregion(&b, &r);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews priv->elements[n].length = r.length;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews priv->elements[n].data = r.base;
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence priv->nelements++;
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley next:
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley READLINE(lex, opt, &token);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley data = NULL;
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley }
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley done:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (check_data(priv, alg, ISC_TRUE) < 0)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews goto fail;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (ISC_R_SUCCESS);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrewsfail:
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews dst__privstruct_free(priv, mctx);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews if (data != NULL)
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington isc_mem_put(mctx, data, MAXFIELDSIZE);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (ret);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews}
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellingtonisc_result_t
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrewsdst__privstruct_writefile(const dst_key_t *key, const dst_private_t *priv,
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews const char *directory)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews{
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews FILE *fp;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews int ret, i;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_result_t result;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews char filename[ISC_DIR_NAMEMAX];
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews char buffer[MAXFIELDSIZE * 2];
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_fsaccess_t access;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington isc_stdtime_t when;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_uint32_t value;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_buffer_t b;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_region_t r;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews int major, minor;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington REQUIRE(priv != NULL);
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews if (check_data(priv, dst_key_alg(key), ISC_FALSE) < 0)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (DST_R_INVALIDPRIVATEKEY);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_buffer_init(&b, filename, sizeof(filename));
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ret = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory, &b);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (ret != ISC_R_SUCCESS)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (ret);
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if ((fp = fopen(filename, "w")) == NULL)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (DST_R_WRITEERROR);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington access = 0;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_fsaccess_add(ISC_FSACCESS_OWNER,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff &access);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley (void)isc_fsaccess_set(filename, access);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dst_key_getprivateformat(key, &major, &minor);
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews if (major == 0 && minor == 0) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff major = DST_MAJOR_VERSION;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley minor = DST_MINOR_VERSION;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley /* XXXDCL return value should be checked for full filesystem */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "%s v%d.%d\n", PRIVATE_KEY_STR, major, minor);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "%s %d ", ALGORITHM_STR, dst_key_alg(key));
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley /* XXXVIX this switch statement is too sparse to gen a jump table. */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley switch (dst_key_alg(key)) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff case DST_ALG_RSAMD5:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "(RSA)\n");
d981ca645597116d227a48bf37cc5edc061c854dBob Halley break;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley case DST_ALG_DH:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "(DH)\n");
d981ca645597116d227a48bf37cc5edc061c854dBob Halley break;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff case DST_ALG_DSA:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "(DSA)\n");
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews break;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff case DST_ALG_RSASHA1:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "(RSASHA1)\n");
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff break;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley case DST_ALG_NSEC3RSASHA1:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fprintf(fp, "(NSEC3RSASHA1)\n");
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff break;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley case DST_ALG_NSEC3DSA:
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer fprintf(fp, "(NSEC3DSA)\n");
break;
case DST_ALG_RSASHA256:
fprintf(fp, "(RSASHA256)\n");
break;
case DST_ALG_RSASHA512:
fprintf(fp, "(RSASHA512)\n");
break;
case DST_ALG_HMACMD5:
fprintf(fp, "(HMAC_MD5)\n");
break;
case DST_ALG_HMACSHA1:
fprintf(fp, "(HMAC_SHA1)\n");
break;
case DST_ALG_HMACSHA224:
fprintf(fp, "(HMAC_SHA224)\n");
break;
case DST_ALG_HMACSHA256:
fprintf(fp, "(HMAC_SHA256)\n");
break;
case DST_ALG_HMACSHA384:
fprintf(fp, "(HMAC_SHA384)\n");
break;
case DST_ALG_HMACSHA512:
fprintf(fp, "(HMAC_SHA512)\n");
break;
default:
fprintf(fp, "(?)\n");
break;
}
for (i = 0; i < priv->nelements; i++) {
const char *s;
s = find_tag(priv->elements[i].tag);
r.base = priv->elements[i].data;
r.length = priv->elements[i].length;
isc_buffer_init(&b, buffer, sizeof(buffer));
result = isc_base64_totext(&r, sizeof(buffer), "", &b);
if (result != ISC_R_SUCCESS) {
fclose(fp);
return (DST_R_INVALIDPRIVATEKEY);
}
isc_buffer_usedregion(&b, &r);
fprintf(fp, "%s ", s);
isc_util_fwrite(r.base, 1, r.length, fp);
fprintf(fp, "\n");
}
/* Add the metadata tags */
if (major > 1 || (major == 1 && minor >= 3)) {
for (i = 0; i < NUMERIC_NTAGS; i++) {
result = dst_key_getnum(key, i, &value);
if (result != ISC_R_SUCCESS)
continue;
fprintf(fp, "%s %u\n", numerictags[i], value);
}
for (i = 0; i < TIMING_NTAGS; i++) {
result = dst_key_gettime(key, i, &when);
if (result != ISC_R_SUCCESS)
continue;
isc_buffer_init(&b, buffer, sizeof(buffer));
result = dns_time32_totext(when, &b);
if (result != ISC_R_SUCCESS)
continue;
isc_buffer_usedregion(&b, &r);
fprintf(fp, "%s ", timetags[i]);
isc_util_fwrite(r.base, 1, r.length, fp);
fprintf(fp, "\n");
}
}
fflush(fp);
result = ferror(fp) ? DST_R_WRITEERROR : ISC_R_SUCCESS;
fclose(fp);
return (result);
}
/*! \file */