xref: /freebsd/contrib/bearssl/src/x509/encode_rsa_pk8der.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
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_rsa_pkcs8_der(void * dest,const br_rsa_private_key * sk,const br_rsa_public_key * pk,const void * d,size_t dlen)29*0957b409SSimon J. Gerraty br_encode_rsa_pkcs8_der(void *dest, const br_rsa_private_key *sk,
30*0957b409SSimon J. Gerraty 	const br_rsa_public_key *pk, const void *d, size_t dlen)
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 'version' field
46*0957b409SSimon J. Gerraty 	 * is an INTEGER that we will set to 0 (meaning 'v1', compatible
47*0957b409SSimon J. Gerraty 	 * with previous versions of PKCS#8). The 'privateKeyAlgorithm'
48*0957b409SSimon J. Gerraty 	 * structure is an AlgorithmIdentifier whose OID should be
49*0957b409SSimon J. Gerraty 	 * rsaEncryption, with NULL parameters. The 'privateKey' is an
50*0957b409SSimon J. Gerraty 	 * OCTET STRING, whose value is the "raw DER" encoding of the
51*0957b409SSimon J. Gerraty 	 * key pair.
52*0957b409SSimon J. Gerraty 	 *
53*0957b409SSimon J. Gerraty 	 * Since the private key value comes last, this function really
54*0957b409SSimon J. Gerraty 	 * adds a header, which is mostly fixed (only some lengths have
55*0957b409SSimon J. Gerraty 	 * to be modified.
56*0957b409SSimon J. Gerraty 	 */
57*0957b409SSimon J. Gerraty 
58*0957b409SSimon J. Gerraty 	/*
59*0957b409SSimon J. Gerraty 	 * Concatenation of:
60*0957b409SSimon J. Gerraty 	 *  - DER encoding of an INTEGER of value 0 (the 'version' field)
61*0957b409SSimon J. Gerraty 	 *  - DER encoding of a PrivateKeyAlgorithmIdentifier that uses
62*0957b409SSimon J. Gerraty 	 *    the rsaEncryption OID, and NULL parameters
63*0957b409SSimon J. Gerraty 	 *  - An OCTET STRING tag
64*0957b409SSimon J. Gerraty 	 */
65*0957b409SSimon J. Gerraty 	static const unsigned char PK8_HEAD[] = {
66*0957b409SSimon J. Gerraty 		0x02, 0x01, 0x00,
67*0957b409SSimon J. Gerraty 		0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
68*0957b409SSimon J. Gerraty 		0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
69*0957b409SSimon J. Gerraty 		0x04
70*0957b409SSimon J. Gerraty 	};
71*0957b409SSimon J. Gerraty 
72*0957b409SSimon J. Gerraty 	size_t len_raw, len_seq;
73*0957b409SSimon J. Gerraty 
74*0957b409SSimon J. Gerraty 	len_raw = br_encode_rsa_raw_der(NULL, sk, pk, d, dlen);
75*0957b409SSimon J. Gerraty 	len_seq = (sizeof PK8_HEAD) + len_of_len(len_raw) + len_raw;
76*0957b409SSimon J. Gerraty 	if (dest == NULL) {
77*0957b409SSimon J. Gerraty 		return 1 + len_of_len(len_seq) + len_seq;
78*0957b409SSimon J. Gerraty 	} else {
79*0957b409SSimon J. Gerraty 		unsigned char *buf;
80*0957b409SSimon J. Gerraty 		size_t lenlen;
81*0957b409SSimon J. Gerraty 
82*0957b409SSimon J. Gerraty 		buf = dest;
83*0957b409SSimon J. Gerraty 		*buf ++ = 0x30;  /* SEQUENCE tag */
84*0957b409SSimon J. Gerraty 		lenlen = br_asn1_encode_length(buf, len_seq);
85*0957b409SSimon J. Gerraty 		buf += lenlen;
86*0957b409SSimon J. Gerraty 
87*0957b409SSimon J. Gerraty 		/* version, privateKeyAlgorithm, privateKey tag */
88*0957b409SSimon J. Gerraty 		memcpy(buf, PK8_HEAD, sizeof PK8_HEAD);
89*0957b409SSimon J. Gerraty 		buf += sizeof PK8_HEAD;
90*0957b409SSimon J. Gerraty 
91*0957b409SSimon J. Gerraty 		/* privateKey */
92*0957b409SSimon J. Gerraty 		buf += br_asn1_encode_length(buf, len_raw);
93*0957b409SSimon J. Gerraty 		br_encode_rsa_raw_der(buf, sk, pk, d, dlen);
94*0957b409SSimon J. Gerraty 
95*0957b409SSimon J. Gerraty 		return 1 + lenlen + len_seq;
96*0957b409SSimon J. Gerraty 	}
97*0957b409SSimon J. Gerraty }
98