Path: blob/main/contrib/bearssl/src/x509/encode_rsa_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 bearssl_x509.h */27size_t28br_encode_rsa_raw_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* RSAPrivateKey ::= SEQUENCE {35* version Version,36* modulus INTEGER, -- n37* publicExponent INTEGER, -- e38* privateExponent INTEGER, -- d39* prime1 INTEGER, -- p40* prime2 INTEGER, -- q41* exponent1 INTEGER, -- d mod (p-1)42* exponent2 INTEGER, -- d mod (q-1)43* coefficient INTEGER, -- (inverse of q) mod p44* otherPrimeInfos OtherPrimeInfos OPTIONAL45* }46*47* The 'version' field is an INTEGER of value 0 (meaning: there48* are exactly two prime factors), and 'otherPrimeInfos' will49* be absent (because there are exactly two prime factors).50*/5152br_asn1_uint num[9];53size_t u, slen;5455/*56* For all INTEGER values, get the pointer and length for the57* data bytes.58*/59num[0] = br_asn1_uint_prepare(NULL, 0);60num[1] = br_asn1_uint_prepare(pk->n, pk->nlen);61num[2] = br_asn1_uint_prepare(pk->e, pk->elen);62num[3] = br_asn1_uint_prepare(d, dlen);63num[4] = br_asn1_uint_prepare(sk->p, sk->plen);64num[5] = br_asn1_uint_prepare(sk->q, sk->qlen);65num[6] = br_asn1_uint_prepare(sk->dp, sk->dplen);66num[7] = br_asn1_uint_prepare(sk->dq, sk->dqlen);67num[8] = br_asn1_uint_prepare(sk->iq, sk->iqlen);6869/*70* Get the length of the SEQUENCE contents.71*/72slen = 0;73for (u = 0; u < 9; u ++) {74uint32_t ilen;7576ilen = num[u].asn1len;77slen += 1 + len_of_len(ilen) + ilen;78}7980if (dest == NULL) {81return 1 + len_of_len(slen) + slen;82} else {83unsigned char *buf;84size_t lenlen;8586buf = dest;87*buf ++ = 0x30; /* SEQUENCE tag */88lenlen = br_asn1_encode_length(buf, slen);89buf += lenlen;90for (u = 0; u < 9; u ++) {91buf += br_asn1_encode_uint(buf, num[u]);92}93return 1 + lenlen + slen;94}95}969798