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