Path: blob/main/contrib/bearssl/src/x509/encode_rsa_pk8der.c
39507 views
/*1* Copyright (c) 2018 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/* see bearssl_x509.h */27size_t28br_encode_rsa_pkcs8_der(void *dest, const br_rsa_private_key *sk,29const br_rsa_public_key *pk, const void *d, size_t dlen)30{31/*32* ASN.1 format:33*34* OneAsymmetricKey ::= SEQUENCE {35* version Version,36* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,37* privateKey PrivateKey,38* attributes [0] Attributes OPTIONAL,39* ...,40* [[2: publicKey [1] PublicKey OPTIONAL ]],41* ...42* }43*44* We don't include attributes or public key. The 'version' field45* is an INTEGER that we will set to 0 (meaning 'v1', compatible46* with previous versions of PKCS#8). The 'privateKeyAlgorithm'47* structure is an AlgorithmIdentifier whose OID should be48* rsaEncryption, with NULL parameters. The 'privateKey' is an49* OCTET STRING, whose value is the "raw DER" encoding of the50* key pair.51*52* Since the private key value comes last, this function really53* adds a header, which is mostly fixed (only some lengths have54* to be modified.55*/5657/*58* Concatenation of:59* - DER encoding of an INTEGER of value 0 (the 'version' field)60* - DER encoding of a PrivateKeyAlgorithmIdentifier that uses61* the rsaEncryption OID, and NULL parameters62* - An OCTET STRING tag63*/64static const unsigned char PK8_HEAD[] = {650x02, 0x01, 0x00,660x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,670xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,680x0469};7071size_t len_raw, len_seq;7273len_raw = br_encode_rsa_raw_der(NULL, sk, pk, d, dlen);74len_seq = (sizeof PK8_HEAD) + len_of_len(len_raw) + len_raw;75if (dest == NULL) {76return 1 + len_of_len(len_seq) + len_seq;77} else {78unsigned char *buf;79size_t lenlen;8081buf = dest;82*buf ++ = 0x30; /* SEQUENCE tag */83lenlen = br_asn1_encode_length(buf, len_seq);84buf += lenlen;8586/* version, privateKeyAlgorithm, privateKey tag */87memcpy(buf, PK8_HEAD, sizeof PK8_HEAD);88buf += sizeof PK8_HEAD;8990/* privateKey */91buf += br_asn1_encode_length(buf, len_raw);92br_encode_rsa_raw_der(buf, sk, pk, d, dlen);9394return 1 + lenlen + len_seq;95}96}979899