1/*
2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "inner.h"
26
27/* see bearssl_x509.h */
28size_t
29br_encode_ec_pkcs8_der(void *dest,
30	const br_ec_private_key *sk, const br_ec_public_key *pk)
31{
32	/*
33	 * ASN.1 format:
34	 *
35	 *   OneAsymmetricKey ::= SEQUENCE {
36	 *     version                   Version,
37	 *     privateKeyAlgorithm       PrivateKeyAlgorithmIdentifier,
38	 *     privateKey                PrivateKey,
39	 *     attributes            [0] Attributes OPTIONAL,
40	 *     ...,
41	 *     [[2: publicKey        [1] PublicKey OPTIONAL ]],
42	 *     ...
43	 *   }
44	 *
45	 * We don't include attributes or public key (the public key
46	 * is included in the private key value instead). The
47	 * 'version' field is an INTEGER that we will set to 0
48	 * (meaning 'v1', compatible with previous versions of PKCS#8).
49	 * The 'privateKeyAlgorithm' structure is an AlgorithmIdentifier
50	 * whose OID should be id-ecPublicKey, with, as parameters, the
51	 * curve OID. The 'privateKey' is an OCTET STRING, whose value
52	 * is the "raw DER" encoding of the key pair.
53	 */
54
55	/*
56	 * OID id-ecPublicKey (1.2.840.10045.2.1), DER-encoded (with
57	 * the tag).
58	 */
59	static const unsigned char OID_ECPUBKEY[] = {
60		0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
61	};
62
63	size_t len_version, len_privateKeyAlgorithm, len_privateKeyValue;
64	size_t len_privateKey, len_seq;
65	const unsigned char *oid;
66
67	oid = br_get_curve_OID(sk->curve);
68	if (oid == NULL) {
69		return 0;
70	}
71	len_version = 3;
72	len_privateKeyAlgorithm = 2 + sizeof OID_ECPUBKEY + 2 + oid[0];
73	len_privateKeyValue = br_encode_ec_raw_der_inner(NULL, sk, pk, 0);
74	len_privateKey = 1 + len_of_len(len_privateKeyValue)
75		+ len_privateKeyValue;
76	len_seq = len_version + len_privateKeyAlgorithm + len_privateKey;
77
78	if (dest == NULL) {
79		return 1 + len_of_len(len_seq) + len_seq;
80	} else {
81		unsigned char *buf;
82		size_t lenlen;
83
84		buf = dest;
85		*buf ++ = 0x30;  /* SEQUENCE tag */
86		lenlen = br_asn1_encode_length(buf, len_seq);
87		buf += lenlen;
88
89		/* version */
90		*buf ++ = 0x02;
91		*buf ++ = 0x01;
92		*buf ++ = 0x00;
93
94		/* privateKeyAlgorithm */
95		*buf ++ = 0x30;
96		*buf ++ = (sizeof OID_ECPUBKEY) + 2 + oid[0];
97		memcpy(buf, OID_ECPUBKEY, sizeof OID_ECPUBKEY);
98		buf += sizeof OID_ECPUBKEY;
99		*buf ++ = 0x06;
100		memcpy(buf, oid, 1 + oid[0]);
101		buf += 1 + oid[0];
102
103		/* privateKey */
104		*buf ++ = 0x04;
105		buf += br_asn1_encode_length(buf, len_privateKeyValue);
106		br_encode_ec_raw_der_inner(buf, sk, pk, 0);
107
108		return 1 + lenlen + len_seq;
109	}
110}
111