2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
27 /* see bearssl_x509.h */
29 br_encode_ec_pkcs8_der(void *dest
,
30 const br_ec_private_key
*sk
, const br_ec_public_key
*pk
)
35 * OneAsymmetricKey ::= SEQUENCE {
37 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
38 * privateKey PrivateKey,
39 * attributes [0] Attributes OPTIONAL,
41 * [[2: publicKey [1] PublicKey OPTIONAL ]],
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.
56 * OID id-ecPublicKey (1.2.840.10045.2.1), DER-encoded (with
59 static const unsigned char OID_ECPUBKEY
[] = {
60 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
63 size_t len_version
, len_privateKeyAlgorithm
, len_privateKeyValue
;
64 size_t len_privateKey
, len_seq
;
65 const unsigned char *oid
;
67 oid
= br_get_curve_OID(sk
->curve
);
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
;
79 return 1 + len_of_len(len_seq
) + len_seq
;
85 *buf
++ = 0x30; /* SEQUENCE tag */
86 lenlen
= br_asn1_encode_length(buf
, len_seq
);
94 /* privateKeyAlgorithm */
96 *buf
++ = (sizeof OID_ECPUBKEY
) + 2 + oid
[0];
97 memcpy(buf
, OID_ECPUBKEY
, sizeof OID_ECPUBKEY
);
98 buf
+= sizeof OID_ECPUBKEY
;
100 memcpy(buf
, oid
, 1 + oid
[0]);
105 buf
+= br_asn1_encode_length(buf
, len_privateKeyValue
);
106 br_encode_ec_raw_der_inner(buf
, sk
, pk
, 0);
108 return 1 + lenlen
+ len_seq
;