xref: /freebsd/contrib/bearssl/src/x509/encode_ec_rawder.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
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 inner.h */
28 const unsigned char *
br_get_curve_OID(int curve)29 br_get_curve_OID(int curve)
30 {
31 	static const unsigned char OID_secp256r1[] = {
32 		0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
33 	};
34 	static const unsigned char OID_secp384r1[] = {
35 		0x05, 0x2b, 0x81, 0x04, 0x00, 0x22
36 	};
37 	static const unsigned char OID_secp521r1[] = {
38 		0x05, 0x2b, 0x81, 0x04, 0x00, 0x23
39 	};
40 
41 	switch (curve) {
42 	case BR_EC_secp256r1:  return OID_secp256r1;
43 	case BR_EC_secp384r1:  return OID_secp384r1;
44 	case BR_EC_secp521r1:  return OID_secp521r1;
45 	default:
46 		return NULL;
47 	}
48 }
49 
50 /* see inner.h */
51 size_t
br_encode_ec_raw_der_inner(void * dest,const br_ec_private_key * sk,const br_ec_public_key * pk,int include_curve_oid)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)
55 {
56 	/*
57 	 * ASN.1 format:
58 	 *
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
64 	 *   }
65 	 *
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.
69 	 *
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.
74 	 *
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).
79 	 */
80 
81 	size_t len_version, len_privateKey, len_parameters, len_publicKey;
82 	size_t len_publicKey_bits, len_seq;
83 	const unsigned char *oid;
84 
85 	if (include_curve_oid) {
86 		oid = br_get_curve_OID(sk->curve);
87 		if (oid == NULL) {
88 			return 0;
89 		}
90 	} else {
91 		oid = NULL;
92 	}
93 	len_version = 3;
94 	len_privateKey = 1 + len_of_len(sk->xlen) + sk->xlen;
95 	if (include_curve_oid) {
96 		len_parameters = 4 + oid[0];
97 	} else {
98 		len_parameters = 0;
99 	}
100 	if (pk == NULL) {
101 		len_publicKey = 0;
102 		len_publicKey_bits = 0;
103 	} else {
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;
107 	}
108 	len_seq = len_version + len_privateKey + len_parameters + len_publicKey;
109 	if (dest == NULL) {
110 		return 1 + len_of_len(len_seq) + len_seq;
111 	} else {
112 		unsigned char *buf;
113 		size_t lenlen;
114 
115 		buf = dest;
116 		*buf ++ = 0x30;  /* SEQUENCE tag */
117 		lenlen = br_asn1_encode_length(buf, len_seq);
118 		buf += lenlen;
119 
120 		/* version */
121 		*buf ++ = 0x02;
122 		*buf ++ = 0x01;
123 		*buf ++ = 0x01;
124 
125 		/* privateKey */
126 		*buf ++ = 0x04;
127 		buf += br_asn1_encode_length(buf, sk->xlen);
128 		memcpy(buf, sk->x, sk->xlen);
129 		buf += sk->xlen;
130 
131 		/* parameters */
132 		if (include_curve_oid) {
133 			*buf ++ = 0xA0;
134 			*buf ++ = oid[0] + 2;
135 			*buf ++ = 0x06;
136 			memcpy(buf, oid, oid[0] + 1);
137 			buf += oid[0] + 1;
138 		}
139 
140 		/* publicKey */
141 		if (pk != NULL) {
142 			*buf ++ = 0xA1;
143 			buf += br_asn1_encode_length(buf, len_publicKey_bits);
144 			*buf ++ = 0x03;
145 			buf += br_asn1_encode_length(buf, pk->qlen + 1);
146 			*buf ++ = 0x00;
147 			memcpy(buf, pk->q, pk->qlen);
148 			/* buf += pk->qlen; */
149 		}
150 
151 		return 1 + lenlen + len_seq;
152 	}
153 }
154 
155 /* see bearssl_x509.h */
156 size_t
br_encode_ec_raw_der(void * dest,const br_ec_private_key * sk,const br_ec_public_key * pk)157 br_encode_ec_raw_der(void *dest,
158 	const br_ec_private_key *sk, const br_ec_public_key *pk)
159 {
160 	return br_encode_ec_raw_der_inner(dest, sk, pk, 1);
161 }
162