<html><body>Hollo Tony,<div><br></div><div>many thanks, it´s an awesome trick.</div><div><br></div><div>I can confirm, that I´m able to "hack" private and public key from KNOT. I tried to re-write information in .key and .private files in BIND, but now it seems to be an issue with the chain used in the zone.</div><div><br></div><div>When I tried to re-sign my zone in BIND by Webmin, then I get this error message below. My original "keytag" is <span style="background-color:transparent">43121. I don´t understand, where is written information like </span><span style="background-color:transparent">example.com/ECDSAP256SHA256/45623</span></div><div><span style="background-color:transparent"><br></span></div><div><span style="background-color:transparent">***</span></div><div>Failed to sign zone : dnssec-signzone: warning: /var/named/example.com:458: signature has expired dnssec-signzone: warning: dns_dnssec_keylistfromrdataset: error reading private key file example.com/ECDSAP256SHA256/45623: file not found dnssec-signzone: warning: dns_dnssec_keylistfromrdataset: error reading private key file example.com/ECDSAP256SHA256/43121: private key is invalid dnssec-signzone: fatal: failed to load the zone keys: private key is invalid<br>***</div><div><br></div><div>Could you please help me dig, where I´m wrong?<br>Best regards, </div><div>-- <br>Smil Milan Jeskyňka Kazatel<br><br><aside> ---------- Původní e-mail ----------<br>Od: Tony Finch <dot@dotat.at><br>Komu: Milan Jeskynka Kazatel <KazatelM@seznam.cz><br>Datum: 12. 3. 2019 17:14:40<br>Předmět: Re: convert Knot DNS sigantures certs to BIND format.</aside><br><blockquote data-email="dot@dotat.at">Milan Jeskynka Kazatel <KazatelM@seznam.cz> wrote:
<br>>
<br>> I received a hint for a tool which allows converting .pem format used in
<br>> Knot to .key and .private used in BIND, but it, unfortunately, does not
<br>> support ECDSAP256SHA256 algorithm which I used.
<br>
<br>Ah, sounds like Knot uses a relatively familiar key format, so we can hack
<br>around with OpenSSL command line tools.
<br>
<br>Unless I have missed something, BIND doesn't have any support for non-BIND
<br>key files: it has its own code for reading and writing keys, which knows
<br>about OpenSSL's in-memory key format. (I think this is related to support
<br>for multiple crypto providers, and the fact that supporting PEM implies
<br>supporting ASN.1 which is not a task any wise programmer would take on.)
<br>
<br>So I think you'll have to get dirty with the key internals; fortunately
<br>the modern key types handle the private material as a blob so you don't
<br>have to fiddle around with half a dozen parameters.
<br>
<br>If you have an ECDSA key in PEM format, you can break it open like
<br>this. The short blob is the private key and the long one is the public
<br>key.
<br>
<br>$ openssl ec </var/lib/knot/keys/keys/c3e8539dc582bb2ceeca0ab9fb7b89d521a4f658.pem |
<br> openssl asn1parse -dump
<br>read EC key
<br>writing EC key
<br> 0:d=0 hl=2 l= 119 cons: SEQUENCE
<br> 2:d=1 hl=2 l= 1 prim: INTEGER :01
<br> 5:d=1 hl=2 l= 32 prim: OCTET STRING
<br> 0000 - f5 60 92 ac fe 6f 49 3a-cf 32 b3 16 21 2c f7 37 .`...oI:.2..!,.7
<br> 0010 - 46 94 eb 06 4f 71 11 f1-71 92 84 f6 0d 16 73 de F...Oq..q.....s.
<br> 39:d=1 hl=2 l= 10 cons: cont [ 0 ]
<br> 41:d=2 hl=2 l= 8 prim: OBJECT :prime256v1
<br> 51:d=1 hl=2 l= 68 cons: cont [ 1 ]
<br> 53:d=2 hl=2 l= 66 prim: BIT STRING
<br> 0000 - 00 04 87 d7 36 06 dc d7-86 36 07 49 d2 c2 f9 7b ....6....6.I...{
<br> 0010 - 2d 30 64 3a 1c 12 e0 a1-ea dc cd 1f be a4 0f e8 -0d:............
<br> 0020 - c2 d5 af fe 30 71 be 12-62 60 ba 07 ea 07 17 28 ....0q..b`.....(
<br> 0030 - 97 5d 08 cd c4 55 c1 88-bf db b6 e5 34 12 1d 0e .]...U......4...
<br> 0040 - d2 ac ..
<br>
<br>BIND wants these in base64. A not completely impossible way to do this is
<br>to feed the binary (DER) form of the key to a bit of perl. (PEM is base64
<br>encoded DER.) This involves some magic numbers for the offsets of the
<br>blobs derived from the asn1 dump above.
<br>
<br>$ openssl ec -outform der </var/lib/knot/keys/keys/c3e8539dc582bb2ceeca0ab9fb7b89d521a4f658.pem |
<br> perl -Mv5.10 -MMIME::Base64 -e '
<br> undef $/; my $k = <STDIN>;
<br> print encode_base64 substr $k, 7, 32;
<br> print encode_base64 substr $k, -64;'
<br>read EC key
<br>writing EC key
<br>9WCSrP5vSTrPMrMWISz3N0aU6wZPcRHxcZKE9g0Wc94=
<br>h9c2BtzXhjYHSdLC+XstMGQ6HBLgoerczR++pA/owtWv/jBxvhJiYLoH6gcXKJddCM3EVcGIv9u2
<br>5TQSHQ7SrA==
<br>
<br>The first line is the private key; the second and third lines are the
<br>public key. We can check it matches:
<br>
<br>$ cat /var/lib/knot/keys/zone_example.com.json
<br>{
<br> "policy": "\u0006policy",
<br> "nsec3_salt": null,
<br> "nsec3_salt_created": null,
<br> "keys": [
<br> {
<br> "id": "c3e8539dc582bb2ceeca0ab9fb7b89d521a4f658",
<br> "keytag": 19633,
<br> "algorithm": 13,
<br> "public_key": "h9c2BtzXhjYHSdLC+XstMGQ6HBLgoerczR++pA/owtWv/jBxvhJiYLoH6gcXKJddCM3EVcGIv9u25TQSHQ7SrA==",
<br> "ksk": false,
<br> "created": "2019-03-12T15:44:02+0000"
<br> }
<br> ]
<br>}
<br>
<br>Probably the easiest way to turn this into BIND key files is to run
<br>`dnssec-keygen -a ecdsa256 example.com` and edit the output to insert the
<br>short private and long public base64 blobs emitted by the perl. You will
<br>also need to rename the files to match the keytag in knot's zone_*.json
<br>file.
<br>
<br>Tony.
<br>--
<br>f.anthony.n.finch <dot@dotat.at> http://dotat.at/
<br>public services available on equal terms to all</blockquote></div></body></html>