]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/i2d_evp.c
Copyright year updates
[thirdparty/openssl.git] / crypto / asn1 / i2d_evp.c
CommitLineData
4227e504 1/*
da1c088f 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4227e504
RL
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
6ed4022c
RL
10/*
11 * Low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
4227e504
RL
15
16#include <stdio.h>
17#include "internal/cryptlib.h"
18#include <openssl/evp.h>
19#include <openssl/encoder.h>
20#include <openssl/buffer.h>
21#include <openssl/x509.h>
6ed4022c 22#include <openssl/rsa.h> /* For i2d_RSAPublicKey */
4227e504
RL
23#include <openssl/dsa.h> /* For i2d_DSAPublicKey */
24#include <openssl/ec.h> /* For i2o_ECPublicKey */
25#include "crypto/asn1.h"
26#include "crypto/evp.h"
27
8ab9c4dd
RL
28struct type_and_structure_st {
29 const char *output_type;
30 const char *output_structure;
31};
32
4227e504 33static int i2d_provided(const EVP_PKEY *a, int selection,
8ab9c4dd 34 const struct type_and_structure_st *output_info,
4227e504
RL
35 unsigned char **pp)
36{
4227e504
RL
37 int ret;
38
39 for (ret = -1;
8ab9c4dd
RL
40 ret == -1 && output_info->output_type != NULL;
41 output_info++) {
4227e504
RL
42 /*
43 * The i2d_ calls don't take a boundary length for *pp. However,
1d73e2ad
RL
44 * OSSL_ENCODER_to_data() needs one, so we make one up. Because
45 * OSSL_ENCODER_to_data() decrements this number by the amount of
46 * bytes written, we need to calculate the length written further
47 * down, when pp != NULL.
4227e504
RL
48 */
49 size_t len = INT_MAX;
a8457b4c 50 int pp_was_NULL = (pp == NULL || *pp == NULL);
0c6c3782 51 OSSL_ENCODER_CTX *ctx;
4227e504 52
8ab9c4dd
RL
53 ctx = OSSL_ENCODER_CTX_new_for_pkey(a, selection,
54 output_info->output_type,
55 output_info->output_structure,
56 NULL);
4227e504
RL
57 if (ctx == NULL)
58 return -1;
1d73e2ad 59 if (OSSL_ENCODER_to_data(ctx, pp, &len)) {
a8457b4c 60 if (pp_was_NULL)
1d73e2ad
RL
61 ret = (int)len;
62 else
63 ret = INT_MAX - (int)len;
64 }
4227e504 65 OSSL_ENCODER_CTX_free(ctx);
4227e504
RL
66 }
67
68 if (ret == -1)
69 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
70 return ret;
71}
72
73int i2d_KeyParams(const EVP_PKEY *a, unsigned char **pp)
74{
75 if (evp_pkey_is_provided(a)) {
8ab9c4dd
RL
76 static const struct type_and_structure_st output_info[] = {
77 { "DER", "type-specific" },
78 { NULL, }
79 };
4227e504 80
8ab9c4dd 81 return i2d_provided(a, EVP_PKEY_KEY_PARAMETERS, output_info, pp);
4227e504
RL
82 }
83 if (a->ameth != NULL && a->ameth->param_encode != NULL)
84 return a->ameth->param_encode(a, pp);
85 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
86 return -1;
87}
88
89int i2d_KeyParams_bio(BIO *bp, const EVP_PKEY *pkey)
90{
91 return ASN1_i2d_bio_of(EVP_PKEY, i2d_KeyParams, bp, pkey);
92}
93
94int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp)
95{
96 if (evp_pkey_is_provided(a)) {
8ab9c4dd
RL
97 static const struct type_and_structure_st output_info[] = {
98 { "DER", "type-specific" },
6a2b8ff3 99 { "DER", "PrivateKeyInfo" },
8ab9c4dd
RL
100 { NULL, }
101 };
4227e504 102
8ab9c4dd 103 return i2d_provided(a, EVP_PKEY_KEYPAIR, output_info, pp);
4227e504
RL
104 }
105 if (a->ameth != NULL && a->ameth->old_priv_encode != NULL) {
106 return a->ameth->old_priv_encode(a, pp);
107 }
108 if (a->ameth != NULL && a->ameth->priv_encode != NULL) {
109 PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
110 int ret = 0;
111
112 if (p8 != NULL) {
113 ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
114 PKCS8_PRIV_KEY_INFO_free(p8);
115 }
116 return ret;
117 }
118 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
119 return -1;
120}
121
122int i2d_PublicKey(const EVP_PKEY *a, unsigned char **pp)
123{
124 if (evp_pkey_is_provided(a)) {
8ab9c4dd
RL
125 static const struct type_and_structure_st output_info[] = {
126 { "DER", "type-specific" },
127 { "blob", NULL }, /* for EC */
128 { NULL, }
129 };
4227e504 130
8ab9c4dd 131 return i2d_provided(a, EVP_PKEY_PUBLIC_KEY, output_info, pp);
4227e504 132 }
8582dccc 133 switch (EVP_PKEY_get_base_id(a)) {
4227e504
RL
134 case EVP_PKEY_RSA:
135 return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(a), pp);
4227e504
RL
136#ifndef OPENSSL_NO_DSA
137 case EVP_PKEY_DSA:
138 return i2d_DSAPublicKey(EVP_PKEY_get0_DSA(a), pp);
139#endif
140#ifndef OPENSSL_NO_EC
141 case EVP_PKEY_EC:
142 return i2o_ECPublicKey(EVP_PKEY_get0_EC_KEY(a), pp);
143#endif
144 default:
145 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
146 return -1;
147 }
148}