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
29 br_get_curve_OID(int curve
)
31 static const unsigned char OID_secp256r1
[] = {
32 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
34 static const unsigned char OID_secp384r1
[] = {
35 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22
37 static const unsigned char OID_secp521r1
[] = {
38 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23
42 case BR_EC_secp256r1
: return OID_secp256r1
;
43 case BR_EC_secp384r1
: return OID_secp384r1
;
44 case BR_EC_secp521r1
: return OID_secp521r1
;
52 br_encode_ec_raw_der_inner(void *dest
,
53 const br_ec_private_key
*sk
, const br_ec_public_key
*pk
,
54 int include_curve_oid
)
59 * ECPrivateKey ::= SEQUENCE {
60 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
61 * privateKey OCTET STRING,
62 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
63 * publicKey [1] BIT STRING OPTIONAL
66 * The tages '[0]' and '[1]' are explicit. The 'ECParameters'
67 * is a CHOICE; in our case, it will always be an OBJECT IDENTIFIER
68 * that identifies the curve.
70 * The value of the 'privateKey' field is the raw unsigned big-endian
71 * encoding of the private key (integer modulo the curve subgroup
72 * order); there is no INTEGER tag, and the leading bit may be 1.
73 * Also, leading bytes of value 0x00 are _not_ removed.
75 * The 'publicKey' contents are the raw encoded public key point,
76 * normally uncompressed (leading byte of value 0x04, followed
77 * by the unsigned big-endian encodings of the X and Y coordinates,
78 * padded to the full field length if necessary).
81 size_t len_version
, len_privateKey
, len_parameters
, len_publicKey
;
82 size_t len_publicKey_bits
, len_seq
;
83 const unsigned char *oid
;
85 if (include_curve_oid
) {
86 oid
= br_get_curve_OID(sk
->curve
);
94 len_privateKey
= 1 + len_of_len(sk
->xlen
) + sk
->xlen
;
95 if (include_curve_oid
) {
96 len_parameters
= 4 + oid
[0];
102 len_publicKey_bits
= 0;
104 len_publicKey_bits
= 2 + len_of_len(pk
->qlen
) + pk
->qlen
;
105 len_publicKey
= 1 + len_of_len(len_publicKey_bits
)
106 + len_publicKey_bits
;
108 len_seq
= len_version
+ len_privateKey
+ len_parameters
+ len_publicKey
;
110 return 1 + len_of_len(len_seq
) + len_seq
;
116 *buf
++ = 0x30; /* SEQUENCE tag */
117 lenlen
= br_asn1_encode_length(buf
, len_seq
);
127 buf
+= br_asn1_encode_length(buf
, sk
->xlen
);
128 memcpy(buf
, sk
->x
, sk
->xlen
);
132 if (include_curve_oid
) {
134 *buf
++ = oid
[0] + 2;
136 memcpy(buf
, oid
, oid
[0] + 1);
143 buf
+= br_asn1_encode_length(buf
, len_publicKey_bits
);
145 buf
+= br_asn1_encode_length(buf
, pk
->qlen
+ 1);
147 memcpy(buf
, pk
->q
, pk
->qlen
);
148 /* buf += pk->qlen; */
151 return 1 + lenlen
+ len_seq
;
155 /* see bearssl_x509.h */
157 br_encode_ec_raw_der(void *dest
,
158 const br_ec_private_key
*sk
, const br_ec_public_key
*pk
)
160 return br_encode_ec_raw_der_inner(dest
, sk
, pk
, 1);