1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file x509_encode_subject_public_key_info.c
14 ASN.1 DER/X.509, encode a SubjectPublicKeyInfo structure --nmav
15 */
16
17 #ifdef LTC_DER
18
19 /* AlgorithmIdentifier := SEQUENCE {
20 * algorithm OBJECT IDENTIFIER,
21 * parameters ANY DEFINED BY algorithm
22 * }
23 *
24 * SubjectPublicKeyInfo := SEQUENCE {
25 * algorithm AlgorithmIdentifier,
26 * subjectPublicKey BIT STRING
27 * }
28 */
29 /**
30 Encode a SubjectPublicKeyInfo
31 @param out The output buffer
32 @param outlen [in/out] Length of buffer and resulting length of output
33 @param algorithm One out of the enum #public_key_algorithms
34 @param public_key The buffer for the public key
35 @param public_key_len The length of the public key buffer
36 @param parameters_type The parameters' type out of the enum ltc_asn1_type
37 @param parameters The parameters to include
38 @param parameters_len The number of parameters to include
39 @return CRYPT_OK on success
40 */
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)41 int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
42 unsigned int algorithm, const void* public_key, unsigned long public_key_len,
43 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len)
44 {
45 int err;
46 ltc_asn1_list alg_id[2];
47 const char *OID;
48 unsigned long oid[16], oidlen;
49
50 LTC_ARGCHK(out != NULL);
51 LTC_ARGCHK(outlen != NULL);
52
53 if ((err = pk_get_oid(algorithm, &OID)) != CRYPT_OK) {
54 return err;
55 }
56
57 oidlen = sizeof(oid)/sizeof(oid[0]);
58 if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
59 return err;
60 }
61
62 LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, oidlen);
63 LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, parameters_len);
64
65 return der_encode_sequence_multi(out, outlen,
66 LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
67 LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
68 LTC_ASN1_EOL, 0UL, NULL);
69
70 }
71
72 #endif
73
74 /* ref: $Format:%D$ */
75 /* git commit: $Format:%H$ */
76 /* commit time: $Format:%ai$ */
77
78