Path: blob/main/contrib/bearssl/src/x509/encode_ec_rawder.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 inner.h */27const unsigned char *28br_get_curve_OID(int curve)29{30static const unsigned char OID_secp256r1[] = {310x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x0732};33static const unsigned char OID_secp384r1[] = {340x05, 0x2b, 0x81, 0x04, 0x00, 0x2235};36static const unsigned char OID_secp521r1[] = {370x05, 0x2b, 0x81, 0x04, 0x00, 0x2338};3940switch (curve) {41case BR_EC_secp256r1: return OID_secp256r1;42case BR_EC_secp384r1: return OID_secp384r1;43case BR_EC_secp521r1: return OID_secp521r1;44default:45return NULL;46}47}4849/* see inner.h */50size_t51br_encode_ec_raw_der_inner(void *dest,52const br_ec_private_key *sk, const br_ec_public_key *pk,53int include_curve_oid)54{55/*56* ASN.1 format:57*58* ECPrivateKey ::= SEQUENCE {59* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),60* privateKey OCTET STRING,61* parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,62* publicKey [1] BIT STRING OPTIONAL63* }64*65* The tages '[0]' and '[1]' are explicit. The 'ECParameters'66* is a CHOICE; in our case, it will always be an OBJECT IDENTIFIER67* that identifies the curve.68*69* The value of the 'privateKey' field is the raw unsigned big-endian70* encoding of the private key (integer modulo the curve subgroup71* order); there is no INTEGER tag, and the leading bit may be 1.72* Also, leading bytes of value 0x00 are _not_ removed.73*74* The 'publicKey' contents are the raw encoded public key point,75* normally uncompressed (leading byte of value 0x04, followed76* by the unsigned big-endian encodings of the X and Y coordinates,77* padded to the full field length if necessary).78*/7980size_t len_version, len_privateKey, len_parameters, len_publicKey;81size_t len_publicKey_bits, len_seq;82const unsigned char *oid;8384if (include_curve_oid) {85oid = br_get_curve_OID(sk->curve);86if (oid == NULL) {87return 0;88}89} else {90oid = NULL;91}92len_version = 3;93len_privateKey = 1 + len_of_len(sk->xlen) + sk->xlen;94if (include_curve_oid) {95len_parameters = 4 + oid[0];96} else {97len_parameters = 0;98}99if (pk == NULL) {100len_publicKey = 0;101len_publicKey_bits = 0;102} else {103len_publicKey_bits = 2 + len_of_len(pk->qlen) + pk->qlen;104len_publicKey = 1 + len_of_len(len_publicKey_bits)105+ len_publicKey_bits;106}107len_seq = len_version + len_privateKey + len_parameters + len_publicKey;108if (dest == NULL) {109return 1 + len_of_len(len_seq) + len_seq;110} else {111unsigned char *buf;112size_t lenlen;113114buf = dest;115*buf ++ = 0x30; /* SEQUENCE tag */116lenlen = br_asn1_encode_length(buf, len_seq);117buf += lenlen;118119/* version */120*buf ++ = 0x02;121*buf ++ = 0x01;122*buf ++ = 0x01;123124/* privateKey */125*buf ++ = 0x04;126buf += br_asn1_encode_length(buf, sk->xlen);127memcpy(buf, sk->x, sk->xlen);128buf += sk->xlen;129130/* parameters */131if (include_curve_oid) {132*buf ++ = 0xA0;133*buf ++ = oid[0] + 2;134*buf ++ = 0x06;135memcpy(buf, oid, oid[0] + 1);136buf += oid[0] + 1;137}138139/* publicKey */140if (pk != NULL) {141*buf ++ = 0xA1;142buf += br_asn1_encode_length(buf, len_publicKey_bits);143*buf ++ = 0x03;144buf += br_asn1_encode_length(buf, pk->qlen + 1);145*buf ++ = 0x00;146memcpy(buf, pk->q, pk->qlen);147/* buf += pk->qlen; */148}149150return 1 + lenlen + len_seq;151}152}153154/* see bearssl_x509.h */155size_t156br_encode_ec_raw_der(void *dest,157const br_ec_private_key *sk, const br_ec_public_key *pk)158{159return br_encode_ec_raw_der_inner(dest, sk, pk, 1);160}161162163