1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file x509_encode_subject_public_key_info.c
7 ASN.1 DER/X.509, encode a SubjectPublicKeyInfo structure --nmav
8 */
9
10 #ifdef LTC_DER
11
12 /* AlgorithmIdentifier := SEQUENCE {
13 * algorithm OBJECT IDENTIFIER,
14 * parameters ANY DEFINED BY algorithm
15 * }
16 *
17 * SubjectPublicKeyInfo := SEQUENCE {
18 * algorithm AlgorithmIdentifier,
19 * subjectPublicKey BIT STRING
20 * }
21 */
22 /**
23 Encode a SubjectPublicKeyInfo
24 @param out The output buffer
25 @param outlen [in/out] Length of buffer and resulting length of output
26 @param algorithm One out of the enum #public_key_algorithms
27 @param public_key The buffer for the public key
28 @param public_key_len The length of the public key buffer
29 @param parameters_type The parameters' type out of the enum ltc_asn1_type
30 @param parameters The parameters to include
31 @param parameters_len The number of parameters to include
32 @return CRYPT_OK on success
33 */
x509_encode_subject_public_key_info(unsigned char * out,unsigned long * outlen,unsigned int algorithm,const void * public_key,unsigned long public_key_len,ltc_asn1_type parameters_type,ltc_asn1_list * parameters,unsigned long parameters_len)34 int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
35 unsigned int algorithm, const void* public_key, unsigned long public_key_len,
36 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len)
37 {
38 int err;
39 ltc_asn1_list alg_id[2];
40 const char *OID;
41 unsigned long oid[16], oidlen;
42
43 LTC_ARGCHK(out != NULL);
44 LTC_ARGCHK(outlen != NULL);
45
46 if ((err = pk_get_oid(algorithm, &OID)) != CRYPT_OK) {
47 return err;
48 }
49
50 oidlen = sizeof(oid)/sizeof(oid[0]);
51 if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
52 return err;
53 }
54
55 LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, oidlen);
56 LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, parameters_len);
57
58 return der_encode_sequence_multi(out, outlen,
59 LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
60 LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
61 LTC_ASN1_EOL, 0UL, NULL);
62
63 }
64
65 #endif
66
67