Path: blob/main/contrib/bearssl/src/x509/encode_ec_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_ec_pkcs8_der(void *dest,29const br_ec_private_key *sk, const br_ec_public_key *pk)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 public key45* is included in the private key value instead). The46* 'version' field is an INTEGER that we will set to 047* (meaning 'v1', compatible with previous versions of PKCS#8).48* The 'privateKeyAlgorithm' structure is an AlgorithmIdentifier49* whose OID should be id-ecPublicKey, with, as parameters, the50* curve OID. The 'privateKey' is an OCTET STRING, whose value51* is the "raw DER" encoding of the key pair.52*/5354/*55* OID id-ecPublicKey (1.2.840.10045.2.1), DER-encoded (with56* the tag).57*/58static const unsigned char OID_ECPUBKEY[] = {590x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x0160};6162size_t len_version, len_privateKeyAlgorithm, len_privateKeyValue;63size_t len_privateKey, len_seq;64const unsigned char *oid;6566oid = br_get_curve_OID(sk->curve);67if (oid == NULL) {68return 0;69}70len_version = 3;71len_privateKeyAlgorithm = 2 + sizeof OID_ECPUBKEY + 2 + oid[0];72len_privateKeyValue = br_encode_ec_raw_der_inner(NULL, sk, pk, 0);73len_privateKey = 1 + len_of_len(len_privateKeyValue)74+ len_privateKeyValue;75len_seq = len_version + len_privateKeyAlgorithm + len_privateKey;7677if (dest == NULL) {78return 1 + len_of_len(len_seq) + len_seq;79} else {80unsigned char *buf;81size_t lenlen;8283buf = dest;84*buf ++ = 0x30; /* SEQUENCE tag */85lenlen = br_asn1_encode_length(buf, len_seq);86buf += lenlen;8788/* version */89*buf ++ = 0x02;90*buf ++ = 0x01;91*buf ++ = 0x00;9293/* privateKeyAlgorithm */94*buf ++ = 0x30;95*buf ++ = (sizeof OID_ECPUBKEY) + 2 + oid[0];96memcpy(buf, OID_ECPUBKEY, sizeof OID_ECPUBKEY);97buf += sizeof OID_ECPUBKEY;98*buf ++ = 0x06;99memcpy(buf, oid, 1 + oid[0]);100buf += 1 + oid[0];101102/* privateKey */103*buf ++ = 0x04;104buf += br_asn1_encode_length(buf, len_privateKeyValue);105br_encode_ec_raw_der_inner(buf, sk, pk, 0);106107return 1 + lenlen + len_seq;108}109}110111112