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 bearssl_x509.h */
28 size_t
29 br_encode_rsa_pkcs8_der(void *dest, const br_rsa_private_key *sk,
30 	const br_rsa_public_key *pk, const void *d, size_t dlen)
31 {
32 	/*
33 	 * ASN.1 format:
34 	 *
35 	 *   OneAsymmetricKey ::= SEQUENCE {
36 	 *     version                   Version,
37 	 *     privateKeyAlgorithm       PrivateKeyAlgorithmIdentifier,
38 	 *     privateKey                PrivateKey,
39 	 *     attributes            [0] Attributes OPTIONAL,
40 	 *     ...,
41 	 *     [[2: publicKey        [1] PublicKey OPTIONAL ]],
42 	 *     ...
43 	 *   }
44 	 *
45 	 * We don't include attributes or public key. The 'version' field
46 	 * is an INTEGER that we will set to 0 (meaning 'v1', compatible
47 	 * with previous versions of PKCS#8). The 'privateKeyAlgorithm'
48 	 * structure is an AlgorithmIdentifier whose OID should be
49 	 * rsaEncryption, with NULL parameters. The 'privateKey' is an
50 	 * OCTET STRING, whose value is the "raw DER" encoding of the
51 	 * key pair.
52 	 *
53 	 * Since the private key value comes last, this function really
54 	 * adds a header, which is mostly fixed (only some lengths have
55 	 * to be modified.
56 	 */
57 
58 	/*
59 	 * Concatenation of:
60 	 *  - DER encoding of an INTEGER of value 0 (the 'version' field)
61 	 *  - DER encoding of a PrivateKeyAlgorithmIdentifier that uses
62 	 *    the rsaEncryption OID, and NULL parameters
63 	 *  - An OCTET STRING tag
64 	 */
65 	static const unsigned char PK8_HEAD[] = {
66 		0x02, 0x01, 0x00,
67 		0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
68 		0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
69 		0x04
70 	};
71 
72 	size_t len_raw, len_seq;
73 
74 	len_raw = br_encode_rsa_raw_der(NULL, sk, pk, d, dlen);
75 	len_seq = (sizeof PK8_HEAD) + len_of_len(len_raw) + len_raw;
76 	if (dest == NULL) {
77 		return 1 + len_of_len(len_seq) + len_seq;
78 	} else {
79 		unsigned char *buf;
80 		size_t lenlen;
81 
82 		buf = dest;
83 		*buf ++ = 0x30;  /* SEQUENCE tag */
84 		lenlen = br_asn1_encode_length(buf, len_seq);
85 		buf += lenlen;
86 
87 		/* version, privateKeyAlgorithm, privateKey tag */
88 		memcpy(buf, PK8_HEAD, sizeof PK8_HEAD);
89 		buf += sizeof PK8_HEAD;
90 
91 		/* privateKey */
92 		buf += br_asn1_encode_length(buf, len_raw);
93 		br_encode_rsa_raw_der(buf, sk, pk, d, dlen);
94 
95 		return 1 + lenlen + len_seq;
96 	}
97 }
98